diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md index 30b1b88..920aba0 100644 --- a/ARCHITECTURE.md +++ b/ARCHITECTURE.md @@ -8,15 +8,15 @@ in the following steps. the `--schema-glob` flag in [cmd/pggen/pggen.go]. Pass the normalized options to `pggen.Generate` in [generate.go]. -1. Start Postgres by either connecting to the database specified in +2. Start Postgres by either connecting to the database specified in `--postgres-connection` or by starting a new Dockerized Postgres instance. [internal/pgdocker/pgdocker.go] creates and destroys Docker images for pggen. -1. Parse each query files into an `*ast.File` containing many +3. Parse each query files into an `*ast.File` containing many `*ast.SourceQuery` nodes in [internal/parser/interface.go]. -1. Infer the Postgres types and nullability for the input parameters and output +4. Infer the Postgres types and nullability for the input parameters and output columns of an `*ast.SourceQuery` and store the results in `pginfer.TypedQuery` in [internal/pginfer/pginfer.go]. @@ -39,13 +39,13 @@ in the following steps. flow analysis to determine nullability. I've started down that road in [pgplan.go](./internal/pgplan/pgplan.go). -1. Transform each `*ast.File` into `codegen.QueryFile` in [generate.go] +5. Transform each `*ast.File` into `codegen.QueryFile` in [generate.go] `parseQueries`. -1. Use a language-specific code generator to transform `codegen.QueryFile` +6. Use a language-specific code generator to transform `codegen.QueryFile` into a `golang.TemplatedFile` like with [internal/codegen/golang/templater.go]. -1. Emit the generated code from `golang.TemplateFile` in +7. Emit the generated code from `golang.TemplateFile` in [internal/codegen/golang/templated_file.go] [cmd/pggen/pggen.go]: cmd/pggen/pggen.go diff --git a/README.md b/README.md index ccbc3c0..811d96a 100644 --- a/README.md +++ b/README.md @@ -326,7 +326,7 @@ Examples embedded in the repo: which should render as `AuthorIDs`, not `AuthorIds`. For the IDs example, use `--acronym ids=IDs`. - 1. Short form: `--acronym `: replaces `` with uppercase + 2. Short form: `--acronym `: replaces `` with uppercase ``. Equivalent to `--acronym =` By default, pggen includes `--acronym id` to render `id` as `ID`. @@ -338,7 +338,7 @@ Examples embedded in the repo: CREATE TYPE device_type AS ENUM ('undefined', 'phone', 'ipad'); ``` - Generates the following Go code when used in a query: + pggen generates the following Go code when used in a query: ```go // DeviceType represents the Postgres enum device_type. @@ -372,7 +372,7 @@ Examples embedded in the repo: means the Go type must fulfill at least one of following: - The Go type is a wrapper around primitive type, like `type AuthorID int`. - pgx will use the decode methods on the underlying primitive type. + pgx will use decode methods on the underlying primitive type. - The Go type implements both [`pgtype.BinaryDecoder`] and [`pgtype.TextDecoder`]. pgx will use the correct decoder based on the wire @@ -404,7 +404,7 @@ Examples embedded in the repo: SELECT ROW (15, 'qux')::"user" AS "user"; ``` - Generates the following Go code: + pggen generates the following Go code: ```go // User represents the Postgres composite type "user". @@ -474,7 +474,7 @@ We'll walk through the generated file `author/query.sql.go`: - The `Querier` interface defines the interface with methods for each SQL query. Each SQL query compiles into three methods, one method for to run - query by itself, and two methods to support batching a query with + the query by itself, and two methods to support batching a query with [`pgx.Batch`]. ```go @@ -574,9 +574,9 @@ We'll walk through the generated file `author/query.sql.go`: nullable types for all built-in Postgres types. pggen tries to infer if a column is nullable or non-nullable. If a column is nullable, pggen uses a `pgtype` Go type like `pgtype.Text`. If a column is non-nullable, pggen uses - a more ergonomic type like `string`. pggen's nullability inference in - [internal/pginfer/nullability.go] is rudimentary; a proper approach requires - a full explain plan with some control flow analysis. + a more ergonomic type like `string`. pggen's nullability inference + implemented in [internal/pginfer/nullability.go] is rudimentary; a proper + approach requires a full explain-plan with some control flow analysis. - Lastly, pggen generates the implementation for each query. diff --git a/example/author/codegen_test.go b/example/author/codegen_test.go index a7339ad..43a66a8 100644 --- a/example/author/codegen_test.go +++ b/example/author/codegen_test.go @@ -26,19 +26,19 @@ func TestGenerate_Go_Example_Author(t *testing.T) { t.Fatalf("Generate() example/author: %s", err) } - wantQueriesFile := "query.sql.go" - gotQueriesFile := filepath.Join(tmpDir, "query.sql.go") - assert.FileExists(t, gotQueriesFile, + wantQueryFile := "query.sql.go" + gotQueryFile := filepath.Join(tmpDir, "query.sql.go") + assert.FileExists(t, gotQueryFile, "Generate() should emit query.sql.go") - wantQueries, err := os.ReadFile(wantQueriesFile) + wantQueries, err := os.ReadFile(wantQueryFile) if err != nil { t.Fatalf("read wanted query.go.sql: %s", err) } - gotQueries, err := os.ReadFile(gotQueriesFile) + 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", - gotQueriesFile, wantQueriesFile) + gotQueryFile, wantQueryFile) } diff --git a/example/complex_params/codegen_test.go b/example/complex_params/codegen_test.go index 81d36ec..ce77990 100644 --- a/example/complex_params/codegen_test.go +++ b/example/complex_params/codegen_test.go @@ -30,18 +30,18 @@ func TestGenerate_Go_Example_ComplexParams(t *testing.T) { t.Fatalf("Generate() example/complex_params: %s", err) } - wantQueriesFile := "query.sql.go" - gotQueriesFile := filepath.Join(tmpDir, "query.sql.go") - assert.FileExists(t, gotQueriesFile, "Generate() should emit query.sql.go") - wantQueries, err := os.ReadFile(wantQueriesFile) + wantQueryFile := "query.sql.go" + gotQueryFile := filepath.Join(tmpDir, "query.sql.go") + assert.FileExists(t, gotQueryFile, "Generate() should emit query.sql.go") + wantQueries, err := os.ReadFile(wantQueryFile) if err != nil { t.Fatalf("read wanted query.go.sql: %s", err) } - gotQueries, err := os.ReadFile(gotQueriesFile) + 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", - gotQueriesFile, wantQueriesFile) + gotQueryFile, wantQueryFile) } diff --git a/example/composite/codegen_test.go b/example/composite/codegen_test.go index ef8d9d1..879035e 100644 --- a/example/composite/codegen_test.go +++ b/example/composite/codegen_test.go @@ -34,19 +34,19 @@ func TestGenerate_Go_Example_Composite(t *testing.T) { t.Fatalf("Generate(): %s", err) } - wantQueriesFile := "query.sql.go" - gotQueriesFile := filepath.Join(tmpDir, "query.sql.go") - assert.FileExists(t, gotQueriesFile, + wantQueryFile := "query.sql.go" + gotQueryFile := filepath.Join(tmpDir, "query.sql.go") + assert.FileExists(t, gotQueryFile, "Generate() should emit query.sql.go") - wantQueries, err := os.ReadFile(wantQueriesFile) + wantQueries, err := os.ReadFile(wantQueryFile) if err != nil { t.Fatalf("read wanted query.go.sql: %s", err) } - gotQueries, err := os.ReadFile(gotQueriesFile) + 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", - gotQueriesFile, wantQueriesFile) + gotQueryFile, wantQueryFile) } diff --git a/example/custom_types/codegen_test.go b/example/custom_types/codegen_test.go index 3a4d0bf..4b28b56 100644 --- a/example/custom_types/codegen_test.go +++ b/example/custom_types/codegen_test.go @@ -32,18 +32,18 @@ func TestGenerate_Go_Example_CustomTypes(t *testing.T) { t.Fatalf("Generate() example/custom_types: %s", err) } - wantQueriesFile := "query.sql.go" - gotQueriesFile := filepath.Join(tmpDir, "query.sql.go") - assert.FileExists(t, gotQueriesFile, "Generate() should emit query.sql.go") - wantQueries, err := os.ReadFile(wantQueriesFile) + wantQueryFile := "query.sql.go" + gotQueryFile := filepath.Join(tmpDir, "query.sql.go") + assert.FileExists(t, gotQueryFile, "Generate() should emit query.sql.go") + wantQueries, err := os.ReadFile(wantQueryFile) if err != nil { t.Fatalf("read wanted query.go.sql: %s", err) } - gotQueries, err := os.ReadFile(gotQueriesFile) + 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", - gotQueriesFile, wantQueriesFile) + gotQueryFile, wantQueryFile) } diff --git a/example/custom_types/mytype/mytypes.go b/example/custom_types/mytype/mytypes.go index 972a27c..fe8dfaa 100644 --- a/example/custom_types/mytype/mytypes.go +++ b/example/custom_types/mytype/mytypes.go @@ -1,4 +1,4 @@ package mytype -// Simple custom type we can use for the Postgres text type. +// String is a simple custom type we can use for the Postgres text type. type String string diff --git a/example/custom_types/types.go b/example/custom_types/types.go index 84abd0f..fde455c 100644 --- a/example/custom_types/types.go +++ b/example/custom_types/types.go @@ -1,4 +1,4 @@ package custom_types -// A custom type in the same package as the query file. +// CustomInt is a custom type in the same package as the query file. type CustomInt int diff --git a/example/device/codegen_test.go b/example/device/codegen_test.go index 1697cbe..e6e015f 100644 --- a/example/device/codegen_test.go +++ b/example/device/codegen_test.go @@ -26,18 +26,18 @@ func TestGenerate_Go_Example_Device(t *testing.T) { t.Fatalf("Generate() example/device: %s", err) } - wantQueriesFile := "query.sql.go" - gotQueriesFile := filepath.Join(tmpDir, "query.sql.go") - assert.FileExists(t, gotQueriesFile, "Generate() should emit query.sql.go") - wantQueries, err := os.ReadFile(wantQueriesFile) + wantQueryFile := "query.sql.go" + gotQueryFile := filepath.Join(tmpDir, "query.sql.go") + assert.FileExists(t, gotQueryFile, "Generate() should emit query.sql.go") + wantQueries, err := os.ReadFile(wantQueryFile) if err != nil { t.Fatalf("read wanted query.go.sql: %s", err) } - gotQueries, err := os.ReadFile(gotQueriesFile) + 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", - gotQueriesFile, wantQueriesFile) + gotQueryFile, wantQueryFile) } diff --git a/example/domain/codegen_test.go b/example/domain/codegen_test.go index feb6ca8..bcc90ae 100644 --- a/example/domain/codegen_test.go +++ b/example/domain/codegen_test.go @@ -26,18 +26,18 @@ func TestGenerate_Go_Example_Domain(t *testing.T) { t.Fatalf("Generate() example/domain: %s", err) } - wantQueriesFile := "query.sql.go" - gotQueriesFile := filepath.Join(tmpDir, "query.sql.go") - assert.FileExists(t, gotQueriesFile, "Generate() should emit query.sql.go") - wantQueries, err := os.ReadFile(wantQueriesFile) + wantQueryFile := "query.sql.go" + gotQueryFile := filepath.Join(tmpDir, "query.sql.go") + assert.FileExists(t, gotQueryFile, "Generate() should emit query.sql.go") + wantQueries, err := os.ReadFile(wantQueryFile) if err != nil { t.Fatalf("read wanted query.go.sql: %s", err) } - gotQueries, err := os.ReadFile(gotQueriesFile) + 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", - gotQueriesFile, wantQueriesFile) + gotQueryFile, wantQueryFile) } diff --git a/example/enums/codegen_test.go b/example/enums/codegen_test.go index 43ada9b..868690c 100644 --- a/example/enums/codegen_test.go +++ b/example/enums/codegen_test.go @@ -26,18 +26,18 @@ func TestGenerate_Go_Example_Enums(t *testing.T) { t.Fatalf("Generate() example/enums: %s", err) } - wantQueriesFile := "query.sql.go" - gotQueriesFile := filepath.Join(tmpDir, "query.sql.go") - assert.FileExists(t, gotQueriesFile, "Generate() should emit query.sql.go") - wantQueries, err := os.ReadFile(wantQueriesFile) + wantQueryFile := "query.sql.go" + gotQueryFile := filepath.Join(tmpDir, "query.sql.go") + assert.FileExists(t, gotQueryFile, "Generate() should emit query.sql.go") + wantQueries, err := os.ReadFile(wantQueryFile) if err != nil { t.Fatalf("read wanted query.go.sql: %s", err) } - gotQueries, err := os.ReadFile(gotQueriesFile) + 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", - gotQueriesFile, wantQueriesFile) + gotQueryFile, wantQueryFile) } diff --git a/example/function/codegen_test.go b/example/function/codegen_test.go index adca690..1ca7701 100644 --- a/example/function/codegen_test.go +++ b/example/function/codegen_test.go @@ -26,19 +26,19 @@ func TestGenerate_Go_Example_Function(t *testing.T) { t.Fatalf("Generate() example/function: %s", err) } - wantQueriesFile := "query.sql.go" - gotQueriesFile := filepath.Join(tmpDir, "query.sql.go") - assert.FileExists(t, gotQueriesFile, + wantQueryFile := "query.sql.go" + gotQueryFile := filepath.Join(tmpDir, "query.sql.go") + assert.FileExists(t, gotQueryFile, "Generate() should emit query.sql.go") - wantQueries, err := os.ReadFile(wantQueriesFile) + wantQueries, err := os.ReadFile(wantQueryFile) if err != nil { t.Fatalf("read wanted query.go.sql: %s", err) } - gotQueries, err := os.ReadFile(gotQueriesFile) + 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", - gotQueriesFile, wantQueriesFile) + gotQueryFile, wantQueryFile) } diff --git a/example/go_pointer_types/codegen_test.go b/example/go_pointer_types/codegen_test.go index 0e75e60..5a07ae2 100644 --- a/example/go_pointer_types/codegen_test.go +++ b/example/go_pointer_types/codegen_test.go @@ -33,18 +33,18 @@ func TestGenerate_Go_Example_GoPointerTypes(t *testing.T) { t.Fatalf("Generate() example/go_pointer_types: %s", err) } - wantQueriesFile := "query.sql.go" - gotQueriesFile := filepath.Join(tmpDir, "query.sql.go") - assert.FileExists(t, gotQueriesFile, "Generate() should emit query.sql.go") - wantQueries, err := os.ReadFile(wantQueriesFile) + wantQueryFile := "query.sql.go" + gotQueryFile := filepath.Join(tmpDir, "query.sql.go") + assert.FileExists(t, gotQueryFile, "Generate() should emit query.sql.go") + wantQueries, err := os.ReadFile(wantQueryFile) if err != nil { t.Fatalf("read wanted query.go.sql: %s", err) } - gotQueries, err := os.ReadFile(gotQueriesFile) + 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", - gotQueriesFile, wantQueriesFile) + gotQueryFile, wantQueryFile) } diff --git a/example/ltree/codegen_test.go b/example/ltree/codegen_test.go index 0901053..4b15750 100644 --- a/example/ltree/codegen_test.go +++ b/example/ltree/codegen_test.go @@ -30,18 +30,18 @@ func TestGenerate_Go_Example_ltree(t *testing.T) { t.Fatalf("Generate() example/ltree: %s", err) } - wantQueriesFile := "query.sql.go" - gotQueriesFile := filepath.Join(tmpDir, "query.sql.go") - assert.FileExists(t, gotQueriesFile, "Generate() should emit query.sql.go") - wantQueries, err := os.ReadFile(wantQueriesFile) + wantQueryFile := "query.sql.go" + gotQueryFile := filepath.Join(tmpDir, "query.sql.go") + assert.FileExists(t, gotQueryFile, "Generate() should emit query.sql.go") + wantQueries, err := os.ReadFile(wantQueryFile) if err != nil { t.Fatalf("read wanted query.go.sql: %s", err) } - gotQueries, err := os.ReadFile(gotQueriesFile) + 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", - gotQueriesFile, wantQueriesFile) + gotQueryFile, wantQueryFile) } diff --git a/example/nested/codegen_test.go b/example/nested/codegen_test.go index 70981c6..780d6c1 100644 --- a/example/nested/codegen_test.go +++ b/example/nested/codegen_test.go @@ -30,18 +30,18 @@ func TestGenerate_Go_Example_nested(t *testing.T) { t.Fatalf("Generate() example/nested: %s", err) } - wantQueriesFile := "query.sql.go" - gotQueriesFile := filepath.Join(tmpDir, "query.sql.go") - assert.FileExists(t, gotQueriesFile, "Generate() should emit query.sql.go") - wantQueries, err := os.ReadFile(wantQueriesFile) + wantQueryFile := "query.sql.go" + gotQueryFile := filepath.Join(tmpDir, "query.sql.go") + assert.FileExists(t, gotQueryFile, "Generate() should emit query.sql.go") + wantQueries, err := os.ReadFile(wantQueryFile) if err != nil { t.Fatalf("read wanted query.go.sql: %s", err) } - gotQueries, err := os.ReadFile(gotQueriesFile) + 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", - gotQueriesFile, wantQueriesFile) + gotQueryFile, wantQueryFile) } diff --git a/example/numeric_external/codegen_test.go b/example/numeric_external/codegen_test.go index 0c17a20..a6db08b 100644 --- a/example/numeric_external/codegen_test.go +++ b/example/numeric_external/codegen_test.go @@ -32,19 +32,19 @@ func TestGenerate_Go_Example_Numeric_External(t *testing.T) { t.Fatalf("Generate(): %s", err) } - wantQueriesFile := "query.sql.go" - gotQueriesFile := filepath.Join(tmpDir, "query.sql.go") - assert.FileExists(t, gotQueriesFile, + wantQueryFile := "query.sql.go" + gotQueryFile := filepath.Join(tmpDir, "query.sql.go") + assert.FileExists(t, gotQueryFile, "Generate() should emit query.sql.go") - wantQueries, err := os.ReadFile(wantQueriesFile) + wantQueries, err := os.ReadFile(wantQueryFile) if err != nil { t.Fatalf("read wanted query.go.sql: %s", err) } - gotQueries, err := os.ReadFile(gotQueriesFile) + 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", - gotQueriesFile, wantQueriesFile) + gotQueryFile, wantQueryFile) } diff --git a/example/pgcrypto/codegen_test.go b/example/pgcrypto/codegen_test.go index 7e46178..e2c9d1b 100644 --- a/example/pgcrypto/codegen_test.go +++ b/example/pgcrypto/codegen_test.go @@ -26,18 +26,18 @@ func TestGenerate_Go_Example_Pgcrypto(t *testing.T) { t.Fatalf("Generate() example/pgcrypto: %s", err) } - wantQueriesFile := "query.sql.go" - gotQueriesFile := filepath.Join(tmpDir, "query.sql.go") - assert.FileExists(t, gotQueriesFile, "Generate() should emit query.sql.go") - wantQueries, err := os.ReadFile(wantQueriesFile) + wantQueryFile := "query.sql.go" + gotQueryFile := filepath.Join(tmpDir, "query.sql.go") + assert.FileExists(t, gotQueryFile, "Generate() should emit query.sql.go") + wantQueries, err := os.ReadFile(wantQueryFile) if err != nil { t.Fatalf("read wanted query.go.sql: %s", err) } - gotQueries, err := os.ReadFile(gotQueriesFile) + 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", - gotQueriesFile, wantQueriesFile) + gotQueryFile, wantQueryFile) } diff --git a/internal/pggen_schema.sql b/example/pggen_schema.sql similarity index 100% rename from internal/pggen_schema.sql rename to example/pggen_schema.sql diff --git a/example/slices/codegen_test.go b/example/slices/codegen_test.go index 9e2d6ff..fc51fb0 100644 --- a/example/slices/codegen_test.go +++ b/example/slices/codegen_test.go @@ -35,14 +35,14 @@ func TestGenerate_Go_Example_Slices(t *testing.T) { t.Fatalf("Generate() example/slices: %s", err) } - wantQueriesFile := "query.sql.go" - gotQueriesFile := filepath.Join(tmpDir, "query.sql.go") - assert.FileExists(t, gotQueriesFile, "Generate() should emit query.sql.go") - wantQueries, err := os.ReadFile(wantQueriesFile) + wantQueryFile := "query.sql.go" + gotQueryFile := filepath.Join(tmpDir, "query.sql.go") + assert.FileExists(t, gotQueryFile, "Generate() should emit query.sql.go") + wantQueries, err := os.ReadFile(wantQueryFile) if err != nil { t.Fatalf("read wanted query.go.sql: %s", err) } - gotQueries, err := os.ReadFile(gotQueriesFile) + gotQueries, err := os.ReadFile(gotQueryFile) if err != nil { t.Fatalf("read generated query.go.sql: %s", err) } diff --git a/example/syntax/codegen_test.go b/example/syntax/codegen_test.go index 9e837f5..0129853 100644 --- a/example/syntax/codegen_test.go +++ b/example/syntax/codegen_test.go @@ -28,18 +28,18 @@ func TestGenerate_Go_Example_Syntax(t *testing.T) { t.Fatalf("Generate() example/syntax: %s", err) } - wantQueriesFile := "query.sql.go" - gotQueriesFile := filepath.Join(tmpDir, "query.sql.go") - assert.FileExists(t, gotQueriesFile, "Generate() should emit query.sql.go") - wantQueries, err := os.ReadFile(wantQueriesFile) + wantQueryFile := "query.sql.go" + gotQueryFile := filepath.Join(tmpDir, "query.sql.go") + assert.FileExists(t, gotQueryFile, "Generate() should emit query.sql.go") + wantQueries, err := os.ReadFile(wantQueryFile) if err != nil { t.Fatalf("read wanted query.go.sql: %s", err) } - gotQueries, err := os.ReadFile(gotQueriesFile) + 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", - gotQueriesFile, wantQueriesFile) + gotQueryFile, wantQueryFile) } diff --git a/example/void/codegen_test.go b/example/void/codegen_test.go index 916928f..df958c8 100644 --- a/example/void/codegen_test.go +++ b/example/void/codegen_test.go @@ -26,18 +26,18 @@ func TestGenerate_Go_Example_void(t *testing.T) { t.Fatalf("Generate() example/void: %s", err) } - wantQueriesFile := "query.sql.go" - gotQueriesFile := filepath.Join(tmpDir, "query.sql.go") - assert.FileExists(t, gotQueriesFile, "Generate() should emit query.sql.go") - wantQueries, err := os.ReadFile(wantQueriesFile) + wantQueryFile := "query.sql.go" + gotQueryFile := filepath.Join(tmpDir, "query.sql.go") + assert.FileExists(t, gotQueryFile, "Generate() should emit query.sql.go") + wantQueries, err := os.ReadFile(wantQueryFile) if err != nil { t.Fatalf("read wanted query.go.sql: %s", err) } - gotQueries, err := os.ReadFile(gotQueriesFile) + 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", - gotQueriesFile, wantQueriesFile) + gotQueryFile, wantQueryFile) } diff --git a/generate.go b/generate.go index 94eac94..3081cb7 100644 --- a/generate.go +++ b/generate.go @@ -86,7 +86,7 @@ func Generate(opts GenerateOptions) (mErr error) { if err != nil { return fmt.Errorf("create zap logger: %w", err) } - defer logger.Sync() // nolint + defer func(logger *zap.Logger) { _ = logger.Sync() }(logger) l := logger.Sugar() // Postgres connection. diff --git a/internal/ast/ast.go b/internal/ast/ast.go index a4a2897..c22d121 100644 --- a/internal/ast/ast.go +++ b/internal/ast/ast.go @@ -32,7 +32,7 @@ func (k NodeKind) String() string { return kindNames[k] } -// All query nodes implement the Decl interface. +// Query nodes implement the Decl interface. type Query interface { Node queryNode() diff --git a/internal/parser/interface.go b/internal/parser/interface.go index 1cc1838..395eeeb 100644 --- a/internal/parser/interface.go +++ b/internal/parser/interface.go @@ -1,4 +1,4 @@ -// This file contains the exported entry points for invoking the parser. +// Package parser contains the exported entry points for invoking the parser. package parser import ( diff --git a/internal/pg/known_types.go b/internal/pg/known_types.go index a346b74..a76a1d6 100644 --- a/internal/pg/known_types.go +++ b/internal/pg/known_types.go @@ -6,7 +6,6 @@ import ( ) // If you add to this list, also add to defaultKnownTypes below. -//goland:noinspection GoNameStartsWithPackageName var ( Bool = BaseType{ID: pgtype.BoolOID, Name: "bool"} Bytea = BaseType{ID: pgtype.ByteaOID, Name: "bytea"} diff --git a/internal/pgtest/pg_test_db.go b/internal/pgtest/pg_test_db.go index 92224d4..e0905fd 100644 --- a/internal/pgtest/pg_test_db.go +++ b/internal/pgtest/pg_test_db.go @@ -2,8 +2,6 @@ package pgtest import ( "context" - crand "crypto/rand" - "encoding/binary" "github.com/jackc/pgx/v4" "math/rand" "os" @@ -13,18 +11,12 @@ import ( "time" ) -func init() { - var rngSeed int64 - _ = binary.Read(crand.Reader, binary.LittleEndian, &rngSeed) - rand.Seed(rngSeed) -} - // CleanupFunc deletes the schema and all database objects. type CleanupFunc func() type Option func(config *pgx.ConnConfig) -// NewPostgresSchema opens a connection with search_path set to a randomly +// NewPostgresSchemaString opens a connection with search_path set to a randomly // named, new schema and loads the sql string. func NewPostgresSchemaString(t *testing.T, sql string, opts ...Option) (*pgx.Conn, CleanupFunc) { t.Helper() diff --git a/internal/scanner/scanner.go b/internal/scanner/scanner.go index b8f03a0..d314e72 100644 --- a/internal/scanner/scanner.go +++ b/internal/scanner/scanner.go @@ -1,5 +1,3 @@ -// Package parser implements a "dumb" parser. The goal of this parser is to -// extract queries from SQL source. package scanner import (