Merge pull request #1824 from hashicorp/b-write-count-for-empty-maps

helper/schema: write "attr.#": "0" for empty maps
This commit is contained in:
Paul Hinze 2015-05-07 10:38:32 -05:00
commit 051ba78554
2 changed files with 15 additions and 15 deletions

View File

@ -141,25 +141,23 @@ func (w *MapFieldWriter) setMap(
v := reflect.ValueOf(value)
vs := make(map[string]interface{})
if value != nil {
if v.Kind() != reflect.Map {
return fmt.Errorf("%s: must be a map", k)
}
if v.Type().Key().Kind() != reflect.String {
return fmt.Errorf("%s: keys must strings", k)
}
for _, mk := range v.MapKeys() {
mv := v.MapIndex(mk)
vs[mk.String()] = mv.Interface()
}
}
if len(vs) == 0 {
if value == nil {
// The empty string here means the map is removed.
w.result[k] = ""
return nil
}
if v.Kind() != reflect.Map {
return fmt.Errorf("%s: must be a map", k)
}
if v.Type().Key().Kind() != reflect.String {
return fmt.Errorf("%s: keys must strings", k)
}
for _, mk := range v.MapKeys() {
mv := v.MapIndex(mk)
vs[mk.String()] = mv.Interface()
}
// Remove the pure key since we're setting the full map value
delete(w.result, k)

View File

@ -2539,7 +2539,9 @@ func TestResourceDataState(t *testing.T) {
},
Result: &terraform.InstanceState{
Attributes: map[string]string{},
Attributes: map[string]string{
"tags.#": "0",
},
},
},