Use generator to generate exceptions from yaml
Signed-off-by: Asai Neko <sugar@sne.moe>
This commit is contained in:
8
cmd/gen_exception/exception.tmpl
Normal file
8
cmd/gen_exception/exception.tmpl
Normal file
@@ -0,0 +1,8 @@
|
||||
// Code generated by gen-exception; DO NOT EDIT.
|
||||
package exception
|
||||
|
||||
const (
|
||||
{{- range .Items }}
|
||||
{{ .Name }} = "{{ .Value }}"
|
||||
{{- end }}
|
||||
)
|
||||
97
cmd/gen_exception/main.go
Normal file
97
cmd/gen_exception/main.go
Normal file
@@ -0,0 +1,97 @@
|
||||
//go:build ignore
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"golang.org/x/text/cases"
|
||||
"golang.org/x/text/language"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type ErrorItem struct {
|
||||
Name string
|
||||
Value string
|
||||
}
|
||||
|
||||
type TplData struct {
|
||||
Items []ErrorItem
|
||||
}
|
||||
|
||||
func toCamel(s string) string {
|
||||
caser := cases.Title(language.English)
|
||||
s = strings.ReplaceAll(s, "-", "_")
|
||||
parts := strings.Split(s, "_")
|
||||
for i := range parts {
|
||||
parts[i] = caser.String(parts[i])
|
||||
}
|
||||
return strings.Join(parts, "")
|
||||
}
|
||||
|
||||
func recursiveParse(prefix string, raw any, items *[]ErrorItem) {
|
||||
switch v := raw.(type) {
|
||||
case map[string]any:
|
||||
for key, val := range v {
|
||||
recursiveParse(prefix+toCamel(key), val, items)
|
||||
}
|
||||
case string:
|
||||
*items = append(*items, ErrorItem{
|
||||
Name: prefix,
|
||||
Value: v,
|
||||
})
|
||||
case int, int64:
|
||||
*items = append(*items, ErrorItem{
|
||||
Name: prefix,
|
||||
Value: fmt.Sprintf("%v", v),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
yamlDir := "internal/exception/definitions"
|
||||
outputDir := "internal/exception"
|
||||
tplPath := "cmd/gen_exception/exception.tmpl"
|
||||
|
||||
if _, err := os.Stat(tplPath); os.IsNotExist(err) {
|
||||
log.Fatalf("Cannot found tmpl %s", tplPath)
|
||||
}
|
||||
|
||||
funcMap := template.FuncMap{"ToCamel": toCamel}
|
||||
tmpl := template.Must(template.New("exception.tmpl").Funcs(funcMap).ParseFiles(tplPath))
|
||||
|
||||
os.MkdirAll(outputDir, 0755)
|
||||
|
||||
files, _ := filepath.Glob(filepath.Join(yamlDir, "*.yaml"))
|
||||
for _, yamlFile := range files {
|
||||
content, err := os.ReadFile(yamlFile)
|
||||
if err != nil {
|
||||
log.Printf("Read file error: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
var rawData any
|
||||
if err := yaml.Unmarshal(content, &rawData); err != nil {
|
||||
log.Printf("Unmarshal error in %s: %v", yamlFile, err)
|
||||
continue
|
||||
}
|
||||
|
||||
var items []ErrorItem
|
||||
recursiveParse("", rawData, &items)
|
||||
|
||||
baseName := strings.TrimSuffix(filepath.Base(yamlFile), filepath.Ext(yamlFile))
|
||||
outputFileName := baseName + "_gen.go"
|
||||
outputPath := filepath.Join(outputDir, outputFileName)
|
||||
|
||||
f, _ := os.Create(outputPath)
|
||||
tmpl.Execute(f, TplData{Items: items})
|
||||
f.Close()
|
||||
|
||||
fmt.Printf("Generated: %s (%d constants)\n", outputPath, len(items))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user