From 3adfdc5005595ad43d77e857326b679171b8d244 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Wed, 24 Mar 2021 10:10:51 +0300 Subject: [PATCH] [#265] reputation: Implement converters and encoding methods on messages Signed-off-by: Leonard Lyubich --- v2/reputation/convert.go | 227 ++++++++++++++++++++++++++++++++++ v2/reputation/json.go | 14 +++ v2/reputation/marshal.go | 118 ++++++++++++++++++ v2/reputation/message_test.go | 19 +++ 4 files changed, 378 insertions(+) create mode 100644 v2/reputation/convert.go create mode 100644 v2/reputation/json.go create mode 100644 v2/reputation/marshal.go create mode 100644 v2/reputation/message_test.go diff --git a/v2/reputation/convert.go b/v2/reputation/convert.go new file mode 100644 index 0000000..51b37bc --- /dev/null +++ b/v2/reputation/convert.go @@ -0,0 +1,227 @@ +package reputation + +import ( + "github.com/nspcc-dev/neofs-api-go/rpc/grpc" + "github.com/nspcc-dev/neofs-api-go/rpc/message" + reputation "github.com/nspcc-dev/neofs-api-go/v2/reputation/grpc" +) + +// ToGRPCMessage converts Trust to gRPC-generated +// reputation.Trust message. +func (x *Trust) ToGRPCMessage() grpc.Message { + var m *reputation.Trust + + if x != nil { + m = new(reputation.Trust) + + m.SetValue(x.val) + m.SetPeer(x.peer) + } + + return m +} + +// FromGRPCMessage tries to restore Trust from grpc.Message. +// +// Returns message.ErrUnexpectedMessageType if m is not +// a gRPC-generated reputation.Trust message. +func (x *Trust) FromGRPCMessage(m grpc.Message) error { + v, ok := m.(*reputation.Trust) + if !ok { + return message.NewUnexpectedMessageType(m, v) + } + + x.val = v.GetValue() + x.peer = v.GetPeer() + + return nil +} + +// TrustsToGRPC converts slice of Trust structures +// to slice of gRPC-generated Trust messages. +func TrustsToGRPC(xs []*Trust) (res []*reputation.Trust) { + if xs != nil { + res = make([]*reputation.Trust, 0, len(xs)) + + for i := range xs { + res = append(res, xs[i].ToGRPCMessage().(*reputation.Trust)) + } + } + + return +} + +// TrustsFromGRPC tries to restore slice of Trust structures from +// slice of gRPC-generated reputation.Trust messages. +func TrustsFromGRPC(xs []*reputation.Trust) (res []*Trust, err error) { + if xs != nil { + res = make([]*Trust, 0, len(xs)) + + for i := range xs { + var x *Trust + + if xs[i] != nil { + x = new(Trust) + + err = x.FromGRPCMessage(xs[i]) + if err != nil { + return + } + } + + res = append(res, x) + } + } + + return +} + +// ToGRPCMessage converts SendLocalTrustRequestBody to gRPC-generated +// reputation.SendLocalTrustRequest_Body message. +func (x *SendLocalTrustRequestBody) ToGRPCMessage() grpc.Message { + var m *reputation.SendLocalTrustRequest_Body + + if x != nil { + m = new(reputation.SendLocalTrustRequest_Body) + + m.SetEpoch(x.epoch) + m.SetTrusts(TrustsToGRPC(x.trusts)) + } + + return m +} + +// FromGRPCMessage tries to restore SendLocalTrustRequestBody from grpc.Message. +// +// Returns message.ErrUnexpectedMessageType if m is not +// a gRPC-generated reputation.SendLocalTrustRequest_Body message. +func (x *SendLocalTrustRequestBody) FromGRPCMessage(m grpc.Message) error { + v, ok := m.(*reputation.SendLocalTrustRequest_Body) + if !ok { + return message.NewUnexpectedMessageType(m, v) + } + + var err error + + x.trusts, err = TrustsFromGRPC(v.GetTrusts()) + if err != nil { + return err + } + + x.epoch = v.GetEpoch() + + return nil +} + +// ToGRPCMessage converts SendLocalTrustRequest to gRPC-generated +// reputation.SendLocalTrustRequest message. +func (x *SendLocalTrustRequest) ToGRPCMessage() grpc.Message { + var m *reputation.SendLocalTrustRequest + + if x != nil { + m = new(reputation.SendLocalTrustRequest) + + m.SetBody(x.body.ToGRPCMessage().(*reputation.SendLocalTrustRequest_Body)) + x.RequestHeaders.ToMessage(m) + } + + return m +} + +// FromGRPCMessage tries to restore SendLocalTrustRequest from grpc.Message. +// +// Returns message.ErrUnexpectedMessageType if m is not +// a gRPC-generated reputation.SendLocalTrustRequest message. +func (x *SendLocalTrustRequest) FromGRPCMessage(m grpc.Message) error { + v, ok := m.(*reputation.SendLocalTrustRequest) + if !ok { + return message.NewUnexpectedMessageType(m, v) + } + + var err error + + body := v.GetBody() + if body == nil { + x.body = nil + } else { + if x.body == nil { + x.body = new(SendLocalTrustRequestBody) + } + + err = x.body.FromGRPCMessage(body) + if err != nil { + return err + } + } + + return x.RequestHeaders.FromMessage(v) +} + +// ToGRPCMessage converts SendLocalTrustResponseBody to gRPC-generated +// reputation.SendLocalTrustResponse_Body message. +func (x *SendLocalTrustResponseBody) ToGRPCMessage() grpc.Message { + var m *reputation.SendLocalTrustResponse_Body + + if x != nil { + m = new(reputation.SendLocalTrustResponse_Body) + } + + return m +} + +// FromGRPCMessage tries to restore SendLocalTrustResponseBody from grpc.Message. +// +// Returns message.ErrUnexpectedMessageType if m is not +// a gRPC-generated reputation.SendLocalTrustResponse_Body message. +func (x *SendLocalTrustResponseBody) FromGRPCMessage(m grpc.Message) error { + v, ok := m.(*reputation.SendLocalTrustResponse_Body) + if !ok { + return message.NewUnexpectedMessageType(m, v) + } + + return nil +} + +// ToGRPCMessage converts SendLocalTrustResponse to gRPC-generated +// reputation.SendLocalTrustResponse message. +func (x *SendLocalTrustResponse) ToGRPCMessage() grpc.Message { + var m *reputation.SendLocalTrustResponse + + if x != nil { + m = new(reputation.SendLocalTrustResponse) + + m.SetBody(x.body.ToGRPCMessage().(*reputation.SendLocalTrustResponse_Body)) + x.ResponseHeaders.ToMessage(m) + } + + return m +} + +// FromGRPCMessage tries to restore SendLocalTrustResponse from grpc.Message. +// +// Returns message.ErrUnexpectedMessageType if m is not +// a gRPC-generated reputation.SendLocalTrustResponse message. +func (x *SendLocalTrustResponse) FromGRPCMessage(m grpc.Message) error { + v, ok := m.(*reputation.SendLocalTrustResponse) + if !ok { + return message.NewUnexpectedMessageType(m, v) + } + + var err error + + body := v.GetBody() + if body == nil { + x.body = nil + } else { + if x.body == nil { + x.body = new(SendLocalTrustResponseBody) + } + + err = x.body.FromGRPCMessage(body) + if err != nil { + return err + } + } + + return x.ResponseHeaders.FromMessage(v) +} diff --git a/v2/reputation/json.go b/v2/reputation/json.go new file mode 100644 index 0000000..202bae2 --- /dev/null +++ b/v2/reputation/json.go @@ -0,0 +1,14 @@ +package reputation + +import ( + "github.com/nspcc-dev/neofs-api-go/rpc/message" + reputation "github.com/nspcc-dev/neofs-api-go/v2/reputation/grpc" +) + +func (x *Trust) MarshalJSON() ([]byte, error) { + return message.MarshalJSON(x) +} + +func (x *Trust) UnmarshalJSON(data []byte) error { + return message.UnmarshalJSON(x, data, new(reputation.Trust)) +} diff --git a/v2/reputation/marshal.go b/v2/reputation/marshal.go new file mode 100644 index 0000000..d627f81 --- /dev/null +++ b/v2/reputation/marshal.go @@ -0,0 +1,118 @@ +package reputation + +import ( + "github.com/nspcc-dev/neofs-api-go/rpc/message" + protoutil "github.com/nspcc-dev/neofs-api-go/util/proto" + reputation "github.com/nspcc-dev/neofs-api-go/v2/reputation/grpc" +) + +const ( + _ = iota + trustPeerFNum + trustValueFNum +) + +func (x *Trust) StableMarshal(buf []byte) ([]byte, error) { + if x == nil { + return []byte{}, nil + } + + if buf == nil { + buf = make([]byte, x.StableSize()) + } + + var ( + offset, n int + err error + ) + + n, err = protoutil.BytesMarshal(trustPeerFNum, buf[offset:], x.peer) + if err != nil { + return nil, err + } + + offset += n + + _, err = protoutil.Float64Marshal(trustValueFNum, buf[offset:], x.val) + if err != nil { + return nil, err + } + + return buf, nil +} + +func (x *Trust) StableSize() (size int) { + size += protoutil.BytesSize(trustPeerFNum, x.peer) + size += protoutil.Float64Size(trustValueFNum, x.val) + + return +} + +func (x *Trust) Unmarshal(data []byte) error { + return message.Unmarshal(x, data, new(reputation.Trust)) +} + +const ( + _ = iota + sendLocalTrustBodyEpochFNum + sendLocalTrustBodyTrustsFNum +) + +func (x *SendLocalTrustRequestBody) StableMarshal(buf []byte) ([]byte, error) { + if x == nil { + return []byte{}, nil + } + + if buf == nil { + buf = make([]byte, x.StableSize()) + } + + var ( + offset, n int + err error + ) + + n, err = protoutil.UInt64Marshal(sendLocalTrustBodyEpochFNum, buf[offset:], x.epoch) + if err != nil { + return nil, err + } + + offset += n + + for i := range x.trusts { + n, err = protoutil.NestedStructureMarshal(sendLocalTrustBodyTrustsFNum, buf[offset:], x.trusts[i]) + if err != nil { + return nil, err + } + + offset += n + } + + return buf, nil +} + +func (x *SendLocalTrustRequestBody) StableSize() (size int) { + size += protoutil.UInt64Size(sendLocalTrustBodyEpochFNum, x.epoch) + + for i := range x.trusts { + size += protoutil.NestedStructureSize(sendLocalTrustBodyTrustsFNum, x.trusts[i]) + } + + return +} + +func (x *SendLocalTrustRequestBody) Unmarshal(data []byte) error { + return message.Unmarshal(x, data, new(reputation.SendLocalTrustRequest_Body)) +} + +func (x *SendLocalTrustResponseBody) StableMarshal(buf []byte) ([]byte, error) { + return buf, nil +} + +func (x *SendLocalTrustResponseBody) StableSize() int { + return 0 +} + +func (x *SendLocalTrustResponseBody) Unmarshal(data []byte) error { + return message.Unmarshal(x, data, new(reputation.SendLocalTrustResponse_Body)) +} diff --git a/v2/reputation/message_test.go b/v2/reputation/message_test.go new file mode 100644 index 0000000..07ca89f --- /dev/null +++ b/v2/reputation/message_test.go @@ -0,0 +1,19 @@ +package reputation_test + +import ( + "testing" + + "github.com/nspcc-dev/neofs-api-go/rpc/message" + messagetest "github.com/nspcc-dev/neofs-api-go/rpc/message/test" + reputationtest "github.com/nspcc-dev/neofs-api-go/v2/reputation/test" +) + +func TestMessageConvert(t *testing.T) { + messagetest.TestRPCMessage(t, + func(empty bool) message.Message { return reputationtest.GenerateTrust(empty) }, + func(empty bool) message.Message { return reputationtest.GenerateSendLocalTrustRequestBody(empty) }, + func(empty bool) message.Message { return reputationtest.GenerateSendLocalTrustRequest(empty) }, + func(empty bool) message.Message { return reputationtest.GenerateSendLocalTrustResponseBody(empty) }, + func(empty bool) message.Message { return reputationtest.GenerateSendLocalTrustResponse(empty) }, + ) +}