[#302] pkg/object: 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 18:58:12 +03:00 committed by Alex Vanin
parent ff851215b0
commit 245a55f715
2 changed files with 21 additions and 0 deletions

View file

@ -16,6 +16,8 @@ type ID refs.ObjectID
var errInvalidIDString = errors.New("incorrect format of the string object ID")
// NewIDFromV2 wraps v2 ObjectID message to ID.
//
// Nil refs.ObjectID converts to nil.
func NewIDFromV2(idV2 *refs.ObjectID) *ID {
return (*ID)(idV2)
}
@ -41,6 +43,8 @@ func (id *ID) Equal(id2 *ID) bool {
}
// ToV2 converts ID to v2 ObjectID message.
//
// Nil ID converts to nil.
func (id *ID) ToV2() *refs.ObjectID {
return (*refs.ObjectID)(id)
}

View file

@ -7,6 +7,7 @@ import (
"testing"
"github.com/mr-tron/base58"
"github.com/nspcc-dev/neofs-api-go/v2/refs"
"github.com/stretchr/testify/require"
)
@ -107,3 +108,19 @@ func TestObjectIDEncoding(t *testing.T) {
require.Equal(t, id, a2)
})
}
func TestNewIDFromV2(t *testing.T) {
t.Run("from nil", func(t *testing.T) {
var x *refs.ObjectID
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())
})
}