2018-10-25 03:30:05 +02:00
|
|
|
package expressions
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-02-13 19:31:18 +02:00
|
|
|
|
2018-10-25 03:30:05 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/collections"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
2019-02-13 19:31:18 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values/types"
|
2018-10-25 03:30:05 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type DataSource struct {
|
2018-10-28 07:45:26 +02:00
|
|
|
src core.SourceMap
|
|
|
|
valVariable string
|
|
|
|
keyVariable string
|
|
|
|
exp core.Expression
|
2018-10-25 03:30:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDataSource(
|
|
|
|
src core.SourceMap,
|
|
|
|
valVariable,
|
|
|
|
keyVariable string,
|
|
|
|
exp core.Expression,
|
|
|
|
) (collections.Iterable, error) {
|
|
|
|
if exp == nil {
|
|
|
|
return nil, core.Error(core.ErrMissedArgument, "expression")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &DataSource{
|
|
|
|
src,
|
2018-10-28 07:45:26 +02:00
|
|
|
valVariable,
|
|
|
|
keyVariable,
|
2018-10-25 03:30:05 +02:00
|
|
|
exp,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *DataSource) Iterate(ctx context.Context, scope *core.Scope) (collections.Iterator, error) {
|
2018-12-22 06:14:41 +02:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, core.ErrTerminated
|
2018-10-25 03:30:05 +02:00
|
|
|
default:
|
2018-12-22 06:14:41 +02:00
|
|
|
data, err := ds.exp.Exec(ctx, scope)
|
2018-11-13 02:58:12 +02:00
|
|
|
|
2018-12-22 06:14:41 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, core.SourceError(ds.src, err)
|
|
|
|
}
|
2018-11-13 02:58:12 +02:00
|
|
|
|
2018-12-22 06:14:41 +02:00
|
|
|
switch data.Type() {
|
2019-02-13 19:31:18 +02:00
|
|
|
case types.Array:
|
2018-10-28 07:45:26 +02:00
|
|
|
return collections.NewIndexedIterator(ds.valVariable, ds.keyVariable, data.(collections.IndexedCollection))
|
2019-02-13 19:31:18 +02:00
|
|
|
case types.Object:
|
2018-10-28 07:45:26 +02:00
|
|
|
return collections.NewKeyedIterator(ds.valVariable, ds.keyVariable, data.(collections.KeyedCollection))
|
2018-10-25 03:30:05 +02:00
|
|
|
default:
|
2018-12-22 06:14:41 +02:00
|
|
|
// fallback to user defined types
|
2019-02-15 14:41:08 +02:00
|
|
|
switch collection := data.(type) {
|
|
|
|
case core.Iterable:
|
2018-12-22 06:14:41 +02:00
|
|
|
iterator, err := collection.Iterate(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-02-15 14:41:08 +02:00
|
|
|
return collections.NewCoreIterator(ds.valVariable, ds.keyVariable, iterator)
|
2018-12-22 06:14:41 +02:00
|
|
|
case collections.KeyedCollection:
|
2019-02-15 14:41:08 +02:00
|
|
|
return collections.NewKeyedIterator(ds.valVariable, ds.keyVariable, collection)
|
2018-12-22 06:14:41 +02:00
|
|
|
case collections.IndexedCollection:
|
2019-02-15 14:41:08 +02:00
|
|
|
return collections.NewIndexedIterator(ds.valVariable, ds.keyVariable, collection)
|
2018-12-22 06:14:41 +02:00
|
|
|
default:
|
|
|
|
return nil, core.TypeError(
|
|
|
|
data.Type(),
|
2019-02-13 19:31:18 +02:00
|
|
|
types.Array,
|
|
|
|
types.Object,
|
2019-02-15 14:41:08 +02:00
|
|
|
core.NewType("Iterable"),
|
2018-12-22 06:14:41 +02:00
|
|
|
)
|
|
|
|
}
|
2018-10-25 03:30:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|