89 lines
2.0 KiB
Go
89 lines
2.0 KiB
Go
package internal
|
|
|
|
const (
|
|
generateConfigTemplate = `
|
|
|
|
// {{.Name}} {{.DisplayName}}
|
|
type {{.Name}} struct {
|
|
{{range $index, $value := .Fields}}{{if eq $value.Server true}}{{if eq $value.Ignore false}}{{$value.Name}} {{$value.Type}} // {{$value.Describe}}{{end}}{{end}}
|
|
{{end}}
|
|
}
|
|
|
|
{{range $index, $value := .Fields}}{{$value}}{{end}}
|
|
`
|
|
|
|
generateGoStructTemplate = `
|
|
{{if eq .Ignore false}}
|
|
type {{.TypeNotStar}} struct {
|
|
{{range $index, $value := .Fields}}
|
|
{{$value.Name}} {{$value.Type}}
|
|
{{end}}
|
|
}
|
|
{{end}}
|
|
`
|
|
|
|
GenerateGoConfigTemplate = `// Code generated by minotaur-config-export. DO NOT EDIT.
|
|
|
|
package {{.Package}}
|
|
|
|
import (
|
|
jsonIter "github.com/json-iterator/go"
|
|
"github.com/kercylan98/minotaur/utils/log"
|
|
"go.uber.org/zap"
|
|
"os"
|
|
)
|
|
|
|
var json = jsonIter.ConfigCompatibleWithStandardLibrary
|
|
|
|
var (
|
|
{{range $index, $config := .Configs}}
|
|
I{{$config.Name}} {{$config.GetVariable}}
|
|
i{{$config.Name}} {{$config.GetVariable}}
|
|
{{end}}
|
|
)
|
|
|
|
func LoadConfig(handle func(filename string, config any) error) {
|
|
var err error
|
|
{{range $index, $config := .Configs}}
|
|
i{{$config.Name}} = {{$config.GetVariableGen}}
|
|
{{if eq $config.IndexCount 0}}
|
|
if err = handle("{{$config.Prefix}}{{$config.Name}}.json", i{{$config.Name}}); err != nil {
|
|
log.Error("Config", zap.String("Name", "{{$config.Name}}"), zap.Bool("Invalid", true), zap.Error(err))
|
|
}
|
|
{{else}}
|
|
if err = handle("{{$config.Prefix}}{{$config.Name}}.json", &i{{$config.Name}}); err != nil {
|
|
log.Error("Config", zap.String("Name", "{{$config.Name}}"), zap.Bool("Invalid", true), zap.Error(err))
|
|
}
|
|
{{end}}
|
|
{{end}}
|
|
}
|
|
|
|
func Refresh() {
|
|
{{range $index, $config := .Configs}}
|
|
I{{$config.Name}} = i{{$config.Name}}
|
|
{{end}}
|
|
}
|
|
|
|
|
|
func DefaultLoad(filepath string) {
|
|
LoadConfig(func(filename string, config any) error {
|
|
bytes, err := os.ReadFile(filepath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return json.Unmarshal(bytes, &config)
|
|
})
|
|
}
|
|
`
|
|
|
|
GenerateGoDefineTemplate = `// Code generated by minotaur-config-export. DO NOT EDIT.
|
|
|
|
package {{.Package}}
|
|
|
|
{{range $index, $config := .Configs}}
|
|
{{$config.String}}
|
|
{{end}}
|
|
|
|
`
|
|
)
|