Skip to content
This repository was archived by the owner on Dec 27, 2024. It is now read-only.

Commit

Permalink
[#351] pkg/container: Implement functions to work with native names
Browse files Browse the repository at this point in the history
Implement `GetNativeNameWithZone` / `SetNativeNameWithZone` function which
gets / sets `__NEOFS_NAME` and `__NEOFS_ZONE` container attributes.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
  • Loading branch information
Leonard Lyubich committed Oct 11, 2021
1 parent fba67c6 commit 7e195a0
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
54 changes: 54 additions & 0 deletions pkg/container/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,57 @@ func (a Attributes) ToV2() []*container.Attribute {

return attrs
}

// sets value of the attribute by key.
func setAttribute(c *Container, key, value string) {
var a *Attribute

iterateAttributes(c, func(a_ *Attribute) bool {
if a_.Key() == key {
a = a_
}

return a != nil
})

if a == nil {
a = NewAttribute()
a.SetKey(key)

c.SetAttributes(append(c.Attributes(), a))
}

a.SetValue(value)
}

// 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) {
return
}
}
}

// SetNativeNameWithZone sets container native name and its zone.
func SetNativeNameWithZone(c *Container, name, zone string) {
setAttribute(c, container.SysAttributeName, name)
setAttribute(c, container.SysAttributeZone, zone)
}

// GetNameWithZone returns container native name and its zone.
func GetNativeNameWithZone(c *Container) (name string, zone string) {
iterateAttributes(c, func(a *Attribute) bool {
if key := a.Key(); key == container.SysAttributeName {
name = a.Value()
} else if key == container.SysAttributeZone {
zone = a.Value()
}

return name != "" && zone != ""
})

return
}
22 changes: 22 additions & 0 deletions pkg/container/attribute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,25 @@ func TestNewAttributeFromV2(t *testing.T) {
require.Nil(t, container.NewAttributeFromV2(x))
})
}

func TestGetNameWithZone(t *testing.T) {
c := container.New()

for _, item := range [...]struct {
name, zone string
}{
{"name1", ""},
{"name1", "zone1"},
{"name2", "zone1"},
{"name2", "zone2"},
{"", "zone2"},
{"", ""},
} {
container.SetNativeNameWithZone(c, item.name, item.zone)

name, zone := container.GetNativeNameWithZone(c)

require.Equal(t, item.name, name, item.name)
require.Equal(t, item.zone, zone, item.zone)
}
}

0 comments on commit 7e195a0

Please sign in to comment.