-
Notifications
You must be signed in to change notification settings - Fork 0
/
inject.go
285 lines (257 loc) · 7.48 KB
/
inject.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
* Copyright 2024 hopeio. All rights reserved.
* Licensed under the MIT License that can be found in the LICENSE file.
* @Created by jyb
*/
package initialize
import (
"bytes"
"errors"
"github.com/hopeio/initialize/conf_dao"
"github.com/hopeio/utils/log"
stringsi "github.com/hopeio/utils/strings"
"reflect"
"slices"
"strings"
)
func (gc *globalConfig) UnmarshalAndSet(data []byte) {
gc.mu.Lock()
err := gc.Viper.MergeConfig(bytes.NewReader(data))
if err != nil {
if gc.editTimes == 0 {
log.Fatal(err)
} else {
log.Error(err)
return
}
}
gc.inject(gc.conf, gc.dao)
gc.editTimes++
gc.mu.Unlock()
}
func (gc *globalConfig) newStruct(conf Config, dao Dao) any {
nameValueMap := make(map[string]reflect.Value)
var structFields []reflect.StructField
var confValue reflect.Value
var confType reflect.Type
// BuiltinConfig
if !gc.initialized {
confValue = reflect.ValueOf(&gc.BuiltinConfig).Elem()
confType = confValue.Type()
for i := 0; i < confValue.NumField(); i++ {
field := confValue.Field(i).Addr()
structField := confType.Field(i)
name := structField.Name
tagSettings := ParseInitTagSettings(structField.Tag.Get(initTagName))
if tagSettings.ConfigName != "" {
name = stringsi.UpperCaseFirst(tagSettings.ConfigName)
}
if field.CanInterface() {
inter := field.Interface()
if c, ok := inter.(BeforeInject); ok {
c.BeforeInject()
}
if c, ok := inter.(BeforeInjectWithRoot); ok {
c.BeforeInjectWithRoot(&gc.RootConfig)
}
}
structFields = append(structFields, reflect.StructField{
Name: name,
Type: field.Type(),
Tag: structField.Tag,
Anonymous: structField.Anonymous,
})
nameValueMap[name] = field
}
}
confValue = reflect.ValueOf(conf).Elem()
confType = confValue.Type()
for i := 0; i < confValue.NumField(); i++ {
field := confValue.Field(i)
fieldType := field.Type()
// panic: reflect: embedded type with methods not implemented if type is not first field // Issue 15924.
if confValue.Field(i).Type() == EmbeddedPresetsType {
continue
}
if fieldType.Kind() != reflect.Ptr && fieldType.Kind() != reflect.Map {
field = field.Addr()
}
structField := confType.Field(i)
name := structField.Name
tagSettings := ParseInitTagSettings(structField.Tag.Get(initTagName))
if tagSettings.ConfigName != "" {
name = stringsi.UpperCaseFirst(tagSettings.ConfigName)
}
if v, ok := nameValueMap[name]; ok {
if v.Type() == field.Type() {
log.Fatalf(`exists builtin config field: %s, please delete the field`, name)
} else {
log.Fatalf(`exists builtin config field: %s, please rename or use init tag [init:"config:{{other config name}}"]`, name)
}
}
if field.CanInterface() {
inter := field.Interface()
if c, ok := inter.(BeforeInject); ok {
c.BeforeInject()
}
if c, ok := inter.(BeforeInjectWithRoot); ok {
c.BeforeInjectWithRoot(&gc.RootConfig)
}
}
structFields = append(structFields, reflect.StructField{
Name: name,
Type: field.Type(),
Tag: structField.Tag,
Anonymous: structField.Anonymous,
})
nameValueMap[name] = field
}
// 不进行二次注入,无法确定业务中是否仍然使用,除非每次加锁,或者说每次业务中都交给一个零时变量?需要规范去控制
if dao != nil {
daoValue := reflect.ValueOf(dao).Elem()
daoType := daoValue.Type()
for i := 0; i < daoValue.NumField(); i++ {
field := daoValue.Field(i)
if field.Type().Kind() == reflect.Struct {
field = field.Addr()
}
if field.CanInterface() {
inter := field.Interface()
if daoField, ok := inter.(conf_dao.DaoField); ok {
structField := daoType.Field(i)
// TODO: 加强校验,必须不为nil
daoConfig := daoField.Config()
if daoConfig == nil {
log.Fatalf("dao %s Config() return nil", structField.Name)
}
name := structField.Name
daoConfigValue := reflect.ValueOf(daoConfig)
daoConfigType := reflect.TypeOf(daoConfig)
tagSettings := ParseInitTagSettings(structField.Tag.Get(initTagName))
if tagSettings.ConfigName != "" {
name = stringsi.UpperCaseFirst(tagSettings.ConfigName)
}
if c, ok := daoConfig.(BeforeInject); ok {
c.BeforeInject()
}
if c, ok := inter.(BeforeInjectWithRoot); ok {
c.BeforeInjectWithRoot(&gc.RootConfig)
}
if _, ok := nameValueMap[name]; ok {
log.Fatalf(`exists field: %s, please rename or use init tag [init:"{{otherConfigName}}"]`, name)
}
structFields = append(structFields, reflect.StructField{
Name: name,
Type: daoConfigType,
Tag: structField.Tag,
})
nameValueMap[name] = daoConfigValue
}
}
}
}
typ := reflect.StructOf(structFields)
newStruct := reflect.New(typ)
gc.setNewStruct(newStruct.Elem(), nameValueMap)
return newStruct.Interface()
}
func (gc *globalConfig) setNewStruct(value reflect.Value, typValueMap map[string]reflect.Value) {
typ := value.Type()
for i := 0; i < value.NumField(); i++ {
structField := typ.Field(i)
name := structField.Name
tagSettings := ParseInitTagSettings(structField.Tag.Get(initTagName))
if tagSettings.ConfigName != "" {
name = stringsi.UpperCaseFirst(tagSettings.ConfigName)
}
field := value.Field(i)
field.Set(typValueMap[name])
}
}
// 注入配置及生成DAO
func (gc *globalConfig) inject(conf Config, dao Dao) {
tmpConfig := gc.newStruct(conf, dao)
err := gc.Viper.Unmarshal(tmpConfig, decoderConfigOptions...)
if err != nil {
if gc.editTimes == 0 {
log.Fatal(err)
} else {
log.Error(err)
return
}
}
applyFlagConfig(gc.Viper, tmpConfig)
conf.AfterInject()
if c, ok := conf.(AfterInjectWithRoot); ok {
c.AfterInjectWithRoot(&gc.RootConfig)
}
if dao != nil {
dao.AfterInjectConfig()
if c, ok := dao.(AfterInjectConfigWithRoot); ok {
c.AfterInjectConfigWithRoot(&gc.RootConfig)
}
gc.injectDao(dao)
}
//log.Debugf("config: %+v", tmpConfig)
}
func (gc *globalConfig) afterInjectConfigCall(tmpConfig any) {
v := reflect.ValueOf(tmpConfig).Elem()
if !v.IsValid() {
return
}
for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
if field.CanInterface() {
inter := field.Interface()
if subconf, ok := inter.(AfterInject); ok {
subconf.AfterInject()
}
if subconf, ok := inter.(AfterInjectWithRoot); ok {
subconf.AfterInjectWithRoot(&gc.RootConfig)
}
}
}
}
func (gc *globalConfig) injectDao(dao Dao) {
v := reflect.ValueOf(dao).Elem()
if !v.IsValid() {
return
}
typ := v.Type()
for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
structFiled := typ.Field(i)
if field.Addr().CanInterface() {
inter := field.Addr().Interface()
if field.Kind() != reflect.Struct {
log.Debug("ignore inject pointer type: ", field.Type().String())
continue
}
confName := strings.ToUpper(structFiled.Name)
if slices.Contains(gConfig.RootConfig.NoInject, confName) {
continue
}
// 根据DaoField接口实现获取配置和要注入的类型
if daofield, ok := inter.(conf_dao.DaoField); ok {
err := daofield.Init()
if err != nil {
log.Fatal(err)
}
}
}
}
dao.AfterInject()
if c, ok := dao.(AfterInjectWithRoot); ok {
c.AfterInjectWithRoot(&gc.RootConfig)
}
}
// 当初始化完成后,仍然有需要注入的config和dao
func (gc *globalConfig) Inject(conf Config, dao Dao) error {
if !gc.initialized {
return errors.New("not initialize, please call initialize.Init or initialize.Start")
}
gc.setConfDao(conf, dao)
gc.beforeInjectCall(conf, dao)
gc.inject(conf, dao)
return nil
}