helper/schema: test real nil pointer to ResourceData.Set

This commit is contained in:
Mitchell Hashimoto 2015-03-02 23:37:43 -08:00
parent c030148259
commit 58a8776c41
2 changed files with 13 additions and 4 deletions

View File

@ -143,9 +143,16 @@ func (d *ResourceData) Set(key string, value interface{}) error {
// simplify the interface. // simplify the interface.
reflectVal := reflect.ValueOf(value) reflectVal := reflect.ValueOf(value)
if reflectVal.Kind() == reflect.Ptr { if reflectVal.Kind() == reflect.Ptr {
reflectVal = reflect.Indirect(reflectVal) if reflectVal.IsNil() {
if reflectVal.Kind() != reflect.Struct { // If the pointer is nil, then the value is just nil
value = reflectVal.Interface() value = nil
} else {
// Otherwise, we dereference the pointer as long as its not
// a pointer to a struct, since struct pointers are allowed.
reflectVal = reflect.Indirect(reflectVal)
if reflectVal.Kind() != reflect.Struct {
value = reflectVal.Interface()
}
} }
} }

View File

@ -1178,6 +1178,8 @@ func TestResourceDataHasChange(t *testing.T) {
} }
func TestResourceDataSet(t *testing.T) { func TestResourceDataSet(t *testing.T) {
var testNilPtr *string
cases := []struct { cases := []struct {
Schema map[string]*Schema Schema map[string]*Schema
State *terraform.InstanceState State *terraform.InstanceState
@ -1649,7 +1651,7 @@ func TestResourceDataSet(t *testing.T) {
Diff: nil, Diff: nil,
Key: "availability_zone", Key: "availability_zone",
Value: nil, Value: testNilPtr,
GetKey: "availability_zone", GetKey: "availability_zone",
GetValue: "", GetValue: "",