-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconf.go
71 lines (62 loc) · 2 KB
/
conf.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
package lightms
import (
"log"
"reflect"
)
var propsConf []any
var reader = newPropReader()
// AddConf registers a conf struct to be loaded
func AddConf[T any]() {
t := new(T)
typ := reflect.TypeOf(t).Elem()
if typ.Kind() != reflect.Struct {
log.Fatalf("AddConf: conf must be a struct, got '%s'", typ.Kind())
}
propsConf = append(propsConf, t)
}
// processInjectionsByReceivers analyze receivers to populate dByAlias and dByTypes
func processInjectionsByReceivers() {
for _, conf := range propsConf {
loadConfMeth(reflect.ValueOf(conf))
}
}
// resolveProps resolves all props fields
func resolveProps() {
for _, conf := range propsConf {
loadConfProps(reflect.ValueOf(conf))
}
}
// loadConfMeth calls receiver method
func loadConfMeth(vconf reflect.Value) {
tconf := vconf.Type()
for i := 0; i < tconf.NumMethod(); i++ {
method := tconf.Method(i)
validateConfMeth(vconf.Type().String(), method.Name, method.Type)
vconf.Method(i).Call(nil)
}
}
// validateConfMeth checks if the receiver method
func validateConfMeth(confName, methName string, methType reflect.Type) {
if methType.NumOut() != 0 {
log.Fatalf("Method '%s' in conf '%s' has wrong number of returns, expected 0 got %d.", methName, confName, methType.NumOut())
}
if methType.NumIn() != 1 {
log.Fatalf("Method '%s' in conf '%s' has wrong number of inputs, expected 0 got %d.", methName, confName, methType.NumIn())
}
}
// loadConfProps loads conf props
func loadConfProps(conf reflect.Value) {
elem := conf.Type().Elem()
for i := 0; i < elem.NumField(); i++ {
field := elem.Field(i)
log.Printf("Loading prop '%s' from conf '%s'", field.Name, elem.String())
if field.Type.Kind() != reflect.Ptr || field.Type.Elem().Kind() != reflect.Struct {
log.Fatalf("Prop '%s' in conf '%s' is not a pointer to a struct", field.Name, elem.String())
}
propInstance := reflect.New(field.Type.Elem()).Interface()
reader.loadProp(propInstance)
vprop := reflect.ValueOf(propInstance)
storePropResolved(vprop)
conf.Elem().Field(i).Set(vprop)
}
}