refactor(gen_v2): split into 7 files by concern (types, loader, params, writers, helpers, templates, main)
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
// writers — генерация Go-файлов ресурсов и registry.go.
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
func writeInstanceResource(outDir string, svc GenResource) error {
|
||||
fileName := fmt.Sprintf("%d_%s_resource.go", svc.ServiceID, svc.Name)
|
||||
filePath := filepath.Join(outDir, fileName)
|
||||
|
||||
tpl, err := template.New("resource").Funcs(template.FuncMap{
|
||||
"ToCamel": toCamel,
|
||||
"ToSnake": toSnake,
|
||||
"ParamType": paramType,
|
||||
"ParamDefaultExpr": paramDefaultExpr,
|
||||
"ParamFormat": paramFormat,
|
||||
"ParamParse": paramParse,
|
||||
"ParamDescription": paramDescription,
|
||||
"OutputType": outputParamType,
|
||||
"OutputIsMap": outputParamIsMap,
|
||||
"OutputIsList": outputParamIsList,
|
||||
"OutputSensitive": outputParamSensitive,
|
||||
"bt": func() string { return "`" },
|
||||
}).Parse(instanceTemplate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
if err := tpl.Execute(&buf, svc); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
formatted := formatSourceOrWarn(filePath, buf.Bytes())
|
||||
|
||||
return os.WriteFile(filePath, formatted, 0644)
|
||||
}
|
||||
|
||||
func writeSubresource(outDir string, sr GenSubresource) error {
|
||||
name := fmt.Sprintf("%s_%s", sr.ServiceName, sr.SubName)
|
||||
fileName := fmt.Sprintf("%d_%s_resource.go", sr.ServiceID, name)
|
||||
filePath := filepath.Join(outDir, fileName)
|
||||
|
||||
tpl, err := template.New("subresource").Funcs(template.FuncMap{
|
||||
"ToCamel": toCamel,
|
||||
"ToSnake": toSnake,
|
||||
"ParamType": paramType,
|
||||
"ParamDefaultExpr": paramDefaultExpr,
|
||||
"ParamFormat": paramFormat,
|
||||
"ParamParse": paramParse,
|
||||
"ParamDescription": paramDescription,
|
||||
"PlanModifierType": planModifierType,
|
||||
"PlanModifierExpr": planModifierExpr,
|
||||
"bt": func() string { return "`" },
|
||||
}).Parse(subresourceTemplate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
if err := tpl.Execute(&buf, sr); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
formatted := formatSourceOrWarn(filePath, buf.Bytes())
|
||||
|
||||
return os.WriteFile(filePath, formatted, 0644)
|
||||
}
|
||||
|
||||
func writeActionResource(outDir string, act GenAction) error {
|
||||
name := fmt.Sprintf("%s_%s", act.ServiceName, act.ActionName)
|
||||
fileName := fmt.Sprintf("%d_%s_action.go", act.ServiceID, name)
|
||||
filePath := filepath.Join(outDir, fileName)
|
||||
|
||||
tpl, err := template.New("action").Funcs(template.FuncMap{
|
||||
"ToCamel": toCamel,
|
||||
"ToSnake": toSnake,
|
||||
"ParamType": paramType,
|
||||
"ParamDefaultExpr": paramDefaultExpr,
|
||||
"ParamFormat": paramFormat,
|
||||
"ParamParse": paramParse,
|
||||
"ParamDescription": paramDescription,
|
||||
"bt": func() string { return "`" },
|
||||
}).Parse(actionTemplate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
if err := tpl.Execute(&buf, act); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
formatted := formatSourceOrWarn(filePath, buf.Bytes())
|
||||
|
||||
return os.WriteFile(filePath, formatted, 0644)
|
||||
}
|
||||
|
||||
func writeRegistry(outDir string, services []GenResource, subs []GenSubresource, actions []GenAction) error {
|
||||
var buf bytes.Buffer
|
||||
buf.WriteString("package resources_gen\n\n")
|
||||
buf.WriteString("import \"github.com/hashicorp/terraform-plugin-framework/resource\"\n\n")
|
||||
buf.WriteString("// Code generated by tools/gen_v2. DO NOT EDIT.\n")
|
||||
buf.WriteString("func AllResources() []func() resource.Resource {\n")
|
||||
buf.WriteString("\treturn []func() resource.Resource{\n")
|
||||
for _, svc := range services {
|
||||
buf.WriteString(fmt.Sprintf("\t\tNew%[1]sResource,\n", toCamel(svc.Name)))
|
||||
}
|
||||
for _, sr := range subs {
|
||||
buf.WriteString(fmt.Sprintf("\t\tNew%[1]sResource,\n", toCamel(sr.ServiceName+"_"+sr.SubName)))
|
||||
}
|
||||
for _, act := range actions {
|
||||
buf.WriteString(fmt.Sprintf("\t\tNew%[1]sResource,\n", toCamel(act.ServiceName+"_"+act.ActionName)))
|
||||
}
|
||||
buf.WriteString("\t}\n")
|
||||
buf.WriteString("}\n")
|
||||
|
||||
regPath := filepath.Join(outDir, "registry.go")
|
||||
formatted := formatSourceOrWarn(regPath, buf.Bytes())
|
||||
|
||||
return os.WriteFile(regPath, formatted, 0644)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user