diff --git a/helper/schema/resource_data.go b/helper/schema/resource_data.go index 615038d2da..119c379073 100644 --- a/helper/schema/resource_data.go +++ b/helper/schema/resource_data.go @@ -137,6 +137,18 @@ func (d *ResourceData) Partial(on bool) { // will be returned. func (d *ResourceData) Set(key string, value interface{}) error { d.once.Do(d.init) + + // If the value is a pointer to a non-struct, get its value and + // use that. This allows Set to take a pointer to primitives to + // simplify the interface. + reflectVal := reflect.ValueOf(value) + if reflectVal.Kind() == reflect.Ptr { + reflectVal = reflect.Indirect(reflectVal) + if reflectVal.Kind() != reflect.Struct { + value = reflectVal.Interface() + } + } + return d.setWriter.WriteField(strings.Split(key, "."), value) } diff --git a/helper/schema/resource_data_test.go b/helper/schema/resource_data_test.go index 90aa6a9d9b..92c4bfd856 100644 --- a/helper/schema/resource_data_test.go +++ b/helper/schema/resource_data_test.go @@ -1588,6 +1588,72 @@ func TestResourceDataSet(t *testing.T) { GetKey: "ratios", GetValue: []interface{}{1.0, 2.2, 5.5}, }, + + // #13: Basic pointer + { + Schema: map[string]*Schema{ + "availability_zone": &Schema{ + Type: TypeString, + Optional: true, + Computed: true, + ForceNew: true, + }, + }, + + State: nil, + + Diff: nil, + + Key: "availability_zone", + Value: testPtrTo("foo"), + + GetKey: "availability_zone", + GetValue: "foo", + }, + + // #14: Basic nil value + { + Schema: map[string]*Schema{ + "availability_zone": &Schema{ + Type: TypeString, + Optional: true, + Computed: true, + ForceNew: true, + }, + }, + + State: nil, + + Diff: nil, + + Key: "availability_zone", + Value: testPtrTo(nil), + + GetKey: "availability_zone", + GetValue: "", + }, + + // #15: Basic nil pointer + { + Schema: map[string]*Schema{ + "availability_zone": &Schema{ + Type: TypeString, + Optional: true, + Computed: true, + ForceNew: true, + }, + }, + + State: nil, + + Diff: nil, + + Key: "availability_zone", + Value: nil, + + GetKey: "availability_zone", + GetValue: "", + }, } for i, tc := range cases { @@ -2788,3 +2854,7 @@ func TestResourceDataSetId_override(t *testing.T) { t.Fatalf("bad: %#v", actual) } } + +func testPtrTo(raw interface{}) interface{} { + return &raw +}