[#302] pkg/reputation: Convert nil PeerID to nil message

Document that `PeerID.ToV2` method return `nil`
when called on `nil`. Document that `PeerIDFromV2`
function 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 19:55:32 +03:00 committed by Alex Vanin
parent c00cdd407d
commit d4327ec018
2 changed files with 23 additions and 2 deletions

View file

@ -17,6 +17,8 @@ func NewPeerID() *PeerID {
}
// PeerIDFromV2 converts NeoFS API v2 reputation.PeerID message to PeerID.
//
// Nil reputation.PeerID converts to nil.
func PeerIDFromV2(id *reputation.PeerID) *PeerID {
return (*PeerID)(id)
}
@ -28,6 +30,8 @@ func (x *PeerID) SetPublicKey(v [crypto.PublicKeyCompressedSize]byte) {
}
// ToV2 converts PeerID to NeoFS API v2 reputation.PeerID message.
//
// Nil PeerID converts to nil.
func (x *PeerID) ToV2() *reputation.PeerID {
return (*reputation.PeerID)(x)
}

View file

@ -5,13 +5,22 @@ import (
"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"
"github.com/stretchr/testify/require"
)
func TestPeerID_ToV2(t *testing.T) {
peerID := reputationtest.GeneratePeerID()
t.Run("nil", func(t *testing.T) {
var x *reputation.PeerID
require.Equal(t, peerID, reputation.PeerIDFromV2(peerID.ToV2()))
require.Nil(t, x.ToV2())
})
t.Run("nil", func(t *testing.T) {
peerID := reputationtest.GeneratePeerID()
require.Equal(t, peerID, reputation.PeerIDFromV2(peerID.ToV2()))
})
}
func TestPeerID_String(t *testing.T) {
@ -50,3 +59,11 @@ func TestPeerIDEncoding(t *testing.T) {
require.Equal(t, id, id2)
})
}
func TestPeerIDFromV2(t *testing.T) {
t.Run("from nil", func(t *testing.T) {
var x *reputationV2.PeerID
require.Nil(t, reputation.PeerIDFromV2(x))
})
}