-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Display node attributes as flattened JSON paths (#29)
* add function to generate json path * fix S1034 lint error * update templates
- Loading branch information
1 parent
188b611
commit 5808a08
Showing
9 changed files
with
140 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package util | ||
|
||
// MakeJSONPath will take a JSON object and return flattened JSON paths | ||
func MakeJSONPath(obj map[string]interface{}, prefix string) map[string]interface{} { | ||
attrs := make(map[string]interface{}) | ||
var nested map[string]interface{} | ||
|
||
for k, v := range obj { | ||
switch V := v.(type) { | ||
default: | ||
attrs[prefix+"."+k] = V | ||
case map[string]interface{}: | ||
nested = MakeJSONPath(V, prefix+"."+k) | ||
for k2, v2 := range nested { | ||
attrs[k2] = v2 | ||
} | ||
} | ||
} | ||
|
||
return attrs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package util | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestMakeJSONPath(t *testing.T) { | ||
|
||
input := map[string]interface{}{ | ||
"stringkey": "stringval", | ||
"somearr": []string{"one", "two", "three"}, | ||
"nested": map[string]interface{}{ | ||
"foo": "bar", | ||
"hello": "world", | ||
"bool": false, | ||
"int": 123, | ||
"deep": map[string]interface{}{ | ||
"nest": "value", | ||
}, | ||
}, | ||
} | ||
|
||
expected := map[string]interface{}{ | ||
"$.stringkey": "stringval", | ||
"$.somearr": []string{"one", "two", "three"}, | ||
"$.nested.foo": "bar", | ||
"$.nested.bool": false, | ||
"$.nested.int": 123, | ||
"$.nested.hello": "world", | ||
"$.nested.deep.nest": "value", | ||
} | ||
|
||
actual := MakeJSONPath(input, "$") | ||
|
||
if !reflect.DeepEqual(expected, actual) { | ||
t.Errorf("failed, in: %v, expected: %v, actual: %v", input, expected, actual) | ||
} | ||
} |