[#168] container: Remove pointer tricks from setAttribute
Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
parent
98d4b5c926
commit
e89a0d88d2
1 changed files with 18 additions and 16 deletions
|
@ -77,32 +77,34 @@ func (a Attributes) ToV2() []container.Attribute {
|
||||||
|
|
||||||
// sets value of the attribute by key.
|
// sets value of the attribute by key.
|
||||||
func setAttribute(c *Container, key, value string) {
|
func setAttribute(c *Container, key, value string) {
|
||||||
var a *Attribute
|
attrs := c.Attributes()
|
||||||
|
found := false
|
||||||
|
|
||||||
iterateAttributes(c, func(a_ *Attribute) bool {
|
for i := range attrs {
|
||||||
if a_.Key() == key {
|
if attrs[i].Key() == key {
|
||||||
a = a_
|
attrs[i].SetValue(value)
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return a != nil
|
if !found {
|
||||||
})
|
index := len(attrs)
|
||||||
|
attrs = append(attrs, Attribute{})
|
||||||
if a == nil {
|
attrs[index].SetKey(key)
|
||||||
a = NewAttribute()
|
attrs[index].SetValue(value)
|
||||||
a.SetKey(key)
|
|
||||||
|
|
||||||
c.SetAttributes(append(c.Attributes(), *a))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
a.SetValue(value)
|
c.SetAttributes(attrs)
|
||||||
}
|
}
|
||||||
|
|
||||||
// iterates over container attributes. Stops at f's true return.
|
// iterates over container attributes. Stops at f's true return.
|
||||||
//
|
//
|
||||||
// Handler must not be nil.
|
// Handler must not be nil.
|
||||||
func iterateAttributes(c *Container, f func(*Attribute) bool) {
|
func iterateAttributes(c *Container, f func(*Attribute) bool) {
|
||||||
for _, a := range c.Attributes() {
|
attrs := c.Attributes()
|
||||||
if f(&a) {
|
for i := range attrs {
|
||||||
|
if f(&attrs[i]) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue