From 0206bd9a5d9eba68a227eb8265479c41d45558b2 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Tue, 8 Jun 2021 16:29:20 +0300 Subject: [PATCH] [#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 --- pkg/container/attribute.go | 8 ++++++++ pkg/container/attribute_test.go | 8 ++++++++ pkg/container/opts.go | 4 ++-- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/pkg/container/attribute.go b/pkg/container/attribute.go index 2e4492fe..ec633e8a 100644 --- a/pkg/container/attribute.go +++ b/pkg/container/attribute.go @@ -50,6 +50,10 @@ func (a *Attribute) ToV2() *container.Attribute { } func NewAttributesFromV2(v []*container.Attribute) Attributes { + if v == nil { + return nil + } + attrs := make(Attributes, 0, len(v)) for i := range v { attrs = append(attrs, NewAttributeFromV2(v[i])) @@ -59,6 +63,10 @@ func NewAttributesFromV2(v []*container.Attribute) Attributes { } func (a Attributes) ToV2() []*container.Attribute { + if a == nil { + return nil + } + attrs := make([]*container.Attribute, 0, len(a)) for i := range a { attrs = append(attrs, a[i].ToV2()) diff --git a/pkg/container/attribute_test.go b/pkg/container/attribute_test.go index e32dfda6..acd7bbd0 100644 --- a/pkg/container/attribute_test.go +++ b/pkg/container/attribute_test.go @@ -61,6 +61,14 @@ func TestAttribute(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 ( keys = []string{"key1", "key2", "key3"} vals = []string{"val1", "val2", "val3"} diff --git a/pkg/container/opts.go b/pkg/container/opts.go index d07b6846..8c15ed1f 100644 --- a/pkg/container/opts.go +++ b/pkg/container/opts.go @@ -28,8 +28,8 @@ func defaultContainerOptions() containerOptions { } return containerOptions{ - acl: acl.PrivateBasicRule, - nonce: rand, + acl: acl.PrivateBasicRule, + nonce: rand, } }