fix: P1 — classifyOperation accepts any underscore prefix + format.Source warns instead of silently writing broken Go

This commit is contained in:
“Naeel”
2026-06-30 18:16:28 +04:00
parent 75c1802530
commit a0e4037d7b
@@ -620,10 +620,7 @@ func writeInstanceResource(outDir string, svc GenResource) error {
return err
}
formatted, err := format.Source(buf.Bytes())
if err != nil {
formatted = buf.Bytes()
}
formatted := formatSourceOrWarn(filePath, buf.Bytes())
return os.WriteFile(filePath, formatted, 0644)
}
@@ -654,10 +651,7 @@ func writeSubresource(outDir string, sr GenSubresource) error {
return err
}
formatted, err := format.Source(buf.Bytes())
if err != nil {
formatted = buf.Bytes()
}
formatted := formatSourceOrWarn(filePath, buf.Bytes())
return os.WriteFile(filePath, formatted, 0644)
}
@@ -686,10 +680,7 @@ func writeActionResource(outDir string, act GenAction) error {
return err
}
formatted, err := format.Source(buf.Bytes())
if err != nil {
formatted = buf.Bytes()
}
formatted := formatSourceOrWarn(filePath, buf.Bytes())
return os.WriteFile(filePath, formatted, 0644)
}
@@ -713,12 +704,10 @@ func writeRegistry(outDir string, services []GenResource, subs []GenSubresource,
buf.WriteString("\t}\n")
buf.WriteString("}\n")
formatted, err := format.Source(buf.Bytes())
if err != nil {
formatted = buf.Bytes()
}
regPath := filepath.Join(outDir, "registry.go")
formatted := formatSourceOrWarn(regPath, buf.Bytes())
return os.WriteFile(filepath.Join(outDir, "registry.go"), formatted, 0644)
return os.WriteFile(regPath, formatted, 0644)
}
func toCamel(s string) string {
@@ -2077,6 +2066,17 @@ func validateSpec(path string, spec *ServiceSpec) error {
return nil
}
// formatSourceOrWarn форматирует Go-код. При ошибке пишет warning в stderr и возвращает неформатированный код.
func formatSourceOrWarn(filePath string, src []byte) []byte {
formatted, err := format.Source(src)
if err != nil {
fmt.Fprintf(os.Stderr, "WARNING: gofmt failed for %s: %v — writing unformatted code\n", filePath, err)
return src
}
return formatted
}
func buildSubresourceForceNewCodes(sr *GenSubresource, createOnly map[string]struct{}) map[string]struct{} {
forceNew := map[string]struct{}{}
if sr.ModifyOpName == "" {