forked from TrueCloudLab/neoneo-go
util: allow to marshal Uint160 in YAML
Signed-off-by: Evgeniy Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
parent
9224f57323
commit
c212b2be95
2 changed files with 37 additions and 0 deletions
|
@ -135,6 +135,25 @@ func (u Uint160) MarshalJSON() ([]byte, error) {
|
||||||
return []byte(`"0x` + u.StringLE() + `"`), nil
|
return []byte(`"0x` + u.StringLE() + `"`), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UnmarshalYAML implements the YAML Unmarshaler interface.
|
||||||
|
func (u *Uint160) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||||
|
var s string
|
||||||
|
|
||||||
|
err := unmarshal(&s)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
s = strings.TrimPrefix(s, "0x")
|
||||||
|
*u, err = Uint160DecodeStringLE(s)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalYAML implements the YAML marshaller interface.
|
||||||
|
func (u Uint160) MarshalYAML() (interface{}, error) {
|
||||||
|
return "0x" + u.StringLE(), nil
|
||||||
|
}
|
||||||
|
|
||||||
// EncodeBinary implements Serializable interface.
|
// EncodeBinary implements Serializable interface.
|
||||||
func (u *Uint160) EncodeBinary(bw *io.BinWriter) {
|
func (u *Uint160) EncodeBinary(bw *io.BinWriter) {
|
||||||
bw.WriteBytes(u[:])
|
bw.WriteBytes(u[:])
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestUint160UnmarshalJSON(t *testing.T) {
|
func TestUint160UnmarshalJSON(t *testing.T) {
|
||||||
|
@ -26,6 +27,23 @@ func TestUint160UnmarshalJSON(t *testing.T) {
|
||||||
assert.Error(t, u2.UnmarshalJSON([]byte(`123`)))
|
assert.Error(t, u2.UnmarshalJSON([]byte(`123`)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUint160UnmarshalYAML(t *testing.T) {
|
||||||
|
str := "0263c1de100292813b5e075e585acc1bae963b2d"
|
||||||
|
expected, err := util.Uint160DecodeStringLE(str)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
var u1, u2 util.Uint160
|
||||||
|
require.NoError(t, yaml.Unmarshal([]byte(`"`+str+`"`), &u1))
|
||||||
|
require.Equal(t, expected, u1)
|
||||||
|
|
||||||
|
data, err := yaml.Marshal(u1)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NoError(t, yaml.Unmarshal(data, &u2))
|
||||||
|
require.Equal(t, expected, u2)
|
||||||
|
|
||||||
|
require.Error(t, yaml.Unmarshal([]byte(`[]`), &u1))
|
||||||
|
}
|
||||||
|
|
||||||
func TestUInt160DecodeString(t *testing.T) {
|
func TestUInt160DecodeString(t *testing.T) {
|
||||||
hexStr := "2d3b96ae1bcc5a585e075e3b81920210dec16302"
|
hexStr := "2d3b96ae1bcc5a585e075e3b81920210dec16302"
|
||||||
val, err := util.Uint160DecodeStringBE(hexStr)
|
val, err := util.Uint160DecodeStringBE(hexStr)
|
||||||
|
|
Loading…
Reference in a new issue