Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move towards consistently specifying the type of a Map in a nested *Schema elemnt #17097

Merged
merged 3 commits into from
Mar 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions helper/schema/field_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ func addrToSchema(addr []string, schemaMap map[string]*Schema) []*Schema {
switch v := current.Elem.(type) {
case ValueType:
current = &Schema{Type: v}
case *Schema:
current, _ = current.Elem.(*Schema)
default:
// maps default to string values. This is all we can have
// if this is nested in another list or map.
Expand Down Expand Up @@ -249,11 +251,10 @@ func readObjectField(
}

// convert map values to the proper primitive type based on schema.Elem
func mapValuesToPrimitive(m map[string]interface{}, schema *Schema) error {

elemType := TypeString
if et, ok := schema.Elem.(ValueType); ok {
elemType = et
func mapValuesToPrimitive(k string, m map[string]interface{}, schema *Schema) error {
elemType, err := getValueType(k, schema)
if err != nil {
return err
}

switch elemType {
Expand Down
2 changes: 1 addition & 1 deletion helper/schema/field_reader_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func (r *ConfigFieldReader) readMap(k string, schema *Schema) (FieldReadResult,
panic(fmt.Sprintf("unknown type: %#v", mraw))
}

err := mapValuesToPrimitive(result, schema)
err := mapValuesToPrimitive(k, result, schema)
if err != nil {
return FieldReadResult{}, nil
}
Expand Down
4 changes: 4 additions & 0 deletions helper/schema/field_reader_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ func TestConfigFieldReader(t *testing.T) {
"one": "1",
"two": "2",
},
"mapIntNestedSchema": map[string]interface{}{
"one": "1",
"two": "2",
},
"mapFloat": map[string]interface{}{
"oneDotTwo": "1.2",
},
Expand Down
3 changes: 2 additions & 1 deletion helper/schema/field_reader_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ func (r *DiffFieldReader) readMap(
result[k] = v.New
}

err = mapValuesToPrimitive(result, schema)
key := address[len(address)-1]
err = mapValuesToPrimitive(key, result, schema)
if err != nil {
return FieldReadResult{}, nil
}
Expand Down
13 changes: 13 additions & 0 deletions helper/schema/field_reader_diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,19 @@ func TestDiffFieldReader(t *testing.T) {
New: "2",
},

"mapIntNestedSchema.%": &terraform.ResourceAttrDiff{
Old: "",
New: "2",
},
"mapIntNestedSchema.one": &terraform.ResourceAttrDiff{
Old: "",
New: "1",
},
"mapIntNestedSchema.two": &terraform.ResourceAttrDiff{
Old: "",
New: "2",
},

"mapFloat.%": &terraform.ResourceAttrDiff{
Old: "",
New: "1",
Expand Down
2 changes: 1 addition & 1 deletion helper/schema/field_reader_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (r *MapFieldReader) readMap(k string, schema *Schema) (FieldReadResult, err
return true
})

err := mapValuesToPrimitive(result, schema)
err := mapValuesToPrimitive(k, result, schema)
if err != nil {
return FieldReadResult{}, nil
}
Expand Down
4 changes: 4 additions & 0 deletions helper/schema/field_reader_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func TestMapFieldReader(t *testing.T) {
"mapInt.one": "1",
"mapInt.two": "2",

"mapIntNestedSchema.%": "2",
"mapIntNestedSchema.one": "1",
"mapIntNestedSchema.two": "2",

"mapFloat.%": "1",
"mapFloat.oneDotTwo": "1.2",

Expand Down
20 changes: 20 additions & 0 deletions helper/schema/field_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,13 @@ func testFieldReader(t *testing.T, f func(map[string]*Schema) FieldReader) {
Type: TypeMap,
Elem: TypeInt,
},

// This is used to verify that the type of a Map can be specified using the
// same syntax as for lists (as a nested *Schema passed to Elem)
"mapIntNestedSchema": &Schema{
Type: TypeMap,
Elem: &Schema{Type: TypeInt},
},
"mapFloat": &Schema{
Type: TypeMap,
Elem: TypeFloat,
Expand Down Expand Up @@ -360,6 +367,19 @@ func testFieldReader(t *testing.T, f func(map[string]*Schema) FieldReader) {
false,
},

"mapIntNestedSchema": {
[]string{"mapIntNestedSchema"},
FieldReadResult{
Value: map[string]interface{}{
"one": 1,
"two": 2,
},
Exists: true,
Computed: false,
},
false,
},

"mapFloat": {
[]string{"mapFloat"},
FieldReadResult{
Expand Down
9 changes: 3 additions & 6 deletions helper/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -1461,13 +1461,10 @@ func getValueType(k string, schema *Schema) (ValueType, error) {
return vt, nil
}

// If a Schema is provided to a Map, we use the Type of that schema
// as the type for each element in the Map.
if s, ok := schema.Elem.(*Schema); ok {
if s.Elem == nil {
return TypeString, nil
}
if vt, ok := s.Elem.(ValueType); ok {
return vt, nil
}
return s.Type, nil
}

if _, ok := schema.Elem.(*Resource); ok {
Expand Down
36 changes: 36 additions & 0 deletions helper/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4481,6 +4481,42 @@ func TestSchemaMap_Validate(t *testing.T) {
},
},

"Map with type specified as value type": {
Schema: map[string]*Schema{
"user_data": &Schema{
Type: TypeMap,
Optional: true,
Elem: TypeBool,
},
},

Config: map[string]interface{}{
"user_data": map[string]interface{}{
"foo": "not_a_bool",
},
},

Err: true,
},

"Map with type specified as nested Schema": {
Schema: map[string]*Schema{
"user_data": &Schema{
Type: TypeMap,
Optional: true,
Elem: &Schema{Type: TypeBool},
},
},

Config: map[string]interface{}{
"user_data": map[string]interface{}{
"foo": "not_a_bool",
},
},

Err: true,
},

"Bad map: just a slice": {
Schema: map[string]*Schema{
"user_data": &Schema{
Expand Down