2024-04-13 06:00:22 +02:00
|
|
|
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
|
|
|
// All rights reserved.
|
|
|
|
|
2024-03-28 06:29:11 +01:00
|
|
|
// simple-var-store.go
|
|
|
|
package expr
|
|
|
|
|
2024-04-09 07:12:22 +02:00
|
|
|
import "fmt"
|
|
|
|
|
2024-03-28 06:29:11 +01:00
|
|
|
type SimpleVarStore struct {
|
2024-04-04 12:54:26 +02:00
|
|
|
varStore map[string]any
|
2024-03-28 06:29:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewSimpleVarStore() *SimpleVarStore {
|
|
|
|
return &SimpleVarStore{
|
2024-04-04 12:54:26 +02:00
|
|
|
varStore: make(map[string]any),
|
2024-03-28 06:29:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-08 23:17:56 +02:00
|
|
|
func (ctx *SimpleVarStore) Clone() (clone ExprContext) {
|
2024-04-02 04:36:03 +02:00
|
|
|
clone = &SimpleVarStore{
|
2024-04-04 12:54:26 +02:00
|
|
|
varStore: CloneMap(ctx.varStore),
|
2024-04-02 04:36:03 +02:00
|
|
|
}
|
|
|
|
return clone
|
|
|
|
}
|
|
|
|
|
2024-04-03 06:29:57 +02:00
|
|
|
func (ctx *SimpleVarStore) GetVar(varName string) (v any, exists bool) {
|
2024-04-04 12:54:26 +02:00
|
|
|
v, exists = ctx.varStore[varName]
|
2024-03-28 06:29:11 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-09 07:12:22 +02:00
|
|
|
func (ctx *SimpleVarStore) setVar(varName string, value any) {
|
2024-04-04 12:54:26 +02:00
|
|
|
ctx.varStore[varName] = value
|
|
|
|
}
|
|
|
|
|
2024-04-09 07:12:22 +02:00
|
|
|
func (ctx *SimpleVarStore) SetVar(varName string, value any) {
|
|
|
|
if allowedValue, ok := fromGenericAny(value); ok {
|
|
|
|
ctx.varStore[varName] = allowedValue
|
|
|
|
} else {
|
|
|
|
panic(fmt.Errorf("unsupported type %T of value %v", value, value))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-04 12:54:26 +02:00
|
|
|
func (ctx *SimpleVarStore) EnumVars(acceptor func(name string) (accept bool)) (varNames []string) {
|
|
|
|
varNames = make([]string, 0)
|
|
|
|
for name := range ctx.varStore {
|
|
|
|
if acceptor != nil {
|
|
|
|
if acceptor(name) {
|
|
|
|
varNames = append(varNames, name)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
varNames = append(varNames, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
2024-03-28 06:29:11 +01:00
|
|
|
}
|
|
|
|
|
2024-04-26 04:36:03 +02:00
|
|
|
func (ctx *SimpleVarStore) GetFuncInfo(name string) (f ExprFunc, exists bool) {
|
2024-03-28 06:29:11 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *SimpleVarStore) Call(name string, args []any) (result any, err error) {
|
|
|
|
return
|
|
|
|
}
|
2024-03-30 06:54:43 +01:00
|
|
|
|
2024-04-02 04:36:03 +02:00
|
|
|
func (ctx *SimpleVarStore) RegisterFunc(name string, functor Functor, minArgs, maxArgs int) {
|
2024-03-30 06:54:43 +01:00
|
|
|
}
|
2024-04-06 01:00:29 +02:00
|
|
|
|
|
|
|
func (ctx *SimpleVarStore) EnumFuncs(acceptor func(name string) (accept bool)) (funcNames []string) {
|
|
|
|
return
|
|
|
|
}
|