Fix UnmarshalTypeError for int decoder

This commit is contained in:
Masaaki Goshima 2020-12-15 12:29:19 +09:00
parent 85577616f8
commit d88eb0986e
4 changed files with 56 additions and 43 deletions

View File

@ -44,15 +44,15 @@ func (d *Decoder) compile(typ *rtype, structName, fieldName string) (decoder, er
case reflect.Uintptr: case reflect.Uintptr:
return d.compileUint(structName, fieldName) return d.compileUint(structName, fieldName)
case reflect.Int: case reflect.Int:
return d.compileInt(structName, fieldName) return d.compileInt(typ, structName, fieldName)
case reflect.Int8: case reflect.Int8:
return d.compileInt8(structName, fieldName) return d.compileInt8(typ, structName, fieldName)
case reflect.Int16: case reflect.Int16:
return d.compileInt16(structName, fieldName) return d.compileInt16(typ, structName, fieldName)
case reflect.Int32: case reflect.Int32:
return d.compileInt32(structName, fieldName) return d.compileInt32(typ, structName, fieldName)
case reflect.Int64: case reflect.Int64:
return d.compileInt64(structName, fieldName) return d.compileInt64(typ, structName, fieldName)
case reflect.Uint: case reflect.Uint:
return d.compileUint(structName, fieldName) return d.compileUint(structName, fieldName)
case reflect.Uint8: case reflect.Uint8:
@ -104,32 +104,32 @@ func (d *Decoder) compilePtr(typ *rtype, structName, fieldName string) (decoder,
return newPtrDecoder(dec, typ.Elem(), structName, fieldName), nil return newPtrDecoder(dec, typ.Elem(), structName, fieldName), nil
} }
func (d *Decoder) compileInt(structName, fieldName string) (decoder, error) { func (d *Decoder) compileInt(typ *rtype, structName, fieldName string) (decoder, error) {
return newIntDecoder(structName, fieldName, func(p unsafe.Pointer, v int64) { return newIntDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v int64) {
*(*int)(p) = int(v) *(*int)(p) = int(v)
}), nil }), nil
} }
func (d *Decoder) compileInt8(structName, fieldName string) (decoder, error) { func (d *Decoder) compileInt8(typ *rtype, structName, fieldName string) (decoder, error) {
return newIntDecoder(structName, fieldName, func(p unsafe.Pointer, v int64) { return newIntDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v int64) {
*(*int8)(p) = int8(v) *(*int8)(p) = int8(v)
}), nil }), nil
} }
func (d *Decoder) compileInt16(structName, fieldName string) (decoder, error) { func (d *Decoder) compileInt16(typ *rtype, structName, fieldName string) (decoder, error) {
return newIntDecoder(structName, fieldName, func(p unsafe.Pointer, v int64) { return newIntDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v int64) {
*(*int16)(p) = int16(v) *(*int16)(p) = int16(v)
}), nil }), nil
} }
func (d *Decoder) compileInt32(structName, fieldName string) (decoder, error) { func (d *Decoder) compileInt32(typ *rtype, structName, fieldName string) (decoder, error) {
return newIntDecoder(structName, fieldName, func(p unsafe.Pointer, v int64) { return newIntDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v int64) {
*(*int32)(p) = int32(v) *(*int32)(p) = int32(v)
}), nil }), nil
} }
func (d *Decoder) compileInt64(structName, fieldName string) (decoder, error) { func (d *Decoder) compileInt64(typ *rtype, structName, fieldName string) (decoder, error) {
return newIntDecoder(structName, fieldName, func(p unsafe.Pointer, v int64) { return newIntDecoder(typ, structName, fieldName, func(p unsafe.Pointer, v int64) {
*(*int64)(p) = v *(*int64)(p) = v
}), nil }), nil
} }

View File

@ -1,17 +1,42 @@
package json package json
import ( import (
"fmt"
"unsafe" "unsafe"
) )
type intDecoder struct { type intDecoder struct {
typ *rtype
op func(unsafe.Pointer, int64) op func(unsafe.Pointer, int64)
structName string structName string
fieldName string fieldName string
} }
func newIntDecoder(structName, fieldName string, op func(unsafe.Pointer, int64)) *intDecoder { func newIntDecoder(typ *rtype, structName, fieldName string, op func(unsafe.Pointer, int64)) *intDecoder {
return &intDecoder{op: op, structName: structName, fieldName: fieldName} return &intDecoder{
typ: typ,
op: op,
structName: structName,
fieldName: fieldName,
}
}
func (d *intDecoder) typeError(buf []byte, offset int64) *UnmarshalTypeError {
return &UnmarshalTypeError{
Value: fmt.Sprintf("number %s", string(buf)),
Type: rtype2type(d.typ),
Offset: offset,
}
}
func (d *intDecoder) annotateError(cursor int64, err error) {
switch e := err.(type) {
case *UnmarshalTypeError:
e.Struct = d.structName
e.Field = d.fieldName
case *SyntaxError:
e.Offset = cursor
}
} }
var ( var (
@ -102,7 +127,7 @@ func (d *intDecoder) decodeStreamByte(s *stream) ([]byte, error) {
} }
goto ERROR goto ERROR
default: default:
goto ERROR return nil, d.typeError([]byte{s.char()}, s.totalOffset())
} }
} }
ERROR: ERROR:
@ -126,7 +151,7 @@ func (d *intDecoder) decodeByte(buf []byte, cursor int64) ([]byte, int64, error)
num := buf[start:cursor] num := buf[start:cursor]
return num, cursor, nil return num, cursor, nil
default: default:
return nil, 0, errInvalidCharacter(buf[cursor], "number(integer)", cursor) return nil, 0, d.typeError([]byte{buf[cursor]}, cursor)
} }
} }
return nil, 0, errUnexpectedEndOfJSON("number(integer)", cursor) return nil, 0, errUnexpectedEndOfJSON("number(integer)", cursor)

View File

@ -5,10 +5,9 @@ import (
) )
type unmarshalJSONDecoder struct { type unmarshalJSONDecoder struct {
typ *rtype typ *rtype
isDoublePointer bool structName string
structName string fieldName string
fieldName string
} }
func newUnmarshalJSONDecoder(typ *rtype, structName, fieldName string) *unmarshalJSONDecoder { func newUnmarshalJSONDecoder(typ *rtype, structName, fieldName string) *unmarshalJSONDecoder {
@ -58,26 +57,13 @@ func (d *unmarshalJSONDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer
return 0, err return 0, err
} }
src := buf[start:end] src := buf[start:end]
if d.isDoublePointer { v := *(*interface{})(unsafe.Pointer(&interfaceHeader{
newptr := unsafe_New(d.typ.Elem()) typ: d.typ,
v := *(*interface{})(unsafe.Pointer(&interfaceHeader{ ptr: p,
typ: d.typ, }))
ptr: newptr, if err := v.(Unmarshaler).UnmarshalJSON(src); err != nil {
})) d.annotateError(cursor, err)
if err := v.(Unmarshaler).UnmarshalJSON(src); err != nil { return 0, err
d.annotateError(cursor, err)
return 0, err
}
*(*unsafe.Pointer)(p) = newptr
} else {
v := *(*interface{})(unsafe.Pointer(&interfaceHeader{
typ: d.typ,
ptr: p,
}))
if err := v.(Unmarshaler).UnmarshalJSON(src); err != nil {
d.annotateError(cursor, err)
return 0, err
}
} }
return end, nil return end, nil
} }

View File

@ -149,6 +149,8 @@ func (e *Encoder) compileKey(ctx *encodeCompileContext) (*opcode, error) {
return e.compileUint32String(ctx) return e.compileUint32String(ctx)
case reflect.Uint64: case reflect.Uint64:
return e.compileUint64String(ctx) return e.compileUint64String(ctx)
case reflect.Uintptr:
return e.compileUintString(ctx)
} }
return nil, &UnsupportedTypeError{Type: rtype2type(typ)} return nil, &UnsupportedTypeError{Type: rtype2type(typ)}
} }