rationalized context convertion to dict and string types

This commit is contained in:
2026-05-21 03:52:48 +02:00
parent a62f27b104
commit e1c24daac4
5 changed files with 92 additions and 72 deletions
+10 -6
View File
@@ -76,9 +76,11 @@ func (dict *DictType) toMultiLine(sb *strings.Builder, opt FmtOpt) {
sb.WriteString(nest) sb.WriteString(nest)
if key, ok := name.(string); ok { if key, ok := name.(string); ok {
sb.WriteString(string('"') + key + string('"')) sb.WriteByte('"')
sb.WriteString(key)
sb.WriteByte('"')
} else { } else {
sb.WriteString(fmt.Sprintf("%v", name)) fmt.Fprintf(sb, "%v", name)
} }
sb.WriteString(": ") sb.WriteString(": ")
if f, ok := value.(Formatter); ok { if f, ok := value.(Formatter); ok {
@@ -86,7 +88,7 @@ func (dict *DictType) toMultiLine(sb *strings.Builder, opt FmtOpt) {
} else if _, ok = value.(Functor); ok { } else if _, ok = value.(Functor); ok {
sb.WriteString("func(){}") sb.WriteString("func(){}")
} else { } else {
sb.WriteString(fmt.Sprintf("%v", value)) fmt.Fprintf(sb, "%v", value)
} }
} }
sb.WriteByte('\n') sb.WriteByte('\n')
@@ -110,9 +112,11 @@ func (dict *DictType) ToString(opt FmtOpt) string {
sb.WriteString(", ") sb.WriteString(", ")
} }
if s, ok := key.(string); ok { if s, ok := key.(string); ok {
sb.WriteString(string('"') + s + string('"')) sb.WriteByte('"')
sb.WriteString(s)
sb.WriteByte('"')
} else { } else {
sb.WriteString(fmt.Sprintf("%v", key)) fmt.Fprintf(&sb, "%v", key)
} }
sb.WriteString(": ") sb.WriteString(": ")
if formatter, ok := value.(Formatter); ok { if formatter, ok := value.(Formatter); ok {
@@ -120,7 +124,7 @@ func (dict *DictType) ToString(opt FmtOpt) string {
} else if t, ok := value.(Term); ok { } else if t, ok := value.(Term); ok {
sb.WriteString(t.String()) sb.WriteString(t.String())
} else { } else {
sb.WriteString(fmt.Sprintf("%#v", value)) fmt.Fprintf(&sb, "%#v", value)
} }
} }
sb.WriteByte('}') sb.WriteByte('}')
+32
View File
@@ -28,4 +28,36 @@ type ExprContext interface {
Call(name string, args map[string]any) (result any, err error) Call(name string, args map[string]any) (result any, err error)
RegisterFuncInfo(info ExprFunc) RegisterFuncInfo(info ExprFunc)
RegisterFunc(name string, f Functor, returnType string, param []ExprFuncParam) (funcInfo ExprFunc, err error) RegisterFunc(name string, f Functor, returnType string, param []ExprFuncParam) (funcInfo ExprFunc, err error)
ToDict() (dict *DictType)
ToString(opt FmtOpt) string
}
func ContextToDict(ctx ExprContext) (dict *DictType) {
var keys []string
// Variables
keys = ctx.EnumVars(nil)
vars := MakeDict()
for _, key := range keys {
value, _ := ctx.GetVar(key)
vars.SetItem(key, value)
}
// Functions
keys = ctx.EnumFuncs(func(name string) bool { return true })
funcs := MakeDict()
for _, key := range keys {
funcInfo, _ := ctx.GetFuncInfo(key)
funcs.SetItem(key, funcInfo)
}
dict = MakeDict()
dict.SetItem("vars", vars)
dict.SetItem("funcs", funcs)
return
}
func ContextToString(ctx ExprContext, opt FmtOpt) string {
dict := ctx.ToDict()
return dict.ToString(opt)
} }
+1 -23
View File
@@ -40,7 +40,7 @@ func evalContextValue(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error
} }
if sourceCtx != nil { if sourceCtx != nil {
v = contextToDict(sourceCtx) v = sourceCtx.ToDict()
} else if childValue != nil { } else if childValue != nil {
it, ok := childValue.(kern.Iterator) it, ok := childValue.(kern.Iterator)
if !ok { if !ok {
@@ -63,28 +63,6 @@ func evalContextValue(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error
return return
} }
func contextToDict(ctx kern.ExprContext) (dict *kern.DictType) {
// Variables
keys := ctx.EnumVars(nil)
vars := kern.MakeDict()
for _, key := range keys {
value, _ := ctx.GetVar(key)
vars.SetItem(key, value)
}
// Functions
keys = ctx.EnumFuncs(func(name string) bool { return true })
funcs := kern.MakeDict()
for _, key := range keys {
funcInfo, _ := ctx.GetFuncInfo(key)
funcs.SetItem(key, funcInfo)
}
dict = kern.MakeDict()
dict.SetItem("vars", vars)
dict.SetItem("funcs", funcs)
return
}
// init // init
func init() { func init() {
scan.RegisterTermConstructor(scan.SymDoubleDollar, newContextTerm) scan.RegisterTermConstructor(scan.SymDoubleDollar, newContextTerm)
+47 -40
View File
@@ -6,7 +6,6 @@ package expr
import ( import (
"fmt" "fmt"
"slices"
"git.portale-stac.it/go-pkg/expr/kern" "git.portale-stac.it/go-pkg/expr/kern"
"git.portale-stac.it/go-pkg/expr/util" "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) { func (ctx *SimpleStore) ToDict() (dict *kern.DictType) {
dict = kern.MakeDict() return kern.ContextToDict(ctx)
(*dict)["variables"] = ctx.varsToDict(kern.MakeDict())
(*dict)["functions"] = ctx.funcsToDict(kern.MakeDict())
return
} }
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) { func (ctx *SimpleStore) GetVar(varName string) (value any, exists bool) {
if value, exists = ctx.varStore[varName]; !exists && ctx.global != nil { if value, exists = ctx.varStore[varName]; !exists && ctx.global != nil {
value, exists = ctx.global.GetVar(varName) value, exists = ctx.global.GetVar(varName)
+2 -3
View File
@@ -74,7 +74,6 @@ func TestCtrlEnable(t *testing.T) {
if !CtrlDisable(ctx, varName) { if !CtrlDisable(ctx, varName) {
t.Errorf(`%s -- CtrlEnable(ctx, %q) should have returned 'true', got 'false'`, section, varName) t.Errorf(`%s -- CtrlEnable(ctx, %q) should have returned 'true', got 'false'`, section, varName)
// t.Errorf(`%s -- CtrlEnable(ctx, %q) should have returned 'false', got 'true'`, section, varName)
} }
} }
@@ -88,7 +87,7 @@ func TestList(t *testing.T) {
/* 3 */ {`string(($$global).funcs.bool)`, `bool(value):boolean{}`, nil}, /* 3 */ {`string(($$global).funcs.bool)`, `bool(value):boolean{}`, nil},
} }
runTestSuiteSpec(t, section, inputs, 3) // runTestSuiteSpec(t, section, inputs, 3)
// runTestSuite(t, section, inputs) runTestSuite(t, section, inputs)
} }