Added simple context objects
This commit is contained in:
parent
fdc2fd8dfd
commit
37f0de5902
66
simple-func-store.go
Normal file
66
simple-func-store.go
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
// simple-var-store.go
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
type funcInfo struct {
|
||||||
|
name string
|
||||||
|
// minArgs int
|
||||||
|
// maxArgs int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (info *funcInfo) Name() string {
|
||||||
|
return info.name
|
||||||
|
}
|
||||||
|
|
||||||
|
func (info *funcInfo) MinArgs() int {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (info *funcInfo) MaxArgs() int {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSimpleFuncStore() *SimpleFuncStore {
|
||||||
|
return &SimpleFuncStore{
|
||||||
|
varStore: make(map[string]any),
|
||||||
|
funcStore: make(map[string]FuncTemplate),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ctx *SimpleFuncStore) GetValue(varName string) (v any, exists bool) {
|
||||||
|
v, exists = ctx.varStore[varName]
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ctx *SimpleFuncStore) SetValue(varName string, value any) {
|
||||||
|
ctx.varStore[varName] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ctx *SimpleFuncStore) GetFuncInfo(name string) (f exprFunc) {
|
||||||
|
var exists bool
|
||||||
|
if _, exists = ctx.funcStore[name]; exists {
|
||||||
|
f = &funcInfo{name: name}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ctx *SimpleFuncStore) addFunc(name string, f FuncTemplate) {
|
||||||
|
ctx.funcStore[name] = f
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ctx *SimpleFuncStore) Call(name string, args []any) (result any, err error) {
|
||||||
|
if f, exists := ctx.funcStore[name]; exists {
|
||||||
|
result, err = f(ctx, name, args)
|
||||||
|
} else {
|
||||||
|
err = fmt.Errorf("unknown function %s()", name)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
29
simple-var-store.go
Normal file
29
simple-var-store.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
// simple-var-store.go
|
||||||
|
package expr
|
||||||
|
|
||||||
|
type SimpleVarStore struct {
|
||||||
|
store map[string]any
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSimpleVarStore() *SimpleVarStore {
|
||||||
|
return &SimpleVarStore{
|
||||||
|
store: make(map[string]any),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ctx *SimpleVarStore) GetValue(varName string) (v any, exists bool) {
|
||||||
|
v, exists = ctx.store[varName]
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ctx *SimpleVarStore) SetValue(varName string, value any) {
|
||||||
|
ctx.store[varName] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ctx *SimpleVarStore) GetFuncInfo(name string) (f exprFunc) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ctx *SimpleVarStore) Call(name string, args []any) (result any, err error) {
|
||||||
|
return
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user