2018-09-18 22:42:38 +02:00
|
|
|
package collections
|
|
|
|
|
2018-10-25 03:30:05 +02:00
|
|
|
import (
|
2018-11-13 02:58:12 +02:00
|
|
|
"context"
|
2018-10-25 03:30:05 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
|
|
|
)
|
2018-09-18 22:42:38 +02:00
|
|
|
|
2018-10-25 03:30:05 +02:00
|
|
|
type (
|
|
|
|
Collection interface {
|
2018-11-13 02:58:12 +02:00
|
|
|
core.Value
|
2018-10-25 03:30:05 +02:00
|
|
|
Length() values.Int
|
|
|
|
}
|
|
|
|
|
|
|
|
IndexedCollection interface {
|
|
|
|
Collection
|
|
|
|
Get(idx values.Int) core.Value
|
|
|
|
Set(idx values.Int, value core.Value) error
|
|
|
|
}
|
|
|
|
|
|
|
|
KeyedCollection interface {
|
|
|
|
Collection
|
|
|
|
Keys() []string
|
|
|
|
Get(key values.String) (core.Value, values.Boolean)
|
|
|
|
Set(key values.String, value core.Value)
|
|
|
|
}
|
2018-11-13 02:58:12 +02:00
|
|
|
|
|
|
|
IterableCollection interface {
|
|
|
|
core.Value
|
|
|
|
Iterate(ctx context.Context) (CollectionIterator, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
CollectionIterator interface {
|
|
|
|
Next(ctx context.Context) (value core.Value, key core.Value, err error)
|
|
|
|
}
|
|
|
|
|
|
|
|
collectionIteratorWrapper struct {
|
|
|
|
valVar string
|
|
|
|
keyVar string
|
|
|
|
values CollectionIterator
|
|
|
|
}
|
2018-10-25 03:30:05 +02:00
|
|
|
)
|
2018-11-13 02:58:12 +02:00
|
|
|
|
|
|
|
func NewCollectionIterator(
|
|
|
|
valVar,
|
|
|
|
keyVar string,
|
|
|
|
values CollectionIterator,
|
|
|
|
) (Iterator, error) {
|
|
|
|
return &collectionIteratorWrapper{valVar, keyVar, values}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (iterator *collectionIteratorWrapper) Next(ctx context.Context, scope *core.Scope) (*core.Scope, error) {
|
|
|
|
val, key, err := iterator.values.Next(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// end of iteration
|
|
|
|
if val == values.None {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
nextScope := scope.Fork()
|
|
|
|
|
|
|
|
if err := nextScope.SetVariable(iterator.valVar, val); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if iterator.keyVar != "" {
|
|
|
|
if err := nextScope.SetVariable(iterator.keyVar, key); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nextScope, nil
|
|
|
|
}
|