1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-16 11:37:36 +02:00
ferret/pkg/runtime/collections/indexed.go
Tim Voronov 549b4abd3b
Feature/#5 collect keyword alt (#141)
Implemented COLLECT key word
2018-10-24 21:30:05 -04:00

51 lines
946 B
Go

package collections
import (
"github.com/MontFerret/ferret/pkg/runtime/values"
)
const (
DefaultValueVar = "value"
DefaultKeyVar = "key"
)
type IndexedIterator struct {
valVar string
keyVar string
values IndexedCollection
pos int
}
func NewIndexedIterator(
valVar,
keyVar string,
input IndexedCollection,
) Iterator {
return &IndexedIterator{valVar, keyVar, input, 0}
}
func NewDefaultIndexedIterator(
input IndexedCollection,
) Iterator {
return &IndexedIterator{DefaultValueVar, DefaultKeyVar, input, 0}
}
func (iterator *IndexedIterator) HasNext() bool {
return int(iterator.values.Length()) > iterator.pos
}
func (iterator *IndexedIterator) Next() (DataSet, error) {
if int(iterator.values.Length()) > iterator.pos {
idx := values.NewInt(iterator.pos)
val := iterator.values.Get(idx)
iterator.pos++
return DataSet{
iterator.valVar: val,
iterator.keyVar: idx,
}, nil
}
return nil, ErrExhausted
}