Serialization for hash panics on TypeMap

The serializeCollectionMemberForHash helper can't be called for the
MapType values, because MapType doesn't have a schema.Elem. Instead, we
can write the key/value pairs directly to the buffer. This still doesn't
allow for nested maps or lists, but we need to define that use case
before committing to it here.
This commit is contained in:
James Bardin 2016-06-09 13:02:30 -04:00
parent a31cb5087e
commit d8fbaa7924
1 changed files with 19 additions and 2 deletions

View File

@ -2,6 +2,7 @@ package schema
import ( import (
"bytes" "bytes"
"fmt"
"sort" "sort"
"strconv" "strconv"
) )
@ -33,6 +34,7 @@ func SerializeValueForHash(buf *bytes.Buffer, val interface{}, schema *Schema) {
} }
buf.WriteRune(')') buf.WriteRune(')')
case TypeMap: case TypeMap:
m := val.(map[string]interface{}) m := val.(map[string]interface{})
var keys []string var keys []string
for k := range m { for k := range m {
@ -42,9 +44,24 @@ func SerializeValueForHash(buf *bytes.Buffer, val interface{}, schema *Schema) {
buf.WriteRune('[') buf.WriteRune('[')
for _, k := range keys { for _, k := range keys {
innerVal := m[k] innerVal := m[k]
if innerVal == nil {
continue
}
buf.WriteString(k) buf.WriteString(k)
buf.WriteRune(':') 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(']') buf.WriteRune(']')
case TypeSet: case TypeSet:
@ -100,6 +117,6 @@ func serializeCollectionMemberForHash(buf *bytes.Buffer, val interface{}, elem i
SerializeResourceForHash(buf, val, tElem) SerializeResourceForHash(buf, val, tElem)
buf.WriteString(">;") buf.WriteString(">;")
default: default:
panic("invalid element type") panic(fmt.Sprintf("invalid element type: %T", tElem))
} }
} }