Skip to content

Commit

Permalink
Add check for empty origin special case (#608)
Browse files Browse the repository at this point in the history
* Add check for empty origin special case

* add doc link
  • Loading branch information
DanG100 authored Nov 30, 2021
1 parent 368c169 commit 032d1f4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
6 changes: 5 additions & 1 deletion util/gnmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ func PathMatchesPathElemPrefix(path, prefix *gpb.Path) bool {
// If either path and query contain nil elements func returns false.
// Both paths must use the gNMI >=0.4.0 PathElem path format.
func PathMatchesQuery(path, query *gpb.Path) bool {
if len(path.GetElem()) < len(query.GetElem()) || path.Origin != query.Origin {
if len(path.GetElem()) < len(query.GetElem()) {
return false
}
// Unset Origin fields can match "openconfig", see https://github.com/openconfig/reference/blob/master/rpc/gnmi/mixed-schema.md#special-values-of-origin.
if path.Origin != query.Origin && !(path.Origin == "" && query.Origin == "openconfig" || path.Origin == "openconfig" && query.Origin == "") {
return false
}
for i, queryElem := range query.Elem {
Expand Down
33 changes: 33 additions & 0 deletions util/gnmi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,24 @@ func TestPathMatchesQuery(t *testing.T) {
}},
},
want: true,
}, {
desc: "valid query with implied openconfig origin path",
inPath: &gpb.Path{
Origin: "",
},
inQuery: &gpb.Path{
Origin: "openconfig",
},
want: true,
}, {
desc: "valid query with implied openconfig origin query",
inPath: &gpb.Path{
Origin: "openconfig",
},
inQuery: &gpb.Path{
Origin: "",
},
want: true,
}, {
desc: "valid query with wildcard name",
inPath: &gpb.Path{
Expand Down Expand Up @@ -648,6 +666,21 @@ func TestPathMatchesQuery(t *testing.T) {
Name: "three",
}},
},
}, {
desc: "invalid longer query",
inPath: &gpb.Path{
Elem: []*gpb.PathElem{
{
Name: "twelve",
}},
},
inQuery: &gpb.Path{
Elem: []*gpb.PathElem{{
Name: "one",
}, {
Name: "two",
}},
},
}, {
desc: "invalid names not equal",
inPath: &gpb.Path{
Expand Down

0 comments on commit 032d1f4

Please sign in to comment.