[#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 <carpawell@nspcc.ru>
remotes/KirillovDenis/feature/refactor-sig-rpc
Pavel Karpy 2021-06-08 15:30:27 +03:00 committed by Alex Vanin
parent 1863694b96
commit 8b7a433864
2 changed files with 22 additions and 0 deletions

View File

@ -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)
}

View File

@ -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))
})
}