-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fixes #issue 42. add num for each key and sort keys by num. make sure keys in map can be sorted. #50
base: master
Are you sure you want to change the base?
fixes #issue 42. add num for each key and sort keys by num. make sure keys in map can be sorted. #50
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
--- ./github.com/jipanyang/gnxi/utils/xpath/xpath.go 2022-10-25 16:16:00.239783329 +0800 | ||
+++ ./github.com/jipanyang/gnxi/utils/xpath/xpath.go 2022-10-25 15:56:18.336168598 +0800 | ||
@@ -20,6 +20,7 @@ | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
+ "strconv" | ||
) | ||
|
||
var ( | ||
@@ -135,9 +136,10 @@ | ||
// For example, given | ||
// "[k1=v1][k2=v2]", | ||
// this API returns | ||
-// map[string]string{"k1": "v1", "k2": "v2"}. | ||
+// map[string]string{"[0]k1": "v1", "[1]k2": "v2"}. | ||
func parseKeyValueString(str string) (map[string]string, error) { | ||
keyValuePairs := make(map[string]string) | ||
+ var num int = 0 | ||
// begin marks the beginning of a key-value pair. | ||
begin := 0 | ||
// end marks the end of a key-value pair. | ||
@@ -170,7 +172,10 @@ | ||
// Recover escaped '[' and ']'. | ||
val = strings.Replace(val, `\]`, `]`, -1) | ||
val = strings.Replace(val, `\[`, `[`, -1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good find! |
||
- keyValuePairs[key] = val | ||
+// keyValuePairs[key] = val | ||
+ key_with_num := "[" + strconv.Itoa(num) + "]" + key | ||
+ keyValuePairs[key_with_num] = val | ||
+ num++ | ||
begin = end + 1 | ||
} | ||
end++ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,8 @@ import ( | |
"context" | ||
"log/syslog" | ||
"github.com/Azure/sonic-mgmt-common/translib/tlerr" | ||
|
||
"strconv" | ||
"sort" | ||
) | ||
|
||
var ( | ||
|
@@ -80,6 +81,9 @@ func ConvertToURI(prefix *gnmipb.Path, path *gnmipb.Path, req *string) error { | |
elems := fullPath.GetElem() | ||
*req = "/" | ||
|
||
num_key_map := make(map[int]string) | ||
key_val_map := make(map[string]string) | ||
|
||
if elems != nil { | ||
/* Iterate through elements. */ | ||
for i, elem := range elems { | ||
|
@@ -94,8 +98,32 @@ func ConvertToURI(prefix *gnmipb.Path, path *gnmipb.Path, req *string) error { | |
/* If keys are present , process the keys. */ | ||
if key != nil { | ||
for k, v := range key { | ||
log.V(6).Infof("elem : %#v %#v", k, v) | ||
*req += "[" + k + "=" + v + "]" | ||
//log.V(6).Infof("elem : %#v %#v", k, v) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove commented lines. |
||
//*req += "[" + k + "=" + v + "]" | ||
num_str := k[strings.Index(k, "[") + 1 : strings.Index(k, "]")] | ||
log.V(6).Infof("num : %#v", num_str) | ||
key_del_num := k[strings.Index(k, "]") + 1 : len(k)] | ||
log.V(6).Infof("elem : %#v %#v", key_del_num, v) | ||
num, err := strconv.Atoi(num_str) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You may remove these 2 log lines. Later log at 115 & 116 should be sufficient. |
||
if err != nil { | ||
return err | ||
} | ||
num_key_map[num] = key_del_num | ||
key_val_map[key_del_num] = v | ||
} | ||
|
||
log.V(6).Infof("num_key_map : %#v", num_key_map) | ||
log.V(6).Infof("key_val_map : %#v", key_val_map) | ||
|
||
if num_key_map != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Always return != nil, as you have made it with make. You can check len instead. |
||
var num_list []int | ||
for num_key_map_k := range num_key_map { | ||
num_list = append(num_list, num_key_map_k) | ||
} | ||
sort.Ints(num_list) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be already sorted? |
||
for _, num_list_v := range num_list { | ||
*req += "[" + num_key_map[num_list_v] + "=" + key_val_map[num_key_map[num_list_v]] + "]" | ||
} | ||
} | ||
|
||
/* Append "/" after all keys are processed. */ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about compatibility?
If we add num for each key, other GNMI client can't communicate with SONiC telemetry?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The scope of this numbering within parameters of a path element.
@pettershao-ragilenetworks -- please correct my statement if needed.