Add some encoding methods (#378)

_Inspired by https://github.com/AxLabs/neofs-shared-lib project._
This commit is contained in:
Roman Khimov 2023-04-13 13:00:45 +03:00 committed by GitHub
commit 9a543b6f64
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 118 additions and 1 deletions

View file

@ -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)
}

View file

@ -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)
}

View file

@ -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.

View file

@ -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)
}

View file

@ -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)
}

View file

@ -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)
}