-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnamespace.go
52 lines (43 loc) · 1 KB
/
namespace.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package config
import (
"log"
"github.com/joho/godotenv"
"github.com/tinh-tinh/tinhtinh/v2/core"
)
// Namespace only available for env file
func ForRootRaw(path ...string) core.Modules {
return func(module core.Module) core.Module {
err := godotenv.Load(path...)
if err != nil {
log.Printf("can read env file because %s\n", err.Error())
}
return module
}
}
func ForFeature[E any](name string, fncs ...func() *E) core.Modules {
return func(module core.Module) core.Module {
var value E
if len(fncs) > 0 {
value = *fncs[0]()
} else {
Scan(&value)
}
cfgModule := module.New(core.NewModuleOptions{})
cfgModule.NewProvider(core.ProviderOptions{
Name: GetNamespace(name),
Value: &value,
})
cfgModule.Export(GetNamespace(name))
return cfgModule
}
}
func GetNamespace(name string) core.Provide {
return core.Provide(name)
}
func InjectNamespace[E any](module core.Module, name string) *E {
cfg, ok := module.Ref(GetNamespace(name)).(*E)
if !ok {
return nil
}
return cfg
}