Fix double pointer

This commit is contained in:
Masaaki Goshima 2020-12-07 10:49:00 +09:00
parent ee52d7f0ae
commit 59f5713178
2 changed files with 6 additions and 9 deletions

View File

@ -20,6 +20,7 @@ func (e *Encoder) compileHead(ctx *encodeCompileContext) (*opcode, error) {
return e.compileMarshalTextPtr(ctx) return e.compileMarshalTextPtr(ctx)
} }
isPtr := false isPtr := false
orgType := typ
if typ.Kind() == reflect.Ptr { if typ.Kind() == reflect.Ptr {
typ = typ.Elem() typ = typ.Elem()
isPtr = true isPtr = true
@ -28,6 +29,10 @@ func (e *Encoder) compileHead(ctx *encodeCompileContext) (*opcode, error) {
return e.compileMap(ctx.withType(typ), isPtr) return e.compileMap(ctx.withType(typ), isPtr)
} else if typ.Kind() == reflect.Struct { } else if typ.Kind() == reflect.Struct {
return e.compileStruct(ctx.withType(typ), isPtr) return e.compileStruct(ctx.withType(typ), isPtr)
} else if isPtr && typ.Implements(marshalTextType) {
typ = orgType
} else if isPtr && typ.Implements(marshalJSONType) {
typ = orgType
} }
return e.compile(ctx.withType(typ)) return e.compile(ctx.withType(typ))
} }
@ -112,12 +117,8 @@ func (e *Encoder) compile(ctx *encodeCompileContext) (*opcode, error) {
func (e *Encoder) compileKey(ctx *encodeCompileContext) (*opcode, error) { func (e *Encoder) compileKey(ctx *encodeCompileContext) (*opcode, error) {
typ := ctx.typ typ := ctx.typ
switch { switch {
case typ.Implements(marshalJSONType):
return e.compileMarshalJSON(ctx)
case rtype_ptrTo(typ).Implements(marshalJSONType): case rtype_ptrTo(typ).Implements(marshalJSONType):
return e.compileMarshalJSONPtr(ctx) return e.compileMarshalJSONPtr(ctx)
case typ.Implements(marshalTextType):
return e.compileMarshalText(ctx)
case rtype_ptrTo(typ).Implements(marshalTextType): case rtype_ptrTo(typ).Implements(marshalTextType):
return e.compileMarshalTextPtr(ctx) return e.compileMarshalTextPtr(ctx)
} }

View File

@ -402,14 +402,10 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error {
isPtr := code.typ.Kind() == reflect.Ptr isPtr := code.typ.Kind() == reflect.Ptr
p := e.ptrToUnsafePtr(ptr) p := e.ptrToUnsafePtr(ptr)
if p == nil { if p == nil {
e.encodeNull() e.encodeBytes([]byte{'"', '"', ','})
e.encodeByte(',')
} else if isPtr && *(*unsafe.Pointer)(p) == nil { } else if isPtr && *(*unsafe.Pointer)(p) == nil {
e.encodeBytes([]byte{'"', '"', ','}) e.encodeBytes([]byte{'"', '"', ','})
} else { } else {
if isPtr && code.typ.Elem().Implements(marshalTextType) {
p = *(*unsafe.Pointer)(p)
}
v := *(*interface{})(unsafe.Pointer(&interfaceHeader{ v := *(*interface{})(unsafe.Pointer(&interfaceHeader{
typ: code.typ, typ: code.typ,
ptr: p, ptr: p,