Skip to content

Commit

Permalink
Merge pull request #36792 from hashicorp/td-generate-tagtests-config
Browse files Browse the repository at this point in the history
Generate configs for tagging tests from template
  • Loading branch information
gdavison authored May 2, 2024
2 parents d10c12f + df2a73e commit 94796ad
Show file tree
Hide file tree
Showing 184 changed files with 15,332 additions and 3,973 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/copyright.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ on:
- .goreleaser.yml

jobs:
go_generate:
copywrite:
name: add headers check
runs-on: ubuntu-latest
steps:
Expand Down
1 change: 1 addition & 0 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ gen: prereq-go ## Run all Go generators
rm -f internal/provider/*_gen.go
rm -f internal/service/**/*_gen.go
rm -f internal/service/**/*_gen_test.go
rm -f internal/service/**/*_gen.tf
rm -f names/caps.md
rm -f names/*_gen.go
rm -f website/docs/guides/custom-service-endpoints.html.md
Expand Down
48 changes: 38 additions & 10 deletions internal/generate/common/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type Destination interface {
Write() error
WriteBytes(body []byte) error
WriteTemplate(templateName, templateBody string, templateData any) error
WriteTemplateSet(templates *template.Template, templateData any) error
}

func (g *Generator) NewGoFileDestination(filename string) Destination {
Expand Down Expand Up @@ -133,17 +134,12 @@ func (d *baseDestination) WriteTemplate(templateName, templateBody string, templ
return err
}

if d.formatter != nil {
unformattedBody := body
body, err = d.formatter(unformattedBody)

if err != nil {
return fmt.Errorf("formatting parsed template:\n%s\n%w", unformattedBody, err)
}
body, err = d.format(body)
if err != nil {
return err
}

_, err = d.buffer.Write(body)
return err
return d.WriteBytes(body)
}

func parseTemplate(templateName, templateBody string, templateData any) ([]byte, error) {
Expand All @@ -156,12 +152,44 @@ func parseTemplate(templateName, templateBody string, templateData any) ([]byte,
return nil, fmt.Errorf("parsing function template: %w", err)
}

return executeTemplate(tmpl, templateData)
}

func executeTemplate(tmpl *template.Template, templateData any) ([]byte, error) {
var buffer bytes.Buffer
err = tmpl.Execute(&buffer, templateData)
err := tmpl.Execute(&buffer, templateData)

if err != nil {
return nil, fmt.Errorf("executing template: %w", err)
}

return buffer.Bytes(), nil
}

func (d *baseDestination) WriteTemplateSet(templates *template.Template, templateData any) error {
body, err := executeTemplate(templates, templateData)
if err != nil {
return err
}

body, err = d.format(body)
if err != nil {
return err
}

return d.WriteBytes(body)
}

func (d *baseDestination) format(body []byte) ([]byte, error) {
if d.formatter == nil {
return body, nil
}

unformattedBody := body
body, err := d.formatter(unformattedBody)
if err != nil {
return nil, fmt.Errorf("formatting parsed template:\n%s\n%w", unformattedBody, err)
}

return body, nil
}
Loading

0 comments on commit 94796ad

Please sign in to comment.