Skip to content

Commit

Permalink
Option to preserve arrays in JSON streams
Browse files Browse the repository at this point in the history
  • Loading branch information
draxil committed Nov 26, 2022
1 parent ec87e76 commit 2aaafe6
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 10 deletions.
26 changes: 20 additions & 6 deletions internal/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package options

import (
"flag"
"fmt"
)

const (
path = "path"
exparray = "expect-array"
version = "version"
OptPath = "path"
OptExpectArray = "expect-array"
OptVersion = "version"
OptPreserveArray = "preserve-array"
)

// New create an option handler that will parse the options from command line args
Expand All @@ -19,24 +21,35 @@ func New(args []string) (Handler, error) {

h.StringVar(
&o.Path,
path,
OptPath,
"",
"path to get to the JSON value you want to extract, e.g key1.key2",
)
h.BoolVar(
&o.ExpectArray,
exparray,
OptExpectArray,
false,
"check that whatever we're processing is an array, and fail if not",
)
h.BoolVar(
&o.JustPrintVersion,
version,
OptVersion,
false,
"print the version description for this tool and exit",
)
h.BoolVar(
&o.PreserveArray,
OptPreserveArray,
false,
"instead of turning the top-level array into NDJSON preserve the array, useful for JSON streams",
)

err := h.Parse(args)

if o.PreserveArray && o.ExpectArray {
return h, fmt.Errorf("options conflict, -%s does not work alongside -%s", OptPreserveArray, OptExpectArray)
}

h.Options = o

return h, err
Expand All @@ -49,6 +62,7 @@ type Handler struct {

type Set struct {
ExpectArray bool
PreserveArray bool
JustPrintVersion bool
Path string
Args []string
Expand Down
22 changes: 22 additions & 0 deletions internal/options/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,28 @@ func TestOptParse(t *testing.T) {
assert.Error(t, e)
},
},
{
name: "preserve array",
in: []string{"-preserve-array"},
exp: Set{
PreserveArray: true,
},
checkErr: func(t *testing.T, e error) {
assert.NoError(t, e)
},
},
{
name: "preserve array + expect array",
in: []string{"-preserve-array", "-expect-array"},
exp: Set{},
checkErr: func(t *testing.T, e error) {
is := assert.Error(t, e)
if !is {
return
}
assert.Contains(t, e.Error(), "options conflict", "error message")
},
},
{
name: "help",
in: []string{"-help"},
Expand Down
7 changes: 4 additions & 3 deletions processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (p processor) run() error {
return errNoJSON()
}

if c != '[' {
if c != '[' || p.options.PreserveArray {
return p.handleNonArray(js, c, true)
}

Expand Down Expand Up @@ -91,6 +91,7 @@ func (p processor) handlePathNodes(nodes []string, scan *json.JSON) error {
}
return p.handlePathNodes(nodes, scan)
}

func (p processor) prepOut() (w io.Writer, finishOut func() error) {
if p.buffered {
bw := bufio.NewWriter(p.out)
Expand Down Expand Up @@ -191,7 +192,7 @@ func (p processor) handleNonArray(j *json.JSON, clue byte, topLevel bool) error
if err != nil {
return err
}
if clue == '[' {
if clue == '[' && !p.options.PreserveArray {
return errArrayInStream()
}

Expand Down Expand Up @@ -285,5 +286,5 @@ func peekErr(e error) error {
}

func errArrayInStream() error {
return fmt.Errorf("Found an array in what seemed to be a stream of other types, this is not handled yet. Report your use-case if you need this")
return fmt.Errorf("Found an array in what seemed to be a JSON stream. Consider using -%s", options.OptPreserveArray)
}
10 changes: 9 additions & 1 deletion processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,10 +378,18 @@ func TestProcessor(t *testing.T) {
},
{
name: "array in the stream",
in: sreader(`{"p": 1234} [1 2 3 4]`),
in: sreader(`{"p": 1234} [1,2,3,4]`),
exp: `{"p": 1234}` + "\n",
expErr: errArrayInStream(),
},
{
name: "two arrays in the stream + preserve array",
in: sreader("[1,2][3,4]"),
opts: options.Set{
PreserveArray: true,
},
exp: "[1,2]\n" + "[3,4]\n",
},
}

for _, tc := range cases {
Expand Down

0 comments on commit 2aaafe6

Please sign in to comment.