refactor: 调整配置导表工具中 Go 配置文件导出结构,将直接读取更改为线程安全的读取

This commit is contained in:
kercylan98 2023-12-05 12:29:42 +08:00
parent 52c92c8844
commit 8b2f2aa168
2 changed files with 27 additions and 8 deletions

Binary file not shown.

View File

@ -48,13 +48,13 @@ func (slf *Golang) Render(templates ...*pce.TmplStruct) (string, error) {
{{.Name}}Sign, {{.Name}}Sign,
{{- end}} {{- end}}
} }
mutex sync.Mutex mutex sync.RWMutex
) )
var ( var (
{{- range .Templates}} {{- range .Templates}}
{{- if $.HasIndex .}} {{- if $.HasIndex .}}
{{.Name}} {{$.GetVariable .}} // {{.Desc}} c{{.Name}} {{$.GetVariable .}} // {{.Desc}}
{{- end}} {{- end}}
{{- end}} {{- end}}
) )
@ -62,7 +62,7 @@ func (slf *Golang) Render(templates ...*pce.TmplStruct) (string, error) {
var ( var (
{{- range .Templates}} {{- range .Templates}}
{{- if $.HasIndex .}}{{- else}} {{- if $.HasIndex .}}{{- else}}
{{.Name}} *{{$.GetConfigName .}} // {{.Desc}} c{{.Name}} *{{$.GetConfigName .}} // {{.Desc}}
{{- end}} {{- end}}
{{- end}} {{- end}}
) )
@ -169,7 +169,7 @@ func (slf *Golang) Render(templates ...*pce.TmplStruct) (string, error) {
{{- else}} {{- else}}
temp := new({{$.GetConfigName .}}) temp := new({{$.GetConfigName .}})
{{- end}} {{- end}}
if err := json.Unmarshal(data, &{{.Name}}); err != nil { if err := json.Unmarshal(data, &c{{.Name}}); err != nil {
log.Error("Config", log.String("Name", "{{.Name}}"), log.Bool("Invalid", true), log.Err(err)) log.Error("Config", log.String("Name", "{{.Name}}"), log.Bool("Invalid", true), log.Err(err))
return return
} }
@ -185,8 +185,8 @@ func (slf *Golang) Render(templates ...*pce.TmplStruct) (string, error) {
cs := make(map[Sign]any) cs := make(map[Sign]any)
{{- range .Templates}} {{- range .Templates}}
{{.Name}} = _{{.Name}} c{{.Name}} = _{{.Name}}
cs[{{.Name}}Sign] = {{.Name}} cs[{{.Name}}Sign] = c{{.Name}}
{{- end}} {{- end}}
configs.Store(&cs) configs.Store(&cs)
@ -194,8 +194,8 @@ func (slf *Golang) Render(templates ...*pce.TmplStruct) (string, error) {
// GetConfigs 获取所有配置 // GetConfigs 获取所有配置
func GetConfigs() map[Sign]any { func GetConfigs() map[Sign]any {
mutex.Lock() mutex.RLock()
defer mutex.Unlock() defer mutex.RUnlock()
return hash.Copy(*configs.Load()) return hash.Copy(*configs.Load())
} }
@ -210,6 +210,25 @@ func (slf *Golang) Render(templates ...*pce.TmplStruct) (string, error) {
defer mutex.Unlock() defer mutex.Unlock()
handle(hash.Copy(*configs.Load())) handle(hash.Copy(*configs.Load()))
} }
{{- range .Templates}}
{{- if $.HasIndex .}}
// {{.Name}} 获取{{.Desc}}
func {{.Name}}() {{$.GetVariable .}} {
mutex.RLock()
defer mutex.RUnlock()
return c{{.Name}}
}
{{- else}}
// {{.Name}} 获取{{.Desc}}
func {{.Name}}() *{{$.GetConfigName .}} {
mutex.RLock()
defer mutex.RUnlock()
return c{{.Name}}
}
{{- end}}
{{- end}}
`, slf) `, slf)
} }