-
-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #535 from hairyhenderson/jsonpath
Adding coll.JSONPath function
- Loading branch information
Showing
14 changed files
with
2,518 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,63 @@ | ||
package coll | ||
|
||
import ( | ||
"reflect" | ||
|
||
"github.com/pkg/errors" | ||
"k8s.io/client-go/util/jsonpath" | ||
) | ||
|
||
// JSONPath - | ||
func JSONPath(p string, in interface{}) (interface{}, error) { | ||
jp, err := parsePath(p) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "couldn't parse JSONPath %s", p) | ||
} | ||
results, err := jp.FindResults(in) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "executing JSONPath failed") | ||
} | ||
|
||
var out interface{} | ||
if len(results) == 1 && len(results[0]) == 1 { | ||
v := results[0][0] | ||
out, err = extractResult(v) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} else { | ||
a := []interface{}{} | ||
for _, r := range results { | ||
for _, v := range r { | ||
o, err := extractResult(v) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if o != nil { | ||
a = append(a, o) | ||
} | ||
} | ||
} | ||
out = a | ||
} | ||
|
||
return out, nil | ||
} | ||
|
||
func parsePath(p string) (*jsonpath.JSONPath, error) { | ||
jp := jsonpath.New("<jsonpath>") | ||
err := jp.Parse("{" + p + "}") | ||
if err != nil { | ||
return nil, err | ||
} | ||
jp.AllowMissingKeys(false) | ||
return jp, nil | ||
} | ||
|
||
func extractResult(v reflect.Value) (interface{}, error) { | ||
if v.CanInterface() { | ||
return v.Interface(), nil | ||
} | ||
|
||
return nil, errors.Errorf("JSONPath couldn't access field") | ||
} |
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,146 @@ | ||
package coll | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type m = map[string]interface{} | ||
type ar = []interface{} | ||
|
||
func TestJSONPath(t *testing.T) { | ||
in := m{ | ||
"store": m{ | ||
"book": ar{ | ||
m{ | ||
"category": "reference", | ||
"author": "Nigel Rees", | ||
"title": "Sayings of the Century", | ||
"price": 8.95, | ||
}, | ||
m{ | ||
"category": "fiction", | ||
"author": "Evelyn Waugh", | ||
"title": "Sword of Honour", | ||
"price": 12.99, | ||
}, | ||
m{ | ||
"category": "fiction", | ||
"author": "Herman Melville", | ||
"title": "Moby Dick", | ||
"isbn": "0-553-21311-3", | ||
"price": 8.99, | ||
}, | ||
m{ | ||
"category": "fiction", | ||
"author": "J. R. R. Tolkien", | ||
"title": "The Lord of the Rings", | ||
"isbn": "0-395-19395-8", | ||
"price": 22.99, | ||
}, | ||
}, | ||
"bicycle": m{ | ||
"color": "red", | ||
"price": 19.95, | ||
}, | ||
}, | ||
} | ||
out, err := JSONPath(".store.bicycle.color", in) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "red", out) | ||
|
||
out, err = JSONPath(".store.bicycle.price", in) | ||
assert.NoError(t, err) | ||
assert.Equal(t, 19.95, out) | ||
|
||
_, err = JSONPath(".store.bogus", in) | ||
assert.Error(t, err) | ||
|
||
_, err = JSONPath("{.store.unclosed", in) | ||
assert.Error(t, err) | ||
|
||
out, err = JSONPath(".store", in) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, in["store"], out) | ||
|
||
out, err = JSONPath("$.store.book[*].author", in) | ||
assert.NoError(t, err) | ||
assert.Len(t, out, 4) | ||
assert.Contains(t, out, "Nigel Rees") | ||
assert.Contains(t, out, "Evelyn Waugh") | ||
assert.Contains(t, out, "Herman Melville") | ||
assert.Contains(t, out, "J. R. R. Tolkien") | ||
|
||
out, err = JSONPath("$..book[?( @.price < 10.0 )]", in) | ||
assert.NoError(t, err) | ||
expected := ar{ | ||
m{ | ||
"category": "reference", | ||
"author": "Nigel Rees", | ||
"title": "Sayings of the Century", | ||
"price": 8.95, | ||
}, | ||
m{ | ||
"category": "fiction", | ||
"author": "Herman Melville", | ||
"title": "Moby Dick", | ||
"isbn": "0-553-21311-3", | ||
"price": 8.99, | ||
}, | ||
} | ||
assert.EqualValues(t, expected, out) | ||
|
||
in = m{ | ||
"a": m{ | ||
"aa": m{ | ||
"foo": m{ | ||
"aaa": m{ | ||
"aaaa": m{ | ||
"bar": 1234, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"ab": m{ | ||
"aba": m{ | ||
"foo": m{ | ||
"abaa": true, | ||
"abab": "baz", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
out, err = JSONPath("..foo.*", in) | ||
assert.NoError(t, err) | ||
assert.Len(t, out, 3) | ||
assert.Contains(t, out, m{"aaaa": m{"bar": 1234}}) | ||
assert.Contains(t, out, true) | ||
assert.Contains(t, out, "baz") | ||
|
||
type bicycleType struct { | ||
Color string | ||
} | ||
type storeType struct { | ||
Bicycle *bicycleType | ||
safe interface{} | ||
} | ||
|
||
structIn := &storeType{ | ||
Bicycle: &bicycleType{ | ||
Color: "red", | ||
}, | ||
safe: "hidden", | ||
} | ||
|
||
out, err = JSONPath(".Bicycle.Color", structIn) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "red", out) | ||
|
||
_, err = JSONPath(".safe", structIn) | ||
assert.Error(t, err) | ||
|
||
_, err = JSONPath(".*", structIn) | ||
assert.Error(t, err) | ||
} |
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
Oops, something went wrong.