helper/schema: zero value of a set should be a set

This commit is contained in:
Mitchell Hashimoto 2015-01-08 18:47:35 -08:00
parent b4bf813151
commit 942a988ac2
2 changed files with 35 additions and 5 deletions

View File

@ -360,8 +360,15 @@ func (d *ResourceData) get(
// If the result doesn't exist, then we set the value to the zero value
if result.Value == nil {
if schema := addrToSchema(addr, d.schema); len(schema) > 0 {
result.Value = schema[len(schema)-1].Type.Zero()
if schemaL := addrToSchema(addr, d.schema); len(schemaL) > 0 {
schema := schemaL[len(schemaL)-1]
result.Value = schema.Type.Zero()
// The zero value of a set is nil, but we want it
// to actually be an empty set object...
if schema.Type == TypeSet && result.Value == nil {
result.Value = &Set{F: schema.Set}
}
}
}
@ -1129,8 +1136,8 @@ func (d *ResourceData) setSet(
for code, elem := range value.(*Set).m {
for field, _ := range t.Schema {
subK := fmt.Sprintf("%s.%d", k, code)
err := d.setObject(
subK, []string{field}, t.Schema, elem.(map[string]interface{})[field])
value := elem.(map[string]interface{})[field]
err := d.setObject(subK, []string{field}, t.Schema, value)
if err != nil {
return err
}

View File

@ -592,6 +592,29 @@ func TestResourceDataGet(t *testing.T) {
},
},
},
// #19 Empty Set
{
Schema: map[string]*Schema{
"ports": &Schema{
Type: TypeSet,
Optional: true,
Computed: true,
Elem: &Schema{Type: TypeInt},
Set: func(a interface{}) int {
return a.(int)
},
},
},
State: nil,
Diff: nil,
Key: "ports",
Value: []interface{}{},
},
}
for i, tc := range cases {
@ -816,7 +839,7 @@ func TestResourceDataGetOk(t *testing.T) {
Diff: nil,
Key: "ports",
Value: nil,
Value: []interface{}{},
Ok: false,
},