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

View file

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

View file

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

View file

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

View file

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