|
@@ -101,6 +101,7 @@ package codec
|
|
|
// check for these error conditions.
|
|
|
|
|
|
import (
|
|
|
+ "bytes"
|
|
|
"encoding"
|
|
|
"encoding/binary"
|
|
|
"errors"
|
|
@@ -111,12 +112,11 @@ import (
|
|
|
"strings"
|
|
|
"sync"
|
|
|
"time"
|
|
|
- "unicode"
|
|
|
- "unicode/utf8"
|
|
|
)
|
|
|
|
|
|
const (
|
|
|
scratchByteArrayLen = 32
|
|
|
+ initCollectionCap = 32 // 32 is defensive. 16 is preferred.
|
|
|
|
|
|
// Support encoding.(Binary|Text)(Unm|M)arshaler.
|
|
|
// This constant flag will enable or disable it.
|
|
@@ -147,6 +147,12 @@ const (
|
|
|
|
|
|
// if derefForIsEmptyValue, deref pointers and interfaces when checking isEmptyValue
|
|
|
derefForIsEmptyValue = false
|
|
|
+
|
|
|
+ // if resetSliceElemToZeroValue, then on decoding a slice, reset the element to a zero value first.
|
|
|
+ // Only concern is that, if the slice already contained some garbage, we will decode into that garbage.
|
|
|
+ // The chances of this are slim, so leave this "optimization".
|
|
|
+ // TODO: should this be true, to ensure that we always decode into a "zero" "empty" value?
|
|
|
+ resetSliceElemToZeroValue bool = false
|
|
|
)
|
|
|
|
|
|
var oneByteArr = [1]byte{0}
|
|
@@ -193,16 +199,43 @@ const (
|
|
|
seqTypeChan
|
|
|
)
|
|
|
|
|
|
+// note that containerMapStart and containerArraySend are not sent.
|
|
|
+// This is because the ReadXXXStart and EncodeXXXStart already does these.
|
|
|
+type containerState uint8
|
|
|
+
|
|
|
+const (
|
|
|
+ _ containerState = iota
|
|
|
+
|
|
|
+ containerMapStart // slot left open, since Driver method already covers it
|
|
|
+ containerMapKey
|
|
|
+ containerMapValue
|
|
|
+ containerMapEnd
|
|
|
+ containerArrayStart // slot left open, since Driver methods already cover it
|
|
|
+ containerArrayElem
|
|
|
+ containerArrayEnd
|
|
|
+)
|
|
|
+
|
|
|
+type containerStateRecv interface {
|
|
|
+ sendContainerState(containerState)
|
|
|
+}
|
|
|
+
|
|
|
+// mirror json.Marshaler and json.Unmarshaler here,
|
|
|
+// so we don't import the encoding/json package
|
|
|
+type jsonMarshaler interface {
|
|
|
+ MarshalJSON() ([]byte, error)
|
|
|
+}
|
|
|
+type jsonUnmarshaler interface {
|
|
|
+ UnmarshalJSON([]byte) error
|
|
|
+}
|
|
|
+
|
|
|
var (
|
|
|
bigen = binary.BigEndian
|
|
|
structInfoFieldName = "_struct"
|
|
|
|
|
|
- cachedTypeInfo = make(map[uintptr]*typeInfo, 64)
|
|
|
- cachedTypeInfoMutex sync.RWMutex
|
|
|
-
|
|
|
- // mapStrIntfTyp = reflect.TypeOf(map[string]interface{}(nil))
|
|
|
- intfSliceTyp = reflect.TypeOf([]interface{}(nil))
|
|
|
- intfTyp = intfSliceTyp.Elem()
|
|
|
+ mapStrIntfTyp = reflect.TypeOf(map[string]interface{}(nil))
|
|
|
+ mapIntfIntfTyp = reflect.TypeOf(map[interface{}]interface{}(nil))
|
|
|
+ intfSliceTyp = reflect.TypeOf([]interface{}(nil))
|
|
|
+ intfTyp = intfSliceTyp.Elem()
|
|
|
|
|
|
stringTyp = reflect.TypeOf("")
|
|
|
timeTyp = reflect.TypeOf(time.Time{})
|
|
@@ -217,6 +250,9 @@ var (
|
|
|
textMarshalerTyp = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()
|
|
|
textUnmarshalerTyp = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
|
|
|
|
|
|
+ jsonMarshalerTyp = reflect.TypeOf((*jsonMarshaler)(nil)).Elem()
|
|
|
+ jsonUnmarshalerTyp = reflect.TypeOf((*jsonUnmarshaler)(nil)).Elem()
|
|
|
+
|
|
|
selferTyp = reflect.TypeOf((*Selfer)(nil)).Elem()
|
|
|
|
|
|
uint8SliceTypId = reflect.ValueOf(uint8SliceTyp).Pointer()
|
|
@@ -225,6 +261,9 @@ var (
|
|
|
timeTypId = reflect.ValueOf(timeTyp).Pointer()
|
|
|
stringTypId = reflect.ValueOf(stringTyp).Pointer()
|
|
|
|
|
|
+ mapStrIntfTypId = reflect.ValueOf(mapStrIntfTyp).Pointer()
|
|
|
+ mapIntfIntfTypId = reflect.ValueOf(mapIntfIntfTyp).Pointer()
|
|
|
+ intfSliceTypId = reflect.ValueOf(intfSliceTyp).Pointer()
|
|
|
// mapBySliceTypId = reflect.ValueOf(mapBySliceTyp).Pointer()
|
|
|
|
|
|
intBitsize uint8 = uint8(reflect.TypeOf(int(0)).Bits())
|
|
@@ -238,6 +277,8 @@ var (
|
|
|
noFieldNameToStructFieldInfoErr = errors.New("no field name passed to parseStructFieldInfo")
|
|
|
)
|
|
|
|
|
|
+var defTypeInfos = NewTypeInfos([]string{"codec", "json"})
|
|
|
+
|
|
|
// Selfer defines methods by which a value can encode or decode itself.
|
|
|
//
|
|
|
// Any type which implements Selfer will be able to encode or decode itself.
|
|
@@ -263,6 +304,11 @@ type MapBySlice interface {
|
|
|
//
|
|
|
// BasicHandle encapsulates the common options and extension functions.
|
|
|
type BasicHandle struct {
|
|
|
+ // TypeInfos is used to get the type info for any type.
|
|
|
+ //
|
|
|
+ // If not configured, the default TypeInfos is used, which uses struct tag keys: codec, json
|
|
|
+ TypeInfos *TypeInfos
|
|
|
+
|
|
|
extHandle
|
|
|
EncodeOptions
|
|
|
DecodeOptions
|
|
@@ -272,6 +318,13 @@ func (x *BasicHandle) getBasicHandle() *BasicHandle {
|
|
|
return x
|
|
|
}
|
|
|
|
|
|
+func (x *BasicHandle) getTypeInfo(rtid uintptr, rt reflect.Type) (pti *typeInfo) {
|
|
|
+ if x.TypeInfos != nil {
|
|
|
+ return x.TypeInfos.get(rtid, rt)
|
|
|
+ }
|
|
|
+ return defTypeInfos.get(rtid, rt)
|
|
|
+}
|
|
|
+
|
|
|
// Handle is the interface for a specific encoding format.
|
|
|
//
|
|
|
// Typically, a Handle is pre-configured before first time use,
|
|
@@ -298,33 +351,45 @@ type RawExt struct {
|
|
|
Value interface{}
|
|
|
}
|
|
|
|
|
|
-// Ext handles custom (de)serialization of custom types / extensions.
|
|
|
-type Ext interface {
|
|
|
+// BytesExt handles custom (de)serialization of types to/from []byte.
|
|
|
+// It is used by codecs (e.g. binc, msgpack, simple) which do custom serialization of the types.
|
|
|
+type BytesExt interface {
|
|
|
// WriteExt converts a value to a []byte.
|
|
|
- // It is used by codecs (e.g. binc, msgpack, simple) which do custom serialization of the types.
|
|
|
+ //
|
|
|
+ // Note: v *may* be a pointer to the extension type, if the extension type was a struct or array.
|
|
|
WriteExt(v interface{}) []byte
|
|
|
|
|
|
// ReadExt updates a value from a []byte.
|
|
|
- // It is used by codecs (e.g. binc, msgpack, simple) which do custom serialization of the types.
|
|
|
ReadExt(dst interface{}, src []byte)
|
|
|
+}
|
|
|
|
|
|
+// InterfaceExt handles custom (de)serialization of types to/from another interface{} value.
|
|
|
+// The Encoder or Decoder will then handle the further (de)serialization of that known type.
|
|
|
+//
|
|
|
+// It is used by codecs (e.g. cbor, json) which use the format to do custom serialization of the types.
|
|
|
+type InterfaceExt interface {
|
|
|
// ConvertExt converts a value into a simpler interface for easy encoding e.g. convert time.Time to int64.
|
|
|
- // It is used by codecs (e.g. cbor) which use the format to do custom serialization of the types.
|
|
|
+ //
|
|
|
+ // Note: v *may* be a pointer to the extension type, if the extension type was a struct or array.
|
|
|
ConvertExt(v interface{}) interface{}
|
|
|
|
|
|
// UpdateExt updates a value from a simpler interface for easy decoding e.g. convert int64 to time.Time.
|
|
|
- // It is used by codecs (e.g. cbor) which use the format to do custom serialization of the types.
|
|
|
UpdateExt(dst interface{}, src interface{})
|
|
|
}
|
|
|
|
|
|
-// bytesExt is a wrapper implementation to support former AddExt exported method.
|
|
|
-type bytesExt struct {
|
|
|
+// Ext handles custom (de)serialization of custom types / extensions.
|
|
|
+type Ext interface {
|
|
|
+ BytesExt
|
|
|
+ InterfaceExt
|
|
|
+}
|
|
|
+
|
|
|
+// addExtWrapper is a wrapper implementation to support former AddExt exported method.
|
|
|
+type addExtWrapper struct {
|
|
|
encFn func(reflect.Value) ([]byte, error)
|
|
|
decFn func(reflect.Value, []byte) error
|
|
|
}
|
|
|
|
|
|
-func (x bytesExt) WriteExt(v interface{}) []byte {
|
|
|
- // fmt.Printf(">>>>>>>>>> WriteExt: %T, %v\n", v, v)
|
|
|
+func (x addExtWrapper) WriteExt(v interface{}) []byte {
|
|
|
bs, err := x.encFn(reflect.ValueOf(v))
|
|
|
if err != nil {
|
|
|
panic(err)
|
|
@@ -332,21 +397,56 @@ func (x bytesExt) WriteExt(v interface{}) []byte {
|
|
|
return bs
|
|
|
}
|
|
|
|
|
|
-func (x bytesExt) ReadExt(v interface{}, bs []byte) {
|
|
|
- // fmt.Printf(">>>>>>>>>> ReadExt: %T, %v\n", v, v)
|
|
|
+func (x addExtWrapper) ReadExt(v interface{}, bs []byte) {
|
|
|
if err := x.decFn(reflect.ValueOf(v), bs); err != nil {
|
|
|
panic(err)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func (x bytesExt) ConvertExt(v interface{}) interface{} {
|
|
|
+func (x addExtWrapper) ConvertExt(v interface{}) interface{} {
|
|
|
return x.WriteExt(v)
|
|
|
}
|
|
|
|
|
|
-func (x bytesExt) UpdateExt(dest interface{}, v interface{}) {
|
|
|
+func (x addExtWrapper) UpdateExt(dest interface{}, v interface{}) {
|
|
|
x.ReadExt(dest, v.([]byte))
|
|
|
}
|
|
|
|
|
|
+type setExtWrapper struct {
|
|
|
+ b BytesExt
|
|
|
+ i InterfaceExt
|
|
|
+}
|
|
|
+
|
|
|
+func (x *setExtWrapper) WriteExt(v interface{}) []byte {
|
|
|
+ if x.b == nil {
|
|
|
+ panic("BytesExt.WriteExt is not supported")
|
|
|
+ }
|
|
|
+ return x.b.WriteExt(v)
|
|
|
+}
|
|
|
+
|
|
|
+func (x *setExtWrapper) ReadExt(v interface{}, bs []byte) {
|
|
|
+ if x.b == nil {
|
|
|
+ panic("BytesExt.WriteExt is not supported")
|
|
|
+
|
|
|
+ }
|
|
|
+ x.b.ReadExt(v, bs)
|
|
|
+}
|
|
|
+
|
|
|
+func (x *setExtWrapper) ConvertExt(v interface{}) interface{} {
|
|
|
+ if x.i == nil {
|
|
|
+ panic("InterfaceExt.ConvertExt is not supported")
|
|
|
+
|
|
|
+ }
|
|
|
+ return x.i.ConvertExt(v)
|
|
|
+}
|
|
|
+
|
|
|
+func (x *setExtWrapper) UpdateExt(dest interface{}, v interface{}) {
|
|
|
+ if x.i == nil {
|
|
|
+ panic("InterfaceExxt.UpdateExt is not supported")
|
|
|
+
|
|
|
+ }
|
|
|
+ x.i.UpdateExt(dest, v)
|
|
|
+}
|
|
|
+
|
|
|
// type errorString string
|
|
|
// func (x errorString) Error() string { return string(x) }
|
|
|
|
|
@@ -399,9 +499,9 @@ type extTypeTagFn struct {
|
|
|
ext Ext
|
|
|
}
|
|
|
|
|
|
-type extHandle []*extTypeTagFn
|
|
|
+type extHandle []extTypeTagFn
|
|
|
|
|
|
-// DEPRECATED: AddExt is deprecated in favor of SetExt. It exists for compatibility only.
|
|
|
+// DEPRECATED: Use SetBytesExt or SetInterfaceExt on the Handle instead.
|
|
|
//
|
|
|
// AddExt registes an encode and decode function for a reflect.Type.
|
|
|
// AddExt internally calls SetExt.
|
|
@@ -413,10 +513,10 @@ func (o *extHandle) AddExt(
|
|
|
if encfn == nil || decfn == nil {
|
|
|
return o.SetExt(rt, uint64(tag), nil)
|
|
|
}
|
|
|
- return o.SetExt(rt, uint64(tag), bytesExt{encfn, decfn})
|
|
|
+ return o.SetExt(rt, uint64(tag), addExtWrapper{encfn, decfn})
|
|
|
}
|
|
|
|
|
|
-// SetExt registers a tag and Ext for a reflect.Type.
|
|
|
+// DEPRECATED: Use SetBytesExt or SetInterfaceExt on the Handle instead.
|
|
|
//
|
|
|
// Note that the type must be a named type, and specifically not
|
|
|
// a pointer or Interface. An error is returned if that is not honored.
|
|
@@ -438,12 +538,17 @@ func (o *extHandle) SetExt(rt reflect.Type, tag uint64, ext Ext) (err error) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- *o = append(*o, &extTypeTagFn{rtid, rt, tag, ext})
|
|
|
+ if *o == nil {
|
|
|
+ *o = make([]extTypeTagFn, 0, 4)
|
|
|
+ }
|
|
|
+ *o = append(*o, extTypeTagFn{rtid, rt, tag, ext})
|
|
|
return
|
|
|
}
|
|
|
|
|
|
func (o extHandle) getExt(rtid uintptr) *extTypeTagFn {
|
|
|
- for _, v := range o {
|
|
|
+ var v *extTypeTagFn
|
|
|
+ for i := range o {
|
|
|
+ v = &o[i]
|
|
|
if v.rtid == rtid {
|
|
|
return v
|
|
|
}
|
|
@@ -452,7 +557,9 @@ func (o extHandle) getExt(rtid uintptr) *extTypeTagFn {
|
|
|
}
|
|
|
|
|
|
func (o extHandle) getExtForTag(tag uint64) *extTypeTagFn {
|
|
|
- for _, v := range o {
|
|
|
+ var v *extTypeTagFn
|
|
|
+ for i := range o {
|
|
|
+ v = &o[i]
|
|
|
if v.tag == tag {
|
|
|
return v
|
|
|
}
|
|
@@ -471,6 +578,10 @@ type structFieldInfo struct {
|
|
|
toArray bool // if field is _struct, is the toArray set?
|
|
|
}
|
|
|
|
|
|
+// func (si *structFieldInfo) isZero() bool {
|
|
|
+// return si.encName == "" && len(si.is) == 0 && si.i == 0 && !si.omitEmpty && !si.toArray
|
|
|
+// }
|
|
|
+
|
|
|
// rv returns the field of the struct.
|
|
|
// If anonymous, it returns an Invalid
|
|
|
func (si *structFieldInfo) field(v reflect.Value, update bool) (rv2 reflect.Value) {
|
|
@@ -516,9 +627,9 @@ func (si *structFieldInfo) setToZeroValue(v reflect.Value) {
|
|
|
}
|
|
|
|
|
|
func parseStructFieldInfo(fname string, stag string) *structFieldInfo {
|
|
|
- if fname == "" {
|
|
|
- panic(noFieldNameToStructFieldInfoErr)
|
|
|
- }
|
|
|
+ // if fname == "" {
|
|
|
+ // panic(noFieldNameToStructFieldInfoErr)
|
|
|
+ // }
|
|
|
si := structFieldInfo{
|
|
|
encName: fname,
|
|
|
}
|
|
@@ -571,6 +682,8 @@ type typeInfo struct {
|
|
|
rt reflect.Type
|
|
|
rtid uintptr
|
|
|
|
|
|
+ numMeth uint16 // number of methods
|
|
|
+
|
|
|
// baseId gives pointer to the base reflect.Type, after deferencing
|
|
|
// the pointers. E.g. base type of ***time.Time is time.Time.
|
|
|
base reflect.Type
|
|
@@ -589,6 +702,11 @@ type typeInfo struct {
|
|
|
tmIndir int8 // number of indirections to get to textMarshaler type
|
|
|
tunmIndir int8 // number of indirections to get to textUnmarshaler type
|
|
|
|
|
|
+ jm bool // base type (T or *T) is a jsonMarshaler
|
|
|
+ junm bool // base type (T or *T) is a jsonUnmarshaler
|
|
|
+ jmIndir int8 // number of indirections to get to jsonMarshaler type
|
|
|
+ junmIndir int8 // number of indirections to get to jsonUnmarshaler type
|
|
|
+
|
|
|
cs bool // base type (T or *T) is a Selfer
|
|
|
csIndir int8 // number of indirections to get to Selfer type
|
|
|
|
|
@@ -623,33 +741,49 @@ func (ti *typeInfo) indexForEncName(name string) int {
|
|
|
return -1
|
|
|
}
|
|
|
|
|
|
-func getStructTag(t reflect.StructTag) (s string) {
|
|
|
+// TypeInfos caches typeInfo for each type on first inspection.
|
|
|
+//
|
|
|
+// It is configured with a set of tag keys, which are used to get
|
|
|
+// configuration for the type.
|
|
|
+type TypeInfos struct {
|
|
|
+ infos map[uintptr]*typeInfo
|
|
|
+ mu sync.RWMutex
|
|
|
+ tags []string
|
|
|
+}
|
|
|
+
|
|
|
+// NewTypeInfos creates a TypeInfos given a set of struct tags keys.
|
|
|
+//
|
|
|
+// This allows users customize the struct tag keys which contain configuration
|
|
|
+// of their types.
|
|
|
+func NewTypeInfos(tags []string) *TypeInfos {
|
|
|
+ return &TypeInfos{tags: tags, infos: make(map[uintptr]*typeInfo, 64)}
|
|
|
+}
|
|
|
+
|
|
|
+func (x *TypeInfos) structTag(t reflect.StructTag) (s string) {
|
|
|
// check for tags: codec, json, in that order.
|
|
|
// this allows seamless support for many configured structs.
|
|
|
- s = t.Get("codec")
|
|
|
- if s == "" {
|
|
|
- s = t.Get("json")
|
|
|
+ for _, x := range x.tags {
|
|
|
+ s = t.Get(x)
|
|
|
+ if s != "" {
|
|
|
+ return s
|
|
|
+ }
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-func getTypeInfo(rtid uintptr, rt reflect.Type) (pti *typeInfo) {
|
|
|
+func (x *TypeInfos) get(rtid uintptr, rt reflect.Type) (pti *typeInfo) {
|
|
|
var ok bool
|
|
|
- cachedTypeInfoMutex.RLock()
|
|
|
- pti, ok = cachedTypeInfo[rtid]
|
|
|
- cachedTypeInfoMutex.RUnlock()
|
|
|
+ x.mu.RLock()
|
|
|
+ pti, ok = x.infos[rtid]
|
|
|
+ x.mu.RUnlock()
|
|
|
if ok {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- cachedTypeInfoMutex.Lock()
|
|
|
- defer cachedTypeInfoMutex.Unlock()
|
|
|
- if pti, ok = cachedTypeInfo[rtid]; ok {
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
+ // do not hold lock while computing this.
|
|
|
+ // it may lead to duplication, but that's ok.
|
|
|
ti := typeInfo{rt: rt, rtid: rtid}
|
|
|
- pti = &ti
|
|
|
+ ti.numMeth = uint16(rt.NumMethod())
|
|
|
|
|
|
var indir int8
|
|
|
if ok, indir = implementsIntf(rt, binaryMarshalerTyp); ok {
|
|
@@ -664,6 +798,12 @@ func getTypeInfo(rtid uintptr, rt reflect.Type) (pti *typeInfo) {
|
|
|
if ok, indir = implementsIntf(rt, textUnmarshalerTyp); ok {
|
|
|
ti.tunm, ti.tunmIndir = true, indir
|
|
|
}
|
|
|
+ if ok, indir = implementsIntf(rt, jsonMarshalerTyp); ok {
|
|
|
+ ti.jm, ti.jmIndir = true, indir
|
|
|
+ }
|
|
|
+ if ok, indir = implementsIntf(rt, jsonUnmarshalerTyp); ok {
|
|
|
+ ti.junm, ti.junmIndir = true, indir
|
|
|
+ }
|
|
|
if ok, indir = implementsIntf(rt, selferTyp); ok {
|
|
|
ti.cs, ti.csIndir = true, indir
|
|
|
}
|
|
@@ -690,11 +830,11 @@ func getTypeInfo(rtid uintptr, rt reflect.Type) (pti *typeInfo) {
|
|
|
if rt.Kind() == reflect.Struct {
|
|
|
var siInfo *structFieldInfo
|
|
|
if f, ok := rt.FieldByName(structInfoFieldName); ok {
|
|
|
- siInfo = parseStructFieldInfo(structInfoFieldName, getStructTag(f.Tag))
|
|
|
+ siInfo = parseStructFieldInfo(structInfoFieldName, x.structTag(f.Tag))
|
|
|
ti.toArray = siInfo.toArray
|
|
|
}
|
|
|
sfip := make([]*structFieldInfo, 0, rt.NumField())
|
|
|
- rgetTypeInfo(rt, nil, make(map[string]bool, 16), &sfip, siInfo)
|
|
|
+ x.rget(rt, nil, make(map[string]bool, 16), &sfip, siInfo)
|
|
|
|
|
|
ti.sfip = make([]*structFieldInfo, len(sfip))
|
|
|
ti.sfi = make([]*structFieldInfo, len(sfip))
|
|
@@ -703,48 +843,78 @@ func getTypeInfo(rtid uintptr, rt reflect.Type) (pti *typeInfo) {
|
|
|
copy(ti.sfi, sfip)
|
|
|
}
|
|
|
// sfi = sfip
|
|
|
- cachedTypeInfo[rtid] = pti
|
|
|
+
|
|
|
+ x.mu.Lock()
|
|
|
+ if pti, ok = x.infos[rtid]; !ok {
|
|
|
+ pti = &ti
|
|
|
+ x.infos[rtid] = pti
|
|
|
+ }
|
|
|
+ x.mu.Unlock()
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-func rgetTypeInfo(rt reflect.Type, indexstack []int, fnameToHastag map[string]bool,
|
|
|
+func (x *TypeInfos) rget(rt reflect.Type, indexstack []int, fnameToHastag map[string]bool,
|
|
|
sfi *[]*structFieldInfo, siInfo *structFieldInfo,
|
|
|
) {
|
|
|
for j := 0; j < rt.NumField(); j++ {
|
|
|
f := rt.Field(j)
|
|
|
- // func types are skipped.
|
|
|
- if tk := f.Type.Kind(); tk == reflect.Func {
|
|
|
+ fkind := f.Type.Kind()
|
|
|
+ // skip if a func type, or is unexported, or structTag value == "-"
|
|
|
+ if fkind == reflect.Func {
|
|
|
continue
|
|
|
}
|
|
|
- stag := getStructTag(f.Tag)
|
|
|
- if stag == "-" {
|
|
|
+ // if r1, _ := utf8.DecodeRuneInString(f.Name); r1 == utf8.RuneError || !unicode.IsUpper(r1) {
|
|
|
+ if f.PkgPath != "" && !f.Anonymous { // unexported, not embedded
|
|
|
continue
|
|
|
}
|
|
|
- if r1, _ := utf8.DecodeRuneInString(f.Name); r1 == utf8.RuneError || !unicode.IsUpper(r1) {
|
|
|
+ stag := x.structTag(f.Tag)
|
|
|
+ if stag == "-" {
|
|
|
continue
|
|
|
}
|
|
|
- // if anonymous and there is no struct tag and its a struct (or pointer to struct), inline it.
|
|
|
- if f.Anonymous && stag == "" {
|
|
|
- ft := f.Type
|
|
|
- for ft.Kind() == reflect.Ptr {
|
|
|
- ft = ft.Elem()
|
|
|
+ var si *structFieldInfo
|
|
|
+ // if anonymous and no struct tag (or it's blank), and a struct (or pointer to struct), inline it.
|
|
|
+ if f.Anonymous && fkind != reflect.Interface {
|
|
|
+ doInline := stag == ""
|
|
|
+ if !doInline {
|
|
|
+ si = parseStructFieldInfo("", stag)
|
|
|
+ doInline = si.encName == ""
|
|
|
+ // doInline = si.isZero()
|
|
|
}
|
|
|
- if ft.Kind() == reflect.Struct {
|
|
|
- indexstack2 := make([]int, len(indexstack)+1, len(indexstack)+4)
|
|
|
- copy(indexstack2, indexstack)
|
|
|
- indexstack2[len(indexstack)] = j
|
|
|
- // indexstack2 := append(append(make([]int, 0, len(indexstack)+4), indexstack...), j)
|
|
|
- rgetTypeInfo(ft, indexstack2, fnameToHastag, sfi, siInfo)
|
|
|
- continue
|
|
|
+ if doInline {
|
|
|
+ ft := f.Type
|
|
|
+ for ft.Kind() == reflect.Ptr {
|
|
|
+ ft = ft.Elem()
|
|
|
+ }
|
|
|
+ if ft.Kind() == reflect.Struct {
|
|
|
+ indexstack2 := make([]int, len(indexstack)+1, len(indexstack)+4)
|
|
|
+ copy(indexstack2, indexstack)
|
|
|
+ indexstack2[len(indexstack)] = j
|
|
|
+ // indexstack2 := append(append(make([]int, 0, len(indexstack)+4), indexstack...), j)
|
|
|
+ x.rget(ft, indexstack2, fnameToHastag, sfi, siInfo)
|
|
|
+ continue
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ // after the anonymous dance: if an unexported field, skip
|
|
|
+ if f.PkgPath != "" { // unexported
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
// do not let fields with same name in embedded structs override field at higher level.
|
|
|
// this must be done after anonymous check, to allow anonymous field
|
|
|
// still include their child fields
|
|
|
if _, ok := fnameToHastag[f.Name]; ok {
|
|
|
continue
|
|
|
}
|
|
|
- si := parseStructFieldInfo(f.Name, stag)
|
|
|
+ if f.Name == "" {
|
|
|
+ panic(noFieldNameToStructFieldInfoErr)
|
|
|
+ }
|
|
|
+ if si == nil {
|
|
|
+ si = parseStructFieldInfo(f.Name, stag)
|
|
|
+ } else if si.encName == "" {
|
|
|
+ si.encName = f.Name
|
|
|
+ }
|
|
|
// si.ikind = int(f.Type.Kind())
|
|
|
if len(indexstack) == 0 {
|
|
|
si.i = int16(j)
|
|
@@ -779,8 +949,9 @@ func panicToErr(err *error) {
|
|
|
// panic(fmt.Errorf("%s: "+format, params2...))
|
|
|
// }
|
|
|
|
|
|
-func isMutableKind(k reflect.Kind) (v bool) {
|
|
|
- return k == reflect.Int ||
|
|
|
+func isImmutableKind(k reflect.Kind) (v bool) {
|
|
|
+ return false ||
|
|
|
+ k == reflect.Int ||
|
|
|
k == reflect.Int8 ||
|
|
|
k == reflect.Int16 ||
|
|
|
k == reflect.Int32 ||
|
|
@@ -790,6 +961,7 @@ func isMutableKind(k reflect.Kind) (v bool) {
|
|
|
k == reflect.Uint16 ||
|
|
|
k == reflect.Uint32 ||
|
|
|
k == reflect.Uint64 ||
|
|
|
+ k == reflect.Uintptr ||
|
|
|
k == reflect.Float32 ||
|
|
|
k == reflect.Float64 ||
|
|
|
k == reflect.Bool ||
|
|
@@ -844,3 +1016,114 @@ func (_ checkOverflow) SignedInt(v uint64) (i int64, overflow bool) {
|
|
|
i = int64(v)
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
+// ------------------ SORT -----------------
|
|
|
+
|
|
|
+func isNaN(f float64) bool { return f != f }
|
|
|
+
|
|
|
+// -----------------------
|
|
|
+
|
|
|
+type intSlice []int64
|
|
|
+type uintSlice []uint64
|
|
|
+type floatSlice []float64
|
|
|
+type boolSlice []bool
|
|
|
+type stringSlice []string
|
|
|
+type bytesSlice [][]byte
|
|
|
+
|
|
|
+func (p intSlice) Len() int { return len(p) }
|
|
|
+func (p intSlice) Less(i, j int) bool { return p[i] < p[j] }
|
|
|
+func (p intSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
|
|
+
|
|
|
+func (p uintSlice) Len() int { return len(p) }
|
|
|
+func (p uintSlice) Less(i, j int) bool { return p[i] < p[j] }
|
|
|
+func (p uintSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
|
|
+
|
|
|
+func (p floatSlice) Len() int { return len(p) }
|
|
|
+func (p floatSlice) Less(i, j int) bool {
|
|
|
+ return p[i] < p[j] || isNaN(p[i]) && !isNaN(p[j])
|
|
|
+}
|
|
|
+func (p floatSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
|
|
+
|
|
|
+func (p stringSlice) Len() int { return len(p) }
|
|
|
+func (p stringSlice) Less(i, j int) bool { return p[i] < p[j] }
|
|
|
+func (p stringSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
|
|
+
|
|
|
+func (p bytesSlice) Len() int { return len(p) }
|
|
|
+func (p bytesSlice) Less(i, j int) bool { return bytes.Compare(p[i], p[j]) == -1 }
|
|
|
+func (p bytesSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
|
|
+
|
|
|
+func (p boolSlice) Len() int { return len(p) }
|
|
|
+func (p boolSlice) Less(i, j int) bool { return !p[i] && p[j] }
|
|
|
+func (p boolSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
|
|
+
|
|
|
+// ---------------------
|
|
|
+
|
|
|
+type intRv struct {
|
|
|
+ v int64
|
|
|
+ r reflect.Value
|
|
|
+}
|
|
|
+type intRvSlice []intRv
|
|
|
+type uintRv struct {
|
|
|
+ v uint64
|
|
|
+ r reflect.Value
|
|
|
+}
|
|
|
+type uintRvSlice []uintRv
|
|
|
+type floatRv struct {
|
|
|
+ v float64
|
|
|
+ r reflect.Value
|
|
|
+}
|
|
|
+type floatRvSlice []floatRv
|
|
|
+type boolRv struct {
|
|
|
+ v bool
|
|
|
+ r reflect.Value
|
|
|
+}
|
|
|
+type boolRvSlice []boolRv
|
|
|
+type stringRv struct {
|
|
|
+ v string
|
|
|
+ r reflect.Value
|
|
|
+}
|
|
|
+type stringRvSlice []stringRv
|
|
|
+type bytesRv struct {
|
|
|
+ v []byte
|
|
|
+ r reflect.Value
|
|
|
+}
|
|
|
+type bytesRvSlice []bytesRv
|
|
|
+
|
|
|
+func (p intRvSlice) Len() int { return len(p) }
|
|
|
+func (p intRvSlice) Less(i, j int) bool { return p[i].v < p[j].v }
|
|
|
+func (p intRvSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
|
|
+
|
|
|
+func (p uintRvSlice) Len() int { return len(p) }
|
|
|
+func (p uintRvSlice) Less(i, j int) bool { return p[i].v < p[j].v }
|
|
|
+func (p uintRvSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
|
|
+
|
|
|
+func (p floatRvSlice) Len() int { return len(p) }
|
|
|
+func (p floatRvSlice) Less(i, j int) bool {
|
|
|
+ return p[i].v < p[j].v || isNaN(p[i].v) && !isNaN(p[j].v)
|
|
|
+}
|
|
|
+func (p floatRvSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
|
|
+
|
|
|
+func (p stringRvSlice) Len() int { return len(p) }
|
|
|
+func (p stringRvSlice) Less(i, j int) bool { return p[i].v < p[j].v }
|
|
|
+func (p stringRvSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
|
|
+
|
|
|
+func (p bytesRvSlice) Len() int { return len(p) }
|
|
|
+func (p bytesRvSlice) Less(i, j int) bool { return bytes.Compare(p[i].v, p[j].v) == -1 }
|
|
|
+func (p bytesRvSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
|
|
+
|
|
|
+func (p boolRvSlice) Len() int { return len(p) }
|
|
|
+func (p boolRvSlice) Less(i, j int) bool { return !p[i].v && p[j].v }
|
|
|
+func (p boolRvSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
|
|
+
|
|
|
+// -----------------
|
|
|
+
|
|
|
+type bytesI struct {
|
|
|
+ v []byte
|
|
|
+ i interface{}
|
|
|
+}
|
|
|
+
|
|
|
+type bytesISlice []bytesI
|
|
|
+
|
|
|
+func (p bytesISlice) Len() int { return len(p) }
|
|
|
+func (p bytesISlice) Less(i, j int) bool { return bytes.Compare(p[i].v, p[j].v) == -1 }
|
|
|
+func (p bytesISlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|