From ddeab46aca876eb4cb74ddcc363f73b09b2ef48c Mon Sep 17 00:00:00 2001 From: Joe Higton Date: Sun, 7 Aug 2022 10:41:34 +0100 Subject: [PATCH] [json] cleanup some lint --- internal/json/json.go | 1 - internal/json/scanner.go | 2 +- internal/json/scanner_test.go | 74 ++++++++++++++++++++++++++--------- 3 files changed, 56 insertions(+), 21 deletions(-) diff --git a/internal/json/json.go b/internal/json/json.go index 6f0bb20..55271f0 100644 --- a/internal/json/json.go +++ b/internal/json/json.go @@ -289,7 +289,6 @@ func (j *JSON) writeCurrentNumber(w io.Writer) (int, error) { return 0, err } if !more { - end = true break } } diff --git a/internal/json/scanner.go b/internal/json/scanner.go index f4d49d0..0422dc4 100644 --- a/internal/json/scanner.go +++ b/internal/json/scanner.go @@ -72,7 +72,7 @@ func (s *state) scan(chunk []byte, idx, max int) (int, error) { if s.seeking { if s.closerBalance == 0 { // TODO: probably not this way - if string(s.keybuf.Bytes()) == string(s.seek) { + if s.keybuf.String() == string(s.seek) { s.seeking = false s.seekFound = true return idx, nil diff --git a/internal/json/scanner_test.go b/internal/json/scanner_test.go index bd0fe6e..9ea9c70 100644 --- a/internal/json/scanner_test.go +++ b/internal/json/scanner_test.go @@ -9,39 +9,58 @@ import ( func TestStateScanSimpleObject(t *testing.T) { s := NewScanState('{') assert.False(t, s.key) - s.scan([]byte{'"'}, 0, 1) + + _, err := s.scan([]byte{'"'}, 0, 1) + assert.NoError(t, err) assert.True(t, s.inStr) assert.True(t, s.key) - s.scan([]byte{'\\'}, 0, 1) + + _, err = s.scan([]byte{'\\'}, 0, 1) + assert.NoError(t, err) assert.True(t, s.inStr) assert.True(t, s.key) - s.scan([]byte{'"'}, 0, 1) + + _, err = s.scan([]byte{'"'}, 0, 1) + assert.NoError(t, err) assert.True(t, s.inStr) assert.True(t, s.key) - s.scan([]byte{'"'}, 0, 1) + + _, err = s.scan([]byte{'"'}, 0, 1) + assert.NoError(t, err) assert.False(t, s.inStr) assert.False(t, s.key) assert.True(t, s.open, "still open after key") - s.scan([]byte(` : `), 0, 3) + + _, err = s.scan([]byte(` : `), 0, 3) + assert.NoError(t, err) assert.False(t, s.inStr) assert.False(t, s.key) - s.scan([]byte(`"y`), 0, 2) + + _, err = s.scan([]byte(`"y`), 0, 2) + assert.NoError(t, err) assert.True(t, s.inStr) assert.False(t, s.key) - s.scan([]byte(`ellow"`), 0, 6) + + _, err = s.scan([]byte(`ellow"`), 0, 6) + assert.NoError(t, err) assert.False(t, s.inStr) assert.True(t, s.open, "still open after value") - s.scan([]byte{'}'}, 0, 1) + + _, err = s.scan([]byte{'}'}, 0, 1) + assert.NoError(t, err) assert.False(t, s.open, "closed by the closer") } func TestScanSubObjectDoesNotClose(t *testing.T) { s := NewScanState('{') buf := []byte(`"x":{"subobject":21}`) - s.scan(buf, 0, len(buf)) + _, err := s.scan(buf, 0, len(buf)) assert.True(t, s.open) - s.scan([]byte{'}'}, 0, 1) + assert.NoError(t, err) + + _, err = s.scan([]byte{'}'}, 0, 1) assert.False(t, s.open) + assert.NoError(t, err) } func TestScanCloseBeforeEnd(t *testing.T) { @@ -55,19 +74,26 @@ func TestScanCloseBeforeEnd(t *testing.T) { func TestScanStringDoesNotCloseObject(t *testing.T) { s := NewScanState('{') + buf := []byte(`"}"`) - s.scan(buf, 0, len(buf)) + _, err := s.scan(buf, 0, len(buf)) + assert.NoError(t, err) assert.True(t, s.open) - s.scan([]byte{'}'}, 0, 1) + + _, err = s.scan([]byte{'}'}, 0, 1) + assert.NoError(t, err) assert.False(t, s.open) } func TestEscapedSubStringDoesNotClose(t *testing.T) { s := NewScanState('"') buf := []byte(`x\"`) - s.scan(buf, 0, len(buf)) + _, err := s.scan(buf, 0, len(buf)) + assert.NoError(t, err) assert.True(t, s.open) - s.scan([]byte{'"'}, 0, 1) + + _, err = s.scan([]byte{'"'}, 0, 1) + assert.NoError(t, err) assert.False(t, s.open) } @@ -82,10 +108,14 @@ func TestScanForSimple(t *testing.T) { s := NewScanState('{') s.seekFor("z") buf := []byte(`"x":99,"y":102,`) - s.scan(buf, 0, len(buf)) + + _, err := s.scan(buf, 0, len(buf)) assert.False(t, s.seekFound, "not found yet") + assert.NoError(t, err) + buf = []byte(`"z":199`) - s.scan(buf, 0, len(buf)) + _, err = s.scan(buf, 0, len(buf)) + assert.NoError(t, err) assert.True(t, s.seekFound, "found") } @@ -95,10 +125,14 @@ func TestScanForSimpleWithNestedTrap(t *testing.T) { s.seekFor("target") assert.False(t, s.seekFound, "not found yet") buf := []byte(`"x":99,"p":{"target":12},`) - s.scan(buf, 0, len(buf)) + + _, err := s.scan(buf, 0, len(buf)) + assert.NoError(t, err) assert.False(t, s.seekFound, "not found yet") + buf = []byte(`"target":199`) - pos, _ := s.scan(buf, 0, len(buf)) + pos, err := s.scan(buf, 0, len(buf)) + assert.NoError(t, err) assert.True(t, s.seekFound, "found") assert.Equal(t, `":199`, string(buf[pos:]), "stopped in the correct place") } @@ -107,8 +141,10 @@ func TestFutileScan(t *testing.T) { s := NewScanState('{') s.seekFor("target") assert.False(t, s.seekFound, "not found yet") + buf := []byte(`"x":99,"p":{"target":12},"z":[{"target":12}]}`) - s.scan(buf, 0, len(buf)) + _, err := s.scan(buf, 0, len(buf)) assert.False(t, s.seekFound, "not found yet") assert.False(t, s.open) + assert.NoError(t, err) }