modules are now represented by a struct that holds import-function, module description and importa status.
It is also available the new function IterateModules() to explore all modules.
This commit is contained in:
parent
6dd8283308
commit
bf8f1a175f
@ -55,5 +55,5 @@ func ImportBuiltinsFuncs(ctx ExprContext) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
registerImport("builtins", ImportBuiltinsFuncs)
|
registerImport("base", ImportBuiltinsFuncs, "Base expression tools like isNil(), int(), etc.")
|
||||||
}
|
}
|
@ -142,5 +142,5 @@ func ImportImportFuncs(ctx ExprContext) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
registerImport("import", ImportImportFuncs)
|
registerImport("import", ImportImportFuncs, "Functions import() and include()")
|
||||||
}
|
}
|
||||||
|
@ -115,5 +115,5 @@ func ImportMathFuncs(ctx ExprContext) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
registerImport("math.arith", ImportMathFuncs)
|
registerImport("math.arith", ImportMathFuncs, "Function add() and mul()")
|
||||||
}
|
}
|
||||||
|
@ -164,5 +164,5 @@ func ImportOsFuncs(ctx ExprContext) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
registerImport("os", ImportOsFuncs)
|
registerImport("os.file", ImportOsFuncs, "Operating system file functions")
|
||||||
}
|
}
|
||||||
|
@ -5,33 +5,49 @@
|
|||||||
package expr
|
package expr
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
var moduleRegister map[string]func(ExprContext)
|
type module struct {
|
||||||
|
importFunc func(ExprContext)
|
||||||
|
description string
|
||||||
|
imported bool
|
||||||
|
}
|
||||||
|
|
||||||
func registerImport(name string, importFunc func(ExprContext)) {
|
func newModule(importFunc func(ExprContext), description string) *module {
|
||||||
|
return &module{importFunc, description, false}
|
||||||
|
}
|
||||||
|
|
||||||
|
var moduleRegister map[string]*module
|
||||||
|
|
||||||
|
func registerImport(name string, importFunc func(ExprContext), description string) {
|
||||||
if moduleRegister == nil {
|
if moduleRegister == nil {
|
||||||
moduleRegister = make(map[string]func(ExprContext))
|
moduleRegister = make(map[string]*module)
|
||||||
}
|
}
|
||||||
moduleRegister[name] = importFunc
|
if _, exists := moduleRegister[name]; exists {
|
||||||
|
panic(fmt.Errorf("module %q already registered", name))
|
||||||
|
}
|
||||||
|
moduleRegister[name] = newModule(importFunc, description)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ImportInContext(ctx ExprContext, name string) (exists bool) {
|
func ImportInContext(ctx ExprContext, name string) (exists bool) {
|
||||||
var importFunc func(ExprContext)
|
var mod *module
|
||||||
if importFunc, exists = moduleRegister[name]; exists {
|
if mod, exists = moduleRegister[name]; exists {
|
||||||
importFunc(ctx)
|
mod.importFunc(ctx)
|
||||||
|
mod.imported = true
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func ImportInContextByGlobPattern(ctx ExprContext, pattern string) (count int, err error) {
|
func ImportInContextByGlobPattern(ctx ExprContext, pattern string) (count int, err error) {
|
||||||
var matched bool
|
var matched bool
|
||||||
for name, importFunc := range moduleRegister {
|
for name, mod := range moduleRegister {
|
||||||
if matched, err = filepath.Match(pattern, name); err == nil {
|
if matched, err = filepath.Match(pattern, name); err == nil {
|
||||||
if matched {
|
if matched {
|
||||||
count++
|
count++
|
||||||
importFunc(ctx)
|
mod.importFunc(ctx)
|
||||||
|
mod.imported = true
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
break
|
break
|
||||||
@ -40,8 +56,19 @@ func ImportInContextByGlobPattern(ctx ExprContext, pattern string) (count int, e
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func IterateModules(op func(name, description string, imported bool) bool) {
|
||||||
if moduleRegister == nil {
|
if op != nil {
|
||||||
moduleRegister = make(map[string]func(ExprContext))
|
for name, mod := range moduleRegister {
|
||||||
|
if !op(name, mod.description, mod.imported) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----
|
||||||
|
func init() {
|
||||||
|
if moduleRegister == nil {
|
||||||
|
moduleRegister = make(map[string]*module)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user