improved position of some common functions

This commit is contained in:
Celestino Amoroso 2024-06-09 07:38:29 +02:00
parent 53bcf90d2a
commit 8eb2d77ea3
4 changed files with 33 additions and 38 deletions

View File

@ -31,11 +31,3 @@ func initDefaultVars(ctx ExprContext) {
ctx.SetVar(ControlBoolShortcut, true)
ctx.SetVar(ControlSearchPath, init_search_path)
}
func getControlString(ctx ExprContext, name string) (s string, exists bool) {
var v any
if v, exists = ctx.GetVar(name); exists {
s, exists = v.(string)
}
return
}

View File

@ -9,8 +9,6 @@ import (
"os"
)
const ENV_EXPR_PATH = "EXPR_PATH"
func importFunc(ctx ExprContext, name string, args []any) (result any, err error) {
return importGeneral(ctx, name, args)
}
@ -21,19 +19,11 @@ func importAllFunc(ctx ExprContext, name string, args []any) (result any, err er
}
func importGeneral(ctx ExprContext, name string, args []any) (result any, err error) {
var dirList []string
dirList = addEnvImportDirs(dirList)
dirList = addPresetImportDirs(ctx, dirList)
dirList := buildSearchDirList("sources", ENV_EXPR_SOURCE_PATH)
result, err = doImport(ctx, name, dirList, NewArrayIterator(args))
return
}
func addPresetImportDirs(ctx ExprContext, dirList []string) []string {
// return addPresetDirs(ctx, ControlImportPath, dirList)
return addSearchDirs(ctx, "sources", dirList)
}
func doImport(ctx ExprContext, name string, dirList []string, it Iterator) (result any, err error) {
var v any
var sourceFilepath string

View File

@ -128,6 +128,14 @@ func CtrlIsEnabled(ctx ExprContext, name string) (status bool) {
return
}
func getControlString(name string) (s string, exists bool) {
var v any
if v, exists = globalCtx.GetVar(name); exists {
s, exists = v.(string)
}
return
}
func init() {
globalCtx = NewSimpleStore()
initDefaultVars(globalCtx)

View File

@ -13,6 +13,11 @@ import (
"strings"
)
const (
ENV_EXPR_SOURCE_PATH = "EXPR_PATH"
ENV_EXPR_PLUGIN_PATH = "EXPR_PLUGIN_PATH"
)
func checkStringParamExpected(funcName string, paramValue any, paramPos int) (err error) {
if !(IsString(paramValue) /*|| isList(paramValue)*/) {
err = fmt.Errorf("%s(): param nr %d has wrong type %s, string expected", funcName, paramPos+1, typeName(paramValue))
@ -20,8 +25,16 @@ func checkStringParamExpected(funcName string, paramValue any, paramPos int) (er
return
}
func addEnvImportDirs(dirList []string) []string {
if dirSpec, exists := os.LookupEnv(ENV_EXPR_PATH); exists {
func addSourceEnvImportDirs(varName string, dirList []string) []string {
return addEnvImportDirs(ENV_EXPR_SOURCE_PATH, dirList)
}
func addPluginEnvImportDirs(varName string, dirList []string) []string {
return addEnvImportDirs(ENV_EXPR_PLUGIN_PATH, dirList)
}
func addEnvImportDirs(envVarName string, dirList []string) []string {
if dirSpec, exists := os.LookupEnv(envVarName); exists {
dirs := strings.Split(dirSpec, ":")
if dirList == nil {
dirList = dirs
@ -32,22 +45,8 @@ func addEnvImportDirs(dirList []string) []string {
return dirList
}
/*
func addPresetDirs(ctx ExprContext, ctrlKey string, dirList []string) []string {
if dirSpec, exists := getControlString(ctx, ctrlKey); exists {
dirs := strings.Split(dirSpec, ":")
if dirList == nil {
dirList = dirs
} else {
dirList = append(dirList, dirs...)
}
}
return dirList
}
*/
func addSearchDirs(ctx ExprContext, endingPath string, dirList []string) []string {
if dirSpec, exists := getControlString(ctx, ControlSearchPath); exists {
func addSearchDirs(endingPath string, dirList []string) []string {
if dirSpec, exists := getControlString(ControlSearchPath); exists {
dirs := strings.Split(dirSpec, ":")
if dirList == nil {
dirList = dirs
@ -64,6 +63,12 @@ func addSearchDirs(ctx ExprContext, endingPath string, dirList []string) []strin
return dirList
}
func buildSearchDirList(endingPath, envVarName string) (dirList []string) {
dirList = addEnvImportDirs(envVarName, dirList)
dirList = addSearchDirs(endingPath, dirList)
return
}
func isFile(filePath string) bool {
info, err := os.Stat(filePath)
return (err == nil || errors.Is(err, os.ErrExist)) && info.Mode().IsRegular()
@ -93,7 +98,7 @@ func makeFilepath(filename string, dirList []string) (filePath string, err error
filePath = searchAmongPath(filename, dirList)
}
if len(filePath) == 0 {
err = fmt.Errorf("source file %q not found", filename)
err = fmt.Errorf("file %q not found", filename)
}
return
}