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

Document that `Attribute.ToV2` method return `nil`
when called on `nil`. Document that `NewAttributeFromV2`
function return `nil` when called on `nil`. Write
corresponding unit tests.

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
remotes/KirillovDenis/feature/refactor-sig-rpc
Pavel Karpy 2021-06-08 18:50:45 +03:00 committed by Alex Vanin
parent 5fda8ef796
commit 941b513eb3
2 changed files with 21 additions and 0 deletions

View File

@ -8,6 +8,8 @@ import (
type Attribute object.Attribute
// NewAttributeFromV2 wraps v2 Attribute message to Attribute.
//
// Nil object.Attribute converts to nil.
func NewAttributeFromV2(aV2 *object.Attribute) *Attribute {
return (*Attribute)(aV2)
}
@ -40,6 +42,8 @@ func (a *Attribute) SetValue(v string) {
}
// ToV2 converts Attribute to v2 Attribute message.
//
// Nil Attribute converts to nil.
func (a *Attribute) ToV2() *object.Attribute {
return (*object.Attribute)(a)
}

View File

@ -3,6 +3,7 @@ package object
import (
"testing"
"github.com/nspcc-dev/neofs-api-go/v2/object"
"github.com/stretchr/testify/require"
)
@ -47,3 +48,19 @@ func TestAttributeEncoding(t *testing.T) {
require.Equal(t, a, a2)
})
}
func TestNewAttributeFromV2(t *testing.T) {
t.Run("from nil", func(t *testing.T) {
var x *object.Attribute
require.Nil(t, NewAttributeFromV2(x))
})
}
func TestAttribute_ToV2(t *testing.T) {
t.Run("nil", func(t *testing.T) {
var x *Attribute
require.Nil(t, x.ToV2())
})
}