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
44 changes: 44 additions & 0 deletions binary_encode_override_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package amino_test

import (
jordaaash marked this conversation as resolved.
Show resolved Hide resolved
"github.com/stretchr/testify/assert"
amino "github.com/tendermint/go-amino"
"testing"
)

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

// func (thing Thing) MarshalAmino() (string, error) {
jordaaash marked this conversation as resolved.
Show resolved Hide resolved
// return thing.Name, nil
// }
//
// func (thing Thing) UnmarshalAmino(name string) error {
// thing.Name = name
// return nil
// }

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 TestMarshalBinaryOverride(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)

var thing2 Thing
err = cdc.UnmarshalBinaryBare(bz, &thing2)
assert.Nil(t, err)
assert.Equal(t, thing1, thing2)
}
jordaaash marked this conversation as resolved.
Show resolved Hide resolved