Skip to content

Commit

Permalink
Add pggen --inline-param-count=2 flag (#87)
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 authored Jun 17, 2023
1 parent c57cb1e commit fe0c28d
Show file tree
Hide file tree
Showing 34 changed files with 2,007 additions and 141 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
11 changes: 6 additions & 5 deletions example/author/codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ func TestGenerate_Go_Example_Author(t *testing.T) {
tmpDir := t.TempDir()
err := pggen.Generate(
pggen.GenerateOptions{
ConnString: conn.Config().ConnString(),
QueryFiles: []string{"query.sql"},
OutputDir: tmpDir,
GoPackage: "author",
Language: pggen.LangGo,
ConnString: conn.Config().ConnString(),
QueryFiles: []string{"query.sql"},
OutputDir: tmpDir,
GoPackage: "author",
Language: pggen.LangGo,
InlineParamCount: 2,
})
if err != nil {
t.Fatalf("Generate() example/author: %s", err)
Expand Down
11 changes: 6 additions & 5 deletions example/complex_params/codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ func TestGenerate_Go_Example_ComplexParams(t *testing.T) {
tmpDir := t.TempDir()
err := pggen.Generate(
pggen.GenerateOptions{
ConnString: conn.Config().ConnString(),
QueryFiles: []string{"query.sql"},
OutputDir: tmpDir,
GoPackage: "complex_params",
Language: pggen.LangGo,
ConnString: conn.Config().ConnString(),
QueryFiles: []string{"query.sql"},
OutputDir: tmpDir,
GoPackage: "complex_params",
Language: pggen.LangGo,
InlineParamCount: 2,
TypeOverrides: map[string]string{
"int4": "int",
"text": "string",
Expand Down
11 changes: 6 additions & 5 deletions example/composite/codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ func TestGenerate_Go_Example_Composite(t *testing.T) {
tmpDir := t.TempDir()
err := pggen.Generate(
pggen.GenerateOptions{
ConnString: conn.Config().ConnString(),
QueryFiles: []string{"query.sql"},
OutputDir: tmpDir,
GoPackage: "composite",
Language: pggen.LangGo,
ConnString: conn.Config().ConnString(),
QueryFiles: []string{"query.sql"},
OutputDir: tmpDir,
GoPackage: "composite",
Language: pggen.LangGo,
InlineParamCount: 2,
TypeOverrides: map[string]string{
"_bool": "[]bool",
"bool": "bool",
Expand Down
11 changes: 6 additions & 5 deletions example/custom_types/codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ func TestGenerate_Go_Example_CustomTypes(t *testing.T) {
tmpDir := t.TempDir()
err := pggen.Generate(
pggen.GenerateOptions{
ConnString: conn.Config().ConnString(),
QueryFiles: []string{"query.sql"},
OutputDir: tmpDir,
GoPackage: "custom_types",
Language: pggen.LangGo,
ConnString: conn.Config().ConnString(),
QueryFiles: []string{"query.sql"},
OutputDir: tmpDir,
GoPackage: "custom_types",
Language: pggen.LangGo,
InlineParamCount: 2,
TypeOverrides: map[string]string{
"text": "github.com/jschaf/pggen/example/custom_types/mytype.String",
"int8": "github.com/jschaf/pggen/example/custom_types.CustomInt",
Expand Down
11 changes: 6 additions & 5 deletions example/device/codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ func TestGenerate_Go_Example_Device(t *testing.T) {
tmpDir := t.TempDir()
err := pggen.Generate(
pggen.GenerateOptions{
ConnString: conn.Config().ConnString(),
QueryFiles: []string{"query.sql"},
OutputDir: tmpDir,
GoPackage: "device",
Language: pggen.LangGo,
ConnString: conn.Config().ConnString(),
QueryFiles: []string{"query.sql"},
OutputDir: tmpDir,
GoPackage: "device",
Language: pggen.LangGo,
InlineParamCount: 2,
})
if err != nil {
t.Fatalf("Generate() example/device: %s", err)
Expand Down
11 changes: 6 additions & 5 deletions example/domain/codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ func TestGenerate_Go_Example_Domain(t *testing.T) {
tmpDir := t.TempDir()
err := pggen.Generate(
pggen.GenerateOptions{
ConnString: conn.Config().ConnString(),
QueryFiles: []string{"query.sql"},
OutputDir: tmpDir,
GoPackage: "domain",
Language: pggen.LangGo,
ConnString: conn.Config().ConnString(),
QueryFiles: []string{"query.sql"},
OutputDir: tmpDir,
GoPackage: "domain",
Language: pggen.LangGo,
InlineParamCount: 2,
})
if err != nil {
t.Fatalf("Generate() example/domain: %s", err)
Expand Down
11 changes: 6 additions & 5 deletions example/enums/codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ func TestGenerate_Go_Example_Enums(t *testing.T) {
tmpDir := t.TempDir()
err := pggen.Generate(
pggen.GenerateOptions{
ConnString: conn.Config().ConnString(),
QueryFiles: []string{"query.sql"},
OutputDir: tmpDir,
GoPackage: "enums",
Language: pggen.LangGo,
ConnString: conn.Config().ConnString(),
QueryFiles: []string{"query.sql"},
OutputDir: tmpDir,
GoPackage: "enums",
Language: pggen.LangGo,
InlineParamCount: 2,
})
if err != nil {
t.Fatalf("Generate() example/enums: %s", err)
Expand Down
13 changes: 7 additions & 6 deletions example/erp/order/codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"testing"
)

func TestGenerate_Go_Example_Order(t *testing.T) {
func TestGenerate_Go_Example_ERP_Order(t *testing.T) {
conn, cleanupFunc := pgtest.NewPostgresSchema(t, []string{
"../01_schema.sql",
"../02_schema.sql",
Expand All @@ -24,11 +24,12 @@ func TestGenerate_Go_Example_Order(t *testing.T) {
"customer.sql",
"price.sql",
},
OutputDir: tmpDir,
GoPackage: "order",
Language: pggen.LangGo,
Acronyms: map[string]string{"mrr": "MRR"},
TypeOverrides: map[string]string{"tenant_id": "int"},
OutputDir: tmpDir,
GoPackage: "order",
Language: pggen.LangGo,
InlineParamCount: 2,
Acronyms: map[string]string{"mrr": "MRR"},
TypeOverrides: map[string]string{"tenant_id": "int"},
})
if err != nil {
t.Fatalf("Generate() example/erp/order: %s", err)
Expand Down
11 changes: 6 additions & 5 deletions example/function/codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ func TestGenerate_Go_Example_Function(t *testing.T) {
tmpDir := t.TempDir()
err := pggen.Generate(
pggen.GenerateOptions{
ConnString: conn.Config().ConnString(),
QueryFiles: []string{"query.sql"},
OutputDir: tmpDir,
GoPackage: "function",
Language: pggen.LangGo,
ConnString: conn.Config().ConnString(),
QueryFiles: []string{"query.sql"},
OutputDir: tmpDir,
GoPackage: "function",
Language: pggen.LangGo,
InlineParamCount: 2,
})
if err != nil {
t.Fatalf("Generate() example/function: %s", err)
Expand Down
11 changes: 6 additions & 5 deletions example/go_pointer_types/codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ func TestGenerate_Go_Example_GoPointerTypes(t *testing.T) {
tmpDir := t.TempDir()
err := pggen.Generate(
pggen.GenerateOptions{
ConnString: conn.Config().ConnString(),
QueryFiles: []string{"query.sql"},
OutputDir: tmpDir,
GoPackage: "go_pointer_types",
Language: pggen.LangGo,
ConnString: conn.Config().ConnString(),
QueryFiles: []string{"query.sql"},
OutputDir: tmpDir,
GoPackage: "go_pointer_types",
Language: pggen.LangGo,
InlineParamCount: 2,
TypeOverrides: map[string]string{
"int4": "*int",
"_int4": "[]int",
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 fe0c28d

Please sign in to comment.