From 52fae76533902c21985177b5c943f1fd70900d1d Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Fri, 13 Nov 2020 13:36:18 +0300 Subject: [PATCH] [#168] sdk/accounting: Refactor Decimal type Signed-off-by: Leonard Lyubich --- pkg/accounting/decimal.go | 37 ++++++++++++++++++++++++++++++++-- pkg/accounting/decimal_test.go | 25 +++++++++++++++++++++++ pkg/client/accounting.go | 4 +--- pkg/client/client_test.go | 2 +- 4 files changed, 62 insertions(+), 6 deletions(-) create mode 100644 pkg/accounting/decimal_test.go diff --git a/pkg/accounting/decimal.go b/pkg/accounting/decimal.go index 9e73e54..b6e275f 100644 --- a/pkg/accounting/decimal.go +++ b/pkg/accounting/decimal.go @@ -4,6 +4,39 @@ import ( "github.com/nspcc-dev/neofs-api-go/v2/accounting" ) -type Decimal struct { - accounting.Decimal +// Decimal represents v2-compatible decimal number. +type Decimal accounting.Decimal + +// NewDecimal creates, initializes and returns blank Decimal instance. +func NewDecimal() *Decimal { + return NewDecimalFromV2(new(accounting.Decimal)) +} + +// NewDecimalFromV2 converts v2 Decimal to Decimal. +func NewDecimalFromV2(d *accounting.Decimal) *Decimal { + return (*Decimal)(d) +} + +// Value returns value of the decimal number. +func (d *Decimal) Value() int64 { + return (*accounting.Decimal)(d). + GetValue() +} + +// SetValue sets value of the decimal number. +func (d *Decimal) SetValue(v int64) { + (*accounting.Decimal)(d). + SetValue(v) +} + +// Precision returns precision of the decimal number. +func (d *Decimal) Precision() uint32 { + return (*accounting.Decimal)(d). + GetPrecision() +} + +// SetPrecision sets precision of the decimal number. +func (d *Decimal) SetPrecision(p uint32) { + (*accounting.Decimal)(d). + SetPrecision(p) } diff --git a/pkg/accounting/decimal_test.go b/pkg/accounting/decimal_test.go new file mode 100644 index 0000000..e02a92f --- /dev/null +++ b/pkg/accounting/decimal_test.go @@ -0,0 +1,25 @@ +package accounting + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestDecimal_Value(t *testing.T) { + d := NewDecimal() + + v := int64(3) + d.SetValue(v) + + require.Equal(t, v, d.Value()) +} + +func TestDecimal_Precision(t *testing.T) { + d := NewDecimal() + + p := uint32(3) + d.SetPrecision(p) + + require.Equal(t, p, d.Precision()) +} diff --git a/pkg/client/accounting.go b/pkg/client/accounting.go index 52d25c4..b9e8660 100644 --- a/pkg/client/accounting.go +++ b/pkg/client/accounting.go @@ -69,9 +69,7 @@ func (c Client) getBalanceV2(ctx context.Context, owner *owner.ID, opts ...CallO return nil, errors.Wrap(err, "can't verify response message") } - return &accounting.Decimal{ - Decimal: *resp.GetBody().GetBalance(), - }, nil + return accounting.NewDecimalFromV2(resp.GetBody().GetBalance()), nil default: return nil, unsupportedProtocolErr } diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index 50b5f56..cb36606 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -27,7 +27,7 @@ func TestExample(t *testing.T) { resp, err := cli.GetSelfBalance(context.Background()) require.NoError(t, err) - fmt.Println(resp.GetValue(), resp.GetPrecision()) + fmt.Println(resp.Value(), resp.Precision()) // create client from grpc connection conn, err := grpc.DialContext(context.Background(), target, grpc.WithBlock(), grpc.WithInsecure())