Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(cmd): Simplify codegen selection logic #1466

Merged
merged 1 commit into from Mar 2, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 9 additions & 13 deletions internal/cmd/generate.go
Expand Up @@ -187,33 +187,25 @@ func Generate(ctx context.Context, e Env, dir, filename string, stderr io.Writer
if debug.Traced {
region = trace.StartRegion(ctx, "codegen")
}
var files map[string]string
var resp *plugin.CodeGenResponse
var genfunc func(req *plugin.CodeGenRequest) (*plugin.CodeGenResponse, error)
var out string
switch {
case sql.Gen.Go != nil:
out = combo.Go.Out
resp, err = golang.Generate(codeGenRequest(result, combo))
genfunc = golang.Generate
case sql.Gen.Kotlin != nil:
out = combo.Kotlin.Out
resp, err = kotlin.Generate(codeGenRequest(result, combo))
genfunc = kotlin.Generate
case sql.Gen.Python != nil:
out = combo.Python.Out
resp, err = python.Generate(codeGenRequest(result, combo))
genfunc = python.Generate
default:
panic("missing language backend")
}
resp, err := genfunc(codeGenRequest(result, combo))
if region != nil {
region.End()
}

if resp != nil {
files = map[string]string{}
for _, file := range resp.Files {
files[file.Name] = string(file.Contents)
}
}

if err != nil {
fmt.Fprintf(stderr, "# package %s\n", name)
fmt.Fprintf(stderr, "error generating code: %s\n", err)
Expand All @@ -223,6 +215,10 @@ func Generate(ctx context.Context, e Env, dir, filename string, stderr io.Writer
}
continue
}
files := map[string]string{}
for _, file := range resp.Files {
files[file.Name] = string(file.Contents)
}
for n, source := range files {
filename := filepath.Join(dir, out, n)
output[filename] = source
Expand Down