Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Leonard Lyubich <leonard@morphbits.io>
  • Loading branch information
cthulhu-rider committed May 15, 2024
1 parent f4a8651 commit d16e584
Show file tree
Hide file tree
Showing 17 changed files with 6,257 additions and 0 deletions.
65 changes: 65 additions & 0 deletions api/accounting/encoding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package accounting

import (
"github.com/nspcc-dev/neofs-sdk-go/internal/proto"
)

const (
_ = iota
fieldNumDecimalValue
fieldNumDecimalPrecision
)

func (x *Decimal) MarshaledSize() int {
var sz int
if x != nil {
sz = proto.SizeVarint(fieldNumDecimalValue, x.Value)
sz += proto.SizeVarint(fieldNumDecimalPrecision, x.Precision)
}
return sz
}

func (x *Decimal) MarshalStable(b []byte) {
if x != nil {
off := proto.MarshalVarint(b, fieldNumDecimalValue, x.Value)
proto.MarshalVarint(b[off:], fieldNumDecimalPrecision, x.Precision)
}
}

const (
_ = iota
fieldNumBalanceReqBodyOwner
)

func (x *BalanceRequest_Body) MarshaledSize() int {
var sz int
if x != nil {
sz = proto.SizeNested(fieldNumBalanceReqBodyOwner, x.OwnerId)
}
return sz
}

func (x *BalanceRequest_Body) MarshalStable(b []byte) {
if x != nil {
proto.MarshalNested(b, fieldNumBalanceReqBodyOwner, x.OwnerId)
}
}

const (
_ = iota
fieldNumBalanceRespBodyBalance
)

func (x *BalanceResponse_Body) MarshaledSize() int {
var sz int
if x != nil {
sz = proto.SizeNested(fieldNumBalanceRespBodyBalance, x.Balance)
}
return sz
}

func (x *BalanceResponse_Body) MarshalStable(b []byte) {
if x != nil {
proto.MarshalNested(b, fieldNumBalanceRespBodyBalance, x.Balance)
}
}
62 changes: 62 additions & 0 deletions api/accounting/encoding_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package accounting_test

import (
"testing"

"github.com/nspcc-dev/neofs-sdk-go/api/accounting"
"github.com/nspcc-dev/neofs-sdk-go/api/refs"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
)

func TestDecimal(t *testing.T) {
v := &accounting.Decimal{
Value: 123,
Precision: 456,
}

sz := v.MarshaledSize()
b := make([]byte, sz)
v.MarshalStable(b)

var res accounting.Decimal
err := proto.Unmarshal(b, &res)
require.NoError(t, err)
require.EqualValues(t, v.Value, res.Value)
require.EqualValues(t, v.Precision, res.Precision)
}

func TestBalanceRequestBody(t *testing.T) {
v := &accounting.BalanceRequest_Body{
OwnerId: &refs.OwnerID{
Value: []byte("any_owner"),
},
}

sz := v.MarshaledSize()
b := make([]byte, sz)
v.MarshalStable(b)

var res accounting.BalanceRequest_Body
err := proto.Unmarshal(b, &res)
require.NoError(t, err)
require.Equal(t, v.OwnerId, res.OwnerId)
}

func TestBalanceResponseBody(t *testing.T) {
v := &accounting.BalanceResponse_Body{
Balance: &accounting.Decimal{
Value: 12,
Precision: 34,
},
}

sz := v.MarshaledSize()
b := make([]byte, sz)
v.MarshalStable(b)

var res accounting.BalanceResponse_Body
err := proto.Unmarshal(b, &res)
require.NoError(t, err)
require.Equal(t, v.Balance, res.Balance)
}
Loading

0 comments on commit d16e584

Please sign in to comment.