Skip to content

Commit

Permalink
Merge pull request #19552 from olindata/bugfix/setting-sets-in-list
Browse files Browse the repository at this point in the history
helper/schema: Fix setting a set in a list caused error
  • Loading branch information
jbardin authored Dec 10, 2018
2 parents b1ed146 + 44a45b7 commit 3d6ec09
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
7 changes: 4 additions & 3 deletions helper/schema/field_writer_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,13 +297,14 @@ func (w *MapFieldWriter) setSet(
// we get the proper order back based on the hash code.
if v := reflect.ValueOf(value); v.Kind() == reflect.Slice {
// Build a temp *ResourceData to use for the conversion
tempAddr := addr[len(addr)-1:]
tempSchema := *schema
tempSchema.Type = TypeList
tempSchemaMap := map[string]*Schema{addr[0]: &tempSchema}
tempSchemaMap := map[string]*Schema{tempAddr[0]: &tempSchema}
tempW := &MapFieldWriter{Schema: tempSchemaMap}

// Set the entire list, this lets us get sane values out of it
if err := tempW.WriteField(addr, value); err != nil {
if err := tempW.WriteField(tempAddr, value); err != nil {
return err
}

Expand All @@ -319,7 +320,7 @@ func (w *MapFieldWriter) setSet(
}
for i := 0; i < v.Len(); i++ {
is := strconv.FormatInt(int64(i), 10)
result, err := tempR.ReadField(append(addrCopy, is))
result, err := tempR.ReadField(append(tempAddr, is))
if err != nil {
return err
}
Expand Down
63 changes: 63 additions & 0 deletions helper/schema/resource_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2059,6 +2059,69 @@ func TestResourceDataSet(t *testing.T) {
GetKey: "availability_zone",
GetValue: "",
},

// #16: Set in a list
{
Schema: map[string]*Schema{
"ports": &Schema{
Type: TypeList,
Elem: &Resource{
Schema: map[string]*Schema{
"set": &Schema{
Type: TypeSet,
Elem: &Schema{Type: TypeInt},
Set: func(a interface{}) int {
return a.(int)
},
},
},
},
},
},

State: nil,

Key: "ports",
Value: []interface{}{
map[string]interface{}{
"set": []interface{}{
1,
},
},
},

GetKey: "ports",
GetValue: []interface{}{
map[string]interface{}{
"set": []interface{}{
1,
},
},
},
GetPreProcess: func(v interface{}) interface{} {
if v == nil {
return v
}
s, ok := v.([]interface{})
if !ok {
return v
}
for _, v := range s {
m, ok := v.(map[string]interface{})
if !ok {
continue
}
if m["set"] == nil {
continue
}
if s, ok := m["set"].(*Set); ok {
m["set"] = s.List()
}
}

return v
},
},
}

oldEnv := os.Getenv(PanicOnErr)
Expand Down

0 comments on commit 3d6ec09

Please sign in to comment.