Skip to content

Commit

Permalink
Merge pull request #159 from nokia/sort-keys
Browse files Browse the repository at this point in the history
sort keys by name when converting a gNMI path to XPATH
  • Loading branch information
karimra authored Jun 28, 2023
2 parents 3d28cc5 + 61679e6 commit 3e39a32
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"log"
"net"
"reflect"
"sort"
"strings"

"github.com/openconfig/gnmi/proto/gnmi"
Expand Down Expand Up @@ -57,22 +58,33 @@ func GnmiPathToXPath(p *gnmi.Path, noKeys bool) string {
if p == nil {
return ""
}
sb := strings.Builder{}
sb := &strings.Builder{}
if p.Origin != "" {
sb.WriteString(p.Origin)
sb.WriteString(":")
}
elems := p.GetElem()
numElems := len(elems)

for i, pe := range elems {
sb.WriteString(pe.GetName())
if !noKeys {
for k, v := range pe.GetKey() {
sb.WriteString("[")
sb.WriteString(k)
sb.WriteString("=")
sb.WriteString(v)
sb.WriteString("]")
numKeys := len(pe.GetKey())
switch numKeys {
case 0:
case 1:
for k := range pe.GetKey() {
writeKey(sb, k, pe.GetKey()[k])
}
default:
keys := make([]string, 0, numKeys)
for k := range pe.GetKey() {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
writeKey(sb, k, pe.GetKey()[k])
}
}
}
if i+1 != numElems {
Expand All @@ -82,6 +94,14 @@ func GnmiPathToXPath(p *gnmi.Path, noKeys bool) string {
return sb.String()
}

func writeKey(sb *strings.Builder, k, v string) {
sb.WriteString("[")
sb.WriteString(k)
sb.WriteString("=")
sb.WriteString(v)
sb.WriteString("]")
}

func GetHost(hostport string) string {
h, _, err := net.SplitHostPort(hostport)
if err != nil {
Expand Down

0 comments on commit 3e39a32

Please sign in to comment.