From e89a0d88d2cbdc17807ff604dfbed73f52da5991 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Fri, 11 Mar 2022 12:59:23 +0300 Subject: [PATCH] [#168] container: Remove pointer tricks from `setAttribute` Signed-off-by: Alex Vanin --- container/attribute.go | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/container/attribute.go b/container/attribute.go index 0d1c42f..1fe4233 100644 --- a/container/attribute.go +++ b/container/attribute.go @@ -77,32 +77,34 @@ func (a Attributes) ToV2() []container.Attribute { // sets value of the attribute by key. func setAttribute(c *Container, key, value string) { - var a *Attribute + attrs := c.Attributes() + found := false - iterateAttributes(c, func(a_ *Attribute) bool { - if a_.Key() == key { - a = a_ + for i := range attrs { + if attrs[i].Key() == key { + attrs[i].SetValue(value) + found = true + break } - - return a != nil - }) - - if a == nil { - a = NewAttribute() - a.SetKey(key) - - c.SetAttributes(append(c.Attributes(), *a)) } - a.SetValue(value) + if !found { + index := len(attrs) + attrs = append(attrs, Attribute{}) + attrs[index].SetKey(key) + attrs[index].SetValue(value) + } + + c.SetAttributes(attrs) } // iterates over container attributes. Stops at f's true return. // // Handler must not be nil. func iterateAttributes(c *Container, f func(*Attribute) bool) { - for _, a := range c.Attributes() { - if f(&a) { + attrs := c.Attributes() + for i := range attrs { + if f(&attrs[i]) { return } }