Skip to content

Commit

Permalink
Allow Longer Rows In Stream Mode
Browse files Browse the repository at this point in the history
Use a larger buffer in stream mode since the json can't be
pretty-printed before parsing by gron, and therefore may have long
lines.
  • Loading branch information
cwarden committed Apr 25, 2018
1 parent 193ed27 commit bd91e9a
Show file tree
Hide file tree
Showing 4 changed files with 3,794 additions and 0 deletions.
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ func gronStream(r io.Reader, w io.Writer, opts int) (int, error) {

// Read the input line by line
sc := bufio.NewScanner(r)
buf := make([]byte, 0, 64*1024)
sc.Buffer(buf, 1024*1024)
i := 0
for sc.Scan() {

Expand Down
38 changes: 38 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,44 @@ func TestGronStream(t *testing.T) {

}

func TestLargeGronStream(t *testing.T) {
cases := []struct {
inFile string
outFile string
}{
{"testdata/long-stream.json", "testdata/long-stream.gron"},
}

for _, c := range cases {
in, err := os.Open(c.inFile)
if err != nil {
t.Fatalf("failed to open input file: %s", err)
}

want, err := ioutil.ReadFile(c.outFile)
if err != nil {
t.Fatalf("failed to open want file: %s", err)
}

out := &bytes.Buffer{}
code, err := gronStream(in, out, optMonochrome)

if code != exitOK {
t.Errorf("want exitOK; have %d", code)
}
if err != nil {
t.Errorf("want nil error; have %s", err)
}

if !reflect.DeepEqual(want, out.Bytes()) {
t.Logf("want: %s", want)
t.Logf("have: %s", out.Bytes())
t.Errorf("gronned %s does not match %s", c.inFile, c.outFile)
}
}

}

func TestUngron(t *testing.T) {
cases := []struct {
inFile string
Expand Down
Loading

0 comments on commit bd91e9a

Please sign in to comment.