Merge pull request #7091 from hashicorp/jbardin/serialize
Serialization for hash panics on TypeMap
This commit is contained in:
commit
2c7b702d1f
|
@ -2,6 +2,7 @@ package schema
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strconv"
|
||||
)
|
||||
|
@ -33,6 +34,7 @@ func SerializeValueForHash(buf *bytes.Buffer, val interface{}, schema *Schema) {
|
|||
}
|
||||
buf.WriteRune(')')
|
||||
case TypeMap:
|
||||
|
||||
m := val.(map[string]interface{})
|
||||
var keys []string
|
||||
for k := range m {
|
||||
|
@ -42,9 +44,24 @@ func SerializeValueForHash(buf *bytes.Buffer, val interface{}, schema *Schema) {
|
|||
buf.WriteRune('[')
|
||||
for _, k := range keys {
|
||||
innerVal := m[k]
|
||||
if innerVal == nil {
|
||||
continue
|
||||
}
|
||||
buf.WriteString(k)
|
||||
buf.WriteRune(':')
|
||||
serializeCollectionMemberForHash(buf, innerVal, schema.Elem)
|
||||
|
||||
switch innerVal := innerVal.(type) {
|
||||
case int:
|
||||
buf.WriteString(strconv.Itoa(innerVal))
|
||||
case float64:
|
||||
buf.WriteString(strconv.FormatFloat(innerVal, 'g', -1, 64))
|
||||
case string:
|
||||
buf.WriteString(innerVal)
|
||||
default:
|
||||
panic(fmt.Sprintf("unknown value type in TypeMap %T", innerVal))
|
||||
}
|
||||
|
||||
buf.WriteRune(';')
|
||||
}
|
||||
buf.WriteRune(']')
|
||||
case TypeSet:
|
||||
|
@ -100,6 +117,6 @@ func serializeCollectionMemberForHash(buf *bytes.Buffer, val interface{}, elem i
|
|||
SerializeResourceForHash(buf, val, tElem)
|
||||
buf.WriteString(">;")
|
||||
default:
|
||||
panic("invalid element type")
|
||||
panic(fmt.Sprintf("invalid element type: %T", tElem))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,7 +13,6 @@ func TestSerializeForHash(t *testing.T) {
|
|||
}
|
||||
|
||||
tests := []testCase{
|
||||
|
||||
testCase{
|
||||
Schema: &Schema{
|
||||
Type: TypeInt,
|
||||
|
@ -193,6 +192,31 @@ func TestSerializeForHash(t *testing.T) {
|
|||
},
|
||||
Expected: "green:1;name:my-fun-database;size:12;",
|
||||
},
|
||||
|
||||
// test TypeMap nested in Schema: GH-7091
|
||||
testCase{
|
||||
Schema: &Resource{
|
||||
Schema: map[string]*Schema{
|
||||
"outer": &Schema{
|
||||
Type: TypeSet,
|
||||
Required: true,
|
||||
Elem: &Schema{
|
||||
Type: TypeMap,
|
||||
Optional: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Value: map[string]interface{}{
|
||||
"outer": NewSet(func(i interface{}) int { return 42 }, []interface{}{
|
||||
map[string]interface{}{
|
||||
"foo": "bar",
|
||||
"baz": "foo",
|
||||
},
|
||||
}),
|
||||
},
|
||||
Expected: "outer:{[baz:foo;foo:bar;];};",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
|
|
Loading…
Reference in New Issue