-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_template.go
74 lines (65 loc) · 2.01 KB
/
config_template.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
/*
* 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 (
"github.com/hopeio/initialize/conf_dao"
"github.com/hopeio/utils/log"
stringsi "github.com/hopeio/utils/strings"
"os"
"reflect"
"unsafe"
)
func (gc *globalConfig) genConfigTemplate(singleTemplateFileConfig bool) {
dir := gc.RootConfig.ConfigTemplateDir
if dir == "" {
return
}
if dir[len(dir)-1] != '/' {
dir += "/"
}
format := gc.RootConfig.ConfigCenter.Format
filename := prefixLocalTemplate + string(format)
confMap := make(map[string]any)
if singleTemplateFileConfig {
filename = prefixConfigTemplate + string(format)
struct2Map(&gc.RootConfig.BasicConfig, confMap)
delete(confMap, fixedFieldNameEnv)
struct2Map(&gc.RootConfig.EnvConfig, confMap)
delete(confMap, fixedFieldNameConfigCenter)
}
struct2Map(&gc.BuiltinConfig, confMap)
struct2Map(gc.conf, confMap)
if gc.dao != nil {
daoConfig2Map(reflect.ValueOf(gc.dao).Elem(), confMap)
}
encoderRegistry := reflect.ValueOf(gc.Viper).Elem().FieldByName(fixedFieldNameEncoderRegistry).Elem()
fieldValue := reflect.NewAt(encoderRegistry.Type(), unsafe.Pointer(encoderRegistry.UnsafeAddr()))
data, err := fieldValue.Interface().(Encoder).Encode(format, confMap)
if err != nil {
log.Fatal(err)
}
err = os.WriteFile(dir+filename, data, 0644)
if err != nil {
log.Fatal(err)
}
}
func daoConfig2Map(value reflect.Value, confMap map[string]any) {
typ := value.Type()
for i := 0; i < value.NumField(); i++ {
field := value.Field(i)
if field.Addr().Type().Implements(conf_dao.DaoFieldType) {
newconfMap := make(map[string]any)
fieldType := typ.Field(i)
name := fieldType.Name
tagSettings := ParseInitTagSettings(fieldType.Tag.Get(initTagName))
if tagSettings.ConfigName != "" {
name = stringsi.UpperCaseFirst(tagSettings.ConfigName)
}
confMap[name] = newconfMap
struct2Map(field.Addr().Interface().(conf_dao.DaoField).Config(), newconfMap)
}
}
}