frostfs-api-go/refs/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

253 lines
5.1 KiB
Go

package refs
import (
refs "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs/grpc"
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/message"
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/util/proto"
"google.golang.org/protobuf/encoding/protowire"
)
const (
ownerIDValField = 1
containerIDValField = 1
objectIDValField = 1
addressContainerField = 1
addressObjectField = 2
checksumTypeField = 1
checksumValueField = 2
signatureKeyField = 1
signatureValueField = 2
signatureSchemeField = 3
versionMajorField = 1
versionMinorField = 2
)
func (o *OwnerID) StableMarshal(buf []byte) []byte {
if o == nil {
return []byte{}
}
if buf == nil {
buf = make([]byte, 0, o.StableSize())
}
buf = proto.BytesMarshal(ownerIDValField, buf, o.val)
return buf
}
func (o *OwnerID) StableSize() int {
if o == nil {
return 0
}
return proto.BytesSize(ownerIDValField, o.val)
}
func (o *OwnerID) Unmarshal(data []byte) error {
return message.Unmarshal(o, data, new(refs.OwnerID))
}
func (c *ContainerID) StableMarshal(buf []byte) []byte {
if c == nil {
return []byte{}
}
if buf == nil {
buf = make([]byte, 0, c.StableSize())
}
buf = proto.BytesMarshal(containerIDValField, buf, c.val)
return buf
}
func (c *ContainerID) StableSize() int {
if c == nil {
return 0
}
return proto.BytesSize(containerIDValField, c.val)
}
func (c *ContainerID) Unmarshal(data []byte) error {
return message.Unmarshal(c, data, new(refs.ContainerID))
}
func (o *ObjectID) StableMarshal(buf []byte) []byte {
if o == nil {
return []byte{}
}
if buf == nil {
buf = make([]byte, 0, o.StableSize())
}
buf = proto.BytesMarshal(objectIDValField, buf, o.val)
return buf
}
// ObjectIDNestedListSize returns byte length of nested
// repeated ObjectID field with fNum number.
func ObjectIDNestedListSize(fNum int64, ids []ObjectID) (sz int) {
for i := range ids {
sz += proto.NestedStructureSize(fNum, &ids[i])
}
return
}
func (o *ObjectID) StableSize() int {
if o == nil {
return 0
}
return proto.BytesSize(objectIDValField, o.val)
}
// ObjectIDNestedListMarshal writes protobuf repeated ObjectID field
// with fNum number to buf.
func ObjectIDNestedListMarshal(fNum int64, buf []byte, ids []ObjectID) []byte {
for i := range ids {
buf = protowire.AppendTag(buf, protowire.Number(fNum), protowire.BytesType)
n := ids[i].StableSize()
buf = protowire.AppendVarint(buf, uint64(n))
buf = proto.BytesMarshal(objectIDValField, buf, ids[i].val)
}
return buf
}
func (o *ObjectID) Unmarshal(data []byte) error {
return message.Unmarshal(o, data, new(refs.ObjectID))
}
func (a *Address) StableMarshal(buf []byte) []byte {
if a == nil {
return []byte{}
}
if buf == nil {
buf = make([]byte, 0, a.StableSize())
}
buf = proto.NestedStructureMarshal(addressContainerField, buf, a.cid)
buf = proto.NestedStructureMarshal(addressObjectField, buf, a.oid)
return buf
}
func (a *Address) StableSize() (size int) {
if a == nil {
return 0
}
size += proto.NestedStructureSize(addressContainerField, a.cid)
size += proto.NestedStructureSize(addressObjectField, a.oid)
return size
}
func (a *Address) Unmarshal(data []byte) error {
return message.Unmarshal(a, data, new(refs.Address))
}
func (c *Checksum) StableMarshal(buf []byte) []byte {
if c == nil {
return []byte{}
}
if buf == nil {
buf = make([]byte, 0, c.StableSize())
}
buf = proto.EnumMarshal(checksumTypeField, buf, int32(c.typ))
buf = proto.BytesMarshal(checksumValueField, buf, c.sum)
return buf
}
func (c *Checksum) StableSize() (size int) {
if c == nil {
return 0
}
size += proto.EnumSize(checksumTypeField, int32(c.typ))
size += proto.BytesSize(checksumValueField, c.sum)
return size
}
func (c *Checksum) Unmarshal(data []byte) error {
return message.Unmarshal(c, data, new(refs.Checksum))
}
func (s *Signature) StableMarshal(buf []byte) []byte {
if s == nil {
return []byte{}
}
if buf == nil {
buf = make([]byte, 0, s.StableSize())
}
buf = proto.BytesMarshal(signatureKeyField, buf, s.key)
buf = proto.BytesMarshal(signatureValueField, buf, s.sign)
buf = proto.EnumMarshal(signatureSchemeField, buf, int32(s.scheme))
return buf
}
func (s *Signature) StableSize() (size int) {
if s == nil {
return 0
}
size += proto.BytesSize(signatureKeyField, s.key)
size += proto.BytesSize(signatureValueField, s.sign)
size += proto.EnumSize(signatureSchemeField, int32(s.scheme))
return size
}
func (s *Signature) Unmarshal(data []byte) error {
return message.Unmarshal(s, data, new(refs.Signature))
}
func (v *Version) StableMarshal(buf []byte) []byte {
if v == nil {
return []byte{}
}
if buf == nil {
buf = make([]byte, 0, v.StableSize())
}
buf = proto.UInt32Marshal(versionMajorField, buf, v.major)
buf = proto.UInt32Marshal(versionMinorField, buf, v.minor)
return buf
}
func (v *Version) StableSize() (size int) {
if v == nil {
return 0
}
size += proto.UInt32Size(versionMajorField, v.major)
size += proto.UInt32Size(versionMinorField, v.minor)
return size
}
func (v *Version) Unmarshal(data []byte) error {
return message.Unmarshal(v, data, new(refs.Version))
}