[#302] pkg/object: Convert nil Address to nil message

Document that `Address.ToV2` method return `nil`
when called on `nil`. Document that `NewAddressFromV2`
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:44:47 +03:00 committed by Alex Vanin
parent 2307c8f8f7
commit c978fa0e21
2 changed files with 21 additions and 0 deletions

View file

@ -19,6 +19,8 @@ const (
) )
// NewAddressFromV2 converts v2 Address message to Address. // NewAddressFromV2 converts v2 Address message to Address.
//
// Nil refs.Address converts to nil.
func NewAddressFromV2(aV2 *refs.Address) *Address { func NewAddressFromV2(aV2 *refs.Address) *Address {
return (*Address)(aV2) return (*Address)(aV2)
} }
@ -31,6 +33,8 @@ func NewAddress() *Address {
} }
// ToV2 converts Address to v2 Address message. // ToV2 converts Address to v2 Address message.
//
// Nil Address converts to nil.
func (a *Address) ToV2() *refs.Address { func (a *Address) ToV2() *refs.Address {
return (*refs.Address)(a) return (*refs.Address)(a)
} }

View file

@ -5,6 +5,7 @@ import (
"testing" "testing"
cidtest "github.com/nspcc-dev/neofs-api-go/pkg/container/id/test" cidtest "github.com/nspcc-dev/neofs-api-go/pkg/container/id/test"
"github.com/nspcc-dev/neofs-api-go/v2/refs"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -84,3 +85,19 @@ func TestAddressEncoding(t *testing.T) {
require.Equal(t, a, a2) require.Equal(t, a, a2)
}) })
} }
func TestNewAddressFromV2(t *testing.T) {
t.Run("from nil", func(t *testing.T) {
var x *refs.Address
require.Nil(t, NewAddressFromV2(x))
})
}
func TestAddress_ToV2(t *testing.T) {
t.Run("nil", func(t *testing.T) {
var x *Address
require.Nil(t, x.ToV2())
})
}