Skip to content

Commit

Permalink
Change marshaler/unmarshaler interface method signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
gagliardetto committed Aug 21, 2021
1 parent e4414b8 commit 1684ce6
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 44 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type Example struct {
Value uint32
}

func (e *Example) UnmarshalBinary(decoder *Decoder) (err error) {
func (e *Example) UnmarshalWithDecoder(decoder *Decoder) (err error) {
if e.Prefix, err = decoder.ReadByte(); err != nil {
return err
}
Expand All @@ -58,7 +58,7 @@ func (e *Example) UnmarshalBinary(decoder *Decoder) (err error) {
return nil
}

func (e *Example) MarshalBinary(encoder *Encoder) error {
func (e *Example) MarshalWithEncoder(encoder *Encoder) error {
if err := encoder.WriteByte(e.Prefix); err != nil {
return err
}
Expand Down
7 changes: 5 additions & 2 deletions borsh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ type CustomEncoding struct {
Value uint32
}

func (e *CustomEncoding) MarshalBinary(encoder *Encoder) error {
func (e *CustomEncoding) MarshalWithEncoder(encoder *Encoder) error {
if err := encoder.WriteByte(e.Prefix); err != nil {
return err
}
return encoder.WriteUint32(e.Value, LE())
}

func (e *CustomEncoding) UnmarshalBinary(decoder *Decoder) (err error) {
func (e *CustomEncoding) UnmarshalWithDecoder(decoder *Decoder) (err error) {
if e.Prefix, err = decoder.ReadByte(); err != nil {
return err
}
Expand All @@ -66,6 +66,9 @@ func (e *CustomEncoding) UnmarshalBinary(decoder *Decoder) (err error) {
}
return nil
}

var _ EncoderDecoder = &CustomEncoding{}

func TestBorsh_kitchenSink(t *testing.T) {
boolTrue := true
uint64Num := uint64(25464132585)
Expand Down
4 changes: 2 additions & 2 deletions decoder_bin.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ func (dec *Decoder) decodeBin(rv reflect.Value, opt *option) (err error) {

if unmarshaler != nil {
if traceEnabled {
zlog.Debug("decode: using UnmarshalBinary method to decode type")
zlog.Debug("decode: using UnmarshalWithDecoder method to decode type")
}
return unmarshaler.UnmarshalBinary(dec)
return unmarshaler.UnmarshalWithDecoder(dec)
}
rt := rv.Type()

Expand Down
4 changes: 2 additions & 2 deletions decoder_borsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ func (dec *Decoder) decodeBorsh(rv reflect.Value, opt *option) (err error) {

if unmarshaler != nil {
if traceEnabled {
zlog.Debug("decode: using UnmarshalBinary method to decode type")
zlog.Debug("decode: using UnmarshalWithDecoder method to decode type")
}
return unmarshaler.UnmarshalBinary(dec)
return unmarshaler.UnmarshalWithDecoder(dec)
}

rt := rv.Type()
Expand Down
2 changes: 1 addition & 1 deletion encoder_bin.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (e *Encoder) encodeBin(rv reflect.Value, opt *option) (err error) {
if traceEnabled {
zlog.Debug("encode: using MarshalerBinary method to encode type")
}
return marshaler.MarshalBinary(e)
return marshaler.MarshalWithEncoder(e)
}

switch rv.Kind() {
Expand Down
2 changes: 1 addition & 1 deletion encoder_borsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (e *Encoder) encodeBorsh(rv reflect.Value, opt *option) (err error) {
if traceEnabled {
zlog.Debug("encode: using MarshalerBinary method to encode type")
}
return marshaler.MarshalBinary(e)
return marshaler.MarshalWithEncoder(e)
}

switch rv.Kind() {
Expand Down
4 changes: 2 additions & 2 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
)

type BinaryMarshaler interface {
MarshalBinary(encoder *Encoder) error
MarshalWithEncoder(encoder *Encoder) error
}

type BinaryUnmarshaler interface {
UnmarshalBinary(decoder *Decoder) error
UnmarshalWithDecoder(decoder *Decoder) error
}

type EncoderDecoder interface {
Expand Down
8 changes: 4 additions & 4 deletions interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type Example struct {
Value uint32
}

func (e *Example) UnmarshalBinary(decoder *Decoder) (err error) {
func (e *Example) UnmarshalWithDecoder(decoder *Decoder) (err error) {
if e.Prefix, err = decoder.ReadByte(); err != nil {
return err
}
Expand All @@ -22,14 +22,14 @@ func (e *Example) UnmarshalBinary(decoder *Decoder) (err error) {
return nil
}

func (e *Example) MarshalBinary(encoder *Encoder) error {
func (e *Example) MarshalWithEncoder(encoder *Encoder) error {
if err := encoder.WriteByte(e.Prefix); err != nil {
return err
}
return encoder.WriteUint32(e.Value, BE())
}

func TestMarshalBinary(t *testing.T) {
func TestMarshalWithEncoder(t *testing.T) {
buf := new(bytes.Buffer)
e := &Example{Value: 72, Prefix: 0xaa}
enc := NewBinEncoder(buf)
Expand All @@ -40,7 +40,7 @@ func TestMarshalBinary(t *testing.T) {
}, buf.Bytes())
}

func TestUnmarshalBinary(t *testing.T) {
func TestUnmarshalWithDecoder(t *testing.T) {
buf := []byte{
0xaa, 0x00, 0x00, 0x00, 0x48,
}
Expand Down
52 changes: 26 additions & 26 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import (

type SafeString string

func (ss SafeString) MarshalBinary(encoder *Encoder) error {
func (ss SafeString) MarshalWithEncoder(encoder *Encoder) error {
return encoder.WriteString(string(ss))
}

func (ss *SafeString) UnmarshalBinary(d *Decoder) error {
func (ss *SafeString) UnmarshalWithDecoder(d *Decoder) error {
s, e := d.SafeReadUTF8String()
if e != nil {
return e
Expand Down Expand Up @@ -46,7 +46,7 @@ func (b *Bool) UnmarshalJSON(data []byte) error {
return nil
}

func (b *Bool) UnmarshalBinary(decoder *Decoder) error {
func (b *Bool) UnmarshalWithDecoder(decoder *Decoder) error {
value, err := decoder.ReadBool()
if err != nil {
return err
Expand All @@ -56,7 +56,7 @@ func (b *Bool) UnmarshalBinary(decoder *Decoder) error {
return nil
}

func (b Bool) MarshalBinary(encoder *Encoder) error {
func (b Bool) MarshalWithEncoder(encoder *Encoder) error {
return encoder.WriteBool(bool(b))
}

Expand All @@ -81,7 +81,7 @@ func (t HexBytes) String() string {
return hex.EncodeToString(t)
}

func (o *HexBytes) UnmarshalBinary(decoder *Decoder) error {
func (o *HexBytes) UnmarshalWithDecoder(decoder *Decoder) error {
value, err := decoder.ReadByteSlice()
if err != nil {
return fmt.Errorf("hex bytes: %s", err)
Expand All @@ -91,13 +91,13 @@ func (o *HexBytes) UnmarshalBinary(decoder *Decoder) error {
return nil
}

func (o HexBytes) MarshalBinary(encoder *Encoder) error {
func (o HexBytes) MarshalWithEncoder(encoder *Encoder) error {
return encoder.WriteBytes([]byte(o), true)
}

type Varint16 int16

func (o *Varint16) UnmarshalBinary(decoder *Decoder) error {
func (o *Varint16) UnmarshalWithDecoder(decoder *Decoder) error {
value, err := decoder.ReadVarint16()
if err != nil {
return fmt.Errorf("varint16: %s", err)
Expand All @@ -107,13 +107,13 @@ func (o *Varint16) UnmarshalBinary(decoder *Decoder) error {
return nil
}

func (o Varint16) MarshalBinary(encoder *Encoder) error {
func (o Varint16) MarshalWithEncoder(encoder *Encoder) error {
return encoder.WriteVarInt(int(o))
}

type Varuint16 uint16

func (o *Varuint16) UnmarshalBinary(decoder *Decoder) error {
func (o *Varuint16) UnmarshalWithDecoder(decoder *Decoder) error {
value, err := decoder.ReadUvarint16()
if err != nil {
return fmt.Errorf("varuint16: %s", err)
Expand All @@ -123,13 +123,13 @@ func (o *Varuint16) UnmarshalBinary(decoder *Decoder) error {
return nil
}

func (o Varuint16) MarshalBinary(encoder *Encoder) error {
func (o Varuint16) MarshalWithEncoder(encoder *Encoder) error {
return encoder.WriteUVarInt(int(o))
}

type Varuint32 uint32

func (o *Varuint32) UnmarshalBinary(decoder *Decoder) error {
func (o *Varuint32) UnmarshalWithDecoder(decoder *Decoder) error {
value, err := decoder.ReadUvarint64()
if err != nil {
return fmt.Errorf("varuint32: %s", err)
Expand All @@ -139,13 +139,13 @@ func (o *Varuint32) UnmarshalBinary(decoder *Decoder) error {
return nil
}

func (o Varuint32) MarshalBinary(encoder *Encoder) error {
func (o Varuint32) MarshalWithEncoder(encoder *Encoder) error {
return encoder.WriteUVarInt(int(o))
}

type Varint32 int32

func (o *Varint32) UnmarshalBinary(decoder *Decoder) error {
func (o *Varint32) UnmarshalWithDecoder(decoder *Decoder) error {
value, err := decoder.ReadVarint32()
if err != nil {
return err
Expand All @@ -155,7 +155,7 @@ func (o *Varint32) UnmarshalBinary(decoder *Decoder) error {
return nil
}

func (o Varint32) MarshalBinary(encoder *Encoder) error {
func (o Varint32) MarshalWithEncoder(encoder *Encoder) error {
return encoder.WriteVarInt(int(o))
}

Expand Down Expand Up @@ -192,7 +192,7 @@ func (f *JSONFloat64) UnmarshalJSON(data []byte) error {
return nil
}

func (f *JSONFloat64) UnmarshalBinary(dec *Decoder) error {
func (f *JSONFloat64) UnmarshalWithDecoder(dec *Decoder) error {
value, err := dec.ReadFloat64(dec.currentFieldOpt.Order)
if err != nil {
return err
Expand All @@ -202,7 +202,7 @@ func (f *JSONFloat64) UnmarshalBinary(dec *Decoder) error {
return nil
}

func (f JSONFloat64) MarshalBinary(enc *Encoder) error {
func (f JSONFloat64) MarshalWithEncoder(enc *Encoder) error {
return enc.WriteFloat64(float64(f), enc.currentFieldOpt.Order)
}

Expand Down Expand Up @@ -252,7 +252,7 @@ func (i *Int64) UnmarshalJSON(data []byte) error {
return nil
}

func (i *Int64) UnmarshalBinary(dec *Decoder) error {
func (i *Int64) UnmarshalWithDecoder(dec *Decoder) error {
value, err := dec.ReadInt64(dec.currentFieldOpt.Order)
if err != nil {
return err
Expand All @@ -262,7 +262,7 @@ func (i *Int64) UnmarshalBinary(dec *Decoder) error {
return nil
}

func (i Int64) MarshalBinary(enc *Encoder) error {
func (i Int64) MarshalWithEncoder(enc *Encoder) error {
return enc.WriteInt64(int64(i), enc.currentFieldOpt.Order)
}

Expand Down Expand Up @@ -312,7 +312,7 @@ func (i *Uint64) UnmarshalJSON(data []byte) error {
return nil
}

func (i *Uint64) UnmarshalBinary(dec *Decoder) error {
func (i *Uint64) UnmarshalWithDecoder(dec *Decoder) error {
value, err := dec.ReadUint64(dec.currentFieldOpt.Order)
if err != nil {
return err
Expand All @@ -322,7 +322,7 @@ func (i *Uint64) UnmarshalBinary(dec *Decoder) error {
return nil
}

func (i Uint64) MarshalBinary(enc *Encoder) error {
func (i Uint64) MarshalWithEncoder(enc *Encoder) error {
return enc.WriteUint64(uint64(i), enc.currentFieldOpt.Order)
}

Expand Down Expand Up @@ -399,7 +399,7 @@ func (i *Uint128) UnmarshalJSON(data []byte) error {
return nil
}

func (i *Uint128) UnmarshalBinary(dec *Decoder) error {
func (i *Uint128) UnmarshalWithDecoder(dec *Decoder) error {
value, err := dec.ReadUint128(dec.currentFieldOpt.Order)
if err != nil {
return err
Expand All @@ -409,7 +409,7 @@ func (i *Uint128) UnmarshalBinary(dec *Decoder) error {
return nil
}

func (i Uint128) MarshalBinary(enc *Encoder) error {
func (i Uint128) MarshalWithEncoder(enc *Encoder) error {
return enc.WriteUint128(i, enc.currentFieldOpt.Order)
}

Expand Down Expand Up @@ -457,7 +457,7 @@ func (i *Int128) UnmarshalJSON(data []byte) error {
return nil
}

func (i *Int128) UnmarshalBinary(dec *Decoder) error {
func (i *Int128) UnmarshalWithDecoder(dec *Decoder) error {
value, err := dec.ReadInt128(dec.currentFieldOpt.Order)
if err != nil {
return err
Expand All @@ -467,7 +467,7 @@ func (i *Int128) UnmarshalBinary(dec *Decoder) error {
return nil
}

func (i Int128) MarshalBinary(enc *Encoder) error {
func (i Int128) MarshalWithEncoder(enc *Encoder) error {
return enc.WriteInt128(i, enc.currentFieldOpt.Order)
}

Expand All @@ -489,7 +489,7 @@ func (i *Float128) UnmarshalJSON(data []byte) error {
return nil
}

func (i *Float128) UnmarshalBinary(dec *Decoder) error {
func (i *Float128) UnmarshalWithDecoder(dec *Decoder) error {
value, err := dec.ReadFloat128(dec.currentFieldOpt.Order)
if err != nil {
return err
Expand All @@ -499,6 +499,6 @@ func (i *Float128) UnmarshalBinary(dec *Decoder) error {
return nil
}

func (i Float128) MarshalBinary(enc *Encoder) error {
func (i Float128) MarshalWithEncoder(enc *Encoder) error {
return enc.WriteUint128(Uint128(i), enc.currentFieldOpt.Order)
}
5 changes: 3 additions & 2 deletions variant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ type NodeInner struct {
Key Uint128
}

func (n *Node) UnmarshalBinary(decoder *Decoder) error {
func (n *Node) UnmarshalWithDecoder(decoder *Decoder) error {
return n.BaseVariant.UnmarshalBinaryVariant(decoder, NodeVariantDef)
}
func (n *Node) MarshalBinary(encoder *Encoder) error {

func (n *Node) MarshalWithEncoder(encoder *Encoder) error {
err := encoder.WriteUint32(n.TypeID, LE())
if err != nil {
return err
Expand Down

0 comments on commit 1684ce6

Please sign in to comment.