From ebbfb2de58f24f757045279e66d3e57004b92d14 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Wed, 11 Dec 2024 13:52:21 +0300 Subject: [PATCH] *: Replace `github.com/nspcc-dev/neofs-api-go/v2` module TBD Signed-off-by: Leonard Lyubich --- accounting/decimal.go | 60 +++++++++++++++++---------------- accounting/decimal_test.go | 17 +++++----- accounting/test/decimal_test.go | 6 ++-- 3 files changed, 41 insertions(+), 42 deletions(-) diff --git a/accounting/decimal.go b/accounting/decimal.go index 9cea6618..a4e560df 100644 --- a/accounting/decimal.go +++ b/accounting/decimal.go @@ -1,36 +1,41 @@ package accounting import ( - "github.com/nspcc-dev/neofs-api-go/v2/accounting" + neofsproto "github.com/nspcc-dev/neofs-sdk-go/internal/proto" + protoaccounting "github.com/nspcc-dev/neofs-sdk-go/proto/accounting" + "google.golang.org/protobuf/proto" ) // Decimal represents decimal number for accounting operations. // -// Decimal is mutually compatible with github.com/nspcc-dev/neofs-api-go/v2/accounting.Decimal -// message. See ReadFromV2 / WriteToV2 methods. +// Decimal is mutually compatible with [protoaccounting.Decimal] message. See +// [Decimal.FromProtoMessage] / [Decimal.FromProtoMessage] methods. // // Instances can be created using built-in var declaration. -// -// Note that direct typecast is not safe and may result in loss of compatibility: -// -// _ = Decimal(accounting.Decimal{}) // not recommended -type Decimal accounting.Decimal +type Decimal struct { + val int64 + prec uint32 +} -// ReadFromV2 reads Decimal from the accounting.Decimal message. Checks if the -// message conforms to NeoFS API V2 protocol. +// FromProtoMessage validates m according to the NeoFS API protocol and restores +// d from it. // -// See also WriteToV2. -func (d *Decimal) ReadFromV2(m accounting.Decimal) error { - *d = Decimal(m) +// See also [Decimal.ProtoMessage]. +func (d *Decimal) FromProtoMessage(m *protoaccounting.Decimal) error { + d.val = m.Value + d.prec = m.Precision return nil } -// WriteToV2 writes Decimal to the accounting.Decimal message. -// The message must not be nil. +// ProtoMessage converts d into message to transmit using the NeoFS API +// protocol. // -// See also ReadFromV2. -func (d Decimal) WriteToV2(m *accounting.Decimal) { - *m = (accounting.Decimal)(d) +// See also [Decimal.FromProtoMessage]. +func (d Decimal) ProtoMessage() *protoaccounting.Decimal { + return &protoaccounting.Decimal{ + Value: d.val, + Precision: d.prec, + } } // Value returns value of the decimal number. @@ -39,14 +44,14 @@ func (d Decimal) WriteToV2(m *accounting.Decimal) { // // See also SetValue. func (d Decimal) Value() int64 { - return (*accounting.Decimal)(&d).GetValue() + return d.val } // SetValue sets value of the decimal number. // // See also Value. func (d *Decimal) SetValue(v int64) { - (*accounting.Decimal)(d).SetValue(v) + d.val = v } // Precision returns precision of the decimal number. @@ -55,14 +60,14 @@ func (d *Decimal) SetValue(v int64) { // // See also SetPrecision. func (d Decimal) Precision() uint32 { - return (*accounting.Decimal)(&d).GetPrecision() + return d.prec } // SetPrecision sets precision of the decimal number. // // See also Precision. func (d *Decimal) SetPrecision(p uint32) { - (*accounting.Decimal)(d).SetPrecision(p) + d.prec = p } // Marshal encodes Decimal into a binary format of the NeoFS API protocol @@ -70,10 +75,7 @@ func (d *Decimal) SetPrecision(p uint32) { // // See also Unmarshal. func (d Decimal) Marshal() []byte { - var m accounting.Decimal - d.WriteToV2(&m) - - return m.StableMarshal(nil) + return neofsproto.MarshalMessage(d.ProtoMessage()) } // Unmarshal decodes NeoFS API protocol binary format into the Decimal @@ -82,12 +84,12 @@ func (d Decimal) Marshal() []byte { // // See also Marshal. func (d *Decimal) Unmarshal(data []byte) error { - var m accounting.Decimal + var m protoaccounting.Decimal - err := m.Unmarshal(data) + err := proto.Unmarshal(data, &m) if err != nil { return err } - return d.ReadFromV2(m) + return d.FromProtoMessage(&m) } diff --git a/accounting/decimal_test.go b/accounting/decimal_test.go index a7a9a9aa..cda0076b 100644 --- a/accounting/decimal_test.go +++ b/accounting/decimal_test.go @@ -4,8 +4,8 @@ import ( "math/rand/v2" "testing" - apiaccounting "github.com/nspcc-dev/neofs-api-go/v2/accounting" "github.com/nspcc-dev/neofs-sdk-go/accounting" + protoaccounting "github.com/nspcc-dev/neofs-sdk-go/proto/accounting" "github.com/stretchr/testify/require" ) @@ -48,23 +48,22 @@ func TestDecimal_SetPrecision(t *testing.T) { testDecimalField(t, accounting.Decimal.Precision, (*accounting.Decimal).SetPrecision) } -func TestDecimal_ReadFromV2(t *testing.T) { - var m apiaccounting.Decimal - m.SetValue(anyValidValue) - m.SetPrecision(anyValidPrecision) +func TestDecimal_FromProtoMessage(t *testing.T) { + var m protoaccounting.Decimal + m.Value = anyValidValue + m.Precision = anyValidPrecision var val accounting.Decimal - require.NoError(t, val.ReadFromV2(m)) + require.NoError(t, val.FromProtoMessage(&m)) require.EqualValues(t, anyValidValue, val.Value()) require.EqualValues(t, anyValidPrecision, val.Precision()) } func TestDecimal_WriteToV2(t *testing.T) { var val accounting.Decimal - var m apiaccounting.Decimal // zero - val.WriteToV2(&m) + m := val.ProtoMessage() require.Zero(t, m.GetValue()) require.Zero(t, m.GetPrecision()) @@ -72,7 +71,7 @@ func TestDecimal_WriteToV2(t *testing.T) { val.SetValue(anyValidValue) val.SetPrecision(anyValidPrecision) - val.WriteToV2(&m) + m = val.ProtoMessage() require.EqualValues(t, anyValidValue, val.Value()) require.EqualValues(t, anyValidPrecision, val.Precision()) } diff --git a/accounting/test/decimal_test.go b/accounting/test/decimal_test.go index 8801f37a..a8f3dcda 100644 --- a/accounting/test/decimal_test.go +++ b/accounting/test/decimal_test.go @@ -3,7 +3,6 @@ package accountingtest_test import ( "testing" - apiaccounting "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" @@ -13,10 +12,9 @@ func TestDecimal(t *testing.T) { d := accountingtest.Decimal() require.NotEqual(t, d, accountingtest.Decimal()) - var m apiaccounting.Decimal - d.WriteToV2(&m) + m := d.ProtoMessage() var d2 accounting.Decimal - require.NoError(t, d2.ReadFromV2(m)) + require.NoError(t, d2.FromProtoMessage(m)) require.Equal(t, d, d2) require.NoError(t, new(accounting.Decimal).Unmarshal(d.Marshal()))