Skip to content

Commit f94f149

Browse files
authored
Merge pull request #760 from earies/flat-root
Fix leading slash handling for flat format
2 parents 2ba48d2 + c539cef commit f94f149

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

pkg/api/path/path.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func GnmiPathToXPath(p *gnmi.Path, noKeys bool) string {
206206
sb := &strings.Builder{}
207207
if p.Origin != "" {
208208
sb.WriteString(p.Origin)
209-
sb.WriteString(":")
209+
sb.WriteString(":/")
210210
}
211211
elems := p.GetElem()
212212
numElems := len(elems)

pkg/formatters/flat.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ func responseFlat(msg proto.Message) (map[string]interface{}, error) {
4040
prefix := path.GnmiPathToXPath(n.GetPrefix(), false)
4141
for _, u := range n.GetUpdate() {
4242
p := path.GnmiPathToXPath(u.GetPath(), false)
43+
// If there is no prefix whatsoever, prepend
44+
// leading slash to the path
45+
if prefix == "" {
46+
p = filepath.Join("/", p)
47+
}
48+
// If a prefix is populated without an origin,
49+
// prepend leading slash to the prefix
50+
if n.GetPrefix().GetOrigin() == "" && n.GetPrefix().GetElem() != nil {
51+
prefix = filepath.Join("/", prefix)
52+
}
4353
vmap, err := getValueFlat(filepath.Join(prefix, p), u.GetVal())
4454
if err != nil {
4555
return nil, err
@@ -61,6 +71,16 @@ func responseFlat(msg proto.Message) (map[string]interface{}, error) {
6171
prefix := path.GnmiPathToXPath(n.GetPrefix(), false)
6272
for _, u := range n.GetUpdate() {
6373
p := path.GnmiPathToXPath(u.GetPath(), false)
74+
// If there is no prefix whatsoever, prepend
75+
// leading slash to the path
76+
if prefix == "" {
77+
p = filepath.Join("/", p)
78+
}
79+
// If a prefix is populated without an origin,
80+
// prepend leading slash to the prefix
81+
if n.GetPrefix().GetOrigin() == "" && n.GetPrefix().GetElem() != nil {
82+
prefix = filepath.Join("/", prefix)
83+
}
6484
vmap, err := getValueFlat(filepath.Join(prefix, p), u.GetVal())
6585
if err != nil {
6686
return nil, err

0 commit comments

Comments
 (0)