Skip to content

Commit

Permalink
helper/schema: Support serialization of bool values in TypeMap values
Browse files Browse the repository at this point in the history
Reference: hashicorp/terraform-provider-aws#13549

This may only be an issue with Terraform 0.11 and earlier with an obscure schema of:

```go
"attr": {
	Type:     schema.TypeSet,
	Optional: true,
	Elem: &schema.Schema{
		Type: schema.TypeMap,
		Elem: &schema.Schema{
			Type: schema.TypeString,
		},
},
```

Previously, this would panic (with updated test):

```console
$ go test ./helper/schema -run=TestSerializeForHash
--- FAIL: TestSerializeForHash (0.00s)
panic: unknown value type in TypeMap bool [recovered]
  panic: unknown value type in TypeMap bool

goroutine 5 [running]:
testing.tRunner.func1.1(0x1a4a5e0, 0xc0004b9920)
  /usr/local/Cellar/go/1.14.3/libexec/src/testing/testing.go:940 +0x2f5
testing.tRunner.func1(0xc000372c60)
  /usr/local/Cellar/go/1.14.3/libexec/src/testing/testing.go:943 +0x3f9
panic(0x1a4a5e0, 0xc0004b9920)
  /usr/local/Cellar/go/1.14.3/libexec/src/runtime/panic.go:969 +0x166
github.com/hashicorp/terraform-plugin-sdk/helper/schema.SerializeValueForHash(0xc000085bb8, 0x1aaef40, 0xc000528450, 0xc000531400)
  /Users/bflad/src/github.com/hashicorp/terraform-plugin-sdk/helper/schema/serialize.go:61 +0x8c9
github.com/hashicorp/terraform-plugin-sdk/helper/schema.TestSerializeForHash(0xc000372c60)
  /Users/bflad/src/github.com/hashicorp/terraform-plugin-sdk/helper/schema/serialize_test.go:230 +0x1520
testing.tRunner(0xc000372c60, 0x1c2e848)
  /usr/local/Cellar/go/1.14.3/libexec/src/testing/testing.go:991 +0xdc
created by testing.(*T).Run
  /usr/local/Cellar/go/1.14.3/libexec/src/testing/testing.go:1042 +0x357
FAIL  github.com/hashicorp/terraform-plugin-sdk/helper/schema 5.797s
```
  • Loading branch information
bflad authored and appilon committed May 29, 2020
1 parent 838e0bd commit aef7266
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 2 additions & 0 deletions helper/schema/serialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ func SerializeValueForHash(buf *bytes.Buffer, val interface{}, schema *Schema) {
buf.WriteRune(':')

switch innerVal := innerVal.(type) {
case bool:
buf.WriteString(strconv.FormatBool(innerVal))
case int:
buf.WriteString(strconv.Itoa(innerVal))
case float64:
Expand Down
8 changes: 5 additions & 3 deletions helper/schema/serialize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,12 @@ func TestSerializeForHash(t *testing.T) {
},
},
Value: map[string]interface{}{
"foo": "bar",
"baz": "foo",
"bool": true,
"float": 1.2,
"int": 1,
"string": "value",
},
Expected: "[baz:foo;foo:bar;];",
Expected: "[bool:true;float:1.2;int:1;string:value;];",
},

{
Expand Down

0 comments on commit aef7266

Please sign in to comment.