Skip to content

Commit

Permalink
[json] cleanup some lint
Browse files Browse the repository at this point in the history
  • Loading branch information
draxil committed Aug 7, 2022
1 parent 6cc0351 commit ddeab46
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 21 deletions.
1 change: 0 additions & 1 deletion internal/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,6 @@ func (j *JSON) writeCurrentNumber(w io.Writer) (int, error) {
return 0, err
}
if !more {
end = true
break
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/json/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
74 changes: 55 additions & 19 deletions internal/json/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}

Expand All @@ -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")

}
Expand All @@ -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")
}
Expand All @@ -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)
}

0 comments on commit ddeab46

Please sign in to comment.