From 98d4b5c9261c87cb6f6b5e7d233057f410758e79 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Fri, 11 Mar 2022 12:13:56 +0300 Subject: [PATCH] [#168] container: Replace []*Attribute with []Attribute Signed-off-by: Alex Vanin --- container/attribute.go | 18 +++++++++--------- container/attribute_test.go | 9 +++------ container/opts.go | 9 ++++----- container/test/generate.go | 2 +- 4 files changed, 17 insertions(+), 21 deletions(-) diff --git a/container/attribute.go b/container/attribute.go index fc87062..0d1c42f 100644 --- a/container/attribute.go +++ b/container/attribute.go @@ -6,7 +6,7 @@ import ( type ( Attribute container.Attribute - Attributes []*Attribute + Attributes []Attribute ) // NewAttribute creates and initializes blank Attribute. @@ -49,27 +49,27 @@ func (a *Attribute) ToV2() *container.Attribute { return (*container.Attribute)(a) } -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, len(v)) for i := range v { - attrs = append(attrs, NewAttributeFromV2(v[i])) + attrs[i] = *NewAttributeFromV2(&v[i]) } return attrs } -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, len(a)) for i := range a { - attrs = append(attrs, a[i].ToV2()) + attrs[i] = *a[i].ToV2() } return attrs @@ -91,7 +91,7 @@ func setAttribute(c *Container, key, value string) { a = NewAttribute() a.SetKey(key) - c.SetAttributes(append(c.Attributes(), a)) + c.SetAttributes(append(c.Attributes(), *a)) } a.SetValue(value) @@ -102,7 +102,7 @@ func setAttribute(c *Container, key, value string) { // Handler must not be nil. func iterateAttributes(c *Container, f func(*Attribute) bool) { for _, a := range c.Attributes() { - if f(a) { + if f(&a) { return } } diff --git a/container/attribute_test.go b/container/attribute_test.go index b4278f4..69b02ab 100644 --- a/container/attribute_test.go +++ b/container/attribute_test.go @@ -74,14 +74,11 @@ func TestAttributes(t *testing.T) { vals = []string{"val1", "val2", "val3"} ) - attrs := make(container.Attributes, 0, len(keys)) + attrs := make(container.Attributes, len(keys)) for i := range keys { - attr := container.NewAttribute() - attr.SetKey(keys[i]) - attr.SetValue(vals[i]) - - attrs = append(attrs, attr) + attrs[i].SetKey(keys[i]) + attrs[i].SetValue(vals[i]) } t.Run("test v2", func(t *testing.T) { diff --git a/container/opts.go b/container/opts.go index af456fa..d6c589b 100644 --- a/container/opts.go +++ b/container/opts.go @@ -81,10 +81,9 @@ func WithPolicy(policy *netmap.PlacementPolicy) Option { func WithAttribute(key, value string) Option { return func(option *containerOptions) { - attr := NewAttribute() - attr.SetKey(key) - attr.SetValue(value) - - option.attributes = append(option.attributes, attr) + index := len(option.attributes) + option.attributes = append(option.attributes, Attribute{}) + option.attributes[index].SetKey(key) + option.attributes[index].SetValue(value) } } diff --git a/container/test/generate.go b/container/test/generate.go index cf2aed2..f16f705 100644 --- a/container/test/generate.go +++ b/container/test/generate.go @@ -20,7 +20,7 @@ func Attribute() *container.Attribute { // Attributes returns random container.Attributes. func Attributes() container.Attributes { - return container.Attributes{Attribute(), Attribute()} + return container.Attributes{*Attribute(), *Attribute()} } // Container returns random container.Container.