/
fx.go
136 lines (117 loc) · 3.07 KB
/
fx.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package configfx
import (
"reflect"
"strings"
"go.uber.org/fx"
"github.com/go-toho/toho/config"
"github.com/go-toho/toho/pkg/fxtags"
)
const configStructSuffix = "config"
var Module = fx.Module("config",
configStructCheck,
ensureConfigOutOption,
)
var (
configStructCheck = fx.Invoke(
fx.Annotate(
func(cfg any) error {
return config.StructCheck(cfg)
},
fx.ParamTags(fxtags.NamedOptional(config.NamedConfigPointerIn)),
),
)
ensureConfigOutOption = fx.Invoke(
fx.Annotate(
func(_ any) {},
fx.ParamTags(fxtags.Named(config.NamedConfigPointerOut)),
),
)
)
func SupplyConfigPointer(c any) fx.Option {
var fxopts []fx.Option
fxopts = append(fxopts, provideConfigPointer(c))
fxopts = append(fxopts, structInnerConfigProviders(c)...)
return fx.Options(fxopts...)
}
func SupplyConfigFile(filename string) fx.Option {
return fx.Provide(
fx.Annotate(
func() string { return filename },
fx.ResultTags(fxtags.Group(config.GroupConfigFiles)),
),
)
}
func SupplyConfigFiles(files []string) fx.Option {
return fx.Provide(
fx.Annotate(
func() []string { return files },
fx.ResultTags(fxtags.GroupFlatten(config.GroupConfigFiles)),
),
)
}
func SupplyConfig(cfg any) fx.Option {
return fx.Provide(
fx.Annotate(
func() any { return cfg },
fx.ResultTags(fxtags.Named(config.NamedConfigPointerOut)),
),
)
}
func provideConfigPointer(c any) fx.Option {
return fx.Provide(
fx.Annotate(
func() any { return c },
fx.ResultTags(fxtags.Named(config.NamedConfigPointerIn)),
),
)
}
func structInnerConfigProviders(structure any) []fx.Option {
var opts []fx.Option
inputType := reflect.TypeOf(structure)
if inputType != nil {
if inputType.Kind() == reflect.Ptr {
if inputType.Elem().Kind() == reflect.Struct {
return appendConfigStructProvider(opts, reflect.ValueOf(structure).Elem(), config.NamedConfigPointerOut)
}
}
}
return opts
}
func appendConfigStructProvider(opts []fx.Option, s reflect.Value, paramName string) []fx.Option {
for i := 0; i < s.NumField(); i++ {
typeName := s.Type().Field(i).Type.String()
if s.Type().Field(i).Type.Kind() == reflect.Struct {
if strings.HasSuffix(strings.ToLower(typeName), configStructSuffix) {
opts = append(opts, innerConfigStructProvider(paramName, typeName, i))
opts = appendConfigStructProvider(opts, s.Field(i), typeName)
}
} else if s.Type().Field(i).Type.Kind() == reflect.Ptr {
if !s.Field(i).IsZero() && s.Field(i).Elem().Type().Kind() == reflect.Struct {
return appendConfigStructProvider(opts, s.Field(i).Elem(), paramName)
}
}
}
return opts
}
func innerConfigStructProvider(inName, outName string, fieldIndex int) fx.Option {
return fx.Provide(
fx.Annotate(
func(c any) any {
if c == nil {
return nil
}
s := reflect.ValueOf(c)
if s.Kind() == reflect.Ptr {
if !s.IsZero() && s.Elem().Type().Kind() == reflect.Struct {
s = s.Elem()
} else {
return nil
}
}
return s.Field(fieldIndex).Interface()
},
fx.ParamTags(fxtags.Named(inName)),
fx.ResultTags(fxtags.Named(outName)),
),
)
}