forked from TrueCloudLab/frostfs-api-go
[#168] sdk/accounting: Refactor Decimal type
Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
e721734599
commit
52fae76533
4 changed files with 62 additions and 6 deletions
|
@ -4,6 +4,39 @@ import (
|
||||||
"github.com/nspcc-dev/neofs-api-go/v2/accounting"
|
"github.com/nspcc-dev/neofs-api-go/v2/accounting"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Decimal struct {
|
// Decimal represents v2-compatible decimal number.
|
||||||
accounting.Decimal
|
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)
|
||||||
}
|
}
|
||||||
|
|
25
pkg/accounting/decimal_test.go
Normal file
25
pkg/accounting/decimal_test.go
Normal file
|
@ -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())
|
||||||
|
}
|
|
@ -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 nil, errors.Wrap(err, "can't verify response message")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &accounting.Decimal{
|
return accounting.NewDecimalFromV2(resp.GetBody().GetBalance()), nil
|
||||||
Decimal: *resp.GetBody().GetBalance(),
|
|
||||||
}, nil
|
|
||||||
default:
|
default:
|
||||||
return nil, unsupportedProtocolErr
|
return nil, unsupportedProtocolErr
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,7 @@ func TestExample(t *testing.T) {
|
||||||
resp, err := cli.GetSelfBalance(context.Background())
|
resp, err := cli.GetSelfBalance(context.Background())
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
fmt.Println(resp.GetValue(), resp.GetPrecision())
|
fmt.Println(resp.Value(), resp.Precision())
|
||||||
|
|
||||||
// create client from grpc connection
|
// create client from grpc connection
|
||||||
conn, err := grpc.DialContext(context.Background(), target, grpc.WithBlock(), grpc.WithInsecure())
|
conn, err := grpc.DialContext(context.Background(), target, grpc.WithBlock(), grpc.WithInsecure())
|
||||||
|
|
Loading…
Reference in a new issue