-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Luis Davim <[email protected]>
- Loading branch information
Showing
14 changed files
with
99 additions
and
2 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
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,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]) |
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,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), | ||
} | ||
} |