Skip to content

Commit

Permalink
Add generated code to separate directories (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
bendbennett committed Sep 4, 2023
1 parent 5cc4e99 commit 4eca0ac
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 16 deletions.
50 changes: 41 additions & 9 deletions internal/cmd/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/google/go-cmp/cmp"
)

// TODO: currently doesn't compare nested directory files
func compareDirectories(t *testing.T, wantDirPath, gotDirPath string) {
t.Helper()

Expand Down Expand Up @@ -42,17 +41,50 @@ func compareDirectories(t *testing.T, wantDirPath, gotDirPath string) {
continue
}

gotFile, err := os.ReadFile(path.Join(gotDirPath, gotEntry.Name()))
if err != nil {
t.Fatalf("unexpected error reading `got` file: %s", err)
}
wantFile, _ := os.ReadFile(path.Join(wantDirPath, wantEntry.Name()))
compareFiles(t, path.Join(gotDirPath, gotEntry.Name()), path.Join(wantDirPath, wantEntry.Name()))
}
}

func compareFiles(t *testing.T, got, want string) {
gotFile, err := os.Open(got)

if err != nil {
t.Fatalf("unexpected error opening %s: %s", got, err)
}

gotFileInfo, err := gotFile.Stat()

if err != nil {
t.Fatalf("unexpected error stat %s: %s", got, err)
}

if gotFileInfo.IsDir() {
dirEntries, err := os.ReadDir(got)

if err != nil {
t.Fatalf("unexpected error reading `want` file: %s", err)
t.Fatalf("unexpected error reading dir %s: %s", got, err)
}

if diff := cmp.Diff(string(gotFile), string(wantFile)); diff != "" {
t.Errorf("unexpected difference in %s: %s", wantEntry.Name(), diff)
for _, dirEntry := range dirEntries {
compareFiles(t, path.Join(got, dirEntry.Name()), path.Join(want, dirEntry.Name()))
}

return
}

gotFileBytes, err := os.ReadFile(got)

if err != nil {
t.Fatalf("unexpected error reading `got` file: %s", err)
}

wantFileBytes, err := os.ReadFile(want)

if err != nil {
t.Fatalf("unexpected error reading `want` file: %s", err)
}

if diff := cmp.Diff(string(gotFileBytes), string(wantFileBytes)); diff != "" {
t.Errorf("unexpected difference in %s: %s", got, diff)
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 24 additions & 3 deletions internal/output/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@ import (

func WriteDataSources(dataSourcesSchema, dataSourcesModels, dataSourcesModelObjectHelpers, dataSourcesToFrom map[string][]byte, outputDir string) error {
for k, v := range dataSourcesSchema {
dirName := fmt.Sprintf("datasource_%s", k)

err := os.MkdirAll(filepath.Join(outputDir, dirName), os.ModePerm)
if err != nil {
return err
}

filename := fmt.Sprintf("%s_data_source_gen.go", k)

f, err := os.Create(filepath.Join(outputDir, filename))
f, err := os.Create(filepath.Join(outputDir, dirName, filename))
if err != nil {
return err
}
Expand Down Expand Up @@ -46,9 +53,16 @@ func WriteDataSources(dataSourcesSchema, dataSourcesModels, dataSourcesModelObje

func WriteResources(resourcesSchema, resourcesModels, resourcesModelObjectHelpers, resourcesToFrom map[string][]byte, outputDir string) error {
for k, v := range resourcesSchema {
dirName := fmt.Sprintf("resource_%s", k)

err := os.MkdirAll(filepath.Join(outputDir, dirName), os.ModePerm)
if err != nil {
return err
}

filename := fmt.Sprintf("%s_resource_gen.go", k)

f, err := os.Create(filepath.Join(outputDir, filename))
f, err := os.Create(filepath.Join(outputDir, dirName, filename))
if err != nil {
return err
}
Expand Down Expand Up @@ -79,9 +93,16 @@ func WriteResources(resourcesSchema, resourcesModels, resourcesModelObjectHelper

func WriteProviders(providersSchema, providerModels, providerModelObjectHelpers, providerToFrom map[string][]byte, outputDir string) error {
for k, v := range providersSchema {
dirName := fmt.Sprintf("provider_%s", k)

err := os.MkdirAll(filepath.Join(outputDir, dirName), os.ModePerm)
if err != nil {
return err
}

filename := fmt.Sprintf("%s_provider_gen.go", k)

f, err := os.Create(filepath.Join(outputDir, filename))
f, err := os.Create(filepath.Join(outputDir, dirName, filename))
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion internal/schema/schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package schema

import (
"bytes"
"fmt"
"strings"
)

// TODO: Field(s) could be added to handle end-user supplying their own templates to allow overriding.
Expand All @@ -24,7 +26,7 @@ func (g GeneratorSchemas) SchemasBytes(packageName, generatorType string) (map[s
for k, s := range g.schemas {

if packageName == "" {
packageName = k
packageName = fmt.Sprintf("%s_%s", strings.ToLower(generatorType), k)
}

b, err := s.SchemaBytes(k, packageName, generatorType)
Expand Down

0 comments on commit 4eca0ac

Please sign in to comment.