-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
104 lines (88 loc) · 2.49 KB
/
config.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package goyamlcfg
import (
"fmt"
"os"
"reflect"
"sync"
"gopkg.in/yaml.v3"
)
type Instance interface{}
type config struct {
mu sync.Mutex
ty reflect.Type
data interface{}
}
var singleton config
// InitializeConfigSingleton will create a instance of the configuration
// to be fetched with GetConfigFromSingleton or MustGetConfigFromSingleton.
// Use this when you are not using a DI system.
func InitializeConfigSingleton[T Instance](path string) error {
if singleton.data == nil {
singleton.mu.Lock()
defer singleton.mu.Unlock()
target, err := GetConfig[T](path)
if err != nil {
return err
}
singleton.ty = reflect.TypeOf(target)
singleton.data = interface{}(target)
}
return nil
}
// MustInitializeConfigSingleton has the same behaviour of InitializeConfigSingleton,
// but will panic if an error occurs.
func MustInitializeConfigSingleton[T Instance](path string) {
err := InitializeConfigSingleton[T](path)
if err != nil {
panic(err)
}
}
// GetConfigFromSingleton will only return a configuration with type T if:
// 1) The singleton has been already initialized.
// 2) The type of the generic is the same of the singleton initialization.
func GetConfigFromSingleton[T Instance]() (T, error) {
if singleton.data != nil {
if singleton.ty != reflect.TypeOf(*new(T)) {
return *new(T), fmt.Errorf("mismatch generic type: the given type is diferent from the singleton data")
}
return (singleton.data).(T), nil
} else {
return *new(T), fmt.Errorf("trying to access an unitialized config data")
}
}
// MustGetConfigFromSingleton has the same behaviour of GetConfigFromSingleton
// but will panic if an error occurs.
func MustGetConfigFromSingleton[T Instance]() T {
conf, err := GetConfigFromSingleton[T]()
if err != nil {
panic(err)
}
return conf
}
// GetConfig will always return a new config instance with the type T if the
// read file and the parsing stages was successful.
func GetConfig[T Instance](path string) (T, error) {
target := new(T)
err := loadAndParseConfig(path, target)
if err != nil {
return *target, err
}
replaceEnvOnTarget(target)
return *target, nil
}
// MustGetConfig has the same behaviour of GetConfig but panics
// when an error happens.
func MustGetConfig[T Instance](path string) T {
target, err := GetConfig[T](path)
if err != nil {
panic(err)
}
return target
}
func loadAndParseConfig[T Instance](path string, target *T) error {
data, err := os.ReadFile(path)
if err != nil {
return err
}
return yaml.Unmarshal(data, target)
}