feat: 配置导出生成的 Go 代码支持获取所有线上配置的集合

This commit is contained in:
kercylan98 2023-07-01 16:37:11 +08:00
parent afdda793bc
commit 68cb5f2516
4 changed files with 64 additions and 42 deletions

View File

@ -6,8 +6,8 @@
}, },
"Other": { "Other": {
"0": { "0": {
"id": 1, "name": "张飞",
"name": "张飞" "id": 1
}, },
"1": { "1": {
"id": 2, "id": 2,

View File

@ -1,58 +1,58 @@
{ {
"1": { "1": {
"b": { "b": {
"Award": {
"0": "asd",
"1": "12"
},
"Other": {
"0": {
"name": "张飞",
"id": 1
},
"1": {
"name": "刘备",
"id": 2
}
},
"Id": 1, "Id": 1,
"Count": "b" "Count": "b",
}
},
"2": {
"c": {
"Award": { "Award": {
"0": "asd", "0": "asd",
"1": "12" "1": "12"
}, },
"Other": {
"0": {
"name": "张飞",
"id": 1
},
"1": {
"id": 2,
"name": "刘备"
}
},
"Id": 2,
"Count": "c"
},
"d": {
"Id": 2,
"Count": "d",
"Award": {
"1": "12",
"0": "asd"
},
"Other": { "Other": {
"0": { "0": {
"id": 1, "id": 1,
"name": "张飞" "name": "张飞"
}, },
"1": {
"name": "刘备",
"id": 2
}
}
}
},
"2": {
"c": {
"Other": {
"0": {
"id": 1,
"name": "张飞"
},
"1": {
"name": "刘备",
"id": 2
}
},
"Id": 2,
"Count": "c",
"Award": {
"0": "asd",
"1": "12"
}
},
"d": {
"Id": 2,
"Count": "d",
"Award": {
"0": "asd",
"1": "12"
},
"Other": {
"1": { "1": {
"id": 2, "id": 2,
"name": "刘备" "name": "刘备"
},
"0": {
"id": 1,
"name": "张飞"
} }
} }
} }

View File

@ -9,6 +9,7 @@ import (
) )
var json = jsonIter.ConfigCompatibleWithStandardLibrary var json = jsonIter.ConfigCompatibleWithStandardLibrary
var full map[string]any
var ( var (
// IndexConfig 有索引 // IndexConfig 有索引
IndexConfig map[int]map[string]*IndexConfigDefine IndexConfig map[int]map[string]*IndexConfigDefine
@ -32,11 +33,16 @@ func LoadConfig(handle func(filename string, config any) error) {
} }
// Refresh 将加载后的配置刷新到线上
func Refresh() { func Refresh() {
full = make(map[string]any)
IndexConfig = _IndexConfig IndexConfig = _IndexConfig
full["IndexConfig"] = IndexConfig
EasyConfig = _EasyConfig EasyConfig = _EasyConfig
full["EasyConfig"] = EasyConfig
} }
// DefaultLoad 默认提供的配置加载函数
func DefaultLoad(filepath string) { func DefaultLoad(filepath string) {
LoadConfig(func(filename string, config any) error { LoadConfig(func(filename string, config any) error {
bytes, err := os.ReadFile(filepath) bytes, err := os.ReadFile(filepath)
@ -47,3 +53,9 @@ func DefaultLoad(filepath string) {
return json.Unmarshal(bytes, &config) return json.Unmarshal(bytes, &config)
}) })
} }
// GetFull 获取所有配置的 map 集合
// - 通常用于前端配置通过后端接口获取的情况
func GetFull() map[string]any {
return full
}

View File

@ -41,6 +41,7 @@ import (
) )
var json = jsonIter.ConfigCompatibleWithStandardLibrary var json = jsonIter.ConfigCompatibleWithStandardLibrary
var full map[string]any
var ( var (
{{range $index, $config := .Configs}} {{range $index, $config := .Configs}}
@ -66,13 +67,16 @@ func LoadConfig(handle func(filename string, config any) error) {
{{end}} {{end}}
} }
// Refresh 将加载后的配置刷新到线上
func Refresh() { func Refresh() {
full = make(map[string]any)
{{range $index, $config := .Configs}} {{range $index, $config := .Configs}}
{{$config.Name}} = _{{$config.Name}} {{$config.Name}} = _{{$config.Name}}
full["{{$config.Name}}"] = {{$config.Name}}
{{end}} {{end}}
} }
// DefaultLoad 默认提供的配置加载函数
func DefaultLoad(filepath string) { func DefaultLoad(filepath string) {
LoadConfig(func(filename string, config any) error { LoadConfig(func(filename string, config any) error {
bytes, err := os.ReadFile(filepath) bytes, err := os.ReadFile(filepath)
@ -82,6 +86,12 @@ func DefaultLoad(filepath string) {
return json.Unmarshal(bytes, &config) return json.Unmarshal(bytes, &config)
}) })
} }
// GetFull 获取所有配置的 map 集合
// - 通常用于前端配置通过后端接口获取的情况
func GetFull() map[string]any {
return full
}
` `
GenerateGoDefineTemplate = `// Code generated by minotaur-config-export. DO NOT EDIT. GenerateGoDefineTemplate = `// Code generated by minotaur-config-export. DO NOT EDIT.