Skip to content

Commit

Permalink
feat: add jmespath module
Browse files Browse the repository at this point in the history
Signed-off-by: Luis Davim <[email protected]>
  • Loading branch information
luisdavim committed Dec 28, 2023
1 parent c4f6fb4 commit 2c6f8a7
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 2 deletions.
2 changes: 2 additions & 0 deletions cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
modFilepath "github.com/risor-io/risor/modules/filepath"
modFmt "github.com/risor-io/risor/modules/fmt"
modHash "github.com/risor-io/risor/modules/hash"
modJmesPath "github.com/risor-io/risor/modules/jmespath"
modJson "github.com/risor-io/risor/modules/json"
modMath "github.com/risor-io/risor/modules/math"
modOs "github.com/risor-io/risor/modules/os"
Expand Down Expand Up @@ -84,6 +85,7 @@ func (cfg *Config) addDefaultGlobals() {
builtins := []map[string]object.Object{
builtins.Builtins(),
modFetch.Builtins(),
modJmesPath.Builtins(),
modFmt.Builtins(),
modHash.Builtins(),
modOs.Builtins(),
Expand Down
13 changes: 13 additions & 0 deletions examples/scripts/jmespath.risor
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env risor --

data := {
"locations": [
{"name": "Seattle", "state": "WA"},
{"name": "New York", "state": "NY"},
{"name": "Bellevue", "state": "WA"},
{"name": "Olympia", "state": "WA"},
],
} | jmespath("locations[?state == 'WA'].name | sort(@) | {WashingtonCities: join(', ', @)}")

print(data)
print(jmespath(data, "split(WashingtonCities, ',')")[0])
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ module github.com/risor-io/risor

go 1.21

require github.com/stretchr/testify v1.8.4
require (
github.com/jmespath-community/go-jmespath v1.1.1
github.com/stretchr/testify v1.8.4
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
golang.org/x/exp v0.0.0-20230314191032-db074128a8ec // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jmespath-community/go-jmespath v1.1.1 h1:bFikPhsi/FdmlZhVgSCd2jj1e7G/rw+zyQfyg5UF+L4=
github.com/jmespath-community/go-jmespath v1.1.1/go.mod h1:4gOyFJsR/Gk+05RgTKYrifT7tBPWD8Lubtb5jRrfy9I=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
Expand All @@ -16,6 +18,8 @@ github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjR
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/exp v0.0.0-20230314191032-db074128a8ec h1:pAv+d8BM2JNnNctsLJ6nnZ6NqXT8N4+eauvZSb3P0I0=
golang.org/x/exp v0.0.0-20230314191032-db074128a8ec/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
Expand Down
2 changes: 1 addition & 1 deletion modules/fetch/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func Fetch(ctx context.Context, args ...object.Object) object.Object {
numArgs := len(args)
if numArgs < 1 || numArgs > 2 {
return object.NewArgsRangeError("fetch", 1, 2, len(args))
return object.NewArgsRangeError("fetch", 1, 2, numArgs)
}
urlArg, argErr := object.AsString(args[0])
if argErr != nil {
Expand Down
48 changes: 48 additions & 0 deletions modules/jmespath/jmespath.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package jmespath

import (
"context"
"fmt"

"github.com/jmespath-community/go-jmespath/pkg/api"
"github.com/jmespath-community/go-jmespath/pkg/parsing"
"github.com/risor-io/risor/object"
)

func Jmespath(ctx context.Context, args ...object.Object) object.Object {
numArgs := len(args)

if numArgs != 2 {
return object.NewArgsError("jmespath", 2, numArgs)
}

data, argsErr := object.AsMap(args[0])
if argsErr != nil {
return argsErr
}

expression, argsErr := object.AsString(args[1])
if argsErr != nil {
return argsErr
}

parser := parsing.NewParser()
if _, err := parser.Parse(expression); err != nil {
if syntaxError, ok := err.(parsing.SyntaxError); ok {
return object.NewError(fmt.Errorf("%s\n%s", syntaxError, syntaxError.HighlightLocation()))
}
return object.NewError(err)
}
result, err := api.Search(expression, data.Interface())
if argsErr != nil {
return object.NewError(err)
}

return object.FromGoType(result)
}

func Builtins() map[string]object.Object {
return map[string]object.Object{
"jmespath": object.NewBuiltin("jmespath", Jmespath),
}
}

0 comments on commit 2c6f8a7

Please sign in to comment.