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

View file

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

View file

@ -76,6 +76,34 @@ func ObjectIDFromGRPCMessage(m *refs.ObjectID) *ObjectID {
return o 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 { func AddressToGRPCMessage(a *Address) *refs.Address {
if a == nil { if a == nil {
return nil return nil

View file

@ -115,6 +115,16 @@ func (o *ObjectID) StableMarshal(buf []byte) ([]byte, error) {
return buf, nil 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 { func (o *ObjectID) StableSize() int {
if o == nil { if o == nil {
return 0 return 0
@ -123,6 +133,23 @@ func (o *ObjectID) StableSize() int {
return proto.BytesSize(objectIDValField, o.val) 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 { func (o *ObjectID) Unmarshal(data []byte) error {
m := new(refs.ObjectID) m := new(refs.ObjectID)
if err := goproto.Unmarshal(data, m); err != nil { if err := goproto.Unmarshal(data, m); err != nil {

View file

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

View file

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