2020-03-10 12:20:34 -07:00
|
|
|
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
|
|
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
|
|
|
|
package codec
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"unicode"
|
2020-06-12 07:41:02 -07:00
|
|
|
|
|
|
|
"github.com/ava-labs/gecko/utils/wrappers"
|
2020-03-10 12:20:34 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-03-12 09:11:45 -07:00
|
|
|
defaultMaxSize = 1 << 18 // default max size, in bytes, of something being marshalled by Marshal()
|
2020-06-12 13:52:58 -07:00
|
|
|
defaultMaxSliceLength = 1 << 18 // default max length of a slice being marshalled by Marshal(). Should be <= math.MaxUint32.
|
2020-06-15 07:06:40 -07:00
|
|
|
|
|
|
|
// initial capacity of byte slice that values are marshaled into.
|
|
|
|
// Larger value --> need less memory allocations but possibly have allocated but unused memory
|
|
|
|
// Smaller value --> need more memory allocations but more efficient use of allocated memory
|
2020-06-15 07:46:22 -07:00
|
|
|
initialSliceCap = 1024
|
2020-03-10 12:20:34 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-06-15 07:06:40 -07:00
|
|
|
errNil = errors.New("can't marshal/unmarshal nil pointer or interface")
|
|
|
|
errNeedPointer = errors.New("argument to unmarshal should be a pointer")
|
2020-03-10 12:20:34 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// Codec handles marshaling and unmarshaling of structs
|
|
|
|
type codec struct {
|
|
|
|
maxSize int
|
|
|
|
maxSliceLen int
|
|
|
|
|
|
|
|
typeIDToType map[uint32]reflect.Type
|
|
|
|
typeToTypeID map[reflect.Type]uint32
|
2020-06-14 07:56:43 -07:00
|
|
|
|
2020-06-15 07:06:40 -07:00
|
|
|
// Key: a struct type
|
|
|
|
// Value: Slice where each element is index in the struct type
|
|
|
|
// of a field that is serialized/deserialized
|
|
|
|
// e.g. Foo --> [1,5,8] means Foo.Field(1), etc. are to be serialized/deserialized
|
|
|
|
// We assume this cache is pretty small (a few hundred keys at most)
|
|
|
|
// and doesn't take up much memory
|
2020-06-14 08:53:19 -07:00
|
|
|
serializedFieldIndices map[reflect.Type][]int
|
2020-03-10 12:20:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Codec marshals and unmarshals
|
|
|
|
type Codec interface {
|
|
|
|
RegisterType(interface{}) error
|
|
|
|
Marshal(interface{}) ([]byte, error)
|
|
|
|
Unmarshal([]byte, interface{}) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new codec
|
|
|
|
func New(maxSize, maxSliceLen int) Codec {
|
2020-06-14 07:56:43 -07:00
|
|
|
return &codec{
|
2020-06-14 08:53:19 -07:00
|
|
|
maxSize: maxSize,
|
|
|
|
maxSliceLen: maxSliceLen,
|
|
|
|
typeIDToType: map[uint32]reflect.Type{},
|
|
|
|
typeToTypeID: map[reflect.Type]uint32{},
|
|
|
|
serializedFieldIndices: map[reflect.Type][]int{},
|
2020-03-10 12:20:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDefault returns a new codec with reasonable default values
|
|
|
|
func NewDefault() Codec { return New(defaultMaxSize, defaultMaxSliceLength) }
|
|
|
|
|
2020-06-10 13:20:40 -07:00
|
|
|
// RegisterType is used to register types that may be unmarshaled into an interface
|
2020-03-10 12:20:34 -07:00
|
|
|
// [val] is a value of the type being registered
|
2020-06-14 07:56:43 -07:00
|
|
|
func (c *codec) RegisterType(val interface{}) error {
|
2020-03-10 12:20:34 -07:00
|
|
|
valType := reflect.TypeOf(val)
|
|
|
|
if _, exists := c.typeToTypeID[valType]; exists {
|
|
|
|
return fmt.Errorf("type %v has already been registered", valType)
|
|
|
|
}
|
|
|
|
c.typeIDToType[uint32(len(c.typeIDToType))] = reflect.TypeOf(val)
|
|
|
|
c.typeToTypeID[valType] = uint32(len(c.typeIDToType) - 1)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// A few notes:
|
|
|
|
// 1) See codec_test.go for examples of usage
|
|
|
|
// 2) We use "marshal" and "serialize" interchangeably, and "unmarshal" and "deserialize" interchangeably
|
|
|
|
// 3) To include a field of a struct in the serialized form, add the tag `serialize:"true"` to it
|
|
|
|
// 4) These typed members of a struct may be serialized:
|
2020-06-10 13:20:40 -07:00
|
|
|
// bool, string, uint[8,16,32,64], int[8,16,32,64],
|
2020-03-10 12:20:34 -07:00
|
|
|
// structs, slices, arrays, interface.
|
2020-06-10 13:20:40 -07:00
|
|
|
// structs, slices and arrays can only be serialized if their constituent values can be.
|
|
|
|
// 5) To marshal an interface, you must pass a pointer to the value
|
|
|
|
// 6) To unmarshal an interface, you must call codec.RegisterType([instance of the type that fulfills the interface]).
|
2020-06-12 07:41:02 -07:00
|
|
|
// 7) Serialized fields must be exported
|
2020-06-12 13:52:58 -07:00
|
|
|
// 8) nil slices are marshaled as empty slices
|
2020-03-10 12:20:34 -07:00
|
|
|
|
2020-06-10 13:20:40 -07:00
|
|
|
// To marshal an interface, [value] must be a pointer to the interface
|
2020-06-14 07:56:43 -07:00
|
|
|
func (c *codec) Marshal(value interface{}) ([]byte, error) {
|
2020-03-10 12:20:34 -07:00
|
|
|
if value == nil {
|
2020-06-15 07:06:40 -07:00
|
|
|
return nil, errNil // can't marshal nil
|
2020-03-10 12:20:34 -07:00
|
|
|
}
|
2020-06-12 16:03:08 -07:00
|
|
|
|
2020-06-15 07:06:40 -07:00
|
|
|
p := &wrappers.Packer{MaxSize: c.maxSize, Bytes: make([]byte, 0, initialSliceCap)}
|
2020-06-14 18:38:07 -07:00
|
|
|
if err := c.marshal(reflect.ValueOf(value), p); err != nil {
|
2020-06-11 15:16:21 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-10 12:20:34 -07:00
|
|
|
|
2020-06-12 07:41:02 -07:00
|
|
|
return p.Bytes, nil
|
2020-06-11 15:16:21 -07:00
|
|
|
}
|
2020-03-10 12:20:34 -07:00
|
|
|
|
2020-06-15 07:06:40 -07:00
|
|
|
// marshal writes the byte representation of [value] to [p]
|
|
|
|
// [value]'s underlying value must not be a nil pointer or interface
|
2020-06-14 18:38:07 -07:00
|
|
|
func (c *codec) marshal(value reflect.Value, p *wrappers.Packer) error {
|
2020-03-10 12:20:34 -07:00
|
|
|
valueKind := value.Kind()
|
|
|
|
switch valueKind {
|
2020-06-11 15:16:21 -07:00
|
|
|
case reflect.Interface, reflect.Ptr, reflect.Invalid:
|
2020-06-14 09:23:05 -07:00
|
|
|
if value.IsNil() { // Can't marshal nil (except nil slices)
|
2020-06-14 18:38:07 -07:00
|
|
|
return errNil
|
2020-03-10 12:20:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch valueKind {
|
|
|
|
case reflect.Uint8:
|
2020-06-14 18:38:07 -07:00
|
|
|
p.PackByte(uint8(value.Uint()))
|
|
|
|
return p.Err
|
2020-03-10 12:20:34 -07:00
|
|
|
case reflect.Int8:
|
2020-06-14 18:38:07 -07:00
|
|
|
p.PackByte(uint8(value.Int()))
|
|
|
|
return p.Err
|
2020-03-10 12:20:34 -07:00
|
|
|
case reflect.Uint16:
|
2020-06-14 18:38:07 -07:00
|
|
|
p.PackShort(uint16(value.Uint()))
|
|
|
|
return p.Err
|
2020-03-10 12:20:34 -07:00
|
|
|
case reflect.Int16:
|
2020-06-14 18:38:07 -07:00
|
|
|
p.PackShort(uint16(value.Int()))
|
|
|
|
return p.Err
|
2020-03-10 12:20:34 -07:00
|
|
|
case reflect.Uint32:
|
2020-06-14 18:38:07 -07:00
|
|
|
p.PackInt(uint32(value.Uint()))
|
|
|
|
return p.Err
|
2020-03-10 12:20:34 -07:00
|
|
|
case reflect.Int32:
|
2020-06-14 18:38:07 -07:00
|
|
|
p.PackInt(uint32(value.Int()))
|
|
|
|
return p.Err
|
2020-03-10 12:20:34 -07:00
|
|
|
case reflect.Uint64:
|
2020-06-14 18:38:07 -07:00
|
|
|
p.PackLong(value.Uint())
|
|
|
|
return p.Err
|
2020-03-10 12:20:34 -07:00
|
|
|
case reflect.Int64:
|
2020-06-14 18:38:07 -07:00
|
|
|
p.PackLong(uint64(value.Int()))
|
|
|
|
return p.Err
|
2020-03-10 12:20:34 -07:00
|
|
|
case reflect.String:
|
2020-06-14 18:38:07 -07:00
|
|
|
p.PackStr(value.String())
|
|
|
|
return p.Err
|
2020-03-10 12:20:34 -07:00
|
|
|
case reflect.Bool:
|
2020-06-14 18:38:07 -07:00
|
|
|
p.PackBool(value.Bool())
|
|
|
|
return p.Err
|
2020-06-11 15:16:21 -07:00
|
|
|
case reflect.Uintptr, reflect.Ptr:
|
2020-06-14 18:38:07 -07:00
|
|
|
return c.marshal(value.Elem(), p)
|
2020-03-10 12:20:34 -07:00
|
|
|
case reflect.Interface:
|
2020-06-14 08:53:19 -07:00
|
|
|
underlyingValue := value.Interface()
|
|
|
|
typeID, ok := c.typeToTypeID[reflect.TypeOf(underlyingValue)] // Get the type ID of the value being marshaled
|
2020-03-10 12:20:34 -07:00
|
|
|
if !ok {
|
2020-06-14 18:38:07 -07:00
|
|
|
return fmt.Errorf("can't marshal unregistered type '%v'", reflect.TypeOf(underlyingValue).String())
|
2020-03-10 12:20:34 -07:00
|
|
|
}
|
2020-06-15 07:06:40 -07:00
|
|
|
p.PackInt(typeID) // Pack type ID so we know what to unmarshal this into
|
2020-06-14 18:38:07 -07:00
|
|
|
if p.Err != nil {
|
2020-06-14 07:56:43 -07:00
|
|
|
return p.Err
|
2020-03-10 12:20:34 -07:00
|
|
|
}
|
2020-06-14 18:38:07 -07:00
|
|
|
if err := c.marshal(value.Elem(), p); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return p.Err
|
2020-06-11 15:16:21 -07:00
|
|
|
case reflect.Slice:
|
2020-06-12 13:52:58 -07:00
|
|
|
numElts := value.Len() // # elements in the slice/array. 0 if this slice is nil.
|
2020-06-11 15:16:21 -07:00
|
|
|
if numElts > c.maxSliceLen {
|
2020-06-14 18:38:07 -07:00
|
|
|
return fmt.Errorf("slice length, %d, exceeds maximum length, %d", numElts, c.maxSliceLen)
|
|
|
|
}
|
|
|
|
p.PackInt(uint32(numElts)) // pack # elements
|
|
|
|
if p.Err != nil {
|
|
|
|
return p.Err
|
2020-03-10 12:20:34 -07:00
|
|
|
}
|
2020-06-12 13:52:58 -07:00
|
|
|
for i := 0; i < numElts; i++ { // Process each element in the slice
|
2020-06-14 18:38:07 -07:00
|
|
|
if err := c.marshal(value.Index(i), p); err != nil {
|
|
|
|
return err
|
2020-06-11 15:16:21 -07:00
|
|
|
}
|
|
|
|
}
|
2020-06-14 18:38:07 -07:00
|
|
|
return nil
|
2020-06-11 15:16:21 -07:00
|
|
|
case reflect.Array:
|
2020-06-12 13:52:58 -07:00
|
|
|
numElts := value.Len()
|
|
|
|
if numElts > c.maxSliceLen {
|
2020-06-14 18:38:07 -07:00
|
|
|
return fmt.Errorf("array length, %d, exceeds maximum length, %d", numElts, c.maxSliceLen)
|
2020-06-11 15:16:21 -07:00
|
|
|
}
|
|
|
|
for i := 0; i < numElts; i++ { // Process each element in the array
|
2020-06-14 18:38:07 -07:00
|
|
|
if err := c.marshal(value.Index(i), p); err != nil {
|
|
|
|
return err
|
2020-06-11 15:16:21 -07:00
|
|
|
}
|
|
|
|
}
|
2020-06-14 18:38:07 -07:00
|
|
|
return nil
|
2020-03-10 12:20:34 -07:00
|
|
|
case reflect.Struct:
|
2020-06-15 07:06:40 -07:00
|
|
|
serializedFields, err := c.getSerializedFieldIndices(value.Type())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-06-14 07:56:43 -07:00
|
|
|
}
|
2020-06-15 07:06:40 -07:00
|
|
|
for _, fieldIndex := range serializedFields { // Go through all fields of this struct that are serialized
|
|
|
|
if err := c.marshal(value.Field(fieldIndex), p); err != nil { // Serialize the field and write to byte array
|
2020-06-14 18:38:07 -07:00
|
|
|
return err
|
2020-06-11 15:16:21 -07:00
|
|
|
}
|
|
|
|
}
|
2020-06-14 18:38:07 -07:00
|
|
|
return nil
|
2020-03-10 12:20:34 -07:00
|
|
|
default:
|
2020-06-15 07:06:40 -07:00
|
|
|
return fmt.Errorf("can't marshal unknown kind %s", valueKind)
|
2020-03-10 12:20:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal unmarshals [bytes] into [dest], where
|
|
|
|
// [dest] must be a pointer or interface
|
2020-06-14 07:56:43 -07:00
|
|
|
func (c *codec) Unmarshal(bytes []byte, dest interface{}) error {
|
2020-06-11 15:16:21 -07:00
|
|
|
switch {
|
|
|
|
case len(bytes) > c.maxSize:
|
2020-06-15 07:06:40 -07:00
|
|
|
return fmt.Errorf("byte array exceeds maximum length, %d", c.maxSize)
|
2020-06-11 15:16:21 -07:00
|
|
|
case dest == nil:
|
2020-03-10 12:20:34 -07:00
|
|
|
return errNil
|
|
|
|
}
|
|
|
|
|
|
|
|
destPtr := reflect.ValueOf(dest)
|
|
|
|
if destPtr.Kind() != reflect.Ptr {
|
|
|
|
return errNeedPointer
|
|
|
|
}
|
|
|
|
|
2020-06-12 07:41:02 -07:00
|
|
|
p := &wrappers.Packer{MaxSize: c.maxSize, Bytes: bytes}
|
2020-03-10 12:20:34 -07:00
|
|
|
destVal := destPtr.Elem()
|
2020-06-12 07:41:02 -07:00
|
|
|
if err := c.unmarshal(p, destVal); err != nil {
|
2020-03-10 12:20:34 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-15 07:06:40 -07:00
|
|
|
// Unmarshal from p.Bytes into [value]. [value] must be addressable.
|
2020-06-14 07:56:43 -07:00
|
|
|
func (c *codec) unmarshal(p *wrappers.Packer, value reflect.Value) error {
|
|
|
|
switch value.Kind() {
|
2020-03-10 12:20:34 -07:00
|
|
|
case reflect.Uint8:
|
2020-06-14 09:06:39 -07:00
|
|
|
value.SetUint(uint64(p.UnpackByte()))
|
2020-06-15 07:06:40 -07:00
|
|
|
if p.Err != nil {
|
|
|
|
return fmt.Errorf("couldn't marshal uint8: %s", p.Err)
|
|
|
|
}
|
|
|
|
return nil
|
2020-03-10 12:20:34 -07:00
|
|
|
case reflect.Int8:
|
2020-06-14 09:06:39 -07:00
|
|
|
value.SetInt(int64(p.UnpackByte()))
|
2020-06-15 07:06:40 -07:00
|
|
|
if p.Err != nil {
|
|
|
|
return fmt.Errorf("couldn't marshal int8: %s", p.Err)
|
|
|
|
}
|
|
|
|
return nil
|
2020-03-10 12:20:34 -07:00
|
|
|
case reflect.Uint16:
|
2020-06-14 09:06:39 -07:00
|
|
|
value.SetUint(uint64(p.UnpackShort()))
|
2020-06-15 07:06:40 -07:00
|
|
|
if p.Err != nil {
|
|
|
|
return fmt.Errorf("couldn't marshal uint16: %s", p.Err)
|
|
|
|
}
|
|
|
|
return nil
|
2020-03-10 12:20:34 -07:00
|
|
|
case reflect.Int16:
|
2020-06-14 09:06:39 -07:00
|
|
|
value.SetInt(int64(p.UnpackShort()))
|
2020-06-15 07:06:40 -07:00
|
|
|
if p.Err != nil {
|
|
|
|
return fmt.Errorf("couldn't marshal int16: %s", p.Err)
|
|
|
|
}
|
|
|
|
return nil
|
2020-03-10 12:20:34 -07:00
|
|
|
case reflect.Uint32:
|
2020-06-14 09:06:39 -07:00
|
|
|
value.SetUint(uint64(p.UnpackInt()))
|
2020-06-15 07:06:40 -07:00
|
|
|
if p.Err != nil {
|
|
|
|
return fmt.Errorf("couldn't marshal uint32: %s", p.Err)
|
|
|
|
}
|
|
|
|
return nil
|
2020-03-10 12:20:34 -07:00
|
|
|
case reflect.Int32:
|
2020-06-14 09:06:39 -07:00
|
|
|
value.SetInt(int64(p.UnpackInt()))
|
2020-06-15 07:06:40 -07:00
|
|
|
if p.Err != nil {
|
|
|
|
return fmt.Errorf("couldn't marshal int32: %s", p.Err)
|
|
|
|
}
|
|
|
|
return nil
|
2020-03-10 12:20:34 -07:00
|
|
|
case reflect.Uint64:
|
2020-06-14 09:06:39 -07:00
|
|
|
value.SetUint(uint64(p.UnpackLong()))
|
2020-06-15 07:06:40 -07:00
|
|
|
if p.Err != nil {
|
|
|
|
return fmt.Errorf("couldn't marshal uint64: %s", p.Err)
|
|
|
|
}
|
|
|
|
return nil
|
2020-03-10 12:20:34 -07:00
|
|
|
case reflect.Int64:
|
2020-06-14 09:06:39 -07:00
|
|
|
value.SetInt(int64(p.UnpackLong()))
|
2020-06-15 07:06:40 -07:00
|
|
|
if p.Err != nil {
|
|
|
|
return fmt.Errorf("couldn't marshal int64: %s", p.Err)
|
|
|
|
}
|
|
|
|
return nil
|
2020-03-10 12:20:34 -07:00
|
|
|
case reflect.Bool:
|
2020-06-14 09:06:39 -07:00
|
|
|
value.SetBool(p.UnpackBool())
|
2020-06-15 07:06:40 -07:00
|
|
|
if p.Err != nil {
|
|
|
|
return fmt.Errorf("couldn't marshal bool: %s", p.Err)
|
|
|
|
}
|
|
|
|
return nil
|
2020-03-10 12:20:34 -07:00
|
|
|
case reflect.Slice:
|
2020-06-12 13:52:58 -07:00
|
|
|
numElts := int(p.UnpackInt())
|
2020-06-12 07:41:02 -07:00
|
|
|
if p.Err != nil {
|
2020-06-15 07:06:40 -07:00
|
|
|
return fmt.Errorf("couldn't marshal slice: %s", p.Err)
|
2020-03-10 12:20:34 -07:00
|
|
|
}
|
2020-06-15 07:06:40 -07:00
|
|
|
// set [value] to be a slice of the appropriate type/capacity (right now it is nil)
|
2020-06-14 08:53:19 -07:00
|
|
|
value.Set(reflect.MakeSlice(value.Type(), numElts, numElts))
|
2020-03-10 12:20:34 -07:00
|
|
|
// Unmarshal each element into the appropriate index of the slice
|
2020-06-11 15:16:21 -07:00
|
|
|
for i := 0; i < numElts; i++ {
|
2020-06-14 07:56:43 -07:00
|
|
|
if err := c.unmarshal(p, value.Index(i)); err != nil {
|
2020-06-15 07:06:40 -07:00
|
|
|
return fmt.Errorf("couldn't marshal slice element: %s", err)
|
2020-03-10 12:20:34 -07:00
|
|
|
}
|
|
|
|
}
|
2020-06-12 07:41:02 -07:00
|
|
|
return nil
|
2020-03-10 12:20:34 -07:00
|
|
|
case reflect.Array:
|
2020-06-14 07:56:43 -07:00
|
|
|
for i := 0; i < value.Len(); i++ {
|
|
|
|
if err := c.unmarshal(p, value.Index(i)); err != nil {
|
2020-06-15 07:06:40 -07:00
|
|
|
return fmt.Errorf("couldn't marshal array element: %s", err)
|
2020-06-11 15:16:21 -07:00
|
|
|
}
|
2020-03-10 12:20:34 -07:00
|
|
|
}
|
2020-06-12 07:41:02 -07:00
|
|
|
return nil
|
2020-03-10 12:20:34 -07:00
|
|
|
case reflect.String:
|
2020-06-14 08:53:19 -07:00
|
|
|
value.SetString(p.UnpackStr())
|
2020-06-15 07:06:40 -07:00
|
|
|
if p.Err != nil {
|
|
|
|
return fmt.Errorf("couldn't marshal string: %s", p.Err)
|
|
|
|
}
|
|
|
|
return nil
|
2020-03-10 12:20:34 -07:00
|
|
|
case reflect.Interface:
|
2020-06-12 07:41:02 -07:00
|
|
|
typeID := p.UnpackInt() // Get the type ID
|
|
|
|
if p.Err != nil {
|
2020-06-15 07:06:40 -07:00
|
|
|
return fmt.Errorf("couldn't marshal interface: %s", p.Err)
|
2020-06-11 15:16:21 -07:00
|
|
|
}
|
2020-06-14 09:06:39 -07:00
|
|
|
// Get a type that implements the interface
|
2020-06-15 07:06:40 -07:00
|
|
|
implementingType, ok := c.typeIDToType[typeID]
|
2020-03-10 12:20:34 -07:00
|
|
|
if !ok {
|
2020-06-15 07:06:40 -07:00
|
|
|
return fmt.Errorf("couldn't marshal interface: unknown type ID %d", typeID)
|
2020-03-10 12:20:34 -07:00
|
|
|
}
|
2020-06-14 09:06:39 -07:00
|
|
|
// Ensure type actually does implement the interface
|
2020-06-15 07:06:40 -07:00
|
|
|
if valueType := value.Type(); !implementingType.Implements(valueType) {
|
|
|
|
return fmt.Errorf("couldn't marshal interface: %s does not implement interface %s", implementingType, valueType)
|
2020-03-26 12:14:45 -07:00
|
|
|
}
|
2020-06-15 07:06:40 -07:00
|
|
|
intfImplementor := reflect.New(implementingType).Elem() // instance of the proper type
|
2020-03-10 12:20:34 -07:00
|
|
|
// Unmarshal into the struct
|
2020-06-15 07:06:40 -07:00
|
|
|
if err := c.unmarshal(p, intfImplementor); err != nil {
|
|
|
|
return fmt.Errorf("couldn't marshal interface: %s", err)
|
2020-03-10 12:20:34 -07:00
|
|
|
}
|
2020-06-14 07:56:43 -07:00
|
|
|
// And assign the filled struct to the value
|
2020-06-15 07:06:40 -07:00
|
|
|
value.Set(intfImplementor)
|
2020-06-12 07:41:02 -07:00
|
|
|
return nil
|
2020-03-10 12:20:34 -07:00
|
|
|
case reflect.Struct:
|
2020-06-15 07:06:40 -07:00
|
|
|
// Get indices of fields that will be unmarshaled into
|
2020-06-14 09:06:39 -07:00
|
|
|
serializedFieldIndices, err := c.getSerializedFieldIndices(value.Type())
|
2020-06-14 07:56:43 -07:00
|
|
|
if err != nil {
|
2020-06-15 07:06:40 -07:00
|
|
|
return fmt.Errorf("couldn't marshal struct: %s", err)
|
2020-06-14 07:56:43 -07:00
|
|
|
}
|
2020-06-15 07:06:40 -07:00
|
|
|
// Go through the fields and umarshal into them
|
2020-06-14 07:56:43 -07:00
|
|
|
for _, index := range serializedFieldIndices {
|
2020-06-15 07:06:40 -07:00
|
|
|
if err := c.unmarshal(p, value.Field(index)); err != nil {
|
|
|
|
return fmt.Errorf("couldn't marshal struct: %s", err)
|
2020-03-10 12:20:34 -07:00
|
|
|
}
|
|
|
|
}
|
2020-06-12 07:41:02 -07:00
|
|
|
return nil
|
2020-03-10 12:20:34 -07:00
|
|
|
case reflect.Ptr:
|
|
|
|
// Get the type this pointer points to
|
2020-06-15 07:06:40 -07:00
|
|
|
t := value.Type().Elem()
|
2020-03-10 12:20:34 -07:00
|
|
|
// Create a new pointer to a new value of the underlying type
|
2020-06-15 07:06:40 -07:00
|
|
|
v := reflect.New(t)
|
2020-03-10 12:20:34 -07:00
|
|
|
// Fill the value
|
2020-06-15 07:06:40 -07:00
|
|
|
if err := c.unmarshal(p, v.Elem()); err != nil {
|
|
|
|
return fmt.Errorf("couldn't marshal pointer: %s", err)
|
2020-03-10 12:20:34 -07:00
|
|
|
}
|
|
|
|
// Assign to the top-level struct's member
|
2020-06-15 07:06:40 -07:00
|
|
|
value.Set(v)
|
2020-06-12 07:41:02 -07:00
|
|
|
return nil
|
2020-03-10 12:20:34 -07:00
|
|
|
case reflect.Invalid:
|
2020-06-12 07:41:02 -07:00
|
|
|
return errNil
|
2020-03-10 12:20:34 -07:00
|
|
|
default:
|
2020-06-15 07:06:40 -07:00
|
|
|
return fmt.Errorf("can't unmarshal unknown type %s", value.Kind().String())
|
2020-03-10 12:20:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-14 07:56:43 -07:00
|
|
|
// Returns the indices of the serializable fields of [t], which is a struct type
|
|
|
|
// Returns an error if a field has tag "serialize: true" but the field is unexported
|
2020-06-15 07:06:40 -07:00
|
|
|
// e.g. getSerializedFieldIndices(Foo) --> [1,5,8] means Foo.Field(1), Foo.Field(5), Foo.Field(8)
|
|
|
|
// are to be serialized/deserialized
|
2020-06-14 07:56:43 -07:00
|
|
|
func (c *codec) getSerializedFieldIndices(t reflect.Type) ([]int, error) {
|
2020-06-14 08:53:19 -07:00
|
|
|
if c.serializedFieldIndices == nil {
|
|
|
|
c.serializedFieldIndices = make(map[reflect.Type][]int)
|
2020-06-14 07:56:43 -07:00
|
|
|
}
|
2020-06-15 07:06:40 -07:00
|
|
|
if serializedFields, ok := c.serializedFieldIndices[t]; ok { // use pre-computed result
|
2020-06-14 07:56:43 -07:00
|
|
|
return serializedFields, nil
|
|
|
|
}
|
|
|
|
numFields := t.NumField()
|
|
|
|
serializedFields := make([]int, 0, numFields)
|
|
|
|
for i := 0; i < numFields; i++ { // Go through all fields of this struct
|
|
|
|
field := t.Field(i)
|
|
|
|
if field.Tag.Get("serialize") != "true" { // Skip fields we don't need to serialize
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if unicode.IsLower(rune(field.Name[0])) { // Can only marshal exported fields
|
|
|
|
return []int{}, fmt.Errorf("can't marshal unexported field %s", field.Name)
|
|
|
|
}
|
|
|
|
serializedFields = append(serializedFields, i)
|
|
|
|
}
|
2020-06-15 07:06:40 -07:00
|
|
|
c.serializedFieldIndices[t] = serializedFields // cache result
|
2020-06-14 07:56:43 -07:00
|
|
|
return serializedFields, nil
|
2020-03-10 12:20:34 -07:00
|
|
|
}
|