frostfs-sdk-go/accounting/decimal.go
Airat Arifullin 6281a25556
All checks were successful
/ DCO (pull_request) Successful in 1m17s
/ Lint (pull_request) Successful in 2m7s
/ Tests (1.19) (pull_request) Successful in 5m56s
/ Tests (1.20) (pull_request) Successful in 6m37s
[#100] types: Make sdk types as protobuf wrappers
Signed-off-by: Airat Arifullin a.arifullin@yadro.com
2023-07-12 19:08:37 +03:00

75 lines
1.7 KiB
Go

package accounting
import (
accgrpc "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/accounting/grpc"
"github.com/golang/protobuf/proto"
)
// Decimal represents decimal number for accounting operations.
//
// Decimal is mutually compatible with git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/accgrpc.Decimal
// message. See ReadFromV2 / WriteToV2 methods.
//
// Instances must be created by NewDecimal() constructor.
//
// Note that direct typecast is not safe and may result in loss of compatibility:
//
// _ = Decimal(accgrpc.Decimal{}) // not recommended
type Decimal struct {
decimal *accgrpc.Decimal
}
func NewDecimal() Decimal {
return Decimal{
decimal: &accgrpc.Decimal{},
}
}
// ReadFromV2 reads Decimal from the accgrpc.Decimal message. Checks if the
// message conforms to FrostFS API V2 protocol.
//
// See also WriteToV2.
func (d *Decimal) ReadFromV2(m *accgrpc.Decimal) {
proto.Merge(d.decimal, m)
}
// WriteToV2 writes Decimal to the accgrpc.Decimal message.
// The message must not be nil.
//
// See also ReadFromV2.
func (d *Decimal) WriteToV2(m *accgrpc.Decimal) {
m.Reset()
proto.Merge(m, d.decimal)
}
// Value returns value of the decimal number.
//
// Zero Decimal has zero value.
//
// See also SetValue.
func (d Decimal) Value() int64 {
return d.decimal.GetValue()
}
// SetValue sets value of the decimal number.
//
// See also Value.
func (d *Decimal) SetValue(v int64) {
d.decimal.SetValue(v)
}
// Precision returns precision of the decimal number.
//
// Zero Decimal has zero precision.
//
// See also SetPrecision.
func (d *Decimal) Precision() uint32 {
return d.decimal.GetPrecision()
}
// SetPrecision sets precision of the decimal number.
//
// See also Precision.
func (d *Decimal) SetPrecision(p uint32) {
d.decimal.SetPrecision(p)
}