48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
|
// All rights reserved.
|
|
|
|
// iter-factory.go
|
|
package expr
|
|
|
|
import (
|
|
"fmt"
|
|
"slices"
|
|
|
|
"git.portale-stac.it/go-pkg/expr/kern"
|
|
)
|
|
|
|
func NewIterator(ctx kern.ExprContext, value any, args map[string]any) (it kern.Iterator, err error) {
|
|
if value == nil {
|
|
return NewArrayIterator([]any{}), nil
|
|
}
|
|
|
|
switch v := value.(type) {
|
|
case *kern.ListType:
|
|
it = NewListIterator(v, nil)
|
|
case *kern.DictType:
|
|
it, err = NewDictIterator(v, nil)
|
|
case []any:
|
|
it = NewArrayIterator(v)
|
|
case kern.Iterator:
|
|
// it = v
|
|
var op kern.Functor
|
|
if len(args) >= 1 {
|
|
if opArg, ok := args["op"]; ok {
|
|
if op, ok = opArg.(kern.Functor); !ok {
|
|
err = fmt.Errorf("the 'op' argument must be a kern.Functor, got %T", opArg)
|
|
}
|
|
}
|
|
}
|
|
if err == nil {
|
|
it, err = NewIterIter(v, ctx, op, args)
|
|
}
|
|
default:
|
|
it = NewArrayIterator([]any{value})
|
|
}
|
|
return
|
|
}
|
|
|
|
func HasStandardOperation(name string) bool {
|
|
return slices.Contains([]string{kern.NextName, kern.ResetName, kern.IndexName, kern.CountName, kern.CurrentName, kern.CleanName}, name)
|
|
}
|