From 3039aa20c7782b7da54b0a696cad6d5aeb556d67 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Mon, 21 Dec 2020 10:51:45 +0300 Subject: [PATCH] [#233] v2/refs: Implement converters for object ID list type Signed-off-by: Leonard Lyubich --- v2/object/convert.go | 22 ++++++---------------- v2/object/marshal.go | 37 +++++++++++++------------------------ v2/refs/convert.go | 28 ++++++++++++++++++++++++++++ v2/refs/marshal.go | 27 +++++++++++++++++++++++++++ v2/storagegroup/convert.go | 23 ++++++----------------- v2/storagegroup/marshal.go | 16 +++++----------- 6 files changed, 85 insertions(+), 68 deletions(-) diff --git a/v2/object/convert.go b/v2/object/convert.go index 84ae218..88b0e0e 100644 --- a/v2/object/convert.go +++ b/v2/object/convert.go @@ -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 } diff --git a/v2/object/marshal.go b/v2/object/marshal.go index c321ed3..59ac0ad 100644 --- a/v2/object/marshal.go +++ b/v2/object/marshal.go @@ -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,15 +296,13 @@ 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]) - if err != nil { - return nil, err - } - - offset += n + 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 { return nil, err @@ -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,17 +1222,13 @@ func (r *SearchResponseBody) StableMarshal(buf []byte) ([]byte, error) { } var ( - offset, n int - err error + offset int + err error ) - for i := range r.idList { - n, err = proto.NestedStructureMarshal(searchRespBodyObjectIDsField, buf[offset:], r.idList[i]) - if err != nil { - return nil, err - } - - offset += n + _, err = refs.ObjectIDNestedListMarshal(searchRespBodyObjectIDsField, buf[offset:], r.idList) + if err != nil { + return nil, err } 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 } diff --git a/v2/refs/convert.go b/v2/refs/convert.go index 00971be..673fbf0 100644 --- a/v2/refs/convert.go +++ b/v2/refs/convert.go @@ -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 diff --git a/v2/refs/marshal.go b/v2/refs/marshal.go index 3c3ef1e..28b4d4e 100644 --- a/v2/refs/marshal.go +++ b/v2/refs/marshal.go @@ -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 { diff --git a/v2/storagegroup/convert.go b/v2/storagegroup/convert.go index 6712619..e78312c 100644 --- a/v2/storagegroup/convert.go +++ b/v2/storagegroup/convert.go @@ -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 } diff --git a/v2/storagegroup/marshal.go b/v2/storagegroup/marshal.go index bfcb896..c7d4e75 100644 --- a/v2/storagegroup/marshal.go +++ b/v2/storagegroup/marshal.go @@ -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,13 +51,9 @@ 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]) - if err != nil { - return nil, err - } - - offset += n + n, err = refs.ObjectIDNestedListMarshal(objectIDsField, buf[offset:], s.members) + if err != nil { + return nil, err } 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 }