frostfs-api-go/accounting/marshal.go
Airat Arifullin 443098cb28
Some checks failed
DCO action / DCO (pull_request) Successful in 48s
Tests and linters / Tests (1.19) (pull_request) Failing after 59s
Tests and linters / Tests with -race (pull_request) Successful in 1m31s
Tests and linters / Tests (1.20) (pull_request) Failing after 5m52s
Tests and linters / Lint (pull_request) Successful in 7m3s
[#50] marshal: Use protowire append methods for marshalers
* Use protowire.AppendTag to encode a tag and append it
* Use protowire.AppendVarint to append numeric data
* Use protowire.AppendBytes and protowire.AppendString

Signed-off-by: Airat Arifullin <a.arifullin@yadro.com>
2023-08-31 12:12:43 +03:00

102 lines
2.1 KiB
Go

package accounting
import (
accounting "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/accounting/grpc"
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/message"
protoutil "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/util/proto"
)
const (
decimalValueField = 1
decimalPrecisionField = 2
balanceReqBodyOwnerField = 1
balanceRespBodyDecimalField = 1
)
func (d *Decimal) StableMarshal(buf []byte) []byte {
if d == nil {
return []byte{}
}
if buf == nil {
buf = make([]byte, 0, d.StableSize())
}
buf = protoutil.Int64Marshal(decimalValueField, buf, d.val)
buf = protoutil.UInt32Marshal(decimalPrecisionField, buf, d.prec)
return buf
}
func (d *Decimal) StableSize() (size int) {
if d == nil {
return 0
}
size += protoutil.Int64Size(decimalValueField, d.val)
size += protoutil.UInt32Size(decimalPrecisionField, d.prec)
return size
}
func (d *Decimal) Unmarshal(data []byte) error {
return message.Unmarshal(d, data, new(accounting.Decimal))
}
func (b *BalanceRequestBody) StableMarshal(buf []byte) []byte {
if b == nil {
return []byte{}
}
if buf == nil {
buf = make([]byte, 0, b.StableSize())
}
buf = protoutil.NestedStructureMarshal(balanceReqBodyOwnerField, buf, b.ownerID)
return buf
}
func (b *BalanceRequestBody) StableSize() (size int) {
if b == nil {
return 0
}
size = protoutil.NestedStructureSize(balanceReqBodyOwnerField, b.ownerID)
return size
}
func (b *BalanceRequestBody) Unmarshal(data []byte) error {
return message.Unmarshal(b, data, new(accounting.BalanceRequest_Body))
}
func (br *BalanceResponseBody) StableMarshal(buf []byte) []byte {
if br == nil {
return []byte{}
}
if buf == nil {
buf = make([]byte, 0, br.StableSize())
}
buf = protoutil.NestedStructureMarshal(balanceRespBodyDecimalField, buf, br.bal)
return buf
}
func (br *BalanceResponseBody) StableSize() (size int) {
if br == nil {
return 0
}
size = protoutil.NestedStructureSize(balanceRespBodyDecimalField, br.bal)
return size
}
func (br *BalanceResponseBody) Unmarshal(data []byte) error {
return message.Unmarshal(br, data, new(accounting.BalanceResponse_Body))
}