Skip to content

Commit a9a2d78

Browse files
authored
refactor(codegen): Extract all inline templates to external files (#697)
1 parent c16178e commit a9a2d78

64 files changed

Lines changed: 4190 additions & 4248 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

assets/terraform/examples/resources/panos_virtual_router/resource.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ resource "panos_virtual_router" "bgp" {
232232
prepend = 1
233233
}
234234
community = {
235-
argument = ["65100:3000", "65100:4000"]
235+
overwrite = ["65100:3000", "65100:4000"]
236236
}
237237
}
238238
}

pkg/generate/generator.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,13 +224,19 @@ func (c *Creator) createFullFilePath(templateName string) string {
224224
}
225225

226226
// listOfTemplates returns a list of templates defined in TemplatesDir.
227+
// Excludes templates in the "partials" subdirectory as those are meant to be
228+
// called from within other templates, not processed independently.
227229
func (c *Creator) listOfTemplates() ([]string, error) {
228230
var files []string
229231
err := filepath.WalkDir(c.TemplatesDir, func(path string, entry os.DirEntry, err error) error {
230232
if err != nil {
231233
return err
232234
}
233235
if entry.IsDir() {
236+
// Skip the partials directory
237+
if entry.Name() == "partials" {
238+
return filepath.SkipDir
239+
}
234240
return nil
235241
}
236242
if strings.HasSuffix(entry.Name(), ".tmpl") {

pkg/properties/normalized.go

Lines changed: 41 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"io/fs"
77
"maps"
8+
"os"
89
"path/filepath"
910
"regexp"
1011
"runtime"
@@ -1125,20 +1126,26 @@ func (spec *Normalization) ResourceXpathVariablesCount() int {
11251126
return len(spec.PanosXpath.Variables)
11261127
}
11271128

1128-
const attributesFromXpathComponentsTmpl = `
1129-
{{- range .Components }}
1130-
{{ if eq .Type "value" }}
1131-
{{ $.Target }}.{{ .Name }} = types.StringValue(components[{{ .Idx }}])
1132-
{{- else if eq .Type "entry" }}
1133-
{
1134-
component := components[{{ .Idx }}]
1135-
component = strings.TrimPrefix(component, "entry[@name='")
1136-
component = strings.TrimSuffix(component, "']")
1137-
{{ $.Target }}.{{ .Name.CamelCase }} = types.StringValue(component)
1129+
// loadTemplate loads a template from either sdk or terraform-provider templates directory
1130+
func loadTemplate(templateType string, templatePath string) (string, error) {
1131+
fullPath := filepath.Join("templates", templateType, templatePath)
1132+
content, err := os.ReadFile(fullPath)
1133+
if err != nil {
1134+
// Try from parent directories (for when running from subdirectories)
1135+
for i := 1; i <= 3; i++ {
1136+
prefix := strings.Repeat("../", i)
1137+
altPath := filepath.Join(prefix, "templates", templateType, templatePath)
1138+
content, err = os.ReadFile(altPath)
1139+
if err == nil {
1140+
break
1141+
}
1142+
}
1143+
if err != nil {
1144+
return "", fmt.Errorf("failed to read template %s: %w", fullPath, err)
1145+
}
1146+
}
1147+
return string(content), nil
11381148
}
1139-
{{- end }}
1140-
{{- end }}
1141-
`
11421149

11431150
func (spec *Normalization) AttributesFromXpathComponents(target string) (string, error) {
11441151
type componentCtx struct {
@@ -1175,66 +1182,43 @@ func (spec *Normalization) AttributesFromXpathComponents(target string) (string,
11751182
Components: components,
11761183
}
11771184

1178-
tmpl := template.Must(template.New("attributes-from-xpath-components").Parse(attributesFromXpathComponentsTmpl))
1185+
tmplContent, err := loadTemplate("terraform-provider", "partials/attributes_from_xpath_components.tmpl")
1186+
if err != nil {
1187+
return "", err
1188+
}
1189+
1190+
tmpl := template.Must(template.New("attributes-from-xpath-components").Parse(tmplContent))
11791191

11801192
var buf bytes.Buffer
1181-
err := tmpl.Execute(&buf, data)
1193+
err = tmpl.Execute(&buf, data)
11821194
if err != nil {
11831195
panic(err)
11841196
}
11851197

11861198
return buf.String(), nil
11871199
}
11881200

1189-
const resourceXpathAssignmentsTmpl = `
1190-
{{- range .Variables }}
1191-
{{- if or (eq .Type "entry") (eq .Type "value") }}
1192-
ans = append(ans, components[{{ .Idx }}])
1193-
{{- else if eq .Type "static" }}
1194-
ans = append(ans, "{{ .Name.Original }}")
1195-
{{- end }}
1196-
{{- end }}
1197-
`
1198-
11991201
func (spec *Normalization) ResourceXpathAssignments() (string, error) {
12001202
data := xpathVariablesCtx{
12011203
Variables: createXpathVariablesContext(spec),
12021204
}
12031205

1204-
tmpl := template.Must(template.New("resource-xpath-assignments").Parse(resourceXpathAssignmentsTmpl))
1206+
tmplContent, err := loadTemplate("sdk", "partials/resource_xpath_assignments.tmpl")
1207+
if err != nil {
1208+
return "", err
1209+
}
1210+
1211+
tmpl := template.Must(template.New("resource-xpath-assignments").Parse(tmplContent))
12051212

12061213
var buf bytes.Buffer
1207-
err := tmpl.Execute(&buf, data)
1214+
err = tmpl.Execute(&buf, data)
12081215
if err != nil {
12091216
panic(err)
12101217
}
12111218

12121219
return buf.String(), nil
12131220
}
12141221

1215-
const xpathVariableChecksTmpl = `
1216-
{{- range .Variables }}
1217-
{{- if eq .Type "entry" }}
1218-
{
1219-
component := components[{{ .Idx }}]
1220-
{{- if .AllowEmpty }}
1221-
if component != "entry" {
1222-
{{- end }}
1223-
if !strings.HasPrefix(component, "entry[@name=\"]") && !strings.HasPrefix(component, "entry[@name='") {
1224-
return nil, errors.NewInvalidXpathComponentError(fmt.Sprintf("{{ .Name.CamelCase }} must be formatted as entry: %s", component))
1225-
}
1226-
1227-
if !strings.HasSuffix(component, "\"]") && !strings.HasSuffix(component, "']") {
1228-
return nil, errors.NewInvalidXpathComponentError(fmt.Sprintf("{{ .Name.CamelCase }} must be formatted as entry: %s", component))
1229-
}
1230-
{{- if .AllowEmpty }}
1231-
}
1232-
{{- end }}
1233-
}
1234-
{{- end }}
1235-
{{- end }}
1236-
`
1237-
12381222
type xpathVariableCtx struct {
12391223
Type object.PanosXpathVariableType
12401224
Name *NameVariant
@@ -1299,10 +1283,15 @@ func (spec *Normalization) ResourceXpathVariableChecks() (string, error) {
12991283
Variables: createXpathVariablesContext(spec),
13001284
}
13011285

1286+
tmplContent, err := loadTemplate("sdk", "partials/xpath_variable_checks.tmpl")
1287+
if err != nil {
1288+
return "", err
1289+
}
1290+
13021291
var buf bytes.Buffer
1303-
tmpl := template.Must(template.New("xpath-variable-checks").Parse(xpathVariableChecksTmpl))
1292+
tmpl := template.Must(template.New("xpath-variable-checks").Parse(tmplContent))
13041293

1305-
err := tmpl.Execute(&buf, data)
1294+
err = tmpl.Execute(&buf, data)
13061295
if err != nil {
13071296
panic(err)
13081297
}

0 commit comments

Comments
 (0)