Skip to content

Commit

Permalink
use NewIter instead of unitIterator and newIntIter
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed May 15, 2021
1 parent c457f97 commit 1e9d96f
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 41 deletions.
4 changes: 2 additions & 2 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ func (c *Code) Run(v interface{}, values ...interface{}) Iter {
// RunWithContext runs the code with context.
func (c *Code) RunWithContext(ctx context.Context, v interface{}, values ...interface{}) Iter {
if len(values) > len(c.variables) {
return unitIterator(&tooManyVariableValuesError{})
return NewIter(&tooManyVariableValuesError{})
} else if len(values) < len(c.variables) {
return unitIterator(&expectedVariableError{c.variables[len(values)]})
return NewIter(&expectedVariableError{c.variables[len(values)]})
}
for i, v := range values {
values[i] = normalizeNumbers(v)
Expand Down
2 changes: 1 addition & 1 deletion compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func BenchmarkCompile(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := gojq.Compile(
query,
gojq.WithInputIter(newIntIter()),
gojq.WithInputIter(gojq.NewIter()),
)
if err != nil {
b.Fatal(err)
Expand Down
17 changes: 0 additions & 17 deletions iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,6 @@ type Iter interface {
Next() (interface{}, bool)
}

func unitIterator(v interface{}) Iter {
return &unitIter{v: v}
}

type unitIter struct {
v interface{}
done bool
}

func (iter *unitIter) Next() (interface{}, bool) {
if iter.done {
return nil, false
}
iter.done = true
return iter.v, true
}

// NewIter creates a new Iter from values.
func NewIter(values ...interface{}) Iter {
iter := sliceIter(values)
Expand Down
20 changes: 1 addition & 19 deletions option_input_iter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,14 @@ import (
"github.com/itchyny/gojq"
)

type intIter struct {
values []int
index int
}

func newIntIter(xs ...int) *intIter {
return &intIter{xs, 0}
}

func (iter *intIter) Next() (interface{}, bool) {
if len(iter.values) == iter.index {
return nil, false
}
v := iter.values[iter.index]
iter.index++
return v, true
}

func ExampleWithInputIter() {
query, err := gojq.Parse("reduce inputs as $x (0; . + $x)")
if err != nil {
log.Fatalln(err)
}
code, err := gojq.Compile(
query,
gojq.WithInputIter(newIntIter(1, 2, 3, 4, 5)),
gojq.WithInputIter(gojq.NewIter(1, 2, 3, 4, 5)),
)
if err != nil {
log.Fatalln(err)
Expand Down
2 changes: 1 addition & 1 deletion option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ func TestWithInputIter(t *testing.T) {
}
code, err := gojq.Compile(
query,
gojq.WithInputIter(newIntIter(1, 2, 3, 4, 5)),
gojq.WithInputIter(gojq.NewIter(1, 2, 3, 4, 5)),
)
if err != nil {
t.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion query.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (e *Query) Run(v interface{}) Iter {
func (e *Query) RunWithContext(ctx context.Context, v interface{}) Iter {
code, err := Compile(e)
if err != nil {
return unitIterator(err)
return NewIter(err)
}
return code.RunWithContext(ctx, v)
}
Expand Down

0 comments on commit 1e9d96f

Please sign in to comment.