Fix decoding of interface type

This commit is contained in:
Masaaki Goshima 2021-02-09 22:13:58 +09:00
parent 5227e49c39
commit 6befcb123e
3 changed files with 64 additions and 15 deletions

View File

@ -42,7 +42,7 @@ var (
) )
) )
func decodeWithUnmarshaler(s *stream, unmarshaler Unmarshaler) error { func decodeStreamUnmarshaler(s *stream, unmarshaler Unmarshaler) error {
start := s.cursor start := s.cursor
if err := s.skipValue(); err != nil { if err := s.skipValue(); err != nil {
return err return err
@ -57,7 +57,7 @@ func decodeWithUnmarshaler(s *stream, unmarshaler Unmarshaler) error {
return nil return nil
} }
func decodeWithTextUnmarshaler(s *stream, unmarshaler encoding.TextUnmarshaler) error { func decodeStreamTextUnmarshaler(s *stream, unmarshaler encoding.TextUnmarshaler) error {
start := s.cursor start := s.cursor
if err := s.skipValue(); err != nil { if err := s.skipValue(); err != nil {
return err return err
@ -72,7 +72,7 @@ func decodeWithTextUnmarshaler(s *stream, unmarshaler encoding.TextUnmarshaler)
return nil return nil
} }
func (d *interfaceDecoder) decodeEmptyInterface(s *stream, p unsafe.Pointer) error { func (d *interfaceDecoder) decodeStreamEmptyInterface(s *stream, p unsafe.Pointer) error {
s.skipWhiteSpace() s.skipWhiteSpace()
for { for {
switch s.char() { switch s.char() {
@ -166,10 +166,10 @@ func (d *interfaceDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
rv := reflect.ValueOf(runtimeInterfaceValue) rv := reflect.ValueOf(runtimeInterfaceValue)
if rv.NumMethod() > 0 && rv.CanInterface() { if rv.NumMethod() > 0 && rv.CanInterface() {
if u, ok := rv.Interface().(Unmarshaler); ok { if u, ok := rv.Interface().(Unmarshaler); ok {
return decodeWithUnmarshaler(s, u) return decodeStreamUnmarshaler(s, u)
} }
if u, ok := rv.Interface().(encoding.TextUnmarshaler); ok { if u, ok := rv.Interface().(encoding.TextUnmarshaler); ok {
return decodeWithTextUnmarshaler(s, u) return decodeStreamTextUnmarshaler(s, u)
} }
return nil return nil
} }
@ -178,7 +178,17 @@ func (d *interfaceDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
typ := ifaceHeader.typ typ := ifaceHeader.typ
if d.typ == typ || typ == nil { if d.typ == typ || typ == nil {
// concrete type is empty interface // concrete type is empty interface
return d.decodeEmptyInterface(s, p) return d.decodeStreamEmptyInterface(s, p)
}
if typ.Kind() == reflect.Ptr && typ.Elem() == d.typ || typ.Kind() != reflect.Ptr {
return d.decodeStreamEmptyInterface(s, p)
}
if s.char() == 'n' {
if err := nullBytes(s); err != nil {
return err
}
*(*interface{})(p) = nil
return nil
} }
decoder, err := d.dec.compileToGetDecoder(uintptr(unsafe.Pointer(typ)), typ) decoder, err := d.dec.compileToGetDecoder(uintptr(unsafe.Pointer(typ)), typ)
if err != nil { if err != nil {
@ -188,6 +198,46 @@ func (d *interfaceDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
} }
func (d *interfaceDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) { func (d *interfaceDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) {
runtimeInterfaceValue := *(*interface{})(unsafe.Pointer(&interfaceHeader{
typ: d.typ,
ptr: p,
}))
rv := reflect.ValueOf(runtimeInterfaceValue)
iface := rv.Interface()
ifaceHeader := (*interfaceHeader)(unsafe.Pointer(&iface))
typ := ifaceHeader.typ
if d.typ == typ || typ == nil {
// concrete type is empty interface
return d.decodeEmptyInterface(buf, cursor, p)
}
if typ.Kind() == reflect.Ptr && typ.Elem() == d.typ || typ.Kind() != reflect.Ptr {
return d.decodeEmptyInterface(buf, cursor, p)
}
if buf[cursor] == 'n' {
if cursor+3 >= int64(len(buf)) {
return 0, errUnexpectedEndOfJSON("null", cursor)
}
if buf[cursor+1] != 'u' {
return 0, errInvalidCharacter(buf[cursor+1], "null", cursor)
}
if buf[cursor+2] != 'l' {
return 0, errInvalidCharacter(buf[cursor+2], "null", cursor)
}
if buf[cursor+3] != 'l' {
return 0, errInvalidCharacter(buf[cursor+3], "null", cursor)
}
cursor += 4
**(**interface{})(unsafe.Pointer(&p)) = nil
return cursor, nil
}
decoder, err := d.dec.compileToGetDecoder(uintptr(unsafe.Pointer(typ)), typ)
if err != nil {
return 0, err
}
return decoder.decode(buf, cursor, ifaceHeader.ptr)
}
func (d *interfaceDecoder) decodeEmptyInterface(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) {
cursor = skipWhiteSpace(buf, cursor) cursor = skipWhiteSpace(buf, cursor)
switch buf[cursor] { switch buf[cursor] {
case '{': case '{':

View File

@ -68,7 +68,9 @@ func (d *ptrDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64,
if buf[cursor+3] != 'l' { if buf[cursor+3] != 'l' {
return 0, errInvalidCharacter(buf[cursor+3], "null", cursor) return 0, errInvalidCharacter(buf[cursor+3], "null", cursor)
} }
if p != nil {
*(*unsafe.Pointer)(p) = nil *(*unsafe.Pointer)(p) = nil
}
cursor += 4 cursor += 4
return cursor, nil return cursor, nil
} }

View File

@ -2091,20 +2091,18 @@ var interfaceSetTests = []struct {
{"foo", `2`, 2.0}, {"foo", `2`, 2.0},
{"foo", `true`, true}, {"foo", `true`, true},
{"foo", `null`, nil}, {"foo", `null`, nil},
{nil, `null`, nil}, {nil, `null`, nil},
{new(int), `null`, nil}, {new(int), `null`, nil},
{(*int)(nil), `null`, nil}, {(*int)(nil), `null`, nil},
{new(*int), `null`, new(*int)}, //{new(*int), `null`, new(*int)},
{(**int)(nil), `null`, nil}, {(**int)(nil), `null`, nil},
{intp(1), `null`, nil}, {intp(1), `null`, nil},
{intpp(nil), `null`, intpp(nil)}, //{intpp(nil), `null`, intpp(nil)},
{intpp(intp(1)), `null`, intpp(nil)}, //{intpp(intp(1)), `null`, intpp(nil)},
} }
/*
func TestInterfaceSet(t *testing.T) { func TestInterfaceSet(t *testing.T) {
for _, tt := range interfaceSetTests { for idx, tt := range interfaceSetTests {
b := struct{ X interface{} }{tt.pre} b := struct{ X interface{} }{tt.pre}
blob := `{"X":` + tt.json + `}` blob := `{"X":` + tt.json + `}`
if err := json.Unmarshal([]byte(blob), &b); err != nil { if err := json.Unmarshal([]byte(blob), &b); err != nil {
@ -2112,11 +2110,10 @@ func TestInterfaceSet(t *testing.T) {
continue continue
} }
if !reflect.DeepEqual(b.X, tt.post) { if !reflect.DeepEqual(b.X, tt.post) {
t.Errorf("Unmarshal %#q into %#v: X=%#v, want %#v", blob, tt.pre, b.X, tt.post) t.Errorf("%d: Unmarshal %#q into %#v: X=%#v, want %#v", idx, blob, tt.pre, b.X, tt.post)
} }
} }
} }
*/
/* /*
// JSON null values should be ignored for primitives and string values instead of resulting in an error. // JSON null values should be ignored for primitives and string values instead of resulting in an error.