operand-iterator.go: commented out an unused function and replaced self receiver

This commit is contained in:
Celestino Amoroso 2024-07-07 16:10:43 +02:00
parent bb9493d0cc
commit 2ed1a1842b

View File

@ -5,28 +5,27 @@
package expr package expr
import ( import (
"fmt"
"slices" "slices"
"strings" "strings"
) )
// -------- iterator term // -------- iterator term
func newDsIteratorTerm(tk *Token, dsTerm *term, args []*term) *term { // func newDsIteratorTerm(tk *Token, dsTerm *term, args []*term) *term {
tk.Sym = SymIterator // tk.Sym = SymIterator
children := make([]*term, 0, 1+len(args)) // children := make([]*term, 0, 1+len(args))
children = append(children, dsTerm) // children = append(children, dsTerm)
children = append(children, args...) // children = append(children, args...)
return &term{ // return &term{
tk: *tk, // tk: *tk,
parent: nil, // parent: nil,
children: children, // children: children,
position: posLeaf, // position: posLeaf,
priority: priValue, // priority: priValue,
evalFunc: evalIterator, // evalFunc: evalIterator,
} // }
} // }
func newIteratorTerm(tk *Token, args []*term) *term { func newIteratorTerm(tk *Token, args []*term) *term {
tk.Sym = SymIterator tk.Sym = SymIterator
@ -42,9 +41,9 @@ func newIteratorTerm(tk *Token, args []*term) *term {
// -------- eval iterator // -------- eval iterator
func evalTermArray(ctx ExprContext, a []*term) (values []any, err error) { func evalTermArray(ctx ExprContext, terms []*term) (values []any, err error) {
values = make([]any, len(a)) values = make([]any, len(terms))
for i, t := range a { for i, t := range terms {
var value any var value any
if value, err = t.compute(ctx); err == nil { if value, err = t.compute(ctx); err == nil {
values[i] = value values[i] = value
@ -55,18 +54,17 @@ func evalTermArray(ctx ExprContext, a []*term) (values []any, err error) {
return return
} }
func evalFirstChild(ctx ExprContext, self *term) (value any, err error) { func evalFirstChild(ctx ExprContext, iteratorTerm *term) (value any, err error) {
if len(self.children) < 1 || self.children[0] == nil { if len(iteratorTerm.children) < 1 || iteratorTerm.children[0] == nil {
err = self.Errorf("missing the data-source parameter") err = iteratorTerm.Errorf("missing the data-source parameter")
return return
} }
value, err = self.children[0].compute(ctx) value, err = iteratorTerm.children[0].compute(ctx)
return return
} }
func getDataSourceDict(ctx ExprContext, self *term, firstChildValue any) (ds map[string]Functor, err error) { func getDataSourceDict(iteratorTerm *term, firstChildValue any) (ds map[string]Functor, err error) {
// if dictAny, ok := firstChildValue.(map[any]any); ok {
if dictAny, ok := firstChildValue.(*DictType); ok { if dictAny, ok := firstChildValue.(*DictType); ok {
requiredFields := []string{currentName, nextName} requiredFields := []string{currentName, nextName}
fieldsMask := 0b11 fieldsMask := 0b11
@ -74,7 +72,6 @@ func getDataSourceDict(ctx ExprContext, self *term, firstChildValue any) (ds map
ds = make(map[string]Functor) ds = make(map[string]Functor)
for keyAny, item := range *dictAny { for keyAny, item := range *dictAny {
if key, ok := keyAny.(string); ok { if key, ok := keyAny.(string); ok {
//if functor, ok := item.(*funcDefFunctor); ok {
if functor, ok := item.(Functor); ok { if functor, ok := item.(Functor); ok {
ds[key] = functor ds[key] = functor
if index := slices.Index(requiredFields, key); index >= 0 { if index := slices.Index(requiredFields, key); index >= 0 {
@ -91,21 +88,22 @@ func getDataSourceDict(ctx ExprContext, self *term, firstChildValue any) (ds map
missingFields = append(missingFields, field) missingFields = append(missingFields, field)
} }
} }
err = fmt.Errorf("the data-source must provide a non-nil %q operator(s)", strings.Join(missingFields, ", ")) // err = fmt.Errorf("the data-source must provide a non-nil %q operator(s)", strings.Join(missingFields, ", "))
err = iteratorTerm.children[0].Errorf("the data-source must provide a non-nil %q operator(s)", strings.Join(missingFields, ", "))
} }
} }
return return
} }
func evalIterator(ctx ExprContext, self *term) (v any, err error) { func evalIterator(ctx ExprContext, iteratorTerm *term) (v any, err error) {
var firstChildValue any var firstChildValue any
var ds map[string]Functor var ds map[string]Functor
if firstChildValue, err = evalFirstChild(ctx, self); err != nil { if firstChildValue, err = evalFirstChild(ctx, iteratorTerm); err != nil {
return return
} }
if ds, err = getDataSourceDict(ctx, self, firstChildValue); err != nil { if ds, err = getDataSourceDict(iteratorTerm, firstChildValue); err != nil {
return return
} }
@ -113,8 +111,8 @@ func evalIterator(ctx ExprContext, self *term) (v any, err error) {
dc := newDataCursor(ctx, ds) dc := newDataCursor(ctx, ds)
if initFunc, exists := ds[initName]; exists && initFunc != nil { if initFunc, exists := ds[initName]; exists && initFunc != nil {
var args []any var args []any
if len(self.children) > 1 { if len(iteratorTerm.children) > 1 {
if args, err = evalTermArray(ctx, self.children[1:]); err != nil { if args, err = evalTermArray(ctx, iteratorTerm.children[1:]); err != nil {
return return
} }
} else { } else {
@ -128,20 +126,20 @@ func evalIterator(ctx ExprContext, self *term) (v any, err error) {
exportObjects(dc.ctx, initCtx) exportObjects(dc.ctx, initCtx)
} }
dc.nextFunc, _ = ds[nextName] dc.nextFunc = ds[nextName]
dc.currentFunc, _ = ds[currentName] dc.currentFunc = ds[currentName]
dc.cleanFunc, _ = ds[cleanName] dc.cleanFunc = ds[cleanName]
dc.resetFunc, _ = ds[resetName] dc.resetFunc = ds[resetName]
v = dc v = dc
} else if list, ok := firstChildValue.(*ListType); ok { } else if list, ok := firstChildValue.(*ListType); ok {
var args []any var args []any
if args, err = evalSibling(ctx, self.children, nil); err == nil { if args, err = evalSibling(ctx, iteratorTerm.children, nil); err == nil {
v = NewListIterator(list, args) v = NewListIterator(list, args)
} }
} else { } else {
var list []any var list []any
if list, err = evalSibling(ctx, self.children, firstChildValue); err == nil { if list, err = evalSibling(ctx, iteratorTerm.children, firstChildValue); err == nil {
v = NewArrayIterator(list) v = NewArrayIterator(list)
} }
} }