[#302] pkg/owner: Convert nil ID to nil message

Document that `ID.ToV2` method return `nil`
when called on `nil`. Document that `NewIDFromV2`
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:49:25 +03:00 committed by Alex Vanin
parent d802a994f5
commit 12574469e5
2 changed files with 21 additions and 0 deletions

View file

@ -16,6 +16,8 @@ type ID refs.OwnerID
var errInvalidIDString = errors.New("incorrect format of the string owner ID") var errInvalidIDString = errors.New("incorrect format of the string owner ID")
// NewIDFromV2 wraps v2 OwnerID message to ID. // NewIDFromV2 wraps v2 OwnerID message to ID.
//
// Nil refs.OwnerID converts to nil.
func NewIDFromV2(idV2 *refs.OwnerID) *ID { func NewIDFromV2(idV2 *refs.OwnerID) *ID {
return (*ID)(idV2) return (*ID)(idV2)
} }
@ -33,6 +35,8 @@ func (id *ID) SetNeo3Wallet(v *NEO3Wallet) {
} }
// ToV2 returns the v2 owner ID message. // ToV2 returns the v2 owner ID message.
//
// Nil ID converts to nil.
func (id *ID) ToV2() *refs.OwnerID { func (id *ID) ToV2() *refs.OwnerID {
return (*refs.OwnerID)(id) return (*refs.OwnerID)(id)
} }

View file

@ -7,6 +7,7 @@ import (
"github.com/mr-tron/base58" "github.com/mr-tron/base58"
. "github.com/nspcc-dev/neofs-api-go/pkg/owner" . "github.com/nspcc-dev/neofs-api-go/pkg/owner"
ownertest "github.com/nspcc-dev/neofs-api-go/pkg/owner/test" ownertest "github.com/nspcc-dev/neofs-api-go/pkg/owner/test"
"github.com/nspcc-dev/neofs-api-go/v2/refs"
"github.com/nspcc-dev/neofs-crypto/test" "github.com/nspcc-dev/neofs-crypto/test"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -99,3 +100,19 @@ func TestID_Equal(t *testing.T) {
ownertest.GenerateFromBytes(data3), ownertest.GenerateFromBytes(data3),
)) ))
} }
func TestNewIDFromV2(t *testing.T) {
t.Run("from nil", func(t *testing.T) {
var x *refs.OwnerID
require.Nil(t, NewIDFromV2(x))
})
}
func TestID_ToV2(t *testing.T) {
t.Run("nil", func(t *testing.T) {
var x *ID
require.Nil(t, x.ToV2())
})
}