1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-16 11:37:36 +02:00
ferret/pkg/runtime/collections/collection.go
Tim Voronov 376ad77404
Feature/#250 wait style (#255)
* Added support for parsed styles

* Added stdlib function.

* Added e2e tests

* Added e2e tests for STYLE_* functions
2019-03-13 14:51:30 -04:00

71 lines
1.3 KiB
Go

package collections
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
)
type (
// Measurable represents an interface of a value that can has length.
Measurable interface {
Length() values.Int
}
IndexedCollection interface {
core.Value
Measurable
Get(idx values.Int) core.Value
Set(idx values.Int, value core.Value) error
}
KeyedCollection interface {
core.Value
Measurable
Keys() []values.String
Get(key values.String) (core.Value, values.Boolean)
Set(key values.String, value core.Value)
}
coreIterator struct {
valVar string
keyVar string
values core.Iterator
}
)
func NewCoreIterator(
valVar,
keyVar string,
values core.Iterator,
) (Iterator, error) {
return &coreIterator{valVar, keyVar, values}, nil
}
func (iterator *coreIterator) 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
}