Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Capture support for readers #5

Merged
merged 3 commits into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ d.ObjBytes(func(d *Decoder, key []byte) error {
## Roadmap
- [ ] Rework and export `Any`
- [ ] Support `Raw` for io.Reader
- [ ] Support `Capture` for io.Reader
- [x] Support `Capture` for io.Reader
- [ ] Improve Num
- Better validation on decoding
- Support BigFloat and BigInt
Expand Down
18 changes: 12 additions & 6 deletions dec_capture.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package jx

import (
"github.com/go-faster/errors"
"bytes"
"io"
)

// Capture calls f and then rolls back to state before call.
//
// Does not work with reader.
func (d *Decoder) Capture(f func(d *Decoder) error) error {
if d.reader != nil {
return errors.New("capture is not supported with reader")
}
if f == nil {
return nil
}

if d.reader != nil {
// TODO(tdakkota): May it be more efficient?
var buf bytes.Buffer
reader := io.TeeReader(d.reader, &buf)
defer func() {
d.reader = io.MultiReader(&buf, d.reader)
}()
d.reader = reader
}
head, tail, depth := d.head, d.tail, d.depth
err := f(d)
d.head, d.tail, d.depth = head, tail, depth
Expand Down
69 changes: 50 additions & 19 deletions dec_capture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,25 +84,56 @@ func BenchmarkIterator_Skip(b *testing.B) {
}

func TestDecoder_Capture(t *testing.T) {
i := DecodeStr(`["foo", "bar", "baz"]`)
var elems int
if err := i.Capture(func(i *Decoder) error {
return i.Arr(func(i *Decoder) error {
elems++
return i.Skip()
})
}); err != nil {
t.Fatal(err)
strs := []string{
"foo",
"bar",
"baz",
}
require.Equal(t, Array, i.Next())
require.Equal(t, 3, elems)
t.Run("Nil", func(t *testing.T) {
require.NoError(t, i.Capture(nil))
require.Equal(t, Array, i.Next())
})
}
test := func(i *Decoder) func(t *testing.T) {
return func(t *testing.T) {
var elems int
if err := i.Capture(func(i *Decoder) error {
return i.Arr(func(i *Decoder) error {
elems++
return i.Skip()
})
}); err != nil {
t.Fatal(err)
}
require.Equal(t, Array, i.Next())
require.Equal(t, 6, elems)
t.Run("Nil", func(t *testing.T) {
require.NoError(t, i.Capture(nil))
require.Equal(t, Array, i.Next())
})

func TestDecoder_Capture_reader(t *testing.T) {
i := Decode(new(bytes.Buffer), 0)
require.Error(t, i.Capture(nil))
idx := 0
require.NoError(t, i.Arr(func(d *Decoder) error {
v, err := d.Str()
if err != nil {
return err
}
require.Equal(t, strs[idx%len(strs)], v)

idx++
return nil
}))
}
}

var e Encoder
e.ArrStart()
for i := 0; i < 6; i++ {
e.Str(strs[i%len(strs)])
}
e.ArrEnd()
testData := e.Bytes()

t.Run("Str", test(DecodeBytes(testData)))
// Check that we get correct result even if buffer smaller than captured data.
decoder := Decoder{
reader: bytes.NewReader(testData),
buf: make([]byte, 8),
}
t.Run("Reader", test(&decoder))
}