Skip to content

Commit 9521303

Browse files
committed
fix: validate before writing
1 parent 237c72d commit 9521303

1 file changed

Lines changed: 12 additions & 10 deletions

File tree

kinde/management_api/fix_oneof.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,26 +65,28 @@ func main() {
6565
}`)
6666
})
6767

68-
if patched > 0 {
69-
if err := os.WriteFile(targetFile, newContent, 0644); err != nil {
70-
fmt.Fprintf(os.Stderr, "Error writing patched file: %v\n", err)
71-
os.Exit(1)
72-
}
73-
fmt.Printf("✅ Patched %d ambiguous oneOf fallback(s) to fail closed\n", patched)
74-
}
75-
68+
// Validate before writing so a failed run leaves the file exactly as ogen
69+
// generated it, never partially patched.
7670
if locs := remainingFallbackPattern.FindAllIndex(newContent, -1); len(locs) > 0 {
77-
fmt.Fprintf(os.Stderr, "Error: %d oneOf fallback(s) still assign s.Type after patching:\n", len(locs))
71+
fmt.Fprintf(os.Stderr, "Error: %d oneOf fallback(s) would still assign s.Type after patching:\n", len(locs))
7872
for _, loc := range locs {
7973
line := 1 + bytes.Count(newContent[:loc[0]], []byte("\n"))
8074
snippet := strings.Join(strings.Fields(string(newContent[loc[0]:loc[1]])), " ")
8175
fmt.Fprintf(os.Stderr, " %s:%d: %s\n", targetFile, line, snippet)
8276
}
83-
fmt.Fprintln(os.Stderr, "The generated shape has likely changed (ogen upgrade?) - update fix_oneof.go")
77+
fmt.Fprintf(os.Stderr, "The generated shape has likely changed (ogen upgrade?) - update fix_oneof.go\n")
78+
fmt.Fprintf(os.Stderr, "%s left unmodified\n", targetFile)
8479
os.Exit(1)
8580
}
8681

8782
if patched == 0 {
8883
fmt.Println("No ambiguous oneOf fallback found - already patched or no longer generated")
84+
return
85+
}
86+
87+
if err := os.WriteFile(targetFile, newContent, 0644); err != nil {
88+
fmt.Fprintf(os.Stderr, "Error writing patched file: %v\n", err)
89+
os.Exit(1)
8990
}
91+
fmt.Printf("✅ Patched %d ambiguous oneOf fallback(s) to fail closed\n", patched)
9092
}

0 commit comments

Comments
 (0)