Fix invalid string option

This commit is contained in:
Masaaki Goshima 2021-03-24 19:09:33 +09:00
parent c7f6ad7048
commit 3a0fba4bdb
2 changed files with 23 additions and 4 deletions

View File

@ -102,6 +102,28 @@ func decodeCompile(typ *rtype, structName, fieldName string, structTypeToDecoder
} }
} }
func isStringTagSupportedType(typ *rtype) bool {
switch {
case rtype_ptrTo(typ).Implements(unmarshalJSONType):
return false
case rtype_ptrTo(typ).Implements(unmarshalTextType):
return false
}
switch typ.Kind() {
case reflect.Map:
return false
case reflect.Slice:
return false
case reflect.Array:
return false
case reflect.Struct:
return false
case reflect.Interface:
return false
}
return true
}
func decodeCompileMapKey(typ *rtype, structName, fieldName string, structTypeToDecoder map[uintptr]decoder) (decoder, error) { func decodeCompileMapKey(typ *rtype, structName, fieldName string, structTypeToDecoder map[uintptr]decoder) (decoder, error) {
if rtype_ptrTo(typ).Implements(unmarshalTextType) { if rtype_ptrTo(typ).Implements(unmarshalTextType) {
return newUnmarshalTextDecoder(rtype_ptrTo(typ), structName, fieldName), nil return newUnmarshalTextDecoder(rtype_ptrTo(typ), structName, fieldName), nil
@ -416,7 +438,7 @@ func decodeCompileStruct(typ *rtype, structName, fieldName string, structTypeToD
} }
} }
} else { } else {
if tag.IsString { if tag.IsString && isStringTagSupportedType(type2rtype(field.Type)) {
dec = newWrappedStringDecoder(type2rtype(field.Type), dec, structName, field.Name) dec = newWrappedStringDecoder(type2rtype(field.Type), dec, structName, field.Name)
} }
var key string var key string

View File

@ -2514,7 +2514,6 @@ func TestInvalidUnmarshalText(t *testing.T) {
} }
} }
/*
// Test that string option is ignored for invalid types. // Test that string option is ignored for invalid types.
// Issue 9812. // Issue 9812.
func TestInvalidStringOption(t *testing.T) { func TestInvalidStringOption(t *testing.T) {
@ -2532,13 +2531,11 @@ func TestInvalidStringOption(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Marshal: %v", err) t.Fatalf("Marshal: %v", err)
} }
err = json.Unmarshal(data, &item) err = json.Unmarshal(data, &item)
if err != nil { if err != nil {
t.Fatalf("Unmarshal: %v", err) t.Fatalf("Unmarshal: %v", err)
} }
} }
*/
// Test unmarshal behavior with regards to embedded unexported structs. // Test unmarshal behavior with regards to embedded unexported structs.
// //