• Cuong Manh Le's avatar
    encoding/json: support TextUnmarshaler for map keys with string underlying types · fb9af841
    Cuong Manh Le authored
    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: default avatarDaniel Martí <mvdan@mvdan.cc>
    fb9af841
decode.go 35 KB