go-json/internal/decoder/anonymous_field.go

38 lines
996 B
Go
Raw Normal View History

2021-06-03 12:49:01 +03:00
package decoder
2020-12-22 15:55:59 +03:00
import (
"unsafe"
2021-06-03 12:49:01 +03:00
"github.com/goccy/go-json/internal/runtime"
2020-12-22 15:55:59 +03:00
)
type anonymousFieldDecoder struct {
2021-06-03 12:49:01 +03:00
structType *runtime.Type
2020-12-22 15:55:59 +03:00
offset uintptr
dec decoder
}
2021-06-03 12:49:01 +03:00
func newAnonymousFieldDecoder(structType *runtime.Type, offset uintptr, dec decoder) *anonymousFieldDecoder {
2020-12-22 15:55:59 +03:00
return &anonymousFieldDecoder{
structType: structType,
offset: offset,
dec: dec,
}
}
2021-06-03 12:49:01 +03:00
func (d *anonymousFieldDecoder) DecodeStream(s *Stream, depth int64, p unsafe.Pointer) error {
2020-12-22 15:55:59 +03:00
if *(*unsafe.Pointer)(p) == nil {
*(*unsafe.Pointer)(p) = unsafe_New(d.structType)
}
p = *(*unsafe.Pointer)(p)
2021-06-03 12:49:01 +03:00
return d.dec.DecodeStream(s, depth, unsafe.Pointer(uintptr(p)+d.offset))
2020-12-22 15:55:59 +03:00
}
2021-06-03 12:49:01 +03:00
func (d *anonymousFieldDecoder) Decode(buf []byte, cursor, depth int64, p unsafe.Pointer) (int64, error) {
2020-12-22 15:55:59 +03:00
if *(*unsafe.Pointer)(p) == nil {
*(*unsafe.Pointer)(p) = unsafe_New(d.structType)
}
p = *(*unsafe.Pointer)(p)
2021-06-03 12:49:01 +03:00
return d.dec.Decode(buf, cursor, depth, unsafe.Pointer(uintptr(p)+d.offset))
2020-12-22 15:55:59 +03:00
}