diff --git a/pkg/util/attributes/parser.go b/pkg/util/attributes/parser.go index cef735985..340443bff 100644 --- a/pkg/util/attributes/parser.go +++ b/pkg/util/attributes/parser.go @@ -28,6 +28,7 @@ func ParseV2Attributes(attrs []string, excl []string) ([]*netmap.NodeAttribute, } cache := make(map[string]*netmap.NodeAttribute, len(attrs)) + result := make([]*netmap.NodeAttribute, 0, len(attrs)) for i := range attrs { line := strings.Trim(attrs[i], pairSeparator) @@ -67,6 +68,7 @@ func ParseV2Attributes(attrs []string, excl []string) ([]*netmap.NodeAttribute, attribute.SetValue(value) cache[key] = attribute + result = append(result, attribute) } if parentKey != "" { @@ -80,11 +82,6 @@ func ParseV2Attributes(attrs []string, excl []string) ([]*netmap.NodeAttribute, } } - result := make([]*netmap.NodeAttribute, 0, len(cache)) - for _, v := range cache { - result = append(result, v) - } - return result, nil } diff --git a/pkg/util/attributes/parser_test.go b/pkg/util/attributes/parser_test.go index 305f55830..c1370332d 100644 --- a/pkg/util/attributes/parser_test.go +++ b/pkg/util/attributes/parser_test.go @@ -94,4 +94,20 @@ func TestParseV2Attributes(t *testing.T) { } require.True(t, flag) }) + + t.Run("consistent order in chain", func(t *testing.T) { + from := []string{"/a:1/b:2/c:3"} + + for i := 0; i < 10000; i++ { + attrs, err := attributes.ParseV2Attributes(from, nil) + require.NoError(t, err) + + require.Equal(t, attrs[0].Key(), "a") + require.Equal(t, attrs[0].Value(), "1") + require.Equal(t, attrs[1].Key(), "b") + require.Equal(t, attrs[1].Value(), "2") + require.Equal(t, attrs[2].Key(), "c") + require.Equal(t, attrs[2].Value(), "3") + } + }) }