rationalized context convertion to dict and string types
This commit is contained in:
+47
-40
@@ -6,7 +6,6 @@ package expr
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
|
||||
"git.portale-stac.it/go-pkg/expr/kern"
|
||||
"git.portale-stac.it/go-pkg/expr/util"
|
||||
@@ -77,48 +76,56 @@ func (ctx *SimpleStore) Clone() kern.ExprContext {
|
||||
// }
|
||||
// }
|
||||
|
||||
func (ctx *SimpleStore) ToString(opt kern.FmtOpt) string {
|
||||
dict := ctx.ToDict()
|
||||
return dict.ToString(opt)
|
||||
}
|
||||
|
||||
func (ctx *SimpleStore) varsToDict(dict *kern.DictType) *kern.DictType {
|
||||
names := ctx.EnumVars(nil)
|
||||
slices.Sort(names)
|
||||
for _, name := range ctx.EnumVars(nil) {
|
||||
value, _ := ctx.GetVar(name)
|
||||
if f, ok := value.(kern.Formatter); ok {
|
||||
(*dict)[name] = f.ToString(0)
|
||||
} else if _, ok = value.(kern.Functor); ok {
|
||||
(*dict)[name] = "func(){}"
|
||||
} else {
|
||||
(*dict)[name] = fmt.Sprintf("%v", value)
|
||||
}
|
||||
}
|
||||
return dict
|
||||
}
|
||||
|
||||
func (ctx *SimpleStore) funcsToDict(dict *kern.DictType) *kern.DictType {
|
||||
names := ctx.EnumFuncs(func(name string) bool { return true })
|
||||
slices.Sort(names)
|
||||
for _, name := range names {
|
||||
value, _ := ctx.GetFuncInfo(name)
|
||||
if formatter, ok := value.(kern.Formatter); ok {
|
||||
(*dict)[name] = formatter.ToString(0)
|
||||
} else {
|
||||
(*dict)[name] = fmt.Sprintf("%v", value)
|
||||
}
|
||||
}
|
||||
return dict
|
||||
}
|
||||
|
||||
func (ctx *SimpleStore) ToDict() (dict *kern.DictType) {
|
||||
dict = kern.MakeDict()
|
||||
(*dict)["variables"] = ctx.varsToDict(kern.MakeDict())
|
||||
(*dict)["functions"] = ctx.funcsToDict(kern.MakeDict())
|
||||
return
|
||||
return kern.ContextToDict(ctx)
|
||||
}
|
||||
|
||||
func (ctx *SimpleStore) ToString(opt kern.FmtOpt) string {
|
||||
return kern.ContextToString(ctx, opt)
|
||||
}
|
||||
|
||||
// func (ctx *SimpleStore) ToString(opt kern.FmtOpt) string {
|
||||
// dict := ctx.ToDict()
|
||||
// return dict.ToString(opt)
|
||||
// }
|
||||
|
||||
// func (ctx *SimpleStore) varsToDict(dict *kern.DictType) *kern.DictType {
|
||||
// names := ctx.EnumVars(nil)
|
||||
// slices.Sort(names)
|
||||
// for _, name := range ctx.EnumVars(nil) {
|
||||
// value, _ := ctx.GetVar(name)
|
||||
// if f, ok := value.(kern.Formatter); ok {
|
||||
// (*dict)[name] = f.ToString(0)
|
||||
// } else if _, ok = value.(kern.Functor); ok {
|
||||
// (*dict)[name] = "func(){}"
|
||||
// } else {
|
||||
// (*dict)[name] = fmt.Sprintf("%v", value)
|
||||
// }
|
||||
// }
|
||||
// return dict
|
||||
// }
|
||||
|
||||
// func (ctx *SimpleStore) funcsToDict(dict *kern.DictType) *kern.DictType {
|
||||
// names := ctx.EnumFuncs(func(name string) bool { return true })
|
||||
// slices.Sort(names)
|
||||
// for _, name := range names {
|
||||
// value, _ := ctx.GetFuncInfo(name)
|
||||
// if formatter, ok := value.(kern.Formatter); ok {
|
||||
// (*dict)[name] = formatter.ToString(0)
|
||||
// } else {
|
||||
// (*dict)[name] = fmt.Sprintf("%v", value)
|
||||
// }
|
||||
// }
|
||||
// return dict
|
||||
// }
|
||||
|
||||
// func (ctx *SimpleStore) ToDict() (dict *kern.DictType) {
|
||||
// dict = kern.MakeDict()
|
||||
// (*dict)["variables"] = ctx.varsToDict(kern.MakeDict())
|
||||
// (*dict)["functions"] = ctx.funcsToDict(kern.MakeDict())
|
||||
// return
|
||||
// }
|
||||
|
||||
func (ctx *SimpleStore) GetVar(varName string) (value any, exists bool) {
|
||||
if value, exists = ctx.varStore[varName]; !exists && ctx.global != nil {
|
||||
value, exists = ctx.global.GetVar(varName)
|
||||
|
||||
Reference in New Issue
Block a user