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

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 {
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 {
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 +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)
}
21 changes: 10 additions & 11 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) {
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)
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
82 changes: 43 additions & 39 deletions checksum/checksum.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,24 @@ import (
"fmt"
"hash"

"github.com/nspcc-dev/neofs-api-go/v2/refs"
"github.com/nspcc-dev/neofs-sdk-go/proto/refs"
"github.com/nspcc-dev/tzhash/tz"
)

// Checksum represents checksum of some digital data.
//
// Checksum is mutually compatible with github.com/nspcc-dev/neofs-api-go/v2/refs.Checksum
// message. See ReadFromV2 / WriteToV2 methods.
// Checksum is mutually compatible with [refs.Checksum] message. See
// [Checksum.FromProtoMessage] / [Checksum.ProtoMessage] methods.
//
// Instances must be created using one of the constructors.
//
// Note that direct typecast is not safe and may result in loss of compatibility:
//
// _ = Checksum(refs.Checksum{}) // not recommended
type Checksum refs.Checksum
type Checksum struct {
typ Type
val []byte
}

// Type represents the enumeration
// of checksum types.
type Type uint32
type Type int32

const (
Unknown Type = iota // Deprecated: use 0 instead.
Expand All @@ -42,19 +41,27 @@ func typeToProto(t Type) refs.ChecksumType {
default:
return refs.ChecksumType(t)
case SHA256:
return refs.SHA256
return refs.ChecksumType_SHA256
case TillichZemor:
return refs.TillichZemor
return refs.ChecksumType_TZ
}
}

func typeFromProto(t refs.ChecksumType) Type {
switch t {
default:
return Type(t)
case refs.ChecksumType_SHA256:
return SHA256
case refs.ChecksumType_TZ:
return TillichZemor
}
}

// New constructs new Checksum instance. It is the caller's responsibility to
// ensure that the hash matches the type.
func New(typ Type, hsh []byte) Checksum {
var res refs.Checksum
res.SetType(typeToProto(typ))
res.SetSum(hsh)
return Checksum(res)
return Checksum{typ: typ, val: hsh}
}

// NewSHA256 constructs new Checksum from SHA-256 hash.
Expand Down Expand Up @@ -87,26 +94,33 @@ func NewFromData(typ Type, data []byte) (Checksum, error) {
}
}

// ReadFromV2 reads Checksum from the refs.Checksum message. Checks if the
// message conforms to NeoFS API V2 protocol.
// FromProtoMessage validates m according to the NeoFS API protocol and restores
// c from it.
//
// See also WriteToV2.
func (c *Checksum) ReadFromV2(m refs.Checksum) error {
if len(m.GetSum()) == 0 {
// See also [Checksum.ProtoMessage].
func (c *Checksum) FromProtoMessage(m *refs.Checksum) error {
if m.Type < 0 {
return fmt.Errorf("negative type %d", m.Type)
}
if len(m.Sum) == 0 {
return errors.New("missing value")
}

*c = Checksum(m)
c.typ = typeFromProto(m.Type)
c.val = m.Sum

return nil
}

// WriteToV2 writes Checksum to the refs.Checksum message.
// The message must not be nil.
// ProtoMessage converts c into message to transmit using the NeoFS API
// protocol.
//
// See also ReadFromV2.
func (c Checksum) WriteToV2(m *refs.Checksum) {
*m = (refs.Checksum)(c)
// See also [Checksum.FromProtoMessage].
func (c Checksum) ProtoMessage() *refs.Checksum {
return &refs.Checksum{
Type: typeToProto(c.typ),
Sum: c.val,
}
}

// Type returns checksum type.
Expand All @@ -115,15 +129,7 @@ func (c Checksum) WriteToV2(m *refs.Checksum) {
//
// See also [NewTillichZemor], [NewSHA256].
func (c Checksum) Type() Type {
v2 := (refs.Checksum)(c)
switch typ := v2.GetType(); typ {
case refs.SHA256:
return SHA256
case refs.TillichZemor:
return TillichZemor
default:
return Type(typ)
}
return c.typ
}

// Value returns checksum bytes. Return value
Expand All @@ -136,8 +142,7 @@ func (c Checksum) Type() Type {
//
// See also [NewTillichZemor], [NewSHA256].
func (c Checksum) Value() []byte {
v2 := (refs.Checksum)(c)
return v2.GetSum()
return c.val
}

// SetSHA256 sets checksum to SHA256 hash.
Expand Down Expand Up @@ -174,8 +179,7 @@ func (c *Checksum) SetTillichZemor(v [tz.Size]byte) { *c = NewTillichZemor(v) }
// String is designed to be human-readable, and its format MAY differ between
// SDK versions.
func (c Checksum) String() string {
v2 := (refs.Checksum)(c)
return fmt.Sprintf("%s:%s", c.Type(), hex.EncodeToString(v2.GetSum()))
return fmt.Sprintf("%s:%s", c.Type(), hex.EncodeToString(c.Value()))
}

// String implements fmt.Stringer.
Expand Down
Loading
Loading