go-json/decode_wrapped_string.go

64 lines
1.4 KiB
Go
Raw Permalink Normal View History

2020-08-20 06:38:50 +03:00
package json
import (
2021-02-16 05:46:00 +03:00
"reflect"
"unsafe"
)
2020-11-19 06:47:42 +03:00
2020-08-20 06:38:50 +03:00
type wrappedStringDecoder struct {
2021-02-16 05:46:00 +03:00
typ *rtype
2020-08-20 06:38:50 +03:00
dec decoder
stringDecoder *stringDecoder
structName string
fieldName string
2021-02-16 05:46:00 +03:00
isPtrType bool
2020-08-20 06:38:50 +03:00
}
2021-02-16 05:46:00 +03:00
func newWrappedStringDecoder(typ *rtype, dec decoder, structName, fieldName string) *wrappedStringDecoder {
2020-08-20 06:38:50 +03:00
return &wrappedStringDecoder{
2021-02-16 05:46:00 +03:00
typ: typ,
2020-08-20 06:38:50 +03:00
dec: dec,
stringDecoder: newStringDecoder(structName, fieldName),
structName: structName,
fieldName: fieldName,
2021-02-16 05:46:00 +03:00
isPtrType: typ.Kind() == reflect.Ptr,
2020-08-20 06:38:50 +03:00
}
}
func (d *wrappedStringDecoder) decodeStream(s *stream, depth int64, p unsafe.Pointer) error {
2020-08-20 06:38:50 +03:00
bytes, err := d.stringDecoder.decodeStreamByte(s)
if err != nil {
return err
}
2021-02-16 05:46:00 +03:00
if bytes == nil {
if d.isPtrType {
*(*unsafe.Pointer)(p) = nil
}
return nil
}
2020-12-05 16:27:33 +03:00
b := make([]byte, len(bytes)+1)
copy(b, bytes)
if _, err := d.dec.decode(b, 0, depth, p); err != nil {
2020-12-05 16:27:33 +03:00
return err
2020-08-20 06:38:50 +03:00
}
return nil
}
func (d *wrappedStringDecoder) decode(buf []byte, cursor, depth int64, p unsafe.Pointer) (int64, error) {
2020-08-20 06:38:50 +03:00
bytes, c, err := d.stringDecoder.decodeByte(buf, cursor)
if err != nil {
return 0, err
}
2021-02-16 05:46:00 +03:00
if bytes == nil {
if d.isPtrType {
*(*unsafe.Pointer)(p) = nil
}
return c, nil
}
2020-08-20 06:38:50 +03:00
bytes = append(bytes, nul)
if _, err := d.dec.decode(bytes, 0, depth, p); err != nil {
2020-08-20 06:38:50 +03:00
return 0, err
}
return c, nil
}