diff --git a/accounting/decimal.go b/accounting/decimal.go index da9281f0..9cea6618 100644 --- a/accounting/decimal.go +++ b/accounting/decimal.go @@ -1,6 +1,8 @@ package accounting -import "github.com/nspcc-dev/neofs-api-go/v2/accounting" +import ( + "github.com/nspcc-dev/neofs-api-go/v2/accounting" +) // Decimal represents decimal number for accounting operations. // @@ -62,3 +64,30 @@ func (d Decimal) Precision() uint32 { func (d *Decimal) SetPrecision(p uint32) { (*accounting.Decimal)(d).SetPrecision(p) } + +// Marshal encodes Decimal into a binary format of the NeoFS API protocol +// (Protocol Buffers with direct field order). +// +// See also Unmarshal. +func (d Decimal) Marshal() []byte { + var m accounting.Decimal + d.WriteToV2(&m) + + return m.StableMarshal(nil) +} + +// Unmarshal decodes NeoFS API protocol binary format into the Decimal +// (Protocol Buffers with direct field order). Returns an error describing +// a format violation. +// +// See also Marshal. +func (d *Decimal) Unmarshal(data []byte) error { + var m accounting.Decimal + + err := m.Unmarshal(data) + if err != nil { + return err + } + + return d.ReadFromV2(m) +} diff --git a/accounting/decimal_test.go b/accounting/decimal_test.go index 97a54689..1b3fc834 100644 --- a/accounting/decimal_test.go +++ b/accounting/decimal_test.go @@ -5,6 +5,7 @@ import ( v2accounting "github.com/nspcc-dev/neofs-api-go/v2/accounting" "github.com/nspcc-dev/neofs-sdk-go/accounting" + accountingtest "github.com/nspcc-dev/neofs-sdk-go/accounting/test" "github.com/stretchr/testify/require" ) @@ -44,3 +45,12 @@ func TestDecimalMessageV2(t *testing.T) { require.EqualValues(t, d.Value(), m2.GetValue()) require.EqualValues(t, d.Precision(), m2.GetPrecision()) } + +func TestDecimal_Marshal(t *testing.T) { + d := *accountingtest.Decimal() + + var d2 accounting.Decimal + require.NoError(t, d2.Unmarshal(d.Marshal())) + + require.Equal(t, d, d2) +} diff --git a/netmap/network_info.go b/netmap/network_info.go index f5826214..7292ff37 100644 --- a/netmap/network_info.go +++ b/netmap/network_info.go @@ -112,6 +112,33 @@ func (x NetworkInfo) WriteToV2(m *netmap.NetworkInfo) { *m = x.m } +// Marshal encodes NetworkInfo into a binary format of the NeoFS API protocol +// (Protocol Buffers with direct field order). +// +// See also Unmarshal. +func (x NetworkInfo) Marshal() []byte { + var m netmap.NetworkInfo + x.WriteToV2(&m) + + return m.StableMarshal(nil) +} + +// Unmarshal decodes NeoFS API protocol binary format into the NetworkInfo +// (Protocol Buffers with direct field order). Returns an error describing +// a format violation. +// +// See also Marshal. +func (x *NetworkInfo) Unmarshal(data []byte) error { + var m netmap.NetworkInfo + + err := m.Unmarshal(data) + if err != nil { + return err + } + + return x.readFromV2(m, false) +} + // CurrentEpoch returns epoch set using SetCurrentEpoch. // // Zero NetworkInfo has zero current epoch. diff --git a/netmap/network_info_test.go b/netmap/network_info_test.go index 710e39ad..195f31a7 100644 --- a/netmap/network_info_test.go +++ b/netmap/network_info_test.go @@ -7,6 +7,7 @@ import ( "github.com/nspcc-dev/neofs-api-go/v2/netmap" . "github.com/nspcc-dev/neofs-sdk-go/netmap" + netmaptest "github.com/nspcc-dev/neofs-sdk-go/netmap/test" "github.com/stretchr/testify/require" ) @@ -251,3 +252,12 @@ func TestNetworkInfo_MaintenanceModeAllowed(t *testing.T) { }, ) } + +func TestNetworkInfo_Marshal(t *testing.T) { + v := netmaptest.NetworkInfo() + + var v2 NetworkInfo + require.NoError(t, v2.Unmarshal(v.Marshal())) + + require.Equal(t, v, v2) +} diff --git a/version/version.go b/version/version.go index 0a05fcc3..ded56b35 100644 --- a/version/version.go +++ b/version/version.go @@ -84,3 +84,29 @@ func (v Version) Equal(v2 Version) bool { return v.Major() == v2.Major() && v.Minor() == v2.Minor() } + +// MarshalJSON encodes Version into a JSON format of the NeoFS API +// protocol (Protocol Buffers JSON). +// +// See also UnmarshalJSON. +func (v Version) MarshalJSON() ([]byte, error) { + var m refs.Version + v.WriteToV2(&m) + + return m.MarshalJSON() +} + +// UnmarshalJSON decodes NeoFS API protocol JSON format into the Version +// (Protocol Buffers JSON). Returns an error describing a format violation. +// +// See also MarshalJSON. +func (v *Version) UnmarshalJSON(data []byte) error { + var m refs.Version + + err := m.UnmarshalJSON(data) + if err != nil { + return err + } + + return v.ReadFromV2(m) +} diff --git a/version/version_test.go b/version/version_test.go index 0d1aa0ab..7dda8ce4 100644 --- a/version/version_test.go +++ b/version/version_test.go @@ -1,6 +1,7 @@ package version import ( + "math/rand" "testing" "github.com/nspcc-dev/neofs-api-go/v2/refs" @@ -48,3 +49,17 @@ func TestSDKVersion(t *testing.T) { require.Equal(t, uint32(sdkMjr), v.Major()) require.Equal(t, uint32(sdkMnr), v.Minor()) } + +func TestVersion_MarshalJSON(t *testing.T) { + var v Version + v.SetMajor(rand.Uint32()) + v.SetMinor(rand.Uint32()) + + data, err := v.MarshalJSON() + require.NoError(t, err) + + var v2 Version + require.NoError(t, v2.UnmarshalJSON(data)) + + require.Equal(t, v, v2) +}