2024-04-19 00:16:49 +02:00
|
|
|
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
|
|
|
// All rights reserved.
|
|
|
|
|
2024-04-28 04:52:02 +02:00
|
|
|
// module-register.go
|
2024-04-19 00:16:49 +02:00
|
|
|
package expr
|
|
|
|
|
|
|
|
import (
|
2024-04-28 05:41:13 +02:00
|
|
|
"fmt"
|
2024-04-19 00:16:49 +02:00
|
|
|
)
|
|
|
|
|
2024-04-28 05:41:13 +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
|
|
|
|
2024-04-28 05:41:13 +02:00
|
|
|
var moduleRegister map[string]*module
|
|
|
|
|
|
|
|
func registerImport(name string, importFunc func(ExprContext), description string) {
|
2024-04-28 04:52:02 +02:00
|
|
|
if moduleRegister == nil {
|
2024-04-28 05:41:13 +02:00
|
|
|
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
|
|
|
}
|
2024-04-28 05:41:13 +02:00
|
|
|
moduleRegister[name] = newModule(importFunc, description)
|
2024-04-19 00:16:49 +02:00
|
|
|
}
|
|
|
|
|
2024-04-28 05:41:13 +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() {
|
2024-04-28 04:52:02 +02:00
|
|
|
if moduleRegister == nil {
|
2024-04-28 05:41:13 +02:00
|
|
|
moduleRegister = make(map[string]*module)
|
2024-04-19 00:16:49 +02:00
|
|
|
}
|
|
|
|
}
|