From 941b513eb3d8b247c5a31cf96b60a71349b5904a Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Tue, 8 Jun 2021 18:50:45 +0300 Subject: [PATCH] [#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 --- pkg/object/attribute.go | 4 ++++ pkg/object/attribute_test.go | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/pkg/object/attribute.go b/pkg/object/attribute.go index cbc318d..23be55d 100644 --- a/pkg/object/attribute.go +++ b/pkg/object/attribute.go @@ -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) } diff --git a/pkg/object/attribute_test.go b/pkg/object/attribute_test.go index a4ab8ae..05735b7 100644 --- a/pkg/object/attribute_test.go +++ b/pkg/object/attribute_test.go @@ -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()) + }) +}