[#345] netmap: Implement an iterator over node attributes
Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
parent
b27f172de9
commit
4b9b54a901
2 changed files with 54 additions and 0 deletions
|
@ -408,8 +408,21 @@ func (x NodeInfo) NumberOfAttributes() int {
|
||||||
return len(x.m.GetAttributes())
|
return len(x.m.GetAttributes())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Attributes returns an iterator over node attributes.
|
||||||
|
func (x NodeInfo) Attributes() iter.Seq2[string, string] {
|
||||||
|
return func(yield func(string, string) bool) {
|
||||||
|
a := x.m.GetAttributes()
|
||||||
|
for i := range a {
|
||||||
|
if !yield(a[i].GetKey(), a[i].GetValue()) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// IterateAttributes iterates over all node attributes and passes the into f.
|
// IterateAttributes iterates over all node attributes and passes the into f.
|
||||||
// Handler MUST NOT be nil.
|
// Handler MUST NOT be nil.
|
||||||
|
// Deprecated: use [NodeInfo.Attributes] instead.
|
||||||
func (x NodeInfo) IterateAttributes(f func(key, value string)) {
|
func (x NodeInfo) IterateAttributes(f func(key, value string)) {
|
||||||
a := x.m.GetAttributes()
|
a := x.m.GetAttributes()
|
||||||
for i := range a {
|
for i := range a {
|
||||||
|
|
|
@ -46,6 +46,47 @@ func TestNodeInfo_NetworkEndpoints(t *testing.T) {
|
||||||
require.Equal(t, []string{"1", "2", "3"}, res)
|
require.Equal(t, []string{"1", "2", "3"}, res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNodeInfo_Attributes(t *testing.T) {
|
||||||
|
t.Run("empty", func(t *testing.T) {
|
||||||
|
var n NodeInfo
|
||||||
|
for range n.Attributes() {
|
||||||
|
t.Fatalf("handler is called, but it shouldn't")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
var n NodeInfo
|
||||||
|
n.SetAttribute("key1", "value1")
|
||||||
|
n.SetAttribute("key2", "value2")
|
||||||
|
n.SetAttribute("key3", "value3")
|
||||||
|
|
||||||
|
t.Run("break", func(t *testing.T) {
|
||||||
|
var res [][2]string
|
||||||
|
for k, v := range n.Attributes() {
|
||||||
|
if k == "key2" {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
res = append(res, [2]string{k, v})
|
||||||
|
}
|
||||||
|
require.Equal(t, [][2]string{{"key1", "value1"}}, res)
|
||||||
|
})
|
||||||
|
t.Run("continue", func(t *testing.T) {
|
||||||
|
var res [][2]string
|
||||||
|
for k, v := range n.Attributes() {
|
||||||
|
if k == "key2" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
res = append(res, [2]string{k, v})
|
||||||
|
}
|
||||||
|
require.Equal(t, [][2]string{{"key1", "value1"}, {"key3", "value3"}}, res)
|
||||||
|
})
|
||||||
|
|
||||||
|
var res [][2]string
|
||||||
|
for k, v := range n.Attributes() {
|
||||||
|
res = append(res, [2]string{k, v})
|
||||||
|
}
|
||||||
|
require.Equal(t, [][2]string{{"key1", "value1"}, {"key2", "value2"}, {"key3", "value3"}}, res)
|
||||||
|
}
|
||||||
|
|
||||||
func TestNodeInfo_SetAttribute(t *testing.T) {
|
func TestNodeInfo_SetAttribute(t *testing.T) {
|
||||||
var n NodeInfo
|
var n NodeInfo
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue