[#302] pkg/container: Convert nil Attributes to nil message

Make `Attributes.ToV2` method to return `nil`
when called on `nil`. Make `NewAttributesFromV2`
function to 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 16:29:20 +03:00 committed by Alex Vanin
parent f92f9cd424
commit 0206bd9a5d
3 changed files with 18 additions and 2 deletions

View file

@ -50,6 +50,10 @@ func (a *Attribute) ToV2() *container.Attribute {
} }
func NewAttributesFromV2(v []*container.Attribute) Attributes { func NewAttributesFromV2(v []*container.Attribute) Attributes {
if v == nil {
return nil
}
attrs := make(Attributes, 0, len(v)) attrs := make(Attributes, 0, len(v))
for i := range v { for i := range v {
attrs = append(attrs, NewAttributeFromV2(v[i])) attrs = append(attrs, NewAttributeFromV2(v[i]))
@ -59,6 +63,10 @@ func NewAttributesFromV2(v []*container.Attribute) Attributes {
} }
func (a Attributes) ToV2() []*container.Attribute { func (a Attributes) ToV2() []*container.Attribute {
if a == nil {
return nil
}
attrs := make([]*container.Attribute, 0, len(a)) attrs := make([]*container.Attribute, 0, len(a))
for i := range a { for i := range a {
attrs = append(attrs, a[i].ToV2()) attrs = append(attrs, a[i].ToV2())

View file

@ -61,6 +61,14 @@ func TestAttribute(t *testing.T) {
} }
func TestAttributes(t *testing.T) { func TestAttributes(t *testing.T) {
t.Run("nil", func(t *testing.T) {
var x container.Attributes
require.Nil(t, x.ToV2())
require.Nil(t, container.NewAttributesFromV2(nil))
})
var ( var (
keys = []string{"key1", "key2", "key3"} keys = []string{"key1", "key2", "key3"}
vals = []string{"val1", "val2", "val3"} vals = []string{"val1", "val2", "val3"}

View file

@ -28,8 +28,8 @@ func defaultContainerOptions() containerOptions {
} }
return containerOptions{ return containerOptions{
acl: acl.PrivateBasicRule, acl: acl.PrivateBasicRule,
nonce: rand, nonce: rand,
} }
} }