Skip to content

Commit

Permalink
feat(dec): skip capture if callback is nil
Browse files Browse the repository at this point in the history
  • Loading branch information
ernado committed Oct 31, 2021
1 parent 030f4cc commit f185a66
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions dec_capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ func (d *Decoder) Capture(f func(i *Decoder) error) error {
if d.reader != nil {
return xerrors.New("capture is not supported with reader")
}
if f == nil {
return nil
}
head, tail, depth := d.head, d.tail, d.depth
err := f(d)
d.head, d.tail, d.depth = head, tail, depth
Expand Down
10 changes: 10 additions & 0 deletions dec_capture_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jx

import (
"bytes"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -95,4 +96,13 @@ func TestDecoder_Capture(t *testing.T) {
}
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())
})
}

func TestDecoder_Capture_reader(t *testing.T) {
i := Decode(new(bytes.Buffer), 0)
require.Error(t, i.Capture(nil))
}

0 comments on commit f185a66

Please sign in to comment.