[#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 ( type (
Attribute container.Attribute Attribute container.Attribute
Attributes []*Attribute Attributes []Attribute
) )
// NewAttribute creates and initializes blank Attribute. // NewAttribute creates and initializes blank Attribute.
@ -49,27 +49,27 @@ func (a *Attribute) ToV2() *container.Attribute {
return (*container.Attribute)(a) return (*container.Attribute)(a)
} }
func NewAttributesFromV2(v []*container.Attribute) Attributes { func NewAttributesFromV2(v []container.Attribute) Attributes {
if v == nil { if v == nil {
return nil return nil
} }
attrs := make(Attributes, 0, len(v)) attrs := make(Attributes, len(v))
for i := range v { for i := range v {
attrs = append(attrs, NewAttributeFromV2(v[i])) attrs[i] = *NewAttributeFromV2(&v[i])
} }
return attrs return attrs
} }
func (a Attributes) ToV2() []*container.Attribute { func (a Attributes) ToV2() []container.Attribute {
if a == nil { if a == nil {
return nil return nil
} }
attrs := make([]*container.Attribute, 0, len(a)) attrs := make([]container.Attribute, len(a))
for i := range a { for i := range a {
attrs = append(attrs, a[i].ToV2()) attrs[i] = *a[i].ToV2()
} }
return attrs return attrs
@ -91,7 +91,7 @@ func setAttribute(c *Container, key, value string) {
a = NewAttribute() a = NewAttribute()
a.SetKey(key) a.SetKey(key)
c.SetAttributes(append(c.Attributes(), a)) c.SetAttributes(append(c.Attributes(), *a))
} }
a.SetValue(value) a.SetValue(value)
@ -102,7 +102,7 @@ func setAttribute(c *Container, key, value string) {
// 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() { for _, a := range c.Attributes() {
if f(a) { if f(&a) {
return return
} }
} }

View file

@ -74,14 +74,11 @@ func TestAttributes(t *testing.T) {
vals = []string{"val1", "val2", "val3"} vals = []string{"val1", "val2", "val3"}
) )
attrs := make(container.Attributes, 0, len(keys)) attrs := make(container.Attributes, len(keys))
for i := range keys { for i := range keys {
attr := container.NewAttribute() attrs[i].SetKey(keys[i])
attr.SetKey(keys[i]) attrs[i].SetValue(vals[i])
attr.SetValue(vals[i])
attrs = append(attrs, attr)
} }
t.Run("test v2", func(t *testing.T) { 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 { func WithAttribute(key, value string) Option {
return func(option *containerOptions) { return func(option *containerOptions) {
attr := NewAttribute() index := len(option.attributes)
attr.SetKey(key) option.attributes = append(option.attributes, Attribute{})
attr.SetValue(value) option.attributes[index].SetKey(key)
option.attributes[index].SetValue(value)
option.attributes = append(option.attributes, attr)
} }
} }

View file

@ -20,7 +20,7 @@ func Attribute() *container.Attribute {
// Attributes returns random container.Attributes. // Attributes returns random container.Attributes.
func Attributes() container.Attributes { func Attributes() container.Attributes {
return container.Attributes{Attribute(), Attribute()} return container.Attributes{*Attribute(), *Attribute()}
} }
// Container returns random container.Container. // Container returns random container.Container.