Skip to content

Commit c8b3ac3

Browse files
committed
fix: Correct validation logic and improve generator error handling
- Fix validation bug in templates/main.tmpl: change '>' to '>=' for min_length comparison. This bug caused names that are exactly min_length characters to be incorrectly marked as invalid. - Add error handling for ExecuteTemplate() calls in main.go (lines 87, 92). Previously, errors were silently ignored. - Add defer file.Close() after opening mainFile and outputsFile for proper resource cleanup. - Add duplicate resource detection: check for duplicate resource names when merging resourceDefinition.json and resourceDefinition_out_of_docs.json. Log fatal error if duplicates are found. - Add validation for required fields (name, slug, length with valid min/max) before template execution. Clear error messages are logged for invalid entries. - Regenerated main.tf with the corrected validation logic. Closes #187
1 parent a837381 commit c8b3ac3

3 files changed

Lines changed: 361 additions & 301 deletions

File tree

main.go

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"io/ioutil"
67
"log"
78
"os"
@@ -30,6 +31,29 @@ type Length struct {
3031
Max int `json:"max"`
3132
}
3233

34+
// validateResource checks that a resource has all required fields
35+
func validateResource(r Resource, source string) error {
36+
if r.Name == "" {
37+
return fmt.Errorf("resource missing required field 'name' in %s", source)
38+
}
39+
if r.Slug == nil || *r.Slug == "" {
40+
return fmt.Errorf("resource '%s' missing required field 'slug' in %s", r.Name, source)
41+
}
42+
if r.Length == nil {
43+
return fmt.Errorf("resource '%s' missing required field 'length' in %s", r.Name, source)
44+
}
45+
if r.Length.Min < 0 {
46+
return fmt.Errorf("resource '%s' has invalid min_length (%d) in %s", r.Name, r.Length.Min, source)
47+
}
48+
if r.Length.Max <= 0 {
49+
return fmt.Errorf("resource '%s' has invalid max_length (%d) in %s", r.Name, r.Length.Max, source)
50+
}
51+
if r.Length.Min > r.Length.Max {
52+
return fmt.Errorf("resource '%s' has min_length (%d) greater than max_length (%d) in %s", r.Name, r.Length.Min, r.Length.Max, source)
53+
}
54+
return nil
55+
}
56+
3357
func main() {
3458
files, err := ioutil.ReadDir("templates")
3559
if err != nil {
@@ -63,6 +87,13 @@ func main() {
6387
log.Fatal(err)
6488
}
6589

90+
// Validate resources from resourceDefinition.json
91+
for _, r := range data {
92+
if err := validateResource(r, "resourceDefinition.json"); err != nil {
93+
log.Fatal(err)
94+
}
95+
}
96+
6697
// Undocumented resource definitions
6798
sourceDefinitionsUndocumented, err := ioutil.ReadFile("resourceDefinition_out_of_docs.json")
6899
if err != nil {
@@ -73,6 +104,26 @@ func main() {
73104
if err != nil {
74105
log.Fatal(err)
75106
}
107+
108+
// Validate resources from resourceDefinition_out_of_docs.json
109+
for _, r := range dataUndocumented {
110+
if err := validateResource(r, "resourceDefinition_out_of_docs.json"); err != nil {
111+
log.Fatal(err)
112+
}
113+
}
114+
115+
// Check for duplicate resource names before merging
116+
seenNames := make(map[string]string)
117+
for _, r := range data {
118+
seenNames[r.Name] = "resourceDefinition.json"
119+
}
120+
for _, r := range dataUndocumented {
121+
if source, exists := seenNames[r.Name]; exists {
122+
log.Fatalf("duplicate resource name '%s' found in both %s and resourceDefinition_out_of_docs.json", r.Name, source)
123+
}
124+
seenNames[r.Name] = "resourceDefinition_out_of_docs.json"
125+
}
126+
76127
data = append(data, dataUndocumented...)
77128

78129
// Sort the documented and undocumented resources alphabetically
@@ -84,10 +135,19 @@ func main() {
84135
if err != nil {
85136
log.Fatal(err)
86137
}
87-
parsedTemplate.ExecuteTemplate(mainFile, "main", data)
138+
defer mainFile.Close()
139+
140+
if err := parsedTemplate.ExecuteTemplate(mainFile, "main", data); err != nil {
141+
log.Fatal(err)
142+
}
143+
88144
outputsFile, err := os.OpenFile("outputs.tf", os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0644)
89145
if err != nil {
90146
log.Fatal(err)
91147
}
92-
parsedTemplate.ExecuteTemplate(outputsFile, "outputs", data)
148+
defer outputsFile.Close()
149+
150+
if err := parsedTemplate.ExecuteTemplate(outputsFile, "outputs", data); err != nil {
151+
log.Fatal(err)
152+
}
93153
}

0 commit comments

Comments
 (0)