Simplify marshal with nested structure helper

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2020-08-18 10:42:47 +03:00 committed by Stanislav Bogatyrev
parent b47ec9e056
commit d4336d8d5b
5 changed files with 183 additions and 645 deletions

View file

@ -62,11 +62,9 @@ func (b *BalanceRequestBody) StableMarshal(buf []byte) ([]byte, error) {
buf = make([]byte, b.StableSize())
}
if b.ownerID != nil {
_, err := proto.NestedStructureMarshal(balanceReqBodyOwnerField, buf, b.ownerID)
if err != nil {
return nil, err
}
_, err := proto.NestedStructureMarshal(balanceReqBodyOwnerField, buf, b.ownerID)
if err != nil {
return nil, err
}
return buf, nil
@ -77,9 +75,7 @@ func (b *BalanceRequestBody) StableSize() (size int) {
return 0
}
if b.ownerID != nil {
size = proto.NestedStructureSize(balanceReqBodyOwnerField, b.ownerID)
}
size = proto.NestedStructureSize(balanceReqBodyOwnerField, b.ownerID)
return size
}
@ -93,11 +89,9 @@ func (br *BalanceResponseBody) StableMarshal(buf []byte) ([]byte, error) {
buf = make([]byte, br.StableSize())
}
if br.bal != nil {
_, err := proto.NestedStructureMarshal(balanceRespBodyDecimalField, buf, br.bal)
if err != nil {
return nil, err
}
_, err := proto.NestedStructureMarshal(balanceRespBodyDecimalField, buf, br.bal)
if err != nil {
return nil, err
}
return buf, nil
@ -108,9 +102,7 @@ func (br *BalanceResponseBody) StableSize() (size int) {
return 0
}
if br.bal != nil {
size = proto.NestedStructureSize(balanceRespBodyDecimalField, br.bal)
}
size = proto.NestedStructureSize(balanceRespBodyDecimalField, br.bal)
return size
}

View file

@ -1,8 +1,6 @@
package acl
import (
"encoding/binary"
"github.com/nspcc-dev/neofs-api-go/util/proto"
)
@ -37,34 +35,18 @@ func (t *Table) StableMarshal(buf []byte) ([]byte, error) {
var (
offset, n int
prefix uint64
err error
)
if t.cid != nil {
prefix, _ = proto.NestedStructurePrefix(tableContainerIDField)
offset += binary.PutUvarint(buf[offset:], prefix)
n = t.cid.StableSize()
offset += binary.PutUvarint(buf[offset:], uint64(n))
_, err = t.cid.StableMarshal(buf[offset:])
if err != nil {
return nil, err
}
offset += n
n, err = proto.NestedStructureMarshal(tableContainerIDField, buf[offset:], t.cid)
if err != nil {
return nil, err
}
prefix, _ = proto.NestedStructurePrefix(tableRecordsField)
offset += n
for i := range t.records {
offset += binary.PutUvarint(buf[offset:], prefix)
n = t.records[i].StableSize()
offset += binary.PutUvarint(buf[offset:], uint64(n))
_, err = t.records[i].StableMarshal(buf[offset:])
n, err = proto.NestedStructureMarshal(tableRecordsField, buf[offset:], t.records[i])
if err != nil {
return nil, err
}
@ -81,17 +63,10 @@ func (t *Table) StableSize() (size int) {
return 0
}
if t.cid != nil {
_, ln := proto.NestedStructurePrefix(tableContainerIDField)
n := t.cid.StableSize()
size += ln + proto.VarUIntSize(uint64(n)) + n
}
_, ln := proto.NestedStructurePrefix(tableRecordsField)
size += proto.NestedStructureSize(tableContainerIDField, t.cid)
for i := range t.records {
n := t.records[i].StableSize()
size += ln + proto.VarUIntSize(uint64(n)) + n
size += proto.NestedStructureSize(tableRecordsField, t.records[i])
}
return size
@ -110,11 +85,10 @@ func (r *Record) StableMarshal(buf []byte) ([]byte, error) {
var (
offset, n int
prefix uint64
err error
)
n, err = proto.EnumMarshal(recordOperationField, buf, int32(r.op))
n, err = proto.EnumMarshal(recordOperationField, buf[offset:], int32(r.op))
if err != nil {
return nil, err
}
@ -128,15 +102,8 @@ func (r *Record) StableMarshal(buf []byte) ([]byte, error) {
offset += n
prefix, _ = proto.NestedStructurePrefix(recordFiltersField)
for i := range r.filters {
offset += binary.PutUvarint(buf[offset:], prefix)
n = r.filters[i].StableSize()
offset += binary.PutUvarint(buf[offset:], uint64(n))
_, err = r.filters[i].StableMarshal(buf[offset:])
n, err = proto.NestedStructureMarshal(recordFiltersField, buf[offset:], r.filters[i])
if err != nil {
return nil, err
}
@ -144,15 +111,8 @@ func (r *Record) StableMarshal(buf []byte) ([]byte, error) {
offset += n
}
prefix, _ = proto.NestedStructurePrefix(recordTargetsField)
for i := range r.targets {
offset += binary.PutUvarint(buf[offset:], prefix)
n = r.targets[i].StableSize()
offset += binary.PutUvarint(buf[offset:], uint64(n))
_, err = r.targets[i].StableMarshal(buf[offset:])
n, err = proto.NestedStructureMarshal(recordTargetsField, buf[offset:], r.targets[i])
if err != nil {
return nil, err
}
@ -172,18 +132,12 @@ func (r *Record) StableSize() (size int) {
size += proto.EnumSize(recordOperationField, int32(r.op))
size += proto.EnumSize(recordActionField, int32(r.op))
_, ln := proto.NestedStructurePrefix(recordFiltersField)
for i := range r.filters {
n := r.filters[i].StableSize()
size += ln + proto.VarUIntSize(uint64(n)) + n
size += proto.NestedStructureSize(recordFiltersField, r.filters[i])
}
_, ln = proto.NestedStructurePrefix(recordTargetsField)
for i := range r.targets {
n := r.targets[i].StableSize()
size += ln + proto.VarUIntSize(uint64(n)) + n
size += proto.NestedStructureSize(recordTargetsField, r.targets[i])
}
return size
@ -205,7 +159,7 @@ func (f *HeaderFilter) StableMarshal(buf []byte) ([]byte, error) {
err error
)
n, err = proto.EnumMarshal(filterHeaderTypeField, buf, int32(f.hdrType))
n, err = proto.EnumMarshal(filterHeaderTypeField, buf[offset:], int32(f.hdrType))
if err != nil {
return nil, err
}
@ -264,7 +218,7 @@ func (t *TargetInfo) StableMarshal(buf []byte) ([]byte, error) {
err error
)
n, err = proto.EnumMarshal(targetTypeField, buf, int32(t.target))
n, err = proto.EnumMarshal(targetTypeField, buf[offset:], int32(t.target))
if err != nil {
return nil, err
}

View file

@ -1,20 +1,18 @@
package refs
import (
"encoding/binary"
"github.com/nspcc-dev/neofs-api-go/util/proto"
)
const (
OwnerIDValField = 1
ownerIDValField = 1
ContainerIDValField = 1
containerIDValField = 1
ObjectIDValField = 1
objectIDValField = 1
AddressContainerField = 1
AddressObjectField = 2
addressContainerField = 1
addressObjectField = 2
)
func (o *OwnerID) StableMarshal(buf []byte) ([]byte, error) {
@ -26,7 +24,7 @@ func (o *OwnerID) StableMarshal(buf []byte) ([]byte, error) {
buf = make([]byte, o.StableSize())
}
_, err := proto.BytesMarshal(OwnerIDValField, buf, o.val)
_, err := proto.BytesMarshal(ownerIDValField, buf, o.val)
if err != nil {
return nil, err
}
@ -38,7 +36,8 @@ func (o *OwnerID) StableSize() int {
if o == nil {
return 0
}
return proto.BytesSize(OwnerIDValField, o.val)
return proto.BytesSize(ownerIDValField, o.val)
}
func (c *ContainerID) StableMarshal(buf []byte) ([]byte, error) {
@ -50,7 +49,7 @@ func (c *ContainerID) StableMarshal(buf []byte) ([]byte, error) {
buf = make([]byte, c.StableSize())
}
_, err := proto.BytesMarshal(ContainerIDValField, buf, c.val)
_, err := proto.BytesMarshal(containerIDValField, buf, c.val)
if err != nil {
return nil, err
}
@ -62,7 +61,8 @@ func (c *ContainerID) StableSize() int {
if c == nil {
return 0
}
return proto.BytesSize(ContainerIDValField, c.val)
return proto.BytesSize(containerIDValField, c.val)
}
func (o *ObjectID) StableMarshal(buf []byte) ([]byte, error) {
@ -74,7 +74,7 @@ func (o *ObjectID) StableMarshal(buf []byte) ([]byte, error) {
buf = make([]byte, o.StableSize())
}
_, err := proto.BytesMarshal(ObjectIDValField, buf, o.val)
_, err := proto.BytesMarshal(objectIDValField, buf, o.val)
if err != nil {
return nil, err
}
@ -87,7 +87,7 @@ func (o *ObjectID) StableSize() int {
return 0
}
return proto.BytesSize(ObjectIDValField, o.val)
return proto.BytesSize(objectIDValField, o.val)
}
func (a *Address) StableMarshal(buf []byte) ([]byte, error) {
@ -101,36 +101,19 @@ func (a *Address) StableMarshal(buf []byte) ([]byte, error) {
var (
offset, n int
prefix uint64
err error
)
if a.cid != nil {
prefix, _ = proto.NestedStructurePrefix(AddressContainerField)
offset = binary.PutUvarint(buf, prefix)
n = a.cid.StableSize()
offset += binary.PutUvarint(buf[offset:], uint64(n))
_, err = a.cid.StableMarshal(buf[offset:])
if err != nil {
return nil, err
}
offset += n
n, err = proto.NestedStructureMarshal(addressContainerField, buf[offset:], a.cid)
if err != nil {
return nil, err
}
if a.oid != nil {
prefix, _ = proto.NestedStructurePrefix(AddressObjectField)
offset += binary.PutUvarint(buf[offset:], prefix)
offset += n
n = a.oid.StableSize()
offset += binary.PutUvarint(buf[offset:], uint64(n))
_, err = a.oid.StableMarshal(buf[offset:])
if err != nil {
return nil, err
}
_, err = proto.NestedStructureMarshal(addressObjectField, buf[offset:], a.oid)
if err != nil {
return nil, err
}
return buf, nil
@ -141,17 +124,9 @@ func (a *Address) StableSize() (size int) {
return 0
}
if a.cid != nil {
_, ln := proto.NestedStructurePrefix(AddressContainerField)
n := a.cid.StableSize()
size += ln + proto.VarUIntSize(uint64(n)) + n
}
size += proto.NestedStructureSize(addressContainerField, a.cid)
if a.oid != nil {
_, ln := proto.NestedStructurePrefix(AddressObjectField)
n := a.oid.StableSize()
size += ln + proto.VarUIntSize(uint64(n)) + n
}
size += proto.NestedStructureSize(addressObjectField, a.oid)
return size
}

View file

@ -1,8 +1,6 @@
package service
import (
"encoding/binary"
"github.com/nspcc-dev/neofs-api-go/util/proto"
)
@ -78,7 +76,7 @@ func (s *Signature) StableMarshal(buf []byte) ([]byte, error) {
err error
)
n, err = proto.BytesMarshal(signatureKeyField, buf, s.key)
n, err = proto.BytesMarshal(signatureKeyField, buf[offset:], s.key)
if err != nil {
return nil, err
}
@ -118,7 +116,7 @@ func (v *Version) StableMarshal(buf []byte) ([]byte, error) {
err error
)
n, err = proto.UInt32Marshal(versionMajorField, buf, v.major)
n, err = proto.UInt32Marshal(versionMajorField, buf[offset:], v.major)
if err != nil {
return nil, err
}
@ -158,7 +156,7 @@ func (x *XHeader) StableMarshal(buf []byte) ([]byte, error) {
err error
)
n, err = proto.StringMarshal(xheaderKeyField, buf, x.key)
n, err = proto.StringMarshal(xheaderKeyField, buf[offset:], x.key)
if err != nil {
return nil, err
}
@ -198,7 +196,7 @@ func (l *TokenLifetime) StableMarshal(buf []byte) ([]byte, error) {
err error
)
n, err = proto.UInt64Marshal(lifetimeExpirationField, buf, l.exp)
n, err = proto.UInt64Marshal(lifetimeExpirationField, buf[offset:], l.exp)
if err != nil {
return nil, err
}
@ -243,29 +241,19 @@ func (c *ObjectSessionContext) StableMarshal(buf []byte) ([]byte, error) {
var (
offset, n int
prefix uint64
err error
)
n, err = proto.EnumMarshal(objectCtxVerbField, buf, int32(c.verb))
n, err = proto.EnumMarshal(objectCtxVerbField, buf[offset:], int32(c.verb))
if err != nil {
return nil, err
}
offset += n
if c.addr != nil {
prefix, _ = proto.NestedStructurePrefix(objectCtxAddressField)
offset += binary.PutUvarint(buf[offset:], prefix)
n = c.addr.StableSize()
offset += binary.PutUvarint(buf[offset:], uint64(n))
_, err = c.addr.StableMarshal(buf[offset:])
if err != nil {
return nil, err
}
_, err = proto.NestedStructureMarshal(objectCtxAddressField, buf[offset:], c.addr)
if err != nil {
return nil, err
}
return buf, nil
@ -277,12 +265,7 @@ func (c *ObjectSessionContext) StableSize() (size int) {
}
size += proto.EnumSize(objectCtxVerbField, int32(c.verb))
if c.addr != nil {
_, ln := proto.NestedStructurePrefix(objectCtxAddressField)
n := c.addr.StableSize()
size += ln + proto.VarUIntSize(uint64(n)) + n
}
size += proto.NestedStructureSize(objectCtxAddressField, c.addr)
return size
}
@ -298,49 +281,30 @@ func (t *SessionTokenBody) StableMarshal(buf []byte) ([]byte, error) {
var (
offset, n int
prefix uint64
err error
)
n, err = proto.BytesMarshal(sessionTokenBodyIDField, buf, t.id)
n, err = proto.BytesMarshal(sessionTokenBodyIDField, buf[offset:], t.id)
if err != nil {
return nil, err
}
offset += n
if t.ownerID != nil {
prefix, _ = proto.NestedStructurePrefix(sessionTokenBodyOwnerField)
offset += binary.PutUvarint(buf[offset:], prefix)
n = t.ownerID.StableSize()
offset += binary.PutUvarint(buf[offset:], uint64(n))
_, err = t.ownerID.StableMarshal(buf[offset:])
if err != nil {
return nil, err
}
offset += n
n, err = proto.NestedStructureMarshal(sessionTokenBodyOwnerField, buf[offset:], t.ownerID)
if err != nil {
return nil, err
}
if t.lifetime != nil {
prefix, _ = proto.NestedStructurePrefix(sessionTokenBodyLifetimeField)
offset += binary.PutUvarint(buf[offset:], prefix)
offset += n
n = t.lifetime.StableSize()
offset += binary.PutUvarint(buf[offset:], uint64(n))
_, err = t.lifetime.StableMarshal(buf[offset:])
if err != nil {
return nil, err
}
offset += n
n, err = proto.NestedStructureMarshal(sessionTokenBodyLifetimeField, buf[offset:], t.lifetime)
if err != nil {
return nil, err
}
offset += n
n, err = proto.BytesMarshal(sessionTokenBodyKeyField, buf[offset:], t.sessionKey)
if err != nil {
return nil, err
@ -351,16 +315,9 @@ func (t *SessionTokenBody) StableMarshal(buf []byte) ([]byte, error) {
if t.ctx != nil {
switch v := t.ctx.(type) {
case *ObjectSessionContext:
prefix, _ = proto.NestedStructurePrefix(sessionTokenBodyObjectCtxField)
offset += binary.PutUvarint(buf[offset:], prefix)
n = v.StableSize()
offset += binary.PutUvarint(buf[offset:], uint64(n))
_, err = v.StableMarshal(buf[offset:])
_, err = proto.NestedStructureMarshal(sessionTokenBodyObjectCtxField, buf[offset:], v)
if err != nil {
return nil, err
}
default:
panic("cannot marshal unknown session token context")
@ -376,27 +333,14 @@ func (t *SessionTokenBody) StableSize() (size int) {
}
size += proto.BytesSize(sessionTokenBodyIDField, t.id)
if t.ownerID != nil {
_, ln := proto.NestedStructurePrefix(sessionTokenBodyOwnerField)
n := t.ownerID.StableSize()
size += ln + proto.VarUIntSize(uint64(n)) + n
}
if t.lifetime != nil {
_, ln := proto.NestedStructurePrefix(sessionTokenBodyLifetimeField)
n := t.lifetime.StableSize()
size += ln + proto.VarUIntSize(uint64(n)) + n
}
size += proto.NestedStructureSize(sessionTokenBodyOwnerField, t.ownerID)
size += proto.NestedStructureSize(sessionTokenBodyLifetimeField, t.lifetime)
size += proto.BytesSize(sessionTokenBodyKeyField, t.sessionKey)
if t.ctx != nil {
switch v := t.ctx.(type) {
case *ObjectSessionContext:
_, ln := proto.NestedStructurePrefix(sessionTokenBodyObjectCtxField)
n := v.StableSize()
size += ln + proto.VarUIntSize(uint64(n)) + n
size += proto.NestedStructureSize(sessionTokenBodyObjectCtxField, v)
default:
panic("cannot marshal unknown session token context")
}
@ -416,38 +360,19 @@ func (t *SessionToken) StableMarshal(buf []byte) ([]byte, error) {
var (
offset, n int
prefix uint64
err error
)
if t.body != nil {
prefix, _ = proto.NestedStructurePrefix(sessionTokenBodyField)
offset += binary.PutUvarint(buf[offset:], prefix)
n = t.body.StableSize()
offset += binary.PutUvarint(buf[offset:], uint64(n))
_, err = t.body.StableMarshal(buf[offset:])
if err != nil {
return nil, err
}
offset += n
n, err = proto.NestedStructureMarshal(sessionTokenBodyField, buf[offset:], t.body)
if err != nil {
return nil, err
}
if t.sig != nil {
prefix, _ = proto.NestedStructurePrefix(sessionTokenSignatureField)
offset += binary.PutUvarint(buf[offset:], prefix)
offset += n
n = t.sig.StableSize()
offset += binary.PutUvarint(buf[offset:], uint64(n))
_, err = t.sig.StableMarshal(buf[offset:])
if err != nil {
return nil, err
}
_, err = proto.NestedStructureMarshal(sessionTokenSignatureField, buf[offset:], t.sig)
if err != nil {
return nil, err
}
return buf, nil
@ -458,17 +383,8 @@ func (t *SessionToken) StableSize() (size int) {
return 0
}
if t.body != nil {
_, ln := proto.NestedStructurePrefix(sessionTokenBodyField)
n := t.body.StableSize()
size += ln + proto.VarUIntSize(uint64(n)) + n
}
if t.sig != nil {
_, ln := proto.NestedStructurePrefix(sessionTokenSignatureField)
n := t.sig.StableSize()
size += ln + proto.VarUIntSize(uint64(n)) + n
}
size += proto.NestedStructureSize(sessionTokenBodyField, t.body)
size += proto.NestedStructureSize(sessionTokenSignatureField, t.sig)
return size
}
@ -484,54 +400,26 @@ func (bt *BearerTokenBody) StableMarshal(buf []byte) ([]byte, error) {
var (
offset, n int
prefix uint64
err error
)
if bt.eacl != nil {
prefix, _ = proto.NestedStructurePrefix(bearerTokenBodyACLField)
offset += binary.PutUvarint(buf[offset:], prefix)
n = bt.eacl.StableSize()
offset += binary.PutUvarint(buf[offset:], uint64(n))
_, err = bt.eacl.StableMarshal(buf[offset:])
if err != nil {
return nil, err
}
offset += n
n, err = proto.NestedStructureMarshal(bearerTokenBodyACLField, buf[offset:], bt.eacl)
if err != nil {
return nil, err
}
if bt.ownerID != nil {
prefix, _ = proto.NestedStructurePrefix(bearerTokenBodyOwnerField)
offset += binary.PutUvarint(buf[offset:], prefix)
offset += n
n = bt.ownerID.StableSize()
offset += binary.PutUvarint(buf[offset:], uint64(n))
_, err = bt.ownerID.StableMarshal(buf[offset:])
if err != nil {
return nil, err
}
offset += n
n, err = proto.NestedStructureMarshal(bearerTokenBodyOwnerField, buf[offset:], bt.ownerID)
if err != nil {
return nil, err
}
if bt.lifetime != nil {
prefix, _ = proto.NestedStructurePrefix(bearerTokenBodyLifetimeField)
offset += binary.PutUvarint(buf[offset:], prefix)
offset += n
n = bt.lifetime.StableSize()
offset += binary.PutUvarint(buf[offset:], uint64(n))
_, err = bt.lifetime.StableMarshal(buf[offset:])
if err != nil {
return nil, err
}
_, err = proto.NestedStructureMarshal(bearerTokenBodyLifetimeField, buf[offset:], bt.lifetime)
if err != nil {
return nil, err
}
return buf, nil
@ -542,23 +430,9 @@ func (bt *BearerTokenBody) StableSize() (size int) {
return 0
}
if bt.eacl != nil {
_, ln := proto.NestedStructurePrefix(bearerTokenBodyACLField)
n := bt.eacl.StableSize()
size += ln + proto.VarUIntSize(uint64(n)) + n
}
if bt.ownerID != nil {
_, ln := proto.NestedStructurePrefix(bearerTokenBodyOwnerField)
n := bt.ownerID.StableSize()
size += ln + proto.VarUIntSize(uint64(n)) + n
}
if bt.lifetime != nil {
_, ln := proto.NestedStructurePrefix(bearerTokenBodyLifetimeField)
n := bt.lifetime.StableSize()
size += ln + proto.VarUIntSize(uint64(n)) + n
}
size += proto.NestedStructureSize(bearerTokenBodyACLField, bt.eacl)
size += proto.NestedStructureSize(bearerTokenBodyOwnerField, bt.ownerID)
size += proto.NestedStructureSize(bearerTokenBodyLifetimeField, bt.lifetime)
return size
}
@ -574,38 +448,19 @@ func (bt *BearerToken) StableMarshal(buf []byte) ([]byte, error) {
var (
offset, n int
prefix uint64
err error
)
if bt.body != nil {
prefix, _ = proto.NestedStructurePrefix(bearerTokenBodyField)
offset += binary.PutUvarint(buf[offset:], prefix)
n = bt.body.StableSize()
offset += binary.PutUvarint(buf[offset:], uint64(n))
_, err = bt.body.StableMarshal(buf[offset:])
if err != nil {
return nil, err
}
offset += n
n, err = proto.NestedStructureMarshal(bearerTokenBodyField, buf[offset:], bt.body)
if err != nil {
return nil, err
}
if bt.sig != nil {
prefix, _ = proto.NestedStructurePrefix(bearerTokenSignatureField)
offset += binary.PutUvarint(buf[offset:], prefix)
offset += n
n = bt.sig.StableSize()
offset += binary.PutUvarint(buf[offset:], uint64(n))
_, err = bt.sig.StableMarshal(buf[offset:])
if err != nil {
return nil, err
}
_, err = proto.NestedStructureMarshal(bearerTokenSignatureField, buf[offset:], bt.sig)
if err != nil {
return nil, err
}
return buf, nil
@ -616,17 +471,8 @@ func (bt *BearerToken) StableSize() (size int) {
return 0
}
if bt.body != nil {
_, ln := proto.NestedStructurePrefix(bearerTokenBodyField)
n := bt.body.StableSize()
size += ln + proto.VarUIntSize(uint64(n)) + n
}
if bt.sig != nil {
_, ln := proto.NestedStructurePrefix(bearerTokenSignatureField)
n := bt.sig.StableSize()
size += ln + proto.VarUIntSize(uint64(n)) + n
}
size += proto.NestedStructureSize(bearerTokenBodyField, bt.body)
size += proto.NestedStructureSize(bearerTokenSignatureField, bt.sig)
return size
}
@ -642,26 +488,16 @@ func (r *RequestMetaHeader) StableMarshal(buf []byte) ([]byte, error) {
var (
offset, n int
prefix uint64
err error
)
if r.version != nil {
prefix, _ = proto.NestedStructurePrefix(reqMetaHeaderVersionField)
offset += binary.PutUvarint(buf[offset:], prefix)
n = r.version.StableSize()
offset += binary.PutUvarint(buf[offset:], uint64(n))
_, err = r.version.StableMarshal(buf[offset:])
if err != nil {
return nil, err
}
offset += n
n, err = proto.NestedStructureMarshal(reqMetaHeaderVersionField, buf[offset:], r.version)
if err != nil {
return nil, err
}
offset += n
n, err = proto.UInt64Marshal(reqMetaHeaderEpochField, buf[offset:], r.epoch)
if err != nil {
return nil, err
@ -676,67 +512,32 @@ func (r *RequestMetaHeader) StableMarshal(buf []byte) ([]byte, error) {
offset += n
prefix, _ = proto.NestedStructurePrefix(reqMetaHeaderXHeadersField)
for i := range r.xHeaders {
offset += binary.PutUvarint(buf[offset:], prefix)
n = r.xHeaders[i].StableSize()
offset += binary.PutUvarint(buf[offset:], uint64(n))
_, err = r.xHeaders[i].StableMarshal(buf[offset:])
n, err = proto.NestedStructureMarshal(reqMetaHeaderXHeadersField, buf[offset:], r.xHeaders[i])
if err != nil {
return nil, err
}
offset += n
}
if r.sessionToken != nil {
prefix, _ = proto.NestedStructurePrefix(reqMetaHeaderSessionTokenField)
offset += binary.PutUvarint(buf[offset:], prefix)
n = r.sessionToken.StableSize()
offset += binary.PutUvarint(buf[offset:], uint64(n))
_, err = r.sessionToken.StableMarshal(buf[offset:])
if err != nil {
return nil, err
}
offset += n
n, err = proto.NestedStructureMarshal(reqMetaHeaderSessionTokenField, buf[offset:], r.sessionToken)
if err != nil {
return nil, err
}
if r.bearerToken != nil {
prefix, _ = proto.NestedStructurePrefix(reqMetaHeaderBearerTokenField)
offset += binary.PutUvarint(buf[offset:], prefix)
offset += n
n = r.bearerToken.StableSize()
offset += binary.PutUvarint(buf[offset:], uint64(n))
_, err = r.bearerToken.StableMarshal(buf[offset:])
if err != nil {
return nil, err
}
offset += n
n, err = proto.NestedStructureMarshal(reqMetaHeaderBearerTokenField, buf[offset:], r.bearerToken)
if err != nil {
return nil, err
}
if r.origin != nil {
prefix, _ = proto.NestedStructurePrefix(reqMetaHeaderOriginField)
offset += binary.PutUvarint(buf[offset:], prefix)
offset += n
n = r.origin.StableSize()
offset += binary.PutUvarint(buf[offset:], uint64(n))
_, err = r.origin.StableMarshal(buf[offset:])
if err != nil {
return nil, err
}
_, err = proto.NestedStructureMarshal(reqMetaHeaderOriginField, buf[offset:], r.origin)
if err != nil {
return nil, err
}
return buf, nil
@ -748,38 +549,19 @@ func (r *RequestMetaHeader) StableSize() (size int) {
}
if r.version != nil {
_, ln := proto.NestedStructurePrefix(reqMetaHeaderVersionField)
n := r.version.StableSize()
size += ln + proto.VarUIntSize(uint64(n)) + n
size += proto.NestedStructureSize(reqMetaHeaderVersionField, r.version)
}
size += proto.UInt64Size(reqMetaHeaderEpochField, r.epoch)
size += proto.UInt32Size(reqMetaHeaderTTLField, r.ttl)
_, ln := proto.NestedStructurePrefix(reqMetaHeaderXHeadersField)
for i := range r.xHeaders {
n := r.xHeaders[i].StableSize()
size += ln + proto.VarUIntSize(uint64(n)) + n
size += proto.NestedStructureSize(reqMetaHeaderXHeadersField, r.xHeaders[i])
}
if r.sessionToken != nil {
_, ln := proto.NestedStructurePrefix(reqMetaHeaderSessionTokenField)
n := r.sessionToken.StableSize()
size += ln + proto.VarUIntSize(uint64(n)) + n
}
if r.bearerToken != nil {
_, ln := proto.NestedStructurePrefix(reqMetaHeaderBearerTokenField)
n := r.bearerToken.StableSize()
size += ln + proto.VarUIntSize(uint64(n)) + n
}
if r.origin != nil {
_, ln := proto.NestedStructurePrefix(reqMetaHeaderOriginField)
n := r.origin.StableSize()
size += ln + proto.VarUIntSize(uint64(n)) + n
}
size += proto.NestedStructureSize(reqMetaHeaderSessionTokenField, r.sessionToken)
size += proto.NestedStructureSize(reqMetaHeaderBearerTokenField, r.bearerToken)
size += proto.NestedStructureSize(reqMetaHeaderOriginField, r.origin)
return size
}
@ -795,70 +577,33 @@ func (r *RequestVerificationHeader) StableMarshal(buf []byte) ([]byte, error) {
var (
offset, n int
prefix uint64
err error
)
if r.bodySig != nil {
prefix, _ = proto.NestedStructurePrefix(reqVerifHeaderBodySignatureField)
offset += binary.PutUvarint(buf[offset:], prefix)
n = r.bodySig.StableSize()
offset += binary.PutUvarint(buf[offset:], uint64(n))
_, err = r.bodySig.StableMarshal(buf[offset:])
if err != nil {
return nil, err
}
offset += n
n, err = proto.NestedStructureMarshal(reqVerifHeaderBodySignatureField, buf[offset:], r.bodySig)
if err != nil {
return nil, err
}
if r.metaSig != nil {
prefix, _ = proto.NestedStructurePrefix(reqVerifHeaderMetaSignatureField)
offset += binary.PutUvarint(buf[offset:], prefix)
offset += n
n = r.metaSig.StableSize()
offset += binary.PutUvarint(buf[offset:], uint64(n))
_, err = r.metaSig.StableMarshal(buf[offset:])
if err != nil {
return nil, err
}
offset += n
n, err = proto.NestedStructureMarshal(reqVerifHeaderMetaSignatureField, buf[offset:], r.metaSig)
if err != nil {
return nil, err
}
if r.originSig != nil {
prefix, _ = proto.NestedStructurePrefix(reqVerifHeaderOriginSignatureField)
offset += binary.PutUvarint(buf[offset:], prefix)
offset += n
n = r.originSig.StableSize()
offset += binary.PutUvarint(buf[offset:], uint64(n))
_, err = r.originSig.StableMarshal(buf[offset:])
if err != nil {
return nil, err
}
offset += n
n, err = proto.NestedStructureMarshal(reqVerifHeaderOriginSignatureField, buf[offset:], r.originSig)
if err != nil {
return nil, err
}
if r.origin != nil {
prefix, _ = proto.NestedStructurePrefix(reqVerifHeaderOriginField)
offset += binary.PutUvarint(buf[offset:], prefix)
offset += n
n = r.origin.StableSize()
offset += binary.PutUvarint(buf[offset:], uint64(n))
_, err = r.origin.StableMarshal(buf[offset:])
if err != nil {
return nil, err
}
_, err = proto.NestedStructureMarshal(reqVerifHeaderOriginField, buf[offset:], r.origin)
if err != nil {
return nil, err
}
return buf, nil
@ -869,29 +614,10 @@ func (r *RequestVerificationHeader) StableSize() (size int) {
return 0
}
if r.bodySig != nil {
_, ln := proto.NestedStructurePrefix(reqVerifHeaderBodySignatureField)
n := r.bodySig.StableSize()
size += ln + proto.VarUIntSize(uint64(n)) + n
}
if r.metaSig != nil {
_, ln := proto.NestedStructurePrefix(reqVerifHeaderMetaSignatureField)
n := r.metaSig.StableSize()
size += ln + proto.VarUIntSize(uint64(n)) + n
}
if r.originSig != nil {
_, ln := proto.NestedStructurePrefix(reqVerifHeaderOriginSignatureField)
n := r.originSig.StableSize()
size += ln + proto.VarUIntSize(uint64(n)) + n
}
if r.origin != nil {
_, ln := proto.NestedStructurePrefix(reqVerifHeaderOriginField)
n := r.origin.StableSize()
size += ln + proto.VarUIntSize(uint64(n)) + n
}
size += proto.NestedStructureSize(reqVerifHeaderBodySignatureField, r.bodySig)
size += proto.NestedStructureSize(reqVerifHeaderMetaSignatureField, r.metaSig)
size += proto.NestedStructureSize(reqVerifHeaderOriginSignatureField, r.originSig)
size += proto.NestedStructureSize(reqVerifHeaderOriginField, r.origin)
return size
}
@ -907,26 +633,16 @@ func (r *ResponseMetaHeader) StableMarshal(buf []byte) ([]byte, error) {
var (
offset, n int
prefix uint64
err error
)
if r.version != nil {
prefix, _ = proto.NestedStructurePrefix(respMetaHeaderVersionField)
offset += binary.PutUvarint(buf[offset:], prefix)
n = r.version.StableSize()
offset += binary.PutUvarint(buf[offset:], uint64(n))
_, err = r.version.StableMarshal(buf[offset:])
if err != nil {
return nil, err
}
offset += n
n, err = proto.NestedStructureMarshal(respMetaHeaderVersionField, buf[offset:], r.version)
if err != nil {
return nil, err
}
offset += n
n, err = proto.UInt64Marshal(respMetaHeaderEpochField, buf[offset:], r.epoch)
if err != nil {
return nil, err
@ -941,35 +657,18 @@ func (r *ResponseMetaHeader) StableMarshal(buf []byte) ([]byte, error) {
offset += n
prefix, _ = proto.NestedStructurePrefix(respMetaHeaderXHeadersField)
for i := range r.xHeaders {
offset += binary.PutUvarint(buf[offset:], prefix)
n = r.xHeaders[i].StableSize()
offset += binary.PutUvarint(buf[offset:], uint64(n))
_, err = r.xHeaders[i].StableMarshal(buf[offset:])
n, err = proto.NestedStructureMarshal(respMetaHeaderXHeadersField, buf[offset:], r.xHeaders[i])
if err != nil {
return nil, err
}
offset += n
}
if r.origin != nil {
prefix, _ = proto.NestedStructurePrefix(respMetaHeaderOriginField)
offset += binary.PutUvarint(buf[offset:], prefix)
n = r.origin.StableSize()
offset += binary.PutUvarint(buf[offset:], uint64(n))
_, err = r.origin.StableMarshal(buf[offset:])
if err != nil {
return nil, err
}
_, err = proto.NestedStructureMarshal(respMetaHeaderOriginField, buf[offset:], r.origin)
if err != nil {
return nil, err
}
return buf, nil
@ -981,26 +680,17 @@ func (r *ResponseMetaHeader) StableSize() (size int) {
}
if r.version != nil {
_, ln := proto.NestedStructurePrefix(respMetaHeaderVersionField)
n := r.version.StableSize()
size += ln + proto.VarUIntSize(uint64(n)) + n
size += proto.NestedStructureSize(respMetaHeaderVersionField, r.version)
}
size += proto.UInt64Size(respMetaHeaderEpochField, r.epoch)
size += proto.UInt32Size(respMetaHeaderTTLField, r.ttl)
_, ln := proto.NestedStructurePrefix(respMetaHeaderXHeadersField)
for i := range r.xHeaders {
n := r.xHeaders[i].StableSize()
size += ln + proto.VarUIntSize(uint64(n)) + n
size += proto.NestedStructureSize(respMetaHeaderXHeadersField, r.xHeaders[i])
}
if r.origin != nil {
_, ln := proto.NestedStructurePrefix(respMetaHeaderOriginField)
n := r.origin.StableSize()
size += ln + proto.VarUIntSize(uint64(n)) + n
}
size += proto.NestedStructureSize(respMetaHeaderOriginField, r.origin)
return size
}
@ -1016,70 +706,33 @@ func (r *ResponseVerificationHeader) StableMarshal(buf []byte) ([]byte, error) {
var (
offset, n int
prefix uint64
err error
)
if r.bodySig != nil {
prefix, _ = proto.NestedStructurePrefix(respVerifHeaderBodySignatureField)
offset += binary.PutUvarint(buf[offset:], prefix)
n = r.bodySig.StableSize()
offset += binary.PutUvarint(buf[offset:], uint64(n))
_, err = r.bodySig.StableMarshal(buf[offset:])
if err != nil {
return nil, err
}
offset += n
n, err = proto.NestedStructureMarshal(respVerifHeaderBodySignatureField, buf[offset:], r.bodySig)
if err != nil {
return nil, err
}
if r.metaSig != nil {
prefix, _ = proto.NestedStructurePrefix(respVerifHeaderMetaSignatureField)
offset += binary.PutUvarint(buf[offset:], prefix)
offset += n
n = r.metaSig.StableSize()
offset += binary.PutUvarint(buf[offset:], uint64(n))
_, err = r.metaSig.StableMarshal(buf[offset:])
if err != nil {
return nil, err
}
offset += n
n, err = proto.NestedStructureMarshal(respVerifHeaderMetaSignatureField, buf[offset:], r.metaSig)
if err != nil {
return nil, err
}
if r.originSig != nil {
prefix, _ = proto.NestedStructurePrefix(respVerifHeaderOriginSignatureField)
offset += binary.PutUvarint(buf[offset:], prefix)
offset += n
n = r.originSig.StableSize()
offset += binary.PutUvarint(buf[offset:], uint64(n))
_, err = r.originSig.StableMarshal(buf[offset:])
if err != nil {
return nil, err
}
offset += n
n, err = proto.NestedStructureMarshal(respVerifHeaderOriginSignatureField, buf[offset:], r.originSig)
if err != nil {
return nil, err
}
if r.origin != nil {
prefix, _ = proto.NestedStructurePrefix(respVerifHeaderOriginField)
offset += binary.PutUvarint(buf[offset:], prefix)
offset += n
n = r.origin.StableSize()
offset += binary.PutUvarint(buf[offset:], uint64(n))
_, err = r.origin.StableMarshal(buf[offset:])
if err != nil {
return nil, err
}
_, err = proto.NestedStructureMarshal(respVerifHeaderOriginField, buf[offset:], r.origin)
if err != nil {
return nil, err
}
return buf, nil
@ -1090,29 +743,10 @@ func (r *ResponseVerificationHeader) StableSize() (size int) {
return 0
}
if r.bodySig != nil {
_, ln := proto.NestedStructurePrefix(respVerifHeaderBodySignatureField)
n := r.bodySig.StableSize()
size += ln + proto.VarUIntSize(uint64(n)) + n
}
if r.metaSig != nil {
_, ln := proto.NestedStructurePrefix(respVerifHeaderMetaSignatureField)
n := r.metaSig.StableSize()
size += ln + proto.VarUIntSize(uint64(n)) + n
}
if r.originSig != nil {
_, ln := proto.NestedStructurePrefix(respVerifHeaderOriginSignatureField)
n := r.originSig.StableSize()
size += ln + proto.VarUIntSize(uint64(n)) + n
}
if r.origin != nil {
_, ln := proto.NestedStructurePrefix(respVerifHeaderOriginField)
n := r.origin.StableSize()
size += ln + proto.VarUIntSize(uint64(n)) + n
}
size += proto.NestedStructureSize(respVerifHeaderBodySignatureField, r.bodySig)
size += proto.NestedStructureSize(respVerifHeaderMetaSignatureField, r.metaSig)
size += proto.NestedStructureSize(respVerifHeaderOriginSignatureField, r.originSig)
size += proto.NestedStructureSize(respVerifHeaderOriginField, r.origin)
return size
}

View file

@ -1,20 +1,14 @@
package storagegroup
import (
"encoding/binary"
"github.com/nspcc-dev/neofs-api-go/util/proto"
)
const (
// SizeField order number from storage group proto definition.
SizeField = 1
// HashField order number from storage group proto definition.
HashField = 2
// ExpirationField order number from storage group proto definition.
ExpirationField = 3
// ObjectIDsField order number from storage group proto definition.
ObjectIDsField = 4
sizeField = 1
hashField = 2
expirationField = 3
objectIDsField = 4
)
// StableMarshal marshals unified storage group structure in a protobuf
@ -30,40 +24,32 @@ func (s *StorageGroup) StableMarshal(buf []byte) ([]byte, error) {
var (
offset, n int
prefix uint64
err error
)
n, err = proto.UInt64Marshal(SizeField, buf, s.size)
n, err = proto.UInt64Marshal(sizeField, buf[offset:], s.size)
if err != nil {
return nil, err
}
offset += n
n, err = proto.BytesMarshal(HashField, buf[offset:], s.hash)
n, err = proto.BytesMarshal(hashField, buf[offset:], s.hash)
if err != nil {
return nil, err
}
offset += n
n, err = proto.UInt64Marshal(ExpirationField, buf[offset:], s.exp)
n, err = proto.UInt64Marshal(expirationField, buf[offset:], s.exp)
if err != nil {
return nil, err
}
offset += n
prefix, _ = proto.NestedStructurePrefix(ObjectIDsField)
for i := range s.members {
offset += binary.PutUvarint(buf[offset:], prefix)
n = s.members[i].StableSize()
offset += binary.PutUvarint(buf[offset:], uint64(n))
_, err = s.members[i].StableMarshal(buf[offset:])
n, err = proto.NestedStructureMarshal(objectIDsField, buf[offset:], s.members[i])
if err != nil {
return nil, err
}
@ -80,15 +66,12 @@ func (s *StorageGroup) StableSize() (size int) {
return 0
}
size += proto.UInt64Size(SizeField, s.size)
size += proto.BytesSize(HashField, s.hash)
size += proto.UInt64Size(ExpirationField, s.exp)
_, ln := proto.NestedStructurePrefix(ObjectIDsField)
size += proto.UInt64Size(sizeField, s.size)
size += proto.BytesSize(hashField, s.hash)
size += proto.UInt64Size(expirationField, s.exp)
for i := range s.members {
n := s.members[i].StableSize()
size += ln + proto.VarUIntSize(uint64(n)) + n
size += proto.NestedStructureSize(objectIDsField, s.members[i])
}
return size