Skip to content

Commit

Permalink
Add pggen --inline-param-count=2 flag
Browse files Browse the repository at this point in the history
Default to two to match previous behavior. Main use case is to always output a
struct for parameters. Useful so that structure doesn't change when adding a
new param.

Fixes #83.
  • Loading branch information
jschaf committed Jun 17, 2023
1 parent c57cb1e commit fd89ba1
Show file tree
Hide file tree
Showing 17 changed files with 1,906 additions and 57 deletions.
19 changes: 11 additions & 8 deletions cmd/pggen/pggen.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ func newGenCmd() *ffcli.Command {
goTypes := flags.Strings(fset, "go-type", nil,
"custom type mapping from Postgres to fully qualified Go type, "+
"like 'device_type=github.com/jschaf/pggen.DeviceType'")
inlineParamCount := fset.Int("inline-param-count", 2,
"number of params (inclusive) to inline when calling querier methods; 0 always generates a struct")
logLvl := zap.InfoLevel
fset.Var(&logLvl, "log", "log level: debug, info, or error")
goSubCmd := &ffcli.Command{
Expand Down Expand Up @@ -159,14 +161,15 @@ func newGenCmd() *ffcli.Command {

// Codegen.
err = pggen.Generate(pggen.GenerateOptions{
Language: pggen.LangGo,
ConnString: *postgresConn,
SchemaFiles: schemas,
QueryFiles: queries,
OutputDir: outDir,
Acronyms: acros,
TypeOverrides: typeOverrides,
LogLevel: logLvl,
Language: pggen.LangGo,
ConnString: *postgresConn,
SchemaFiles: schemas,
QueryFiles: queries,
OutputDir: outDir,
Acronyms: acros,
TypeOverrides: typeOverrides,
LogLevel: logLvl,
InlineParamCount: *inlineParamCount,
})
if err != nil {
return err
Expand Down
43 changes: 39 additions & 4 deletions example/acceptance_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build acceptance_test
// +build acceptance_test

package example

Expand Down Expand Up @@ -140,6 +139,42 @@ func TestExamples(t *testing.T) {
"--go-type", "_int4=[]int",
},
},
{
name: "example/inline_param_count/inline0",
args: []string{
"--schema-glob", "example/inline_param_count/schema.sql",
"--query-glob", "example/inline_param_count/query.sql",
"--output-dir", "example/inline_param_count/inline0",
"--inline-param-count", "0",
},
},
{
name: "example/inline_param_count/inline1",
args: []string{
"--schema-glob", "example/inline_param_count/schema.sql",
"--query-glob", "example/inline_param_count/query.sql",
"--output-dir", "example/inline_param_count/inline1",
"--inline-param-count", "1",
},
},
{
name: "example/inline_param_count/inline2",
args: []string{
"--schema-glob", "example/inline_param_count/schema.sql",
"--query-glob", "example/inline_param_count/query.sql",
"--output-dir", "example/inline_param_count/inline2",
"--inline-param-count", "2",
},
},
{
name: "example/inline_param_count/inline3",
args: []string{
"--schema-glob", "example/inline_param_count/schema.sql",
"--query-glob", "example/inline_param_count/query.sql",
"--output-dir", "example/inline_param_count/inline3",
"--inline-param-count", "3",
},
},
{
name: "example/ltree",
args: []string{
Expand Down Expand Up @@ -251,7 +286,7 @@ func TestExamples(t *testing.T) {
args := append(tt.args, "--postgres-connection", connStr)
runPggen(t, pggen, args...)
if !*update {
assertNoDiff(t)
assertNoGitDiff(t)
}
})
}
Expand All @@ -263,7 +298,7 @@ func runPggen(t *testing.T, pggen string, args ...string) string {
Args: append([]string{pggen, "gen", "go"}, args...),
Dir: projDir,
}
t.Log("running pggen")
t.Logf("running pggen: %s", cmd.String())
output, err := cmd.CombinedOutput()
if err != nil {
t.Log("pggen output:\n" + string(bytes.TrimSpace(output)))
Expand Down Expand Up @@ -300,7 +335,7 @@ var (
gitBinOnce = &sync.Once{}
)

func assertNoDiff(t *testing.T) {
func assertNoGitDiff(t *testing.T) {
gitBinOnce.Do(func() {
gitBin, gitBinErr = exec.LookPath("git")
if gitBinErr != nil {
Expand Down
90 changes: 90 additions & 0 deletions example/inline_param_count/codegen_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package author

import (
"github.com/jschaf/pggen"
"github.com/jschaf/pggen/internal/pgtest"
"github.com/stretchr/testify/assert"
"os"
"path/filepath"
"testing"
)

func TestGenerate_Go_Example_InlineParamCount(t *testing.T) {
conn, cleanupFunc := pgtest.NewPostgresSchema(t, []string{"schema.sql"})
defer cleanupFunc()

tests := []struct {
name string
opts pggen.GenerateOptions
wantQueryPath string
}{
{
name: "inline0",
opts: pggen.GenerateOptions{
ConnString: conn.Config().ConnString(),
QueryFiles: []string{"query.sql"},
GoPackage: "inline0",
Language: pggen.LangGo,
InlineParamCount: 0,
},
wantQueryPath: "inline0/query.sql.go",
},
{
name: "inline1",
opts: pggen.GenerateOptions{
ConnString: conn.Config().ConnString(),
QueryFiles: []string{"query.sql"},
GoPackage: "inline1",
Language: pggen.LangGo,
InlineParamCount: 1,
},
wantQueryPath: "inline1/query.sql.go",
},
{
name: "inline2",
opts: pggen.GenerateOptions{
ConnString: conn.Config().ConnString(),
QueryFiles: []string{"query.sql"},
GoPackage: "inline2",
Language: pggen.LangGo,
InlineParamCount: 2,
},
wantQueryPath: "inline2/query.sql.go",
},
{
name: "inline3",
opts: pggen.GenerateOptions{
ConnString: conn.Config().ConnString(),
QueryFiles: []string{"query.sql"},
GoPackage: "inline3",
Language: pggen.LangGo,
InlineParamCount: 3,
},
wantQueryPath: "inline3/query.sql.go",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := t.TempDir()
tt.opts.OutputDir = tmpDir
err := pggen.Generate(tt.opts)
if err != nil {
t.Fatalf("Generate() example/author %s: %s", tt.name, err.Error())
}

gotQueryFile := filepath.Join(tmpDir, "query.sql.go")
assert.FileExists(t, gotQueryFile, "Generate() should emit query.sql.go")
wantQueries, err := os.ReadFile(tt.wantQueryPath)
if err != nil {
t.Fatalf("read wanted query.go.sql: %s", err)
}
gotQueries, err := os.ReadFile(gotQueryFile)
if err != nil {
t.Fatalf("read generated query.go.sql: %s", err)
}
assert.Equalf(t, string(wantQueries), string(gotQueries),
"Got file %s; does not match contents of %s",
gotQueryFile, tt.wantQueryPath)
})
}
}
Loading

0 comments on commit fd89ba1

Please sign in to comment.