You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
534 B
28 lines
534 B
3 years ago
|
package jx
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"io"
|
||
|
)
|
||
|
|
||
|
// Capture calls f and then rolls back to state before call.
|
||
|
func (d *Decoder) Capture(f func(d *Decoder) error) error {
|
||
|
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
|
||
|
return err
|
||
|
}
|