expr/module-register.go

49 lines
1.0 KiB
Go
Raw Normal View History

2024-04-19 00:16:49 +02:00
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
// All rights reserved.
// module-register.go
2024-04-19 00:16:49 +02:00
package expr
import (
"fmt"
2024-04-19 00:16:49 +02:00
)
type module struct {
importFunc func(ExprContext)
description string
imported bool
}
func newModule(importFunc func(ExprContext), description string) *module {
return &module{importFunc, description, false}
}
2024-04-19 00:16:49 +02:00
var moduleRegister map[string]*module
func registerImport(name string, importFunc func(ExprContext), description string) {
if moduleRegister == nil {
moduleRegister = make(map[string]*module)
}
if _, exists := moduleRegister[name]; exists {
panic(fmt.Errorf("module %q already registered", name))
2024-04-19 00:16:49 +02:00
}
moduleRegister[name] = newModule(importFunc, description)
2024-04-19 00:16:49 +02:00
}
func IterateModules(op func(name, description string, imported bool) bool) {
if op != nil {
for name, mod := range moduleRegister {
if !op(name, mod.description, mod.imported) {
break
}
}
}
}
// ----
2024-04-19 00:16:49 +02:00
func init() {
if moduleRegister == nil {
moduleRegister = make(map[string]*module)
2024-04-19 00:16:49 +02:00
}
}