Added the RegisterFunc() interface to the expression context

This commit is contained in:
Celestino Amoroso 2024-03-30 06:54:43 +01:00
parent 20007a5a81
commit e012afa691
4 changed files with 8 additions and 4 deletions

View File

@ -4,6 +4,8 @@
// context.go
package expr
type FuncTemplate func(ctx exprContext, name string, args []any) (result any, err error)
type exprFunc interface {
Name() string
MinArgs() int
@ -15,4 +17,5 @@ type exprContext interface {
SetValue(varName string, value any)
GetFuncInfo(name string) exprFunc
Call(name string, args []any) (result any, err error)
RegisterFunc(name string, f FuncTemplate, minArgs, maxArgs int)
}

View File

@ -33,7 +33,7 @@ func EvalStringV(source string, args []EvalArg) (result any, err error) {
for _, arg := range args {
if isFunc(arg.value) {
if f, ok := arg.value.(FuncTemplate); ok {
ctx.addFunc(arg.name, f)
ctx.RegisterFunc(arg.name, f, 0, -1)
} else {
err = fmt.Errorf("invalid function specification: %q", arg.name)
}

View File

@ -3,8 +3,6 @@ package expr
import "fmt"
type FuncTemplate func(ctx exprContext, name string, args []any) (result any, err error)
type SimpleFuncStore struct {
varStore map[string]any
funcStore map[string]FuncTemplate
@ -52,7 +50,7 @@ func (ctx *SimpleFuncStore) GetFuncInfo(name string) (f exprFunc) {
return
}
func (ctx *SimpleFuncStore) addFunc(name string, f FuncTemplate) {
func (ctx *SimpleFuncStore) RegisterFunc(name string, f FuncTemplate, minArgs, maxArgs int) {
ctx.funcStore[name] = f
}

View File

@ -27,3 +27,6 @@ func (ctx *SimpleVarStore) GetFuncInfo(name string) (f exprFunc) {
func (ctx *SimpleVarStore) Call(name string, args []any) (result any, err error) {
return
}
func (ctx *SimpleVarStore) RegisterFunc(name string, f FuncTemplate, minArgs, maxArgs int) {
}