Skip to content

Commit

Permalink
private/protocola/json/jsonutil: Fix Unmarshal map[string]bool (aws#320)
Browse files Browse the repository at this point in the history
Fixes the JSON unmarshaling of maps of bools. The unmarshal case was
missing the condition for bool value, in addition the bool pointer.

Fix aws#319
  • Loading branch information
nlowe authored and jasdel committed Jun 11, 2019
1 parent da6472f commit db223f6
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
2 changes: 2 additions & 0 deletions private/protocol/json/jsonutil/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ func unmarshalScalar(value reflect.Value, data interface{}, tag reflect.StructTa
switch value.Interface().(type) {
case *bool:
value.Set(reflect.ValueOf(&d))
case bool:
value.Set(reflect.ValueOf(d))
default:
return errf()
}
Expand Down
57 changes: 57 additions & 0 deletions private/protocol/json/jsonutil/unmarshal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package jsonutil

import (
"bytes"
"encoding/json"
"reflect"
"testing"
"time"
)

func TestUnmarshal(t *testing.T) {
type scalarMaps struct {
Bools map[string]bool
Strings map[string]string
Int64s map[string]int64
Float64s map[string]float64
Times map[string]time.Time
}

cases := map[string]struct {
Input json.RawMessage
Output interface{}
Expect interface{}
}{
"scalar maps": {
Input: json.RawMessage(`
{
"Bools": {"a": true},
"Strings": {"b": "123"},
"Int64s": {"c": 456},
"Float64s": {"d": 789},
"Times": {"e": 1257894000}
}`),
Output: &scalarMaps{},
Expect: &scalarMaps{
Bools: map[string]bool{"a": true},
Strings: map[string]string{"b": "123"},
Int64s: map[string]int64{"c": 456},
Float64s: map[string]float64{"d": 789},
Times: map[string]time.Time{"e": time.Unix(1257894000, 0).UTC()},
},
},
}

for name, c := range cases {
t.Run(name, func(t *testing.T) {
err := UnmarshalJSON(c.Output, bytes.NewReader(c.Input))
if err != nil {
t.Fatalf("expect no error, got %v", err)
}

if e, a := c.Expect, c.Output; !reflect.DeepEqual(e, a) {
t.Errorf("expect:\n%#v\nactual:\n%#v\n", e, a)
}
})
}
}

0 comments on commit db223f6

Please sign in to comment.