Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MarshalBinary/UnmarshalBinary interface support #300

Open
wants to merge 12 commits into
base: ismail/any_amino
Choose a base branch
from
50 changes: 49 additions & 1 deletion amino.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package amino

import (
"bytes"
"encoding"
"fmt"
"io"
"reflect"
Expand Down Expand Up @@ -205,13 +206,33 @@ func (cdc *Codec) MarshalBinaryBare(o interface{}) ([]byte, error) {
}

// Encode Amino:binary bytes.
var bz []byte
buf := new(bytes.Buffer)
rt := rv.Type()
info, err := cdc.getTypeInfoWlock(rt)
if err != nil {
return nil, err
}

if rv.Type().Implements(binaryMarshalerType) {
jordaaash marked this conversation as resolved.
Show resolved Hide resolved
if info.Registered {
pb := info.Prefix.Bytes()
buf.Write(pb)
}

bz, err := rv.Interface().(encoding.BinaryMarshaler).MarshalBinary()
jordaaash marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}

_, err = buf.Write(bz)
if err != nil {
return nil, err
}

return buf.Bytes(), nil
}

var bz []byte
// in the case of of a repeated struct (e.g. type Alias []SomeStruct),
// we do not need to prepend with `(field_number << 3) | wire_type` as this
// would need to be done for each struct and not only for the first.
Expand Down Expand Up @@ -398,6 +419,33 @@ func (cdc *Codec) UnmarshalBinaryBare(bz []byte, ptr interface{}) error {
return err
}

if rv.CanAddr() {
addr := rv.Addr()
if addr.Type().Implements(binaryUnmarshalerType) {
if info.Registered {
pb := info.Prefix.Bytes()
l := len(pb)
if len(bz) < l {
return fmt.Errorf(
"unmarshalBinaryBare expected to read prefix bytes %X (since it is registered concrete) but got %X",
pb, bz,
)
}

pb2 := bz[:l]
bz = bz[l:]
if !bytes.Equal(pb2, pb) {
return fmt.Errorf(
"unmarshalBinaryBare expected to read prefix bytes %X (since it is registered concrete) but got %X",
pb, pb2,
)
}
}

return addr.Interface().(encoding.BinaryUnmarshaler).UnmarshalBinary(bz)
}
}

// If registered concrete, consume and verify prefix bytes.
if info.Registered {
aminoAny := &RegisteredAny{}
Expand Down
83 changes: 83 additions & 0 deletions binary_encode_override_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package amino_test

import (
jordaaash marked this conversation as resolved.
Show resolved Hide resolved
"testing"

"github.com/stretchr/testify/assert"
"github.com/tendermint/go-amino"
)

type Thing struct {
jordaaash marked this conversation as resolved.
Show resolved Hide resolved
Name string
}

func (thing Thing) MarshalBinary() ([]byte, error) {
return []byte(thing.Name), nil
}

func (thing *Thing) UnmarshalBinary(bz []byte) error {
thing.Name = string(bz)
return nil
}

func TestMarshalBinaryOverrideBare(t *testing.T) {
var cdc = amino.NewCodec()
cdc.RegisterConcrete(&Thing{}, "amino/thing", nil)

thing1 := Thing{Name: "a"}

bz, err := cdc.MarshalBinaryBare(thing1)
assert.Nil(t, err)
assert.Equal(t, bz, []byte{140, 74, 30, 175, 97})

var thing2 Thing
err = cdc.UnmarshalBinaryBare(bz, &thing2)
assert.Nil(t, err)
assert.Equal(t, thing1, thing2)
}

func TestMarshalBinaryOverrideLengthPrefixed(t *testing.T) {
var cdc = amino.NewCodec()
cdc.RegisterConcrete(&Thing{}, "amino/thing", nil)

thing1 := Thing{Name: "a"}

bz, err := cdc.MarshalBinaryLengthPrefixed(thing1)
assert.Nil(t, err)
assert.Equal(t, bz, []byte{5, 140, 74, 30, 175, 97})

var thing2 Thing
err = cdc.UnmarshalBinaryLengthPrefixed(bz, &thing2)
assert.Nil(t, err)
assert.Equal(t, thing1, thing2)
}

type Bytes [16]byte

func (bytes Bytes) MarshalBinary() ([]byte, error) {
bz := make([]byte, 17)
copy(bz[:1], []byte{16})
copy(bz[1:], bytes[:])
return bz, nil
}

func (bytes *Bytes) UnmarshalBinary(bz []byte) error {
copy(bytes[:], bz[1:])
return nil
}

func TestMarshalBinaryOverrideBytes(t *testing.T) {
var cdc = amino.NewCodec()
cdc.RegisterConcrete(&Bytes{}, "amino/bytes", nil)

bytes1 := Bytes{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}

bz, err := cdc.MarshalBinaryBare(bytes1)
assert.Nil(t, err)
assert.Equal(t, bz, []byte{207, 109, 94, 111, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1})

var bytes2 Bytes
err = cdc.UnmarshalBinaryBare(bz, &bytes2)
assert.Nil(t, err)
assert.Equal(t, bytes1, bytes2)
}
11 changes: 7 additions & 4 deletions reflect.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package amino

import (
"encoding"
"encoding/json"
"fmt"
"reflect"
Expand All @@ -13,10 +14,12 @@ import (
const printLog = false

var (
timeType = reflect.TypeOf(time.Time{})
jsonMarshalerType = reflect.TypeOf(new(json.Marshaler)).Elem()
jsonUnmarshalerType = reflect.TypeOf(new(json.Unmarshaler)).Elem()
errorType = reflect.TypeOf(new(error)).Elem()
timeType = reflect.TypeOf(time.Time{})
jsonMarshalerType = reflect.TypeOf(new(json.Marshaler)).Elem()
jsonUnmarshalerType = reflect.TypeOf(new(json.Unmarshaler)).Elem()
binaryMarshalerType = reflect.TypeOf(new(encoding.BinaryMarshaler)).Elem()
binaryUnmarshalerType = reflect.TypeOf(new(encoding.BinaryUnmarshaler)).Elem()
errorType = reflect.TypeOf(new(error)).Elem()
)

//----------------------------------------
Expand Down