From 8b7a43386445563e6e5f9be79849dc1f1ac6b463 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Tue, 8 Jun 2021 15:30:27 +0300 Subject: [PATCH] [#302] pkg/container: Convert nil `Attribute` to nil message Document that `Attribute.ToV2` method returns `nil` when is called on `nil`. Add corresponding unit test. Signed-off-by: Pavel Karpy --- pkg/container/attribute.go | 7 +++++++ pkg/container/attribute_test.go | 15 +++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/pkg/container/attribute.go b/pkg/container/attribute.go index 6f72c4c..8964a04 100644 --- a/pkg/container/attribute.go +++ b/pkg/container/attribute.go @@ -29,10 +29,17 @@ func (a *Attribute) Value() string { return (*container.Attribute)(a).GetValue() } +// NewAttributeFromV2 wraps protocol dependent version of +// Attribute message. +// +// Nil container.Attribute converts to nil. func NewAttributeFromV2(v *container.Attribute) *Attribute { return (*Attribute)(v) } +// ToV2 converts Attribute to v2 Attribute message. +// +// Nil Attribute converts to nil. func (a *Attribute) ToV2() *container.Attribute { return (*container.Attribute)(a) } diff --git a/pkg/container/attribute_test.go b/pkg/container/attribute_test.go index a7a700d..ea23286 100644 --- a/pkg/container/attribute_test.go +++ b/pkg/container/attribute_test.go @@ -4,10 +4,17 @@ import ( "testing" "github.com/nspcc-dev/neofs-api-go/pkg/container" + containerv2 "github.com/nspcc-dev/neofs-api-go/v2/container" "github.com/stretchr/testify/require" ) func TestAttribute(t *testing.T) { + t.Run("nil", func(t *testing.T) { + var x *container.Attribute + + require.Nil(t, x.ToV2()) + }) + const ( key = "key" value = "value" @@ -82,3 +89,11 @@ func TestAttributes(t *testing.T) { } }) } + +func TestNewAttributeFromV2(t *testing.T) { + t.Run("from nil", func(t *testing.T) { + var x *containerv2.Attribute + + require.Nil(t, container.NewAttributeFromV2(x)) + }) +}