[#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 <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-06-08 20:07:09 +03:00 committed by Alex Vanin
parent cce7ecbc00
commit 8ab3abab4b
2 changed files with 61 additions and 0 deletions

View file

@ -20,12 +20,16 @@ func NewTrust() *Trust {
// TrustFromV2 converts NeoFS API v2 // TrustFromV2 converts NeoFS API v2
// reputation.Trust message structure to Trust. // reputation.Trust message structure to Trust.
//
// Nil reputation.Trust converts to nil.
func TrustFromV2(t *reputation.Trust) *Trust { func TrustFromV2(t *reputation.Trust) *Trust {
return (*Trust)(t) return (*Trust)(t)
} }
// ToV2 converts Trust to NeoFS API v2 // ToV2 converts Trust to NeoFS API v2
// reputation.Trust message structure. // reputation.Trust message structure.
//
// Nil Trust converts to nil.
func (x *Trust) ToV2() *reputation.Trust { func (x *Trust) ToV2() *reputation.Trust {
return (*reputation.Trust)(x) return (*reputation.Trust)(x)
} }
@ -111,12 +115,16 @@ func NewPeerToPeerTrust() *PeerToPeerTrust {
// PeerToPeerTrustFromV2 converts NeoFS API v2 // PeerToPeerTrustFromV2 converts NeoFS API v2
// reputation.PeerToPeerTrust message structure to PeerToPeerTrust. // reputation.PeerToPeerTrust message structure to PeerToPeerTrust.
//
// Nil reputation.PeerToPeerTrust converts to nil.
func PeerToPeerTrustFromV2(t *reputation.PeerToPeerTrust) *PeerToPeerTrust { func PeerToPeerTrustFromV2(t *reputation.PeerToPeerTrust) *PeerToPeerTrust {
return (*PeerToPeerTrust)(t) return (*PeerToPeerTrust)(t)
} }
// ToV2 converts PeerToPeerTrust to NeoFS API v2 // ToV2 converts PeerToPeerTrust to NeoFS API v2
// reputation.PeerToPeerTrust message structure. // reputation.PeerToPeerTrust message structure.
//
// Nil PeerToPeerTrust converts to nil.
func (x *PeerToPeerTrust) ToV2() *reputation.PeerToPeerTrust { func (x *PeerToPeerTrust) ToV2() *reputation.PeerToPeerTrust {
return (*reputation.PeerToPeerTrust)(x) return (*reputation.PeerToPeerTrust)(x)
} }
@ -195,12 +203,16 @@ func NewGlobalTrust() *GlobalTrust {
// GlobalTrustFromV2 converts NeoFS API v2 // GlobalTrustFromV2 converts NeoFS API v2
// reputation.GlobalTrust message structure to GlobalTrust. // reputation.GlobalTrust message structure to GlobalTrust.
//
// Nil reputation.GlobalTrust converts to nil.
func GlobalTrustFromV2(t *reputation.GlobalTrust) *GlobalTrust { func GlobalTrustFromV2(t *reputation.GlobalTrust) *GlobalTrust {
return (*GlobalTrust)(t) return (*GlobalTrust)(t)
} }
// ToV2 converts GlobalTrust to NeoFS API v2 // ToV2 converts GlobalTrust to NeoFS API v2
// reputation.GlobalTrust message structure. // reputation.GlobalTrust message structure.
//
// Nil GlobalTrust converts to nil.
func (x *GlobalTrust) ToV2() *reputation.GlobalTrust { func (x *GlobalTrust) ToV2() *reputation.GlobalTrust {
return (*reputation.GlobalTrust)(x) return (*reputation.GlobalTrust)(x)
} }

View file

@ -6,6 +6,7 @@ import (
"github.com/nspcc-dev/neofs-api-go/pkg" "github.com/nspcc-dev/neofs-api-go/pkg"
"github.com/nspcc-dev/neofs-api-go/pkg/reputation" "github.com/nspcc-dev/neofs-api-go/pkg/reputation"
reputationtest "github.com/nspcc-dev/neofs-api-go/pkg/reputation/test" 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" reputationtestV2 "github.com/nspcc-dev/neofs-api-go/v2/reputation/test"
"github.com/stretchr/testify/require" "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())
})
}