Skip to content

Commit

Permalink
jpp: Add --stream, -s bool flag
Browse files Browse the repository at this point in the history
Rebased jmespath/jp#14 on jpp master.
  • Loading branch information
James Haggerty authored and zmedico committed Jun 15, 2021
1 parent ee70420 commit ba4e883
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 26 deletions.
64 changes: 38 additions & 26 deletions cmd/jpp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"

Expand Down Expand Up @@ -36,6 +37,10 @@ func main() {
Name: "unquoted, u",
Usage: "If the final result is a string, it will be printed without quotes.",
},
cli.BoolFlag{
Name: "stream, s",
Usage: "Parse JSON elements until the input stream is exhausted (rather than just the first).",
},
cli.BoolFlag{
Name: "ast",
Usage: "Only print the AST of the parsed expression. Do not rely on this output, only useful for debugging purposes.",
Expand Down Expand Up @@ -85,7 +90,6 @@ func runMain(c *cli.Context) int {
fmt.Printf("%s\n", parsed)
return 0
}
var input interface{}
var jsonParser *json.Decoder
if c.String("filename") != "" {
f, err := os.Open(c.String("filename"))
Expand All @@ -97,35 +101,43 @@ func runMain(c *cli.Context) int {
} else {
jsonParser = json.NewDecoder(os.Stdin)
}
if err := jsonParser.Decode(&input); err != nil {
errMsg("Error parsing input json: %s\n", err)
return 2
}
result, err := jmespath.Search(expression, input)
if err != nil {
if syntaxError, ok := err.(jmespath.SyntaxError); ok {
return errMsg("%s\n%s\n",
syntaxError,
syntaxError.HighlightLocation())
for {
var input interface{}
if err := jsonParser.Decode(&input); err == io.EOF {
break
} else if err != nil {
errMsg("Error parsing input json: %s\n", err)
return 2
}
return errMsg("Error evaluating JMESPath expression: %s", err)
}
converted, isString := result.(string)
if c.Bool("unquoted") && isString {
os.Stdout.WriteString(converted)
} else {
var toJSON []byte
if c.Bool("compact") {
toJSON, err = json.Marshal(result)
result, err := jmespath.Search(expression, input)
if err != nil {
if syntaxError, ok := err.(jmespath.SyntaxError); ok {
return errMsg("%s\n%s\n",
syntaxError,
syntaxError.HighlightLocation())
}
return errMsg("Error evaluating JMESPath expression: %s", err)
}
converted, isString := result.(string)
if c.Bool("unquoted") && isString {
os.Stdout.WriteString(converted)
} else {
toJSON, err = json.MarshalIndent(result, "", " ")
var toJSON []byte
if c.Bool("compact") {
toJSON, err = json.Marshal(result)
} else {
toJSON, err = json.MarshalIndent(result, "", " ")
}
if err != nil {
errMsg("Error marshalling result to JSON: %s\n", err)
return 3
}
os.Stdout.Write(toJSON)
}
if err != nil {
errMsg("Error marshalling result to JSON: %s\n", err)
return 3
os.Stdout.WriteString("\n")
if !c.Bool("stream") {
break
}
os.Stdout.Write(toJSON)
}
os.Stdout.WriteString("\n")
return 0
}
11 changes: 11 additions & 0 deletions test/cases/search.bats
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@
[ "$output" == "\"bar\"" ]
}

@test "Ignores subsequent data" {
output=$(echo '{"foo": "bar"}blah' | ./jp foo)
[ "$output" == "\"bar\"" ]
}

@test "Processes subsequent data in stream mode" {
output=$(echo '{"foo": "bar"}{"foo": "x"}' | ./jpp -s foo)
echo "$output"
[ "$output" == $'\"bar\"\n\"x\"' ]
}

@test "Can search subexpr expression" {
output=$(echo '{"foo": {"bar": "baz"}}' | ./jp foo.bar)
[ "$output" == "\"baz\"" ]
Expand Down

0 comments on commit ba4e883

Please sign in to comment.