[#168] container: Replace []*Attribute with []Attribute

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2022-03-11 12:13:56 +03:00 committed by Alex Vanin
parent cef9872b39
commit 98d4b5c926
4 changed files with 17 additions and 21 deletions

View file

@ -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
}
}

View file

@ -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) {

View file

@ -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)
}
}

View file

@ -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.