Added the RegisterFunc() interface to the expression context
This commit is contained in:
parent
20007a5a81
commit
e012afa691
@ -4,6 +4,8 @@
|
|||||||
// context.go
|
// context.go
|
||||||
package expr
|
package expr
|
||||||
|
|
||||||
|
type FuncTemplate func(ctx exprContext, name string, args []any) (result any, err error)
|
||||||
|
|
||||||
type exprFunc interface {
|
type exprFunc interface {
|
||||||
Name() string
|
Name() string
|
||||||
MinArgs() int
|
MinArgs() int
|
||||||
@ -15,4 +17,5 @@ type exprContext interface {
|
|||||||
SetValue(varName string, value any)
|
SetValue(varName string, value any)
|
||||||
GetFuncInfo(name string) exprFunc
|
GetFuncInfo(name string) exprFunc
|
||||||
Call(name string, args []any) (result any, err error)
|
Call(name string, args []any) (result any, err error)
|
||||||
|
RegisterFunc(name string, f FuncTemplate, minArgs, maxArgs int)
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@ func EvalStringV(source string, args []EvalArg) (result any, err error) {
|
|||||||
for _, arg := range args {
|
for _, arg := range args {
|
||||||
if isFunc(arg.value) {
|
if isFunc(arg.value) {
|
||||||
if f, ok := arg.value.(FuncTemplate); ok {
|
if f, ok := arg.value.(FuncTemplate); ok {
|
||||||
ctx.addFunc(arg.name, f)
|
ctx.RegisterFunc(arg.name, f, 0, -1)
|
||||||
} else {
|
} else {
|
||||||
err = fmt.Errorf("invalid function specification: %q", arg.name)
|
err = fmt.Errorf("invalid function specification: %q", arg.name)
|
||||||
}
|
}
|
||||||
|
@ -3,8 +3,6 @@ package expr
|
|||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
type FuncTemplate func(ctx exprContext, name string, args []any) (result any, err error)
|
|
||||||
|
|
||||||
type SimpleFuncStore struct {
|
type SimpleFuncStore struct {
|
||||||
varStore map[string]any
|
varStore map[string]any
|
||||||
funcStore map[string]FuncTemplate
|
funcStore map[string]FuncTemplate
|
||||||
@ -52,7 +50,7 @@ func (ctx *SimpleFuncStore) GetFuncInfo(name string) (f exprFunc) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ctx *SimpleFuncStore) addFunc(name string, f FuncTemplate) {
|
func (ctx *SimpleFuncStore) RegisterFunc(name string, f FuncTemplate, minArgs, maxArgs int) {
|
||||||
ctx.funcStore[name] = f
|
ctx.funcStore[name] = f
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,3 +27,6 @@ func (ctx *SimpleVarStore) GetFuncInfo(name string) (f exprFunc) {
|
|||||||
func (ctx *SimpleVarStore) Call(name string, args []any) (result any, err error) {
|
func (ctx *SimpleVarStore) Call(name string, args []any) (result any, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ctx *SimpleVarStore) RegisterFunc(name string, f FuncTemplate, minArgs, maxArgs int) {
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user