Allow MapWalk to handle []interface{} elements that are []uint8 (#5800)

* Allow MapWalk to handle []interface{} elements that are []uint8

* Ensure ints are left alone.
This commit is contained in:
Matt Keeler 2019-05-07 11:40:48 -04:00 committed by GitHub
parent a3ad908b56
commit 2f3998fc0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 1 deletions

View File

@ -34,6 +34,7 @@ func MapWalk(input interface{}) (map[string]interface{}, error) {
} }
var typMapIfaceIface = reflect.TypeOf(map[interface{}]interface{}{}) var typMapIfaceIface = reflect.TypeOf(map[interface{}]interface{}{})
var typByteSlice = reflect.TypeOf([]byte{})
// mapWalker implements interfaces for the reflectwalk package // mapWalker implements interfaces for the reflectwalk package
// (github.com/mitchellh/reflectwalk) that can be used to automatically // (github.com/mitchellh/reflectwalk) that can be used to automatically
@ -127,7 +128,7 @@ func (w *mapWalker) Slice(v reflect.Value) error {
// If we find a []byte slice, it is an HCL-string converted to []byte. // If we find a []byte slice, it is an HCL-string converted to []byte.
// Convert it back to a Go string and replace the value so that JSON // Convert it back to a Go string and replace the value so that JSON
// doesn't base64-encode it. // doesn't base64-encode it.
if v.Type() == reflect.TypeOf([]byte{}) { if v.Type() == typByteSlice {
resultVal := reflect.ValueOf(string(v.Interface().([]byte))) resultVal := reflect.ValueOf(string(v.Interface().([]byte)))
switch w.lastLoc { switch w.lastLoc {
case reflectwalk.MapKey: case reflectwalk.MapKey:
@ -185,6 +186,8 @@ func (w *mapWalker) SliceElem(i int, elem reflect.Value) error {
} }
elem.Set(reflect.ValueOf(target)) elem.Set(reflect.ValueOf(target))
} else if inner := elem.Elem(); inner.Type() == typByteSlice {
elem.Set(reflect.ValueOf(string(inner.Interface().([]byte))))
} }
return nil return nil

View File

@ -53,6 +53,26 @@ func TestMapWalk(t *testing.T) {
"bar": "baz", "bar": "baz",
}, },
}, },
"map with slice": tcase{
input: map[string]interface{}{
"foo": []uint8("bar"),
"bar": []interface{}{
[]uint8("one"),
[]uint8("two"),
3,
4,
},
},
expected: map[string]interface{}{
"foo": "bar",
"bar": []interface{}{
"one",
"two",
3,
4,
},
},
},
} }
for name, tcase := range cases { for name, tcase := range cases {