[#168] accounting: Implement binary/JSON encoders/decoders on Decimal

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-11-13 13:38:49 +03:00 committed by Alex Vanin
parent 52fae76533
commit 9bebc1247d
6 changed files with 139 additions and 25 deletions

26
v2/accounting/json.go Normal file
View file

@ -0,0 +1,26 @@
package accounting
import (
accounting "github.com/nspcc-dev/neofs-api-go/v2/accounting/grpc"
"google.golang.org/protobuf/encoding/protojson"
)
func (d *Decimal) MarshalJSON() ([]byte, error) {
return protojson.MarshalOptions{
EmitUnpopulated: true,
}.Marshal(
DecimalToGRPCMessage(d),
)
}
func (d *Decimal) UnmarshalJSON(data []byte) error {
msg := new(accounting.Decimal)
if err := protojson.Unmarshal(data, msg); err != nil {
return err
}
*d = *DecimalFromGRPCMessage(msg)
return nil
}

View file

@ -0,0 +1,20 @@
package accounting_test
import (
"testing"
"github.com/nspcc-dev/neofs-api-go/v2/accounting"
"github.com/stretchr/testify/require"
)
func TestDecimalJSON(t *testing.T) {
i := generateDecimal(10)
data, err := i.MarshalJSON()
require.NoError(t, err)
i2 := new(accounting.Decimal)
require.NoError(t, i2.UnmarshalJSON(data))
require.Equal(t, i, i2)
}

View file

@ -1,7 +1,9 @@
package accounting
import (
"github.com/nspcc-dev/neofs-api-go/util/proto"
protoutil "github.com/nspcc-dev/neofs-api-go/util/proto"
accounting "github.com/nspcc-dev/neofs-api-go/v2/accounting/grpc"
"google.golang.org/protobuf/proto"
)
const (
@ -27,14 +29,14 @@ func (d *Decimal) StableMarshal(buf []byte) ([]byte, error) {
err error
)
n, err = proto.Int64Marshal(decimalValueField, buf[offset:], d.val)
n, err = protoutil.Int64Marshal(decimalValueField, buf[offset:], d.val)
if err != nil {
return nil, err
}
offset += n
n, err = proto.UInt32Marshal(decimalPrecisionField, buf[offset:], d.prec)
n, err = protoutil.UInt32Marshal(decimalPrecisionField, buf[offset:], d.prec)
if err != nil {
return nil, err
}
@ -47,12 +49,23 @@ func (d *Decimal) StableSize() (size int) {
return 0
}
size += proto.Int64Size(decimalValueField, d.val)
size += proto.UInt32Size(decimalPrecisionField, d.prec)
size += protoutil.Int64Size(decimalValueField, d.val)
size += protoutil.UInt32Size(decimalPrecisionField, d.prec)
return size
}
func (d *Decimal) Unmarshal(data []byte) error {
m := new(accounting.Decimal)
if err := proto.Unmarshal(data, m); err != nil {
return err
}
*d = *DecimalFromGRPCMessage(m)
return nil
}
func (b *BalanceRequestBody) StableMarshal(buf []byte) ([]byte, error) {
if b == nil {
return []byte{}, nil
@ -62,7 +75,7 @@ func (b *BalanceRequestBody) StableMarshal(buf []byte) ([]byte, error) {
buf = make([]byte, b.StableSize())
}
_, err := proto.NestedStructureMarshal(balanceReqBodyOwnerField, buf, b.ownerID)
_, err := protoutil.NestedStructureMarshal(balanceReqBodyOwnerField, buf, b.ownerID)
if err != nil {
return nil, err
}
@ -75,7 +88,7 @@ func (b *BalanceRequestBody) StableSize() (size int) {
return 0
}
size = proto.NestedStructureSize(balanceReqBodyOwnerField, b.ownerID)
size = protoutil.NestedStructureSize(balanceReqBodyOwnerField, b.ownerID)
return size
}
@ -89,7 +102,7 @@ func (br *BalanceResponseBody) StableMarshal(buf []byte) ([]byte, error) {
buf = make([]byte, br.StableSize())
}
_, err := proto.NestedStructureMarshal(balanceRespBodyDecimalField, buf, br.bal)
_, err := protoutil.NestedStructureMarshal(balanceRespBodyDecimalField, buf, br.bal)
if err != nil {
return nil, err
}
@ -102,7 +115,7 @@ func (br *BalanceResponseBody) StableSize() (size int) {
return 0
}
size = proto.NestedStructureSize(balanceRespBodyDecimalField, br.bal)
size = protoutil.NestedStructureSize(balanceRespBodyDecimalField, br.bal)
return size
}

View file

@ -10,22 +10,6 @@ import (
goproto "google.golang.org/protobuf/proto"
)
func TestDecimal_StableMarshal(t *testing.T) {
decimalFrom := generateDecimal(888)
transport := new(grpc.Decimal)
t.Run("non empty", func(t *testing.T) {
wire, err := decimalFrom.StableMarshal(nil)
require.NoError(t, err)
err = goproto.Unmarshal(wire, transport)
require.NoError(t, err)
decimalTo := accounting.DecimalFromGRPCMessage(transport)
require.Equal(t, decimalFrom, decimalTo)
})
}
func TestBalanceRequestBody_StableMarshal(t *testing.T) {
requestBodyFrom := generateBalanceRequestBody("Owner ID")
transport := new(grpc.BalanceRequest_Body)
@ -82,3 +66,16 @@ func generateBalanceResponseBody(val int64) *accounting.BalanceResponseBody {
return response
}
func TestDecimalMarshal(t *testing.T) {
d := generateDecimal(3)
data, err := d.StableMarshal(nil)
require.NoError(t, err)
d2 := new(accounting.Decimal)
require.NoError(t, d2.Unmarshal(data))
require.Equal(t, d, d2)
}