From 8f6a146a7dfe2d213f3eba08ff1c3753eac6fc08 Mon Sep 17 00:00:00 2001 From: itchyny Date: Tue, 29 Oct 2024 21:40:35 +0900 Subject: [PATCH] refactor stream input iterator to use jsonInputIter --- cli/inputs.go | 68 +++++++++------------------------------------------ 1 file changed, 12 insertions(+), 56 deletions(-) diff --git a/cli/inputs.go b/cli/inputs.go index 7a35dcdf..5cb90150 100644 --- a/cli/inputs.go +++ b/cli/inputs.go @@ -68,7 +68,7 @@ type inputIter interface { } type jsonInputIter struct { - dec *json.Decoder + next func() (any, error) ir *inputReader fname string offset int64 @@ -80,15 +80,16 @@ func newJSONInputIter(r io.Reader, fname string) inputIter { ir := newInputReader(r) dec := json.NewDecoder(ir) dec.UseNumber() - return &jsonInputIter{dec: dec, ir: ir, fname: fname} + next := func() (v any, err error) { err = dec.Decode(&v); return } + return &jsonInputIter{next: next, ir: ir, fname: fname} } func (i *jsonInputIter) Next() (any, bool) { if i.err != nil { return nil, false } - var v any - if err := i.dec.Decode(&v); err != nil { + v, err := i.next() + if err != nil { if err == io.EOF { i.err = err return nil, false @@ -119,6 +120,13 @@ func (i *jsonInputIter) Name() string { return i.fname } +func newStreamInputIter(r io.Reader, fname string) inputIter { + ir := newInputReader(r) + dec := json.NewDecoder(ir) + dec.UseNumber() + return &jsonInputIter{next: newJSONStream(dec).next, ir: ir, fname: fname} +} + type nullInputIter struct { err error } @@ -253,58 +261,6 @@ func (i *rawInputIter) Name() string { return i.fname } -type streamInputIter struct { - stream *jsonStream - ir *inputReader - fname string - offset int64 - line int - err error -} - -func newStreamInputIter(r io.Reader, fname string) inputIter { - ir := newInputReader(r) - dec := json.NewDecoder(ir) - dec.UseNumber() - return &streamInputIter{stream: newJSONStream(dec), ir: ir, fname: fname} -} - -func (i *streamInputIter) Next() (any, bool) { - if i.err != nil { - return nil, false - } - v, err := i.stream.next() - if err != nil { - if err == io.EOF { - i.err = err - return nil, false - } - var offset *int64 - var line *int - if err, ok := err.(*json.SyntaxError); ok { - err.Offset -= i.offset - offset, line = &err.Offset, &i.line - } - i.err = &jsonParseError{i.fname, i.ir.getContents(offset, line), i.line, err} - return i.err, true - } - if buf := i.ir.buf; buf != nil && buf.Len() >= 16*1024 { - i.offset += int64(buf.Len()) - i.line += bytes.Count(buf.Bytes(), []byte{'\n'}) - buf.Reset() - } - return v, true -} - -func (i *streamInputIter) Close() error { - i.err = io.EOF - return nil -} - -func (i *streamInputIter) Name() string { - return i.fname -} - type yamlInputIter struct { dec *yaml.Decoder ir *inputReader