2020-08-18 06:49:55 +00:00
|
|
|
package accounting
|
|
|
|
|
|
|
|
import (
|
2020-11-13 10:38:49 +00:00
|
|
|
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"
|
2020-08-18 06:49:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
decimalValueField = 1
|
|
|
|
decimalPrecisionField = 2
|
|
|
|
|
|
|
|
balanceReqBodyOwnerField = 1
|
|
|
|
|
|
|
|
balanceRespBodyDecimalField = 1
|
|
|
|
)
|
|
|
|
|
|
|
|
func (d *Decimal) StableMarshal(buf []byte) ([]byte, error) {
|
|
|
|
if d == nil {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if buf == nil {
|
|
|
|
buf = make([]byte, d.StableSize())
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
offset, n int
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2020-11-13 10:38:49 +00:00
|
|
|
n, err = protoutil.Int64Marshal(decimalValueField, buf[offset:], d.val)
|
2020-08-18 06:49:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += n
|
|
|
|
|
2021-01-11 16:32:37 +00:00
|
|
|
_, err = protoutil.UInt32Marshal(decimalPrecisionField, buf[offset:], d.prec)
|
2020-08-18 06:49:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Decimal) StableSize() (size int) {
|
|
|
|
if d == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-11-13 10:38:49 +00:00
|
|
|
size += protoutil.Int64Size(decimalValueField, d.val)
|
|
|
|
size += protoutil.UInt32Size(decimalPrecisionField, d.prec)
|
2020-08-18 06:49:55 +00:00
|
|
|
|
|
|
|
return size
|
|
|
|
}
|
|
|
|
|
2020-11-13 10:38:49 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-08-18 06:49:55 +00:00
|
|
|
func (b *BalanceRequestBody) StableMarshal(buf []byte) ([]byte, error) {
|
|
|
|
if b == nil {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if buf == nil {
|
|
|
|
buf = make([]byte, b.StableSize())
|
|
|
|
}
|
|
|
|
|
2020-11-13 10:38:49 +00:00
|
|
|
_, err := protoutil.NestedStructureMarshal(balanceReqBodyOwnerField, buf, b.ownerID)
|
2020-08-18 07:42:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-08-18 06:49:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BalanceRequestBody) StableSize() (size int) {
|
|
|
|
if b == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-11-13 10:38:49 +00:00
|
|
|
size = protoutil.NestedStructureSize(balanceReqBodyOwnerField, b.ownerID)
|
2020-08-18 06:49:55 +00:00
|
|
|
|
|
|
|
return size
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *BalanceResponseBody) StableMarshal(buf []byte) ([]byte, error) {
|
|
|
|
if br == nil {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if buf == nil {
|
|
|
|
buf = make([]byte, br.StableSize())
|
|
|
|
}
|
|
|
|
|
2020-11-13 10:38:49 +00:00
|
|
|
_, err := protoutil.NestedStructureMarshal(balanceRespBodyDecimalField, buf, br.bal)
|
2020-08-18 07:42:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-08-18 06:49:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *BalanceResponseBody) StableSize() (size int) {
|
|
|
|
if br == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-11-13 10:38:49 +00:00
|
|
|
size = protoutil.NestedStructureSize(balanceRespBodyDecimalField, br.bal)
|
2020-08-18 06:49:55 +00:00
|
|
|
|
|
|
|
return size
|
|
|
|
}
|