1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-21 01:21:34 +02:00
ferret/pkg/runtime/collections/iterator.go

35 lines
610 B
Go
Raw Normal View History

2018-09-18 22:42:38 +02:00
package collections
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
)
type (
Iterator interface {
2018-10-28 07:45:26 +02:00
Next(ctx context.Context, scope *core.Scope) (*core.Scope, error)
2018-09-18 22:42:38 +02:00
}
Iterable interface {
Iterate(ctx context.Context, scope *core.Scope) (Iterator, error)
}
)
2018-10-28 07:45:26 +02:00
func ToSlice(ctx context.Context, scope *core.Scope, iterator Iterator) ([]*core.Scope, error) {
res := make([]*core.Scope, 0, 10)
2018-09-18 22:42:38 +02:00
2018-10-28 07:45:26 +02:00
for {
nextScope, err := iterator.Next(ctx, scope.Fork())
2018-09-18 22:42:38 +02:00
if err != nil {
return nil, err
}
2018-10-28 07:45:26 +02:00
if nextScope == nil {
return res, nil
}
2018-09-18 22:42:38 +02:00
2018-10-28 07:45:26 +02:00
res = append(res, nextScope)
}
2018-09-18 22:42:38 +02:00
}