1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-16 11:37:36 +02:00
ferret/pkg/drivers/common/iterator.go

38 lines
739 B
Go
Raw Normal View History

package common
import (
"context"
"github.com/MontFerret/ferret/pkg/drivers"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
type Iterator struct {
node drivers.HTMLElement
pos values.Int
}
func NewIterator(
node drivers.HTMLElement,
) (core.Iterator, error) {
if node == nil {
return nil, core.Error(core.ErrMissedArgument, "result")
}
return &Iterator{node, 0}, nil
}
func (iterator *Iterator) Next(ctx context.Context) (value core.Value, key core.Value, err error) {
if iterator.node.Length() > iterator.pos {
idx := iterator.pos
val := iterator.node.GetChildNode(ctx, idx)
iterator.pos++
return val, idx, nil
}
return values.None, values.None, nil
}