[#233] v2/refs: Implement converters for object ID list type

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-12-21 10:51:45 +03:00
parent 51e8e318c2
commit 3039aa20c7
6 changed files with 85 additions and 68 deletions

View file

@ -1258,14 +1258,9 @@ func SearchResponseBodyToGRPCMessage(r *SearchResponseBody) *object.SearchRespon
m := new(object.SearchResponse_Body)
ids := r.GetIDList()
idMsg := make([]*refsGRPC.ObjectID, 0, len(ids))
for i := range ids {
idMsg = append(idMsg, refs.ObjectIDToGRPCMessage(ids[i]))
}
m.SetIdList(idMsg)
m.SetIdList(
refs.ObjectIDListToGRPCMessage(r.GetIDList()),
)
return m
}
@ -1277,14 +1272,9 @@ func SearchResponseBodyFromGRPCMessage(m *object.SearchResponse_Body) *SearchRes
r := new(SearchResponseBody)
idMsg := m.GetIdList()
ids := make([]*refs.ObjectID, 0, len(idMsg))
for i := range idMsg {
ids = append(ids, refs.ObjectIDFromGRPCMessage(idMsg[i]))
}
r.SetIDList(ids)
r.SetIDList(
refs.ObjectIDListFromGRPCMessage(m.GetIdList()),
)
return r
}

View file

@ -3,6 +3,7 @@ package object
import (
"github.com/nspcc-dev/neofs-api-go/util/proto"
object "github.com/nspcc-dev/neofs-api-go/v2/object/grpc"
"github.com/nspcc-dev/neofs-api-go/v2/refs"
goproto "google.golang.org/protobuf/proto"
)
@ -295,14 +296,12 @@ func (h *SplitHeader) StableMarshal(buf []byte) ([]byte, error) {
offset += n
for i := range h.children {
n, err = proto.NestedStructureMarshal(splitHdrChildrenField, buf[offset:], h.children[i])
n, err = refs.ObjectIDNestedListMarshal(splitHdrChildrenField, buf[offset:], h.children)
if err != nil {
return nil, err
}
offset += n
}
_, err = proto.BytesMarshal(splitHdrSplitIDField, buf[offset:], h.splitID)
if err != nil {
@ -321,11 +320,7 @@ func (h *SplitHeader) StableSize() (size int) {
size += proto.NestedStructureSize(splitHdrPreviousField, h.prev)
size += proto.NestedStructureSize(splitHdrParentSignatureField, h.parSig)
size += proto.NestedStructureSize(splitHdrParentHeaderField, h.parHdr)
for i := range h.children {
size += proto.NestedStructureSize(splitHdrChildrenField, h.children[i])
}
size += refs.ObjectIDNestedListSize(splitHdrChildrenField, h.children)
size += proto.BytesSize(splitHdrSplitIDField, h.splitID)
return size
@ -1227,19 +1222,15 @@ func (r *SearchResponseBody) StableMarshal(buf []byte) ([]byte, error) {
}
var (
offset, n int
offset int
err error
)
for i := range r.idList {
n, err = proto.NestedStructureMarshal(searchRespBodyObjectIDsField, buf[offset:], r.idList[i])
_, err = refs.ObjectIDNestedListMarshal(searchRespBodyObjectIDsField, buf[offset:], r.idList)
if err != nil {
return nil, err
}
offset += n
}
return buf, nil
}
@ -1248,9 +1239,7 @@ func (r *SearchResponseBody) StableSize() (size int) {
return 0
}
for i := range r.idList {
size += proto.NestedStructureSize(searchRespBodyObjectIDsField, r.idList[i])
}
size += refs.ObjectIDNestedListSize(searchRespBodyObjectIDsField, r.idList)
return size
}

View file

@ -76,6 +76,34 @@ func ObjectIDFromGRPCMessage(m *refs.ObjectID) *ObjectID {
return o
}
func ObjectIDListToGRPCMessage(ids []*ObjectID) []*refs.ObjectID {
if ids == nil {
return nil
}
idsV2 := make([]*refs.ObjectID, 0, len(ids))
for i := range ids {
idsV2 = append(idsV2, ObjectIDToGRPCMessage(ids[i]))
}
return idsV2
}
func ObjectIDListFromGRPCMessage(idsV2 []*refs.ObjectID) []*ObjectID {
if idsV2 == nil {
return nil
}
ids := make([]*ObjectID, 0, len(idsV2))
for i := range idsV2 {
ids = append(ids, ObjectIDFromGRPCMessage(idsV2[i]))
}
return ids
}
func AddressToGRPCMessage(a *Address) *refs.Address {
if a == nil {
return nil

View file

@ -115,6 +115,16 @@ func (o *ObjectID) StableMarshal(buf []byte) ([]byte, error) {
return buf, nil
}
// 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
@ -123,6 +133,23 @@ func (o *ObjectID) StableSize() int {
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) (off int, err error) {
for i := range ids {
var n int
n, err = proto.NestedStructureMarshal(fNum, buf[off:], ids[i])
if err != nil {
return
}
off += n
}
return
}
func (o *ObjectID) Unmarshal(data []byte) error {
m := new(refs.ObjectID)
if err := goproto.Unmarshal(data, m); err != nil {

View file

@ -2,7 +2,6 @@ package storagegroup
import (
"github.com/nspcc-dev/neofs-api-go/v2/refs"
refsGRPC "github.com/nspcc-dev/neofs-api-go/v2/refs/grpc"
sg "github.com/nspcc-dev/neofs-api-go/v2/storagegroup/grpc"
)
@ -20,14 +19,9 @@ func StorageGroupToGRPCMessage(s *StorageGroup) *sg.StorageGroup {
)
m.SetExpirationEpoch(s.GetExpirationEpoch())
members := s.GetMembers()
memberMsg := make([]*refsGRPC.ObjectID, 0, len(members))
for i := range members {
memberMsg = append(memberMsg, refs.ObjectIDToGRPCMessage(members[i]))
}
m.SetMembers(memberMsg)
m.SetMembers(
refs.ObjectIDListToGRPCMessage(s.GetMembers()),
)
return m
}
@ -46,14 +40,9 @@ func StorageGroupFromGRPCMessage(m *sg.StorageGroup) *StorageGroup {
)
s.SetExpirationEpoch(m.GetExpirationEpoch())
memberMsg := m.GetMembers()
members := make([]*refs.ObjectID, 0, len(memberMsg))
for i := range memberMsg {
members = append(members, refs.ObjectIDFromGRPCMessage(memberMsg[i]))
}
s.SetMembers(members)
s.SetMembers(
refs.ObjectIDListFromGRPCMessage(m.GetMembers()),
)
return s
}

View file

@ -2,6 +2,7 @@ package storagegroup
import (
"github.com/nspcc-dev/neofs-api-go/util/proto"
"github.com/nspcc-dev/neofs-api-go/v2/refs"
storagegroup "github.com/nspcc-dev/neofs-api-go/v2/storagegroup/grpc"
goproto "google.golang.org/protobuf/proto"
)
@ -50,15 +51,11 @@ func (s *StorageGroup) StableMarshal(buf []byte) ([]byte, error) {
offset += n
for i := range s.members {
n, err = proto.NestedStructureMarshal(objectIDsField, buf[offset:], s.members[i])
n, err = refs.ObjectIDNestedListMarshal(objectIDsField, buf[offset:], s.members)
if err != nil {
return nil, err
}
offset += n
}
return buf, nil
}
@ -71,10 +68,7 @@ func (s *StorageGroup) StableSize() (size int) {
size += proto.UInt64Size(sizeField, s.size)
size += proto.NestedStructureSize(hashField, s.hash)
size += proto.UInt64Size(expirationField, s.exp)
for i := range s.members {
size += proto.NestedStructureSize(objectIDsField, s.members[i])
}
size += refs.ObjectIDNestedListSize(objectIDsField, s.members)
return size
}