Skip to content

Commit

Permalink
Merge pull request #28 from lalyos/jmespath
Browse files Browse the repository at this point in the history
Add jmespath function
  • Loading branch information
josegonzalez authored Apr 22, 2017
2 parents 2e2d6e0 + c483539 commit c9b82aa
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file.
### Fixed

### Added
- Add [jmespath](http://jmespath.org) function

### Removed

Expand Down
44 changes: 30 additions & 14 deletions builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/dustin/go-jsonpointer"
"github.com/flynn/go-shlex"
"github.com/gliderlabs/sigil"
"github.com/jmespath/go-jmespath"
"gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -49,19 +50,20 @@ func init() {
"sh": Shell,
"httpget": HttpGet,
// structured data
"pointer": Pointer,
"json": Json,
"tojson": ToJson,
"yaml": Yaml,
"toyaml": ToYaml,
"uniq": Uniq,
"drop": Drop,
"append": Append,
"seq": Seq,
"join": Join,
"joinkv": JoinKv,
"split": Split,
"splitkv": SplitKv,
"pointer": Pointer,
"json": Json,
"jmespath": JmesPath,
"tojson": ToJson,
"yaml": Yaml,
"toyaml": ToYaml,
"uniq": Uniq,
"drop": Drop,
"append": Append,
"seq": Seq,
"join": Join,
"joinkv": JoinKv,
"split": Split,
"splitkv": SplitKv,
})
}

Expand Down Expand Up @@ -94,7 +96,7 @@ func HttpGet(in interface{}) (interface{}, error) {
if err != nil {
return "", err
}
return sigil.NamedReader{resp.Body, "<"+in_+">"}, nil
return sigil.NamedReader{resp.Body, "<" + in_ + ">"}, nil
}

func JoinKv(sep string, in interface{}) ([]interface{}, error) {
Expand Down Expand Up @@ -358,6 +360,20 @@ func Pointer(path string, in interface{}) (interface{}, error) {
return jsonpointer.Get(m, path), nil
}

func JmesPath(path string, in interface{}) (interface{}, error) {
precompiled, err := jmespath.Compile(path)
if err != nil {
return nil, fmt.Errorf("JmesPath compileerror: %v", err)
}

result, err := precompiled.Search(in)
if err != nil {
return nil, fmt.Errorf("JmesPath search error: %v", err)
}

return result, nil
}

func Render(args ...interface{}) (interface{}, error) {
if len(args) == 0 {
fmt.Errorf("render cannot be used without arguments")
Expand Down
4 changes: 4 additions & 0 deletions tests/sigil.bash
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,7 @@ EOF
[[ "$result" == '{"a":"Easy!","b":{"c":2,"d":[3,4]},"c":{"list":["one","two","tree"]}}' ]]
}

T_jmespath() {
result="$(echo '[{"name":"bob","age":20},{"name":"jim","age":30},{"name":"joe","age":40}]' | $SIGIL -i '{{stdin | json | jmespath "[? age >= `30`].name | reverse(@)" | join ","}}')"
[[ "$result" == 'joe,jim' ]]
}

0 comments on commit c9b82aa

Please sign in to comment.