Skip to content

Commit

Permalink
*: Replace github.com/nspcc-dev/neofs-api-go/v2 module
Browse files Browse the repository at this point in the history
TBD

Signed-off-by: Leonard Lyubich <leonard@morphbits.io>
  • Loading branch information
cthulhu-rider committed Dec 11, 2024
1 parent fef245e commit ebbfb2d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 42 deletions.
60 changes: 31 additions & 29 deletions accounting/decimal.go
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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.
Expand All @@ -55,25 +60,22 @@ 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
// (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)
return neofsproto.MarshalMessage(d.ProtoMessage())
}

// Unmarshal decodes NeoFS API protocol binary format into the Decimal
Expand All @@ -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)
}
17 changes: 8 additions & 9 deletions accounting/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -48,31 +48,30 @@ 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())

// filled
val.SetValue(anyValidValue)
val.SetPrecision(anyValidPrecision)

val.WriteToV2(&m)
m = val.ProtoMessage()
require.EqualValues(t, anyValidValue, val.Value())
require.EqualValues(t, anyValidPrecision, val.Precision())
}
Expand Down
6 changes: 2 additions & 4 deletions accounting/test/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()))
Expand Down

0 comments on commit ebbfb2d

Please sign in to comment.