[#352] container: Implement iterators over attributes

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2025-04-05 07:51:32 +03:00 committed by Evgenii Stratonikov
parent 88bc9eeb26
commit a0e4d16dbb
3 changed files with 133 additions and 7 deletions

View file

@ -5,6 +5,7 @@ import (
"crypto/sha256"
"errors"
"fmt"
"iter"
"strconv"
"strings"
"time"
@ -337,10 +338,41 @@ func (x Container) Attribute(key string) string {
return ""
}
// Attributes returns an iterator over all Container attributes.
//
// See also [Container.SetAttribute], [Container.UserAttributes].
func (x Container) Attributes() iter.Seq2[string, string] {
return func(yield func(string, string) bool) {
attrs := x.v2.GetAttributes()
for i := range attrs {
if !yield(attrs[i].GetKey(), attrs[i].GetValue()) {
return
}
}
}
}
// Attributes returns an iterator over all non-system Container attributes.
//
// See also [Container.SetAttribute], [Container.Attributes].
func (x Container) UserAttributes() iter.Seq2[string, string] {
return func(yield func(string, string) bool) {
for key, value := range x.Attributes() {
if !strings.HasPrefix(key, container.SysAttributePrefix) {
if !yield(key, value) {
return
}
}
}
}
}
// IterateAttributes iterates over all Container attributes and passes them
// into f. The handler MUST NOT be nil.
//
// See also SetAttribute, Attribute.
//
// Deprecated: use [Container.Attributes] instead.
func (x Container) IterateAttributes(f func(key, val string)) {
attrs := x.v2.GetAttributes()
for i := range attrs {
@ -352,6 +384,8 @@ func (x Container) IterateAttributes(f func(key, val string)) {
// into f. The handler MUST NOT be nil.
//
// See also SetAttribute, Attribute.
//
// Deprecated: use [Container.UserAttributes] instead.
func (x Container) IterateUserAttributes(f func(key, val string)) {
attrs := x.v2.GetAttributes()
for _, attr := range attrs {