frostfs-api-go/refs/convert.go

297 lines
4.9 KiB
Go
Raw Normal View History

package refs
import (
refs "github.com/nspcc-dev/neofs-api-go/v2/refs/grpc"
"github.com/nspcc-dev/neofs-api-go/v2/rpc/grpc"
"github.com/nspcc-dev/neofs-api-go/v2/rpc/message"
)
func (o *OwnerID) ToGRPCMessage() grpc.Message {
var m *refs.OwnerID
if o != nil {
m = new(refs.OwnerID)
m.SetValue(o.val)
}
return m
}
func (o *OwnerID) FromGRPCMessage(m grpc.Message) error {
v, ok := m.(*refs.OwnerID)
if !ok {
return message.NewUnexpectedMessageType(m, v)
}
o.val = v.GetValue()
return nil
}
func (c *ContainerID) ToGRPCMessage() grpc.Message {
var m *refs.ContainerID
if c != nil {
m = new(refs.ContainerID)
m.SetValue(c.val)
}
return m
}
func (c *ContainerID) FromGRPCMessage(m grpc.Message) error {
v, ok := m.(*refs.ContainerID)
if !ok {
return message.NewUnexpectedMessageType(m, v)
}
c.val = v.GetValue()
return nil
}
func ContainerIDsToGRPCMessage(ids []ContainerID) (res []*refs.ContainerID) {
if ids != nil {
res = make([]*refs.ContainerID, 0, len(ids))
for i := range ids {
res = append(res, ids[i].ToGRPCMessage().(*refs.ContainerID))
}
}
return
}
func ContainerIDsFromGRPCMessage(idsV2 []*refs.ContainerID) (res []ContainerID, err error) {
if idsV2 != nil {
res = make([]ContainerID, len(idsV2))
for i := range idsV2 {
if idsV2[i] != nil {
err = res[i].FromGRPCMessage(idsV2[i])
if err != nil {
return
}
}
}
}
return
}
func (o *ObjectID) ToGRPCMessage() grpc.Message {
var m *refs.ObjectID
if o != nil {
m = new(refs.ObjectID)
m.SetValue(o.val)
}
return m
}
func (o *ObjectID) FromGRPCMessage(m grpc.Message) error {
v, ok := m.(*refs.ObjectID)
if !ok {
return message.NewUnexpectedMessageType(m, v)
}
o.val = v.GetValue()
return nil
}
[#376] refs: Replace []*ObjectID with []ObjectID ``` ObjectIDSlice/0_elements/to_grpc_message-8 5.64ns ± 2% 5.89ns ± 4% +4.56% (p=0.000 n=10+10) ObjectIDSlice/0_elements/from_grpc_message-8 6.68ns ± 3% 6.81ns ± 8% ~ (p=0.143 n=10+10) ObjectIDSlice/0_elements/marshal-8 7.41ns ± 3% 7.91ns ± 4% +6.63% (p=0.000 n=10+10) ObjectIDSlice/1_elements/to_grpc_message-8 69.8ns ± 3% 80.5ns ± 7% +15.39% (p=0.000 n=10+10) ObjectIDSlice/1_elements/from_grpc_message-8 56.4ns ± 6% 34.7ns ± 5% -38.55% (p=0.000 n=10+9) ObjectIDSlice/1_elements/marshal-8 68.4ns ± 4% 67.6ns ± 4% ~ (p=0.404 n=10+10) ObjectIDSlice/50_elements/to_grpc_message-8 2.52µs ± 7% 2.56µs ± 4% ~ (p=0.315 n=10+9) ObjectIDSlice/50_elements/from_grpc_message-8 1.83µs ± 8% 0.44µs ± 1% -75.73% (p=0.000 n=10+8) ObjectIDSlice/50_elements/marshal-8 2.32µs ±17% 2.22µs ± 3% ~ (p=0.247 n=10+10) name old alloc/op new alloc/op delta ObjectIDSlice/0_elements/to_grpc_message-8 0.00B 0.00B ~ (all equal) ObjectIDSlice/0_elements/from_grpc_message-8 0.00B 0.00B ~ (all equal) ObjectIDSlice/0_elements/marshal-8 0.00B 0.00B ~ (all equal) ObjectIDSlice/1_elements/to_grpc_message-8 72.0B ± 0% 72.0B ± 0% ~ (all equal) ObjectIDSlice/1_elements/from_grpc_message-8 32.0B ± 0% 24.0B ± 0% -25.00% (p=0.000 n=10+10) ObjectIDSlice/1_elements/marshal-8 48.0B ± 0% 48.0B ± 0% ~ (all equal) ObjectIDSlice/50_elements/to_grpc_message-8 3.62kB ± 0% 3.62kB ± 0% ~ (all equal) ObjectIDSlice/50_elements/from_grpc_message-8 1.62kB ± 0% 1.28kB ± 0% -20.79% (p=0.000 n=10+10) ObjectIDSlice/50_elements/marshal-8 2.05kB ± 0% 2.05kB ± 0% ~ (all equal) name old allocs/op new allocs/op delta ObjectIDSlice/0_elements/to_grpc_message-8 0.00 0.00 ~ (all equal) ObjectIDSlice/0_elements/from_grpc_message-8 0.00 0.00 ~ (all equal) ObjectIDSlice/0_elements/marshal-8 0.00 0.00 ~ (all equal) ObjectIDSlice/1_elements/to_grpc_message-8 2.00 ± 0% 2.00 ± 0% ~ (all equal) ObjectIDSlice/1_elements/from_grpc_message-8 2.00 ± 0% 1.00 ± 0% -50.00% (p=0.000 n=10+10) ObjectIDSlice/1_elements/marshal-8 1.00 ± 0% 1.00 ± 0% ~ (all equal) ObjectIDSlice/50_elements/to_grpc_message-8 51.0 ± 0% 51.0 ± 0% ~ (all equal) ObjectIDSlice/50_elements/from_grpc_message-8 51.0 ± 0% 1.0 ± 0% -98.04% (p=0.000 n=10+10) ObjectIDSlice/50_elements/marshal-8 1.00 ± 0% 1.00 ± 0% ~ (all equal) ``` Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2022-03-01 12:50:09 +00:00
func ObjectIDListToGRPCMessage(ids []ObjectID) (res []*refs.ObjectID) {
if ids != nil {
res = make([]*refs.ObjectID, 0, len(ids))
for i := range ids {
res = append(res, ids[i].ToGRPCMessage().(*refs.ObjectID))
}
}
return
}
[#376] refs: Replace []*ObjectID with []ObjectID ``` ObjectIDSlice/0_elements/to_grpc_message-8 5.64ns ± 2% 5.89ns ± 4% +4.56% (p=0.000 n=10+10) ObjectIDSlice/0_elements/from_grpc_message-8 6.68ns ± 3% 6.81ns ± 8% ~ (p=0.143 n=10+10) ObjectIDSlice/0_elements/marshal-8 7.41ns ± 3% 7.91ns ± 4% +6.63% (p=0.000 n=10+10) ObjectIDSlice/1_elements/to_grpc_message-8 69.8ns ± 3% 80.5ns ± 7% +15.39% (p=0.000 n=10+10) ObjectIDSlice/1_elements/from_grpc_message-8 56.4ns ± 6% 34.7ns ± 5% -38.55% (p=0.000 n=10+9) ObjectIDSlice/1_elements/marshal-8 68.4ns ± 4% 67.6ns ± 4% ~ (p=0.404 n=10+10) ObjectIDSlice/50_elements/to_grpc_message-8 2.52µs ± 7% 2.56µs ± 4% ~ (p=0.315 n=10+9) ObjectIDSlice/50_elements/from_grpc_message-8 1.83µs ± 8% 0.44µs ± 1% -75.73% (p=0.000 n=10+8) ObjectIDSlice/50_elements/marshal-8 2.32µs ±17% 2.22µs ± 3% ~ (p=0.247 n=10+10) name old alloc/op new alloc/op delta ObjectIDSlice/0_elements/to_grpc_message-8 0.00B 0.00B ~ (all equal) ObjectIDSlice/0_elements/from_grpc_message-8 0.00B 0.00B ~ (all equal) ObjectIDSlice/0_elements/marshal-8 0.00B 0.00B ~ (all equal) ObjectIDSlice/1_elements/to_grpc_message-8 72.0B ± 0% 72.0B ± 0% ~ (all equal) ObjectIDSlice/1_elements/from_grpc_message-8 32.0B ± 0% 24.0B ± 0% -25.00% (p=0.000 n=10+10) ObjectIDSlice/1_elements/marshal-8 48.0B ± 0% 48.0B ± 0% ~ (all equal) ObjectIDSlice/50_elements/to_grpc_message-8 3.62kB ± 0% 3.62kB ± 0% ~ (all equal) ObjectIDSlice/50_elements/from_grpc_message-8 1.62kB ± 0% 1.28kB ± 0% -20.79% (p=0.000 n=10+10) ObjectIDSlice/50_elements/marshal-8 2.05kB ± 0% 2.05kB ± 0% ~ (all equal) name old allocs/op new allocs/op delta ObjectIDSlice/0_elements/to_grpc_message-8 0.00 0.00 ~ (all equal) ObjectIDSlice/0_elements/from_grpc_message-8 0.00 0.00 ~ (all equal) ObjectIDSlice/0_elements/marshal-8 0.00 0.00 ~ (all equal) ObjectIDSlice/1_elements/to_grpc_message-8 2.00 ± 0% 2.00 ± 0% ~ (all equal) ObjectIDSlice/1_elements/from_grpc_message-8 2.00 ± 0% 1.00 ± 0% -50.00% (p=0.000 n=10+10) ObjectIDSlice/1_elements/marshal-8 1.00 ± 0% 1.00 ± 0% ~ (all equal) ObjectIDSlice/50_elements/to_grpc_message-8 51.0 ± 0% 51.0 ± 0% ~ (all equal) ObjectIDSlice/50_elements/from_grpc_message-8 51.0 ± 0% 1.0 ± 0% -98.04% (p=0.000 n=10+10) ObjectIDSlice/50_elements/marshal-8 1.00 ± 0% 1.00 ± 0% ~ (all equal) ``` Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2022-03-01 12:50:09 +00:00
func ObjectIDListFromGRPCMessage(idsV2 []*refs.ObjectID) (res []ObjectID, err error) {
if idsV2 != nil {
[#376] refs: Replace []*ObjectID with []ObjectID ``` ObjectIDSlice/0_elements/to_grpc_message-8 5.64ns ± 2% 5.89ns ± 4% +4.56% (p=0.000 n=10+10) ObjectIDSlice/0_elements/from_grpc_message-8 6.68ns ± 3% 6.81ns ± 8% ~ (p=0.143 n=10+10) ObjectIDSlice/0_elements/marshal-8 7.41ns ± 3% 7.91ns ± 4% +6.63% (p=0.000 n=10+10) ObjectIDSlice/1_elements/to_grpc_message-8 69.8ns ± 3% 80.5ns ± 7% +15.39% (p=0.000 n=10+10) ObjectIDSlice/1_elements/from_grpc_message-8 56.4ns ± 6% 34.7ns ± 5% -38.55% (p=0.000 n=10+9) ObjectIDSlice/1_elements/marshal-8 68.4ns ± 4% 67.6ns ± 4% ~ (p=0.404 n=10+10) ObjectIDSlice/50_elements/to_grpc_message-8 2.52µs ± 7% 2.56µs ± 4% ~ (p=0.315 n=10+9) ObjectIDSlice/50_elements/from_grpc_message-8 1.83µs ± 8% 0.44µs ± 1% -75.73% (p=0.000 n=10+8) ObjectIDSlice/50_elements/marshal-8 2.32µs ±17% 2.22µs ± 3% ~ (p=0.247 n=10+10) name old alloc/op new alloc/op delta ObjectIDSlice/0_elements/to_grpc_message-8 0.00B 0.00B ~ (all equal) ObjectIDSlice/0_elements/from_grpc_message-8 0.00B 0.00B ~ (all equal) ObjectIDSlice/0_elements/marshal-8 0.00B 0.00B ~ (all equal) ObjectIDSlice/1_elements/to_grpc_message-8 72.0B ± 0% 72.0B ± 0% ~ (all equal) ObjectIDSlice/1_elements/from_grpc_message-8 32.0B ± 0% 24.0B ± 0% -25.00% (p=0.000 n=10+10) ObjectIDSlice/1_elements/marshal-8 48.0B ± 0% 48.0B ± 0% ~ (all equal) ObjectIDSlice/50_elements/to_grpc_message-8 3.62kB ± 0% 3.62kB ± 0% ~ (all equal) ObjectIDSlice/50_elements/from_grpc_message-8 1.62kB ± 0% 1.28kB ± 0% -20.79% (p=0.000 n=10+10) ObjectIDSlice/50_elements/marshal-8 2.05kB ± 0% 2.05kB ± 0% ~ (all equal) name old allocs/op new allocs/op delta ObjectIDSlice/0_elements/to_grpc_message-8 0.00 0.00 ~ (all equal) ObjectIDSlice/0_elements/from_grpc_message-8 0.00 0.00 ~ (all equal) ObjectIDSlice/0_elements/marshal-8 0.00 0.00 ~ (all equal) ObjectIDSlice/1_elements/to_grpc_message-8 2.00 ± 0% 2.00 ± 0% ~ (all equal) ObjectIDSlice/1_elements/from_grpc_message-8 2.00 ± 0% 1.00 ± 0% -50.00% (p=0.000 n=10+10) ObjectIDSlice/1_elements/marshal-8 1.00 ± 0% 1.00 ± 0% ~ (all equal) ObjectIDSlice/50_elements/to_grpc_message-8 51.0 ± 0% 51.0 ± 0% ~ (all equal) ObjectIDSlice/50_elements/from_grpc_message-8 51.0 ± 0% 1.0 ± 0% -98.04% (p=0.000 n=10+10) ObjectIDSlice/50_elements/marshal-8 1.00 ± 0% 1.00 ± 0% ~ (all equal) ``` Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2022-03-01 12:50:09 +00:00
res = make([]ObjectID, len(idsV2))
for i := range idsV2 {
if idsV2[i] != nil {
[#376] refs: Replace []*ObjectID with []ObjectID ``` ObjectIDSlice/0_elements/to_grpc_message-8 5.64ns ± 2% 5.89ns ± 4% +4.56% (p=0.000 n=10+10) ObjectIDSlice/0_elements/from_grpc_message-8 6.68ns ± 3% 6.81ns ± 8% ~ (p=0.143 n=10+10) ObjectIDSlice/0_elements/marshal-8 7.41ns ± 3% 7.91ns ± 4% +6.63% (p=0.000 n=10+10) ObjectIDSlice/1_elements/to_grpc_message-8 69.8ns ± 3% 80.5ns ± 7% +15.39% (p=0.000 n=10+10) ObjectIDSlice/1_elements/from_grpc_message-8 56.4ns ± 6% 34.7ns ± 5% -38.55% (p=0.000 n=10+9) ObjectIDSlice/1_elements/marshal-8 68.4ns ± 4% 67.6ns ± 4% ~ (p=0.404 n=10+10) ObjectIDSlice/50_elements/to_grpc_message-8 2.52µs ± 7% 2.56µs ± 4% ~ (p=0.315 n=10+9) ObjectIDSlice/50_elements/from_grpc_message-8 1.83µs ± 8% 0.44µs ± 1% -75.73% (p=0.000 n=10+8) ObjectIDSlice/50_elements/marshal-8 2.32µs ±17% 2.22µs ± 3% ~ (p=0.247 n=10+10) name old alloc/op new alloc/op delta ObjectIDSlice/0_elements/to_grpc_message-8 0.00B 0.00B ~ (all equal) ObjectIDSlice/0_elements/from_grpc_message-8 0.00B 0.00B ~ (all equal) ObjectIDSlice/0_elements/marshal-8 0.00B 0.00B ~ (all equal) ObjectIDSlice/1_elements/to_grpc_message-8 72.0B ± 0% 72.0B ± 0% ~ (all equal) ObjectIDSlice/1_elements/from_grpc_message-8 32.0B ± 0% 24.0B ± 0% -25.00% (p=0.000 n=10+10) ObjectIDSlice/1_elements/marshal-8 48.0B ± 0% 48.0B ± 0% ~ (all equal) ObjectIDSlice/50_elements/to_grpc_message-8 3.62kB ± 0% 3.62kB ± 0% ~ (all equal) ObjectIDSlice/50_elements/from_grpc_message-8 1.62kB ± 0% 1.28kB ± 0% -20.79% (p=0.000 n=10+10) ObjectIDSlice/50_elements/marshal-8 2.05kB ± 0% 2.05kB ± 0% ~ (all equal) name old allocs/op new allocs/op delta ObjectIDSlice/0_elements/to_grpc_message-8 0.00 0.00 ~ (all equal) ObjectIDSlice/0_elements/from_grpc_message-8 0.00 0.00 ~ (all equal) ObjectIDSlice/0_elements/marshal-8 0.00 0.00 ~ (all equal) ObjectIDSlice/1_elements/to_grpc_message-8 2.00 ± 0% 2.00 ± 0% ~ (all equal) ObjectIDSlice/1_elements/from_grpc_message-8 2.00 ± 0% 1.00 ± 0% -50.00% (p=0.000 n=10+10) ObjectIDSlice/1_elements/marshal-8 1.00 ± 0% 1.00 ± 0% ~ (all equal) ObjectIDSlice/50_elements/to_grpc_message-8 51.0 ± 0% 51.0 ± 0% ~ (all equal) ObjectIDSlice/50_elements/from_grpc_message-8 51.0 ± 0% 1.0 ± 0% -98.04% (p=0.000 n=10+10) ObjectIDSlice/50_elements/marshal-8 1.00 ± 0% 1.00 ± 0% ~ (all equal) ``` Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2022-03-01 12:50:09 +00:00
err = res[i].FromGRPCMessage(idsV2[i])
if err != nil {
return
}
}
}
}
return
}
func (a *Address) ToGRPCMessage() grpc.Message {
var m *refs.Address
if a != nil {
m = new(refs.Address)
m.SetContainerId(a.cid.ToGRPCMessage().(*refs.ContainerID))
m.SetObjectId(a.oid.ToGRPCMessage().(*refs.ObjectID))
}
return m
}
func (a *Address) FromGRPCMessage(m grpc.Message) error {
v, ok := m.(*refs.Address)
if !ok {
return message.NewUnexpectedMessageType(m, v)
}
var err error
cid := v.GetContainerId()
if cid == nil {
a.cid = nil
} else {
if a.cid == nil {
a.cid = new(ContainerID)
}
err = a.cid.FromGRPCMessage(cid)
if err != nil {
return err
}
}
oid := v.GetObjectId()
if oid == nil {
a.oid = nil
} else {
if a.oid == nil {
a.oid = new(ObjectID)
}
err = a.oid.FromGRPCMessage(oid)
}
return err
}
func ChecksumTypeToGRPC(t ChecksumType) refs.ChecksumType {
return refs.ChecksumType(t)
}
func ChecksumTypeFromGRPC(t refs.ChecksumType) ChecksumType {
return ChecksumType(t)
}
func (c *Checksum) ToGRPCMessage() grpc.Message {
var m *refs.Checksum
if c != nil {
m = new(refs.Checksum)
m.SetChecksumType(ChecksumTypeToGRPC(c.typ))
m.SetSum(c.sum)
}
return m
}
func (c *Checksum) FromGRPCMessage(m grpc.Message) error {
v, ok := m.(*refs.Checksum)
if !ok {
return message.NewUnexpectedMessageType(m, v)
}
c.typ = ChecksumTypeFromGRPC(v.GetType())
c.sum = v.GetSum()
return nil
}
func (v *Version) ToGRPCMessage() grpc.Message {
var m *refs.Version
if v != nil {
m = new(refs.Version)
m.SetMajor(v.major)
m.SetMinor(v.minor)
}
return m
}
func (v *Version) FromGRPCMessage(m grpc.Message) error {
ver, ok := m.(*refs.Version)
if !ok {
return message.NewUnexpectedMessageType(m, v)
}
v.major = ver.GetMajor()
v.minor = ver.GetMinor()
return nil
}
func (s *Signature) ToGRPCMessage() grpc.Message {
var m *refs.Signature
if s != nil {
m = new(refs.Signature)
m.SetKey(s.key)
m.SetSign(s.sign)
m.SetScheme(refs.SignatureScheme(s.scheme))
}
return m
}
func (s *Signature) FromGRPCMessage(m grpc.Message) error {
v, ok := m.(*refs.Signature)
if !ok {
return message.NewUnexpectedMessageType(m, s)
}
s.key = v.GetKey()
s.sign = v.GetSign()
s.scheme = SignatureScheme(v.GetScheme())
return nil
}
// ToGRPCMessage forms refs.SubnetID message and returns it as grpc.Message.
func (s *SubnetID) ToGRPCMessage() grpc.Message {
var m *refs.SubnetID
if s != nil {
m = new(refs.SubnetID)
m.SetValue(s.value)
}
return m
}
// FromGRPCMessage restores Info from grpc.Message.
//
// Supported types:
// - refs.SubnetID.
func (s *SubnetID) FromGRPCMessage(m grpc.Message) error {
v, ok := m.(*refs.SubnetID)
if !ok {
return message.NewUnexpectedMessageType(m, v)
}
s.value = v.GetValue()
return nil
}