Skip to content

Commit

Permalink
refactor(cmd): Simplify codegen selection logic (#1466)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleconroy committed Mar 2, 2022
1 parent c417ff8 commit 3415bdd
Showing 1 changed file with 9 additions and 13 deletions.
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

0 comments on commit 3415bdd

Please sign in to comment.