Skip to content

Commit

Permalink
identities that have a base identity are not sorted Fixes #234 (#235)
Browse files Browse the repository at this point in the history
* identities that have a base identity are not sorted Fixes #234

* updating after review comments
  • Loading branch information
SeanCondon authored Jul 20, 2022
1 parent ee6fd6d commit 125c5f3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
4 changes: 4 additions & 0 deletions pkg/yang/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package yang

import (
"fmt"
"sort"
"sync"
)

Expand Down Expand Up @@ -179,6 +180,9 @@ func (ms *Modules) resolveIdentities() []error {
for _, j := range i.Identity.Values {
newValues = addChildren(j, newValues)
}
sort.SliceStable(newValues, func(j, k int) bool {
return newValues[j].Name < newValues[k].Name
})
i.Identity.Values = newValues
}

Expand Down
19 changes: 13 additions & 6 deletions pkg/yang/identity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,19 +388,19 @@ var treeTestCases = []identityTestCase{
module: "base",
name: "GREATGRANDFATHER",
values: []string{
"BROTHER", // Order is alphabetical
"FATHER",
"GRANDFATHER",
"GREATUNCLE",
"FATHER",
"UNCLE",
"SON",
"BROTHER",
"UNCLE",
},
},
{
module: "base",
name: "GRANDFATHER",
baseNames: []string{"GREATGRANDFATHER"},
values: []string{"FATHER", "UNCLE", "SON", "BROTHER"},
values: []string{"BROTHER", "FATHER", "SON", "UNCLE"},
},
{
module: "base",
Expand All @@ -411,7 +411,7 @@ var treeTestCases = []identityTestCase{
module: "base",
name: "FATHER",
baseNames: []string{"GRANDFATHER"},
values: []string{"SON", "BROTHER"},
values: []string{"BROTHER", "SON"},
},
{
module: "base",
Expand All @@ -426,6 +426,7 @@ var treeTestCases = []identityTestCase{
},
},
{
name: "tree-test-case-3",
in: []inputModule{
{
name: "base.yang",
Expand Down Expand Up @@ -468,6 +469,7 @@ var treeTestCases = []identityTestCase{
},
},
{
name: "tree-test-case-4",
in: []inputModule{
{
name: "base.yang",
Expand Down Expand Up @@ -514,6 +516,7 @@ var treeTestCases = []identityTestCase{
},
},
{
name: "tree-test-case-5",
in: []inputModule{
{
name: "base.yang",
Expand Down Expand Up @@ -717,13 +720,17 @@ func TestIdentityTree(t *testing.T) {

valueMap := make(map[string]bool)

for _, val := range chkID.values {
for i, val := range chkID.values {
valueMap[val] = false
// Check that IsDefined returns the right result
if !foundID.IsDefined(val) {
t.Errorf("Couldn't find defined value %s for %s", val, chkID.name)
}

// Check that the values are sorted in a consistent order
if foundID.Values[i].Name != val {
t.Errorf("Invalid order for value #%d. Expecting %s Got %s", i, foundID.Values[i].Name, val)
}
// Check that GetValue returns the right Identity
idval := foundID.GetValue(val)
if idval == nil {
Expand Down

0 comments on commit 125c5f3

Please sign in to comment.