From db223f6d5a456d1c9dfaeb7ef865cc63e03ec2ce Mon Sep 17 00:00:00 2001 From: Nathan Lowe Date: Tue, 11 Jun 2019 19:54:58 -0400 Subject: [PATCH] private/protocola/json/jsonutil: Fix Unmarshal map[string]bool (#320) Fixes the JSON unmarshaling of maps of bools. The unmarshal case was missing the condition for bool value, in addition the bool pointer. Fix #319 --- private/protocol/json/jsonutil/unmarshal.go | 2 + .../protocol/json/jsonutil/unmarshal_test.go | 57 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 private/protocol/json/jsonutil/unmarshal_test.go diff --git a/private/protocol/json/jsonutil/unmarshal.go b/private/protocol/json/jsonutil/unmarshal.go index dce3a67b1c0..f65bca26782 100644 --- a/private/protocol/json/jsonutil/unmarshal.go +++ b/private/protocol/json/jsonutil/unmarshal.go @@ -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() } diff --git a/private/protocol/json/jsonutil/unmarshal_test.go b/private/protocol/json/jsonutil/unmarshal_test.go new file mode 100644 index 00000000000..265f2fee44c --- /dev/null +++ b/private/protocol/json/jsonutil/unmarshal_test.go @@ -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) + } + }) + } +}