From 2f3998fc0a40a55c338368589b923a73d02fcec8 Mon Sep 17 00:00:00 2001 From: Matt Keeler Date: Tue, 7 May 2019 11:40:48 -0400 Subject: [PATCH] Allow MapWalk to handle []interface{} elements that are []uint8 (#5800) * Allow MapWalk to handle []interface{} elements that are []uint8 * Ensure ints are left alone. --- lib/map_walker.go | 5 ++++- lib/map_walker_test.go | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/map_walker.go b/lib/map_walker.go index 95827a0ee..ca103806d 100644 --- a/lib/map_walker.go +++ b/lib/map_walker.go @@ -34,6 +34,7 @@ func MapWalk(input interface{}) (map[string]interface{}, error) { } var typMapIfaceIface = reflect.TypeOf(map[interface{}]interface{}{}) +var typByteSlice = reflect.TypeOf([]byte{}) // mapWalker implements interfaces for the reflectwalk package // (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. // Convert it back to a Go string and replace the value so that JSON // doesn't base64-encode it. - if v.Type() == reflect.TypeOf([]byte{}) { + if v.Type() == typByteSlice { resultVal := reflect.ValueOf(string(v.Interface().([]byte))) switch w.lastLoc { case reflectwalk.MapKey: @@ -185,6 +186,8 @@ func (w *mapWalker) SliceElem(i int, elem reflect.Value) error { } elem.Set(reflect.ValueOf(target)) + } else if inner := elem.Elem(); inner.Type() == typByteSlice { + elem.Set(reflect.ValueOf(string(inner.Interface().([]byte)))) } return nil diff --git a/lib/map_walker_test.go b/lib/map_walker_test.go index 45c8c0d5e..ad276c684 100644 --- a/lib/map_walker_test.go +++ b/lib/map_walker_test.go @@ -53,6 +53,26 @@ func TestMapWalk(t *testing.T) { "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 {