2021-10-26 10:31:31 +00:00
|
|
|
package accounting
|
|
|
|
|
2023-01-31 07:51:49 +00:00
|
|
|
import (
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/accounting"
|
|
|
|
)
|
2021-10-26 10:31:31 +00:00
|
|
|
|
|
|
|
// Decimal represents decimal number for accounting operations.
|
|
|
|
//
|
2022-03-12 16:15:26 +00:00
|
|
|
// Decimal is mutually compatible with github.com/nspcc-dev/neofs-api-go/v2/accounting.Decimal
|
2022-03-17 13:45:26 +00:00
|
|
|
// message. See ReadFromV2 / WriteToV2 methods.
|
2022-03-12 16:15:26 +00:00
|
|
|
//
|
|
|
|
// Instances can be created using built-in var declaration.
|
|
|
|
//
|
|
|
|
// Note that direct typecast is not safe and may result in loss of compatibility:
|
2022-08-24 14:17:40 +00:00
|
|
|
//
|
|
|
|
// _ = Decimal(accounting.Decimal{}) // not recommended
|
2022-03-12 16:15:26 +00:00
|
|
|
type Decimal accounting.Decimal
|
2021-10-26 10:31:31 +00:00
|
|
|
|
2022-07-07 10:51:05 +00:00
|
|
|
// ReadFromV2 reads Decimal from the accounting.Decimal message. Checks if the
|
|
|
|
// message conforms to NeoFS API V2 protocol.
|
2021-10-26 10:31:31 +00:00
|
|
|
//
|
2022-03-17 13:45:26 +00:00
|
|
|
// See also WriteToV2.
|
2022-07-07 10:51:05 +00:00
|
|
|
func (d *Decimal) ReadFromV2(m accounting.Decimal) error {
|
2022-03-12 16:15:26 +00:00
|
|
|
*d = Decimal(m)
|
2022-07-07 10:51:05 +00:00
|
|
|
return nil
|
2021-10-26 10:31:31 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 13:45:26 +00:00
|
|
|
// WriteToV2 writes Decimal to the accounting.Decimal message.
|
2022-03-12 16:15:26 +00:00
|
|
|
// The message must not be nil.
|
2021-10-26 10:31:31 +00:00
|
|
|
//
|
2022-03-17 13:45:26 +00:00
|
|
|
// See also ReadFromV2.
|
|
|
|
func (d Decimal) WriteToV2(m *accounting.Decimal) {
|
2022-03-12 16:15:26 +00:00
|
|
|
*m = (accounting.Decimal)(d)
|
2021-10-26 10:31:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Value returns value of the decimal number.
|
2022-03-12 16:15:26 +00:00
|
|
|
//
|
|
|
|
// Zero Decimal has zero value.
|
|
|
|
//
|
|
|
|
// See also SetValue.
|
|
|
|
func (d Decimal) Value() int64 {
|
|
|
|
return (*accounting.Decimal)(&d).GetValue()
|
2021-10-26 10:31:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetValue sets value of the decimal number.
|
2022-03-12 16:15:26 +00:00
|
|
|
//
|
|
|
|
// See also Value.
|
2021-10-26 10:31:31 +00:00
|
|
|
func (d *Decimal) SetValue(v int64) {
|
|
|
|
(*accounting.Decimal)(d).SetValue(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Precision returns precision of the decimal number.
|
2022-03-12 16:15:26 +00:00
|
|
|
//
|
|
|
|
// Zero Decimal has zero precision.
|
|
|
|
//
|
|
|
|
// See also SetPrecision.
|
|
|
|
func (d Decimal) Precision() uint32 {
|
|
|
|
return (*accounting.Decimal)(&d).GetPrecision()
|
2021-10-26 10:31:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetPrecision sets precision of the decimal number.
|
2022-03-12 16:15:26 +00:00
|
|
|
//
|
|
|
|
// See also Precision.
|
2021-10-26 10:31:31 +00:00
|
|
|
func (d *Decimal) SetPrecision(p uint32) {
|
|
|
|
(*accounting.Decimal)(d).SetPrecision(p)
|
|
|
|
}
|
2023-01-31 07:51:49 +00:00
|
|
|
|
|
|
|
// Marshal encodes Decimal into a binary format of the NeoFS API protocol
|
|
|
|
// (Protocol Buffers with direct field order).
|
|
|
|
//
|
|
|
|
// See also Unmarshal.
|
|
|
|
func (d Decimal) Marshal() []byte {
|
|
|
|
var m accounting.Decimal
|
|
|
|
d.WriteToV2(&m)
|
|
|
|
|
|
|
|
return m.StableMarshal(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal decodes NeoFS API protocol binary format into the Decimal
|
|
|
|
// (Protocol Buffers with direct field order). Returns an error describing
|
|
|
|
// a format violation.
|
|
|
|
//
|
|
|
|
// See also Marshal.
|
|
|
|
func (d *Decimal) Unmarshal(data []byte) error {
|
|
|
|
var m accounting.Decimal
|
|
|
|
|
|
|
|
err := m.Unmarshal(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return d.ReadFromV2(m)
|
|
|
|
}
|