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)
}
isPtr := false
orgType := typ
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
isPtr = true
@ -28,6 +29,10 @@ func (e *Encoder) compileHead(ctx *encodeCompileContext) (*opcode, error) {
return e.compileMap(ctx.withType(typ), isPtr)
} else if typ.Kind() == reflect.Struct {
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))
}
@ -112,12 +117,8 @@ func (e *Encoder) compile(ctx *encodeCompileContext) (*opcode, error) {
func (e *Encoder) compileKey(ctx *encodeCompileContext) (*opcode, error) {
typ := ctx.typ
switch {
case typ.Implements(marshalJSONType):
return e.compileMarshalJSON(ctx)
case rtype_ptrTo(typ).Implements(marshalJSONType):
return e.compileMarshalJSONPtr(ctx)
case typ.Implements(marshalTextType):
return e.compileMarshalText(ctx)
case rtype_ptrTo(typ).Implements(marshalTextType):
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
p := e.ptrToUnsafePtr(ptr)
if p == nil {
e.encodeNull()
e.encodeByte(',')
e.encodeBytes([]byte{'"', '"', ','})
} else if isPtr && *(*unsafe.Pointer)(p) == nil {
e.encodeBytes([]byte{'"', '"', ','})
} else {
if isPtr && code.typ.Elem().Implements(marshalTextType) {
p = *(*unsafe.Pointer)(p)
}
v := *(*interface{})(unsafe.Pointer(&interfaceHeader{
typ: code.typ,
ptr: p,