function-register.go module-register.go

This commit is contained in:
2024-04-28 04:52:02 +02:00
parent 06ab303b9e
commit 6dd8283308
+47
View File
@@ -0,0 +1,47 @@
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
// All rights reserved.
// module-register.go
package expr
import (
"path/filepath"
)
var moduleRegister map[string]func(ExprContext)
func registerImport(name string, importFunc func(ExprContext)) {
if moduleRegister == nil {
moduleRegister = make(map[string]func(ExprContext))
}
moduleRegister[name] = importFunc
}
func ImportInContext(ctx ExprContext, name string) (exists bool) {
var importFunc func(ExprContext)
if importFunc, exists = moduleRegister[name]; exists {
importFunc(ctx)
}
return
}
func ImportInContextByGlobPattern(ctx ExprContext, pattern string) (count int, err error) {
var matched bool
for name, importFunc := range moduleRegister {
if matched, err = filepath.Match(pattern, name); err == nil {
if matched {
count++
importFunc(ctx)
}
} else {
break
}
}
return
}
func init() {
if moduleRegister == nil {
moduleRegister = make(map[string]func(ExprContext))
}
}