Convert the map fields values when reading diff

Convert the value to the correct type when reading a diff and the map
schema has an primitive Elem type.
This commit is contained in:
James Bardin 2016-11-17 14:34:18 -05:00
parent ad34f1ec74
commit 730014b33e
2 changed files with 22 additions and 3 deletions

View File

@ -69,6 +69,12 @@ func (r *DiffFieldReader) readMap(
resultSet = true resultSet = true
} }
// Determine what element type the map contains, defaulting to string
elemType := TypeString
if et, ok := schema.Elem.(ValueType); ok {
elemType = et
}
// Next, read all the elements we have in our diff, and apply // Next, read all the elements we have in our diff, and apply
// the diff to our result. // the diff to our result.
prefix := strings.Join(address, ".") + "." prefix := strings.Join(address, ".") + "."
@ -89,7 +95,20 @@ func (r *DiffFieldReader) readMap(
continue continue
} }
result[k] = v.New // Replace the new value with one of the correct Elem type if needed.
// We don't supported arbitrarily nested schemas, so we can only handle
// the primitive types here.
var vNew interface{} = v.New
switch elemType {
case TypeBool, TypeInt, TypeFloat:
v, err := stringToPrimitive(v.New, false, &Schema{Type: elemType})
if err != nil {
return FieldReadResult{}, err
}
vNew = v
}
result[k] = vNew
} }
var resultVal interface{} var resultVal interface{}

View File

@ -2983,7 +2983,7 @@ func TestResourceData_nonStringValuesInMap(t *testing.T) {
}, },
"boolMap.boolField": &terraform.ResourceAttrDiff{ "boolMap.boolField": &terraform.ResourceAttrDiff{
Old: "", Old: "",
New: "1", New: "true",
}, },
}, },
}, },
@ -2995,7 +2995,7 @@ func TestResourceData_nonStringValuesInMap(t *testing.T) {
Schema: map[string]*Schema{ Schema: map[string]*Schema{
"intMap": &Schema{ "intMap": &Schema{
Type: TypeMap, Type: TypeMap,
Elem: TypeBool, Elem: TypeInt,
Optional: true, Optional: true,
}, },
}, },