Skip to content

Commit eac47f5

Browse files
retain order of keys for containers
1 parent 34001be commit eac47f5

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

pkg/schema/container.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
package schema
1616

1717
import (
18-
"sort"
18+
"cmp"
19+
"slices"
1920
"strings"
2021

2122
"github.com/openconfig/goyang/pkg/yang"
@@ -55,10 +56,7 @@ func (sc *Schema) containerFromYEntry(e *yang.Entry, withDesc bool) (*sdcpb.Cont
5556
}
5657
}
5758

58-
keys := map[string]struct{}{}
59-
for _, key := range strings.Fields(e.Key) {
60-
keys[key] = struct{}{}
61-
}
59+
keys := strings.Fields(e.Key)
6260
for _, child := range getChildren(e) {
6361
switch {
6462
case child.IsDir():
@@ -70,7 +68,7 @@ func (sc *Schema) containerFromYEntry(e *yang.Entry, withDesc bool) (*sdcpb.Cont
7068
}
7169
switch o := o.Schema.(type) {
7270
case *sdcpb.SchemaElem_Field:
73-
if _, ok := keys[child.Name]; ok {
71+
if slices.Index(keys, child.Name) != -1 {
7472
c.Keys = append(c.Keys, o.Field)
7573
continue
7674
}
@@ -80,9 +78,10 @@ func (sc *Schema) containerFromYEntry(e *yang.Entry, withDesc bool) (*sdcpb.Cont
8078
}
8179
}
8280
}
83-
sort.Strings(c.Children)
84-
sort.Slice(c.Keys, func(i, j int) bool {
85-
return c.Keys[i].GetName() < c.Keys[j].GetName()
81+
slices.Sort(c.Children)
82+
// keep ordering based on schema
83+
slices.SortFunc(c.Keys, func(a, b *sdcpb.LeafSchema) int {
84+
return cmp.Compare(slices.Index(keys, a.GetName()), slices.Index(keys, b.GetName()))
8685
})
8786
return c, nil
8887
}

0 commit comments

Comments
 (0)