Skip to content

Commit ba4e883

Browse files
James Haggertyzmedico
authored andcommitted
jpp: Add --stream, -s bool flag
Rebased jmespath/jp#14 on jpp master.
1 parent ee70420 commit ba4e883

2 files changed

Lines changed: 49 additions & 26 deletions

File tree

cmd/jpp/main.go

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"encoding/json"
55
"fmt"
6+
"io"
67
"io/ioutil"
78
"os"
89

@@ -36,6 +37,10 @@ func main() {
3637
Name: "unquoted, u",
3738
Usage: "If the final result is a string, it will be printed without quotes.",
3839
},
40+
cli.BoolFlag{
41+
Name: "stream, s",
42+
Usage: "Parse JSON elements until the input stream is exhausted (rather than just the first).",
43+
},
3944
cli.BoolFlag{
4045
Name: "ast",
4146
Usage: "Only print the AST of the parsed expression. Do not rely on this output, only useful for debugging purposes.",
@@ -85,7 +90,6 @@ func runMain(c *cli.Context) int {
8590
fmt.Printf("%s\n", parsed)
8691
return 0
8792
}
88-
var input interface{}
8993
var jsonParser *json.Decoder
9094
if c.String("filename") != "" {
9195
f, err := os.Open(c.String("filename"))
@@ -97,35 +101,43 @@ func runMain(c *cli.Context) int {
97101
} else {
98102
jsonParser = json.NewDecoder(os.Stdin)
99103
}
100-
if err := jsonParser.Decode(&input); err != nil {
101-
errMsg("Error parsing input json: %s\n", err)
102-
return 2
103-
}
104-
result, err := jmespath.Search(expression, input)
105-
if err != nil {
106-
if syntaxError, ok := err.(jmespath.SyntaxError); ok {
107-
return errMsg("%s\n%s\n",
108-
syntaxError,
109-
syntaxError.HighlightLocation())
104+
for {
105+
var input interface{}
106+
if err := jsonParser.Decode(&input); err == io.EOF {
107+
break
108+
} else if err != nil {
109+
errMsg("Error parsing input json: %s\n", err)
110+
return 2
110111
}
111-
return errMsg("Error evaluating JMESPath expression: %s", err)
112-
}
113-
converted, isString := result.(string)
114-
if c.Bool("unquoted") && isString {
115-
os.Stdout.WriteString(converted)
116-
} else {
117-
var toJSON []byte
118-
if c.Bool("compact") {
119-
toJSON, err = json.Marshal(result)
112+
result, err := jmespath.Search(expression, input)
113+
if err != nil {
114+
if syntaxError, ok := err.(jmespath.SyntaxError); ok {
115+
return errMsg("%s\n%s\n",
116+
syntaxError,
117+
syntaxError.HighlightLocation())
118+
}
119+
return errMsg("Error evaluating JMESPath expression: %s", err)
120+
}
121+
converted, isString := result.(string)
122+
if c.Bool("unquoted") && isString {
123+
os.Stdout.WriteString(converted)
120124
} else {
121-
toJSON, err = json.MarshalIndent(result, "", " ")
125+
var toJSON []byte
126+
if c.Bool("compact") {
127+
toJSON, err = json.Marshal(result)
128+
} else {
129+
toJSON, err = json.MarshalIndent(result, "", " ")
130+
}
131+
if err != nil {
132+
errMsg("Error marshalling result to JSON: %s\n", err)
133+
return 3
134+
}
135+
os.Stdout.Write(toJSON)
122136
}
123-
if err != nil {
124-
errMsg("Error marshalling result to JSON: %s\n", err)
125-
return 3
137+
os.Stdout.WriteString("\n")
138+
if !c.Bool("stream") {
139+
break
126140
}
127-
os.Stdout.Write(toJSON)
128141
}
129-
os.Stdout.WriteString("\n")
130142
return 0
131143
}

test/cases/search.bats

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@
1414
[ "$output" == "\"bar\"" ]
1515
}
1616

17+
@test "Ignores subsequent data" {
18+
output=$(echo '{"foo": "bar"}blah' | ./jp foo)
19+
[ "$output" == "\"bar\"" ]
20+
}
21+
22+
@test "Processes subsequent data in stream mode" {
23+
output=$(echo '{"foo": "bar"}{"foo": "x"}' | ./jpp -s foo)
24+
echo "$output"
25+
[ "$output" == $'\"bar\"\n\"x\"' ]
26+
}
27+
1728
@test "Can search subexpr expression" {
1829
output=$(echo '{"foo": {"bar": "baz"}}' | ./jp foo.bar)
1930
[ "$output" == "\"baz\"" ]

0 commit comments

Comments
 (0)