Skip to content

Commit 4644ec3

Browse files
authored
change NewIter() into a generic function (#285)
1 parent 85cb9b6 commit 4644ec3

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

compiler_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ func BenchmarkCompile(b *testing.B) {
352352
for b.Loop() {
353353
_, err := gojq.Compile(
354354
query,
355-
gojq.WithInputIter(gojq.NewIter()),
355+
gojq.WithInputIter(gojq.NewIter[any]()),
356356
)
357357
if err != nil {
358358
b.Fatal(err)

iter.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ type Iter interface {
66
}
77

88
// NewIter creates a new [Iter] from values.
9-
func NewIter(values ...any) Iter {
9+
func NewIter[T any](values ...T) Iter {
1010
switch len(values) {
1111
case 0:
1212
return emptyIter{}
1313
case 1:
1414
return &unitIter{value: values[0]}
1515
default:
16-
iter := sliceIter(values)
16+
iter := sliceIter[T](values)
1717
return &iter
1818
}
1919
}
@@ -37,9 +37,9 @@ func (iter *unitIter) Next() (any, bool) {
3737
return iter.value, true
3838
}
3939

40-
type sliceIter []any
40+
type sliceIter[T any] []T
4141

42-
func (iter *sliceIter) Next() (any, bool) {
42+
func (iter *sliceIter[T]) Next() (any, bool) {
4343
if len(*iter) == 0 {
4444
return nil, false
4545
}

option_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ func TestWithIterFunction(t *testing.T) {
629629
return gojq.NewIter(fmt.Errorf("g cannot be applied to: %v", xs))
630630
}),
631631
gojq.WithIterFunction("h", 0, 0, func(any, []any) gojq.Iter {
632-
return gojq.NewIter()
632+
return gojq.NewIter[any]()
633633
}),
634634
)
635635
if err != nil {
@@ -659,7 +659,7 @@ func TestWithIterFunctionError(t *testing.T) {
659659
}
660660
code, err := gojq.Compile(query,
661661
gojq.WithIterFunction("f", 0, 0, func(any, []any) gojq.Iter {
662-
return gojq.NewIter(1, errors.New("error"), 3)
662+
return gojq.NewIter[any](1, errors.New("error"), 3)
663663
}),
664664
)
665665
if err != nil {
@@ -750,7 +750,7 @@ func TestWithIterFunctionPathEmpty(t *testing.T) {
750750
}
751751
code, err := gojq.Compile(query,
752752
gojq.WithIterFunction("f", 0, 0, func(any, []any) gojq.Iter {
753-
return gojq.NewIter()
753+
return gojq.NewIter[any]()
754754
}),
755755
)
756756
if err != nil {
@@ -838,7 +838,7 @@ func TestWithIterFunctionDefineError(t *testing.T) {
838838
return 0
839839
}),
840840
gojq.WithIterFunction("f", 0, 0, func(any, []any) gojq.Iter {
841-
return gojq.NewIter()
841+
return gojq.NewIter[any]()
842842
}),
843843
))
844844
}

0 commit comments

Comments
 (0)