From 1684ce6f0dcfbf0abceaa33237efbd0d773b0cb5 Mon Sep 17 00:00:00 2001 From: Slavomir Date: Sat, 21 Aug 2021 16:46:18 +0200 Subject: [PATCH] Change marshaler/unmarshaler interface method signatures --- README.md | 4 ++-- borsh_test.go | 7 +++++-- decoder_bin.go | 4 ++-- decoder_borsh.go | 4 ++-- encoder_bin.go | 2 +- encoder_borsh.go | 2 +- interface.go | 4 ++-- interface_test.go | 8 ++++---- types.go | 52 +++++++++++++++++++++++------------------------ variant_test.go | 5 +++-- 10 files changed, 48 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index a1d1d50..56b79e2 100644 --- a/README.md +++ b/README.md @@ -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 } @@ -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 } diff --git a/borsh_test.go b/borsh_test.go index f74e0e2..00c6164 100644 --- a/borsh_test.go +++ b/borsh_test.go @@ -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 } @@ -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) diff --git a/decoder_bin.go b/decoder_bin.go index 5544fe4..c1e20ca 100644 --- a/decoder_bin.go +++ b/decoder_bin.go @@ -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() diff --git a/decoder_borsh.go b/decoder_borsh.go index e076325..0413894 100644 --- a/decoder_borsh.go +++ b/decoder_borsh.go @@ -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() diff --git a/encoder_bin.go b/encoder_bin.go index d8bc783..75a83a8 100644 --- a/encoder_bin.go +++ b/encoder_bin.go @@ -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() { diff --git a/encoder_borsh.go b/encoder_borsh.go index c1f2326..6dc71f9 100644 --- a/encoder_borsh.go +++ b/encoder_borsh.go @@ -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() { diff --git a/interface.go b/interface.go index b659a32..741474d 100644 --- a/interface.go +++ b/interface.go @@ -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 { diff --git a/interface_test.go b/interface_test.go index d4e83de..3b7deb0 100644 --- a/interface_test.go +++ b/interface_test.go @@ -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 } @@ -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) @@ -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, } diff --git a/types.go b/types.go index c70a145..07d5a29 100644 --- a/types.go +++ b/types.go @@ -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 @@ -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 @@ -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)) } @@ -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) @@ -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) @@ -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) @@ -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) @@ -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 @@ -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)) } @@ -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 @@ -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) } @@ -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 @@ -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) } @@ -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 @@ -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) } @@ -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 @@ -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) } @@ -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 @@ -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) } @@ -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 @@ -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) } diff --git a/variant_test.go b/variant_test.go index ebaad15..d03b243 100644 --- a/variant_test.go +++ b/variant_test.go @@ -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