|
@@ -309,32 +309,41 @@ type RawExt struct {
|
|
|
Value interface{}
|
|
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.
|
|
// WriteExt converts a value to a []byte.
|
|
|
- // It is used by codecs (e.g. binc, msgpack, simple) which do custom serialization of the types.
|
|
|
|
|
WriteExt(v interface{}) []byte
|
|
WriteExt(v interface{}) []byte
|
|
|
|
|
|
|
|
// ReadExt updates a value from a []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)
|
|
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.
|
|
// 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.
|
|
|
|
|
ConvertExt(v interface{}) interface{}
|
|
ConvertExt(v interface{}) interface{}
|
|
|
|
|
|
|
|
// UpdateExt updates a value from a simpler interface for easy decoding e.g. convert int64 to time.Time.
|
|
// 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{})
|
|
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)
|
|
encFn func(reflect.Value) ([]byte, error)
|
|
|
decFn func(reflect.Value, []byte) error
|
|
decFn func(reflect.Value, []byte) error
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (x bytesExt) WriteExt(v interface{}) []byte {
|
|
|
|
|
|
|
+func (x addExtWrapper) WriteExt(v interface{}) []byte {
|
|
|
// fmt.Printf(">>>>>>>>>> WriteExt: %T, %v\n", v, v)
|
|
// fmt.Printf(">>>>>>>>>> WriteExt: %T, %v\n", v, v)
|
|
|
bs, err := x.encFn(reflect.ValueOf(v))
|
|
bs, err := x.encFn(reflect.ValueOf(v))
|
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -343,21 +352,57 @@ func (x bytesExt) WriteExt(v interface{}) []byte {
|
|
|
return bs
|
|
return bs
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (x bytesExt) ReadExt(v interface{}, bs []byte) {
|
|
|
|
|
|
|
+func (x addExtWrapper) ReadExt(v interface{}, bs []byte) {
|
|
|
// fmt.Printf(">>>>>>>>>> ReadExt: %T, %v\n", v, v)
|
|
// fmt.Printf(">>>>>>>>>> ReadExt: %T, %v\n", v, v)
|
|
|
if err := x.decFn(reflect.ValueOf(v), bs); err != nil {
|
|
if err := x.decFn(reflect.ValueOf(v), bs); err != nil {
|
|
|
panic(err)
|
|
panic(err)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (x bytesExt) ConvertExt(v interface{}) interface{} {
|
|
|
|
|
|
|
+func (x addExtWrapper) ConvertExt(v interface{}) interface{} {
|
|
|
return x.WriteExt(v)
|
|
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))
|
|
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
|
|
// type errorString string
|
|
|
// func (x errorString) Error() string { return string(x) }
|
|
// func (x errorString) Error() string { return string(x) }
|
|
|
|
|
|
|
@@ -412,7 +457,7 @@ type extTypeTagFn struct {
|
|
|
|
|
|
|
|
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 registes an encode and decode function for a reflect.Type.
|
|
|
// AddExt internally calls SetExt.
|
|
// AddExt internally calls SetExt.
|
|
@@ -424,10 +469,10 @@ func (o *extHandle) AddExt(
|
|
|
if encfn == nil || decfn == nil {
|
|
if encfn == nil || decfn == nil {
|
|
|
return o.SetExt(rt, uint64(tag), 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
|
|
// 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.
|
|
// a pointer or Interface. An error is returned if that is not honored.
|