]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/json: support TextUnmarshaler for map keys with string underlying types
authorCuong Manh Le <cuong.manhle.vn@gmail.com>
Thu, 10 Oct 2019 03:19:12 +0000 (10:19 +0700)
committerDaniel Martí <mvdan@mvdan.cc>
Thu, 10 Oct 2019 12:59:11 +0000 (12:59 +0000)
When unmarshaling to a map, the map's key type must either be a string,
an integer, or implement encoding.TextUnmarshaler. But for a user
defined type, reflect.Kind will not distinguish between the static type
and the underlying type. In:

var x MyString = "x"
t := reflect.TypeOf(x)
println(t.Kind() == reflect.String)

the Kind of x is still reflect.String, even though the static type of x
is MyString.

Moreover, checking for the map's key type is a string occurs first, so
even if the map key type MyString implements encoding.TextUnmarshaler,
it will be ignored.

To fix the bug, check for encoding.TextUnmarshaler first.

Fixes #34437

Change-Id: I780e0b084575e1dddfbb433fe03857adf71d05fb
Reviewed-on: https://go-review.googlesource.com/c/go/+/200237
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
src/encoding/json/decode.go
src/encoding/json/decode_test.go

index 407fbcedbe9855fd2fcae5a5d49eb437b7e17130..86d8a69db7e6d76aa40ca9d4b307ea2f43e909b7 100644 (file)
@@ -773,14 +773,14 @@ func (d *decodeState) object(v reflect.Value) error {
                        kt := t.Key()
                        var kv reflect.Value
                        switch {
-                       case kt.Kind() == reflect.String:
-                               kv = reflect.ValueOf(key).Convert(kt)
                        case reflect.PtrTo(kt).Implements(textUnmarshalerType):
                                kv = reflect.New(kt)
                                if err := d.literalStore(item, kv, true); err != nil {
                                        return err
                                }
                                kv = kv.Elem()
+                       case kt.Kind() == reflect.String:
+                               kv = reflect.ValueOf(key).Convert(kt)
                        default:
                                switch kt.Kind() {
                                case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
index 4cbd2172d060446328b4d237af8f24c6012814ec..498bd97b46ef41dc9a2ceba27d449be428fc6d30 100644 (file)
@@ -2411,3 +2411,23 @@ func TestUnmarshalRecursivePointer(t *testing.T) {
                t.Fatal(err)
        }
 }
+
+type textUnmarshalerString string
+
+func (m *textUnmarshalerString) UnmarshalText(text []byte) error {
+       *m = textUnmarshalerString(strings.ToLower(string(text)))
+       return nil
+}
+
+// Test unmarshal to a map, with map key is a user defined type.
+// See golang.org/issues/34437.
+func TestUnmarshalMapWithTextUnmarshalerStringKey(t *testing.T) {
+       var p map[textUnmarshalerString]string
+       if err := Unmarshal([]byte(`{"FOO": "1"}`), &p); err != nil {
+               t.Fatalf("Unmarshal unexpected error: %v", err)
+       }
+
+       if _, ok := p["foo"]; !ok {
+               t.Errorf(`Key "foo" is not existed in map: %v`, p)
+       }
+}