From 8ab3abab4b4ae2825eb3aae14ff36e36e5afee0e Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Tue, 8 Jun 2021 20:07:09 +0300 Subject: [PATCH] [#302] pkg/reputation: Convert nil `Trust`, `PeerToPeerTrust` and `GlobalTrust` to nil message Document that `Trust.ToV2`, `PeerToPeerTrust.ToV2` and `GlobalTrust.ToV2` methods return `nil`when called on `nil`. Document that `TrustFromV2`, `PeerToPeerTrustFromV2`and `GlobalTrustFromV2` functions return `nil` when called on `nil`. Write corresponding unit tests. Signed-off-by: Pavel Karpy --- pkg/reputation/trust.go | 12 +++++++++ pkg/reputation/trust_test.go | 49 ++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/pkg/reputation/trust.go b/pkg/reputation/trust.go index 5a25b69..c584740 100644 --- a/pkg/reputation/trust.go +++ b/pkg/reputation/trust.go @@ -20,12 +20,16 @@ func NewTrust() *Trust { // TrustFromV2 converts NeoFS API v2 // reputation.Trust message structure to Trust. +// +// Nil reputation.Trust converts to nil. func TrustFromV2(t *reputation.Trust) *Trust { return (*Trust)(t) } // ToV2 converts Trust to NeoFS API v2 // reputation.Trust message structure. +// +// Nil Trust converts to nil. func (x *Trust) ToV2() *reputation.Trust { return (*reputation.Trust)(x) } @@ -111,12 +115,16 @@ func NewPeerToPeerTrust() *PeerToPeerTrust { // PeerToPeerTrustFromV2 converts NeoFS API v2 // reputation.PeerToPeerTrust message structure to PeerToPeerTrust. +// +// Nil reputation.PeerToPeerTrust converts to nil. func PeerToPeerTrustFromV2(t *reputation.PeerToPeerTrust) *PeerToPeerTrust { return (*PeerToPeerTrust)(t) } // ToV2 converts PeerToPeerTrust to NeoFS API v2 // reputation.PeerToPeerTrust message structure. +// +// Nil PeerToPeerTrust converts to nil. func (x *PeerToPeerTrust) ToV2() *reputation.PeerToPeerTrust { return (*reputation.PeerToPeerTrust)(x) } @@ -195,12 +203,16 @@ func NewGlobalTrust() *GlobalTrust { // GlobalTrustFromV2 converts NeoFS API v2 // reputation.GlobalTrust message structure to GlobalTrust. +// +// Nil reputation.GlobalTrust converts to nil. func GlobalTrustFromV2(t *reputation.GlobalTrust) *GlobalTrust { return (*GlobalTrust)(t) } // ToV2 converts GlobalTrust to NeoFS API v2 // reputation.GlobalTrust message structure. +// +// Nil GlobalTrust converts to nil. func (x *GlobalTrust) ToV2() *reputation.GlobalTrust { return (*reputation.GlobalTrust)(x) } diff --git a/pkg/reputation/trust_test.go b/pkg/reputation/trust_test.go index 0e908ec..844997b 100644 --- a/pkg/reputation/trust_test.go +++ b/pkg/reputation/trust_test.go @@ -6,6 +6,7 @@ import ( "github.com/nspcc-dev/neofs-api-go/pkg" "github.com/nspcc-dev/neofs-api-go/pkg/reputation" reputationtest "github.com/nspcc-dev/neofs-api-go/pkg/reputation/test" + reputationV2 "github.com/nspcc-dev/neofs-api-go/v2/reputation" reputationtestV2 "github.com/nspcc-dev/neofs-api-go/v2/reputation/test" "github.com/stretchr/testify/require" ) @@ -150,3 +151,51 @@ func TestGlobalTrust(t *testing.T) { }) }) } + +func TestTrustFromV2(t *testing.T) { + t.Run("from nil", func(t *testing.T) { + var x *reputationV2.Trust + + require.Nil(t, reputation.TrustFromV2(x)) + }) +} + +func TestPeerToPeerTrustFromV2(t *testing.T) { + t.Run("from nil", func(t *testing.T) { + var x *reputationV2.PeerToPeerTrust + + require.Nil(t, reputation.PeerToPeerTrustFromV2(x)) + }) +} + +func TestGlobalTrustFromV2(t *testing.T) { + t.Run("from nil", func(t *testing.T) { + var x *reputationV2.GlobalTrust + + require.Nil(t, reputation.GlobalTrustFromV2(x)) + }) +} + +func TestTrust_ToV2(t *testing.T) { + t.Run("nil", func(t *testing.T) { + var x *reputation.Trust + + require.Nil(t, x.ToV2()) + }) +} + +func TestPeerToPeerTrust_ToV2(t *testing.T) { + t.Run("nil", func(t *testing.T) { + var x *reputation.PeerToPeerTrust + + require.Nil(t, x.ToV2()) + }) +} + +func TestGlobalTrust_ToV2(t *testing.T) { + t.Run("nil", func(t *testing.T) { + var x *reputation.GlobalTrust + + require.Nil(t, x.ToV2()) + }) +}