Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: Replace github.com/nspcc-dev/neofs-api-go/v2 module #667

Merged
merged 10 commits into from
Dec 27, 2024
Merged
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# neofs-sdk-go
Go implementation of NeoFS SDK. It contains high-level version-independent wrappers
for structures from [neofs-api-go](https://github.com/nspcc-dev/neofs-api-go) as well as
for structures from [proto](https://github.com/nspcc-dev/neofs-sdk-go/proto) packages as well as
helper functions for simplifying node/dApp implementations.

## Repository structure
Expand Down
62 changes: 28 additions & 34 deletions accounting/decimal.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
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"
)

// 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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest to drop such statements since regular user is not interested in this

@roman-khimov @carpawell

// [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 {
roman-khimov marked this conversation as resolved.
Show resolved Hide resolved
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
carpawell marked this conversation as resolved.
Show resolved Hide resolved
// d from it.
//
// See also WriteToV2.
func (d *Decimal) ReadFromV2(m accounting.Decimal) error {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note that temporary Deprecated mark wont work: conflicting import panic occurs

*d = Decimal(m)
// See also [Decimal.ProtoMessage].
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and this, Client and node developers know what to use

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 +43,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 +59,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.Marshal(d)
}

// Unmarshal decodes NeoFS API protocol binary format into the Decimal
Expand All @@ -82,12 +83,5 @@ func (d Decimal) Marshal() []byte {
//
// 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)
return neofsproto.Unmarshal(data, d)
}
25 changes: 12 additions & 13 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,33 +48,32 @@ 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) {
func TestDecimal_ProtoMessage(t *testing.T) {
var val accounting.Decimal
var m apiaccounting.Decimal

// zero
val.WriteToV2(&m)
require.Zero(t, m.GetValue())
m := val.ProtoMessage()
require.Zero(t, m.GetValue())
require.Zero(t, m.GetPrecision())

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

val.WriteToV2(&m)
require.EqualValues(t, anyValidValue, val.Value())
require.EqualValues(t, anyValidPrecision, val.Precision())
m = val.ProtoMessage()
require.EqualValues(t, anyValidValue, m.GetValue())
require.EqualValues(t, anyValidPrecision, m.GetPrecision())
}

func TestToken_Marshal(t *testing.T) {
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
Loading
Loading