Skip to content

Commit

Permalink
feat(config): Enables the configuration of copyfrom.go similar to qui…
Browse files Browse the repository at this point in the history
…erer and friends (#2727)

* enables the configration of copyfrom.go similar to quierer and friends

* adds a copyfrom to the output_file_name tests that support it

---------

Co-authored-by: xander <xander@backbonesystems.nl>
  • Loading branch information
XanderAtGoingDutch and XanderAtBackboneSystems committed Sep 19, 2023
1 parent b3c3474 commit 33398b7
Show file tree
Hide file tree
Showing 25 changed files with 362 additions and 166 deletions.
5 changes: 5 additions & 0 deletions docs/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ The `gen` mapping supports the following keys:
- Customize the name of the models file. Defaults to `models.go`.
- `output_querier_file_name`:
- Customize the name of the querier file. Defaults to `querier.go`.
- `output_copyfrom_file_name`:
- Customize the name of the copyfrom file. Defaults to `copyfrom.go`.
- `output_files_suffix`:
- If specified the suffix will be added to the name of the generated files.
- `query_parameter_limit`:
Expand Down Expand Up @@ -469,6 +471,7 @@ packages:
output_db_file_name: "db.go"
output_models_file_name: "models.go"
output_querier_file_name: "querier.go"
output_copyfrom_file_name: "copyfrom.go"
```

### packages
Expand Down Expand Up @@ -527,6 +530,8 @@ Each mapping in the `packages` collection has the following keys:
- Customize the name of the models file. Defaults to `models.go`.
- `output_querier_file_name`:
- Customize the name of the querier file. Defaults to `querier.go`.
- `output_copyfrom_file_name`:
- Customize the name of the copyfrom file. Defaults to `copyfrom.go`.
- `output_files_suffix`:
- If specified the suffix will be added to the name of the generated files.
- `query_parameter_limit`:
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func pluginGoCode(s config.SQLGo) *plugin.GoCode {
OutputBatchFileName: s.OutputBatchFileName,
OutputModelsFileName: s.OutputModelsFileName,
OutputQuerierFileName: s.OutputQuerierFileName,
OutputCopyfromFileName: s.OutputCopyFromFileName,
OutputFilesSuffix: s.OutputFilesSuffix,
InflectionExcludeTableNames: s.InflectionExcludeTableNames,
QueryParameterLimit: s.QueryParameterLimit,
Expand Down
4 changes: 3 additions & 1 deletion internal/codegen/golang/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,9 @@ func generate(req *plugin.CodeGenRequest, enums []Enum, structs []Struct, querie
querierFileName = golang.OutputQuerierFileName
}
copyfromFileName := "copyfrom.go"
// TODO(Jille): Make this configurable.
if golang.OutputCopyfromFileName != "" {
copyfromFileName = golang.OutputCopyfromFileName
}

batchFileName := "batch.go"
if golang.OutputBatchFileName != "" {
Expand Down
3 changes: 3 additions & 0 deletions internal/codegen/golang/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ func (i *importer) Imports(filename string) [][]ImportSpec {
querierFileName = i.Settings.Go.OutputQuerierFileName
}
copyfromFileName := "copyfrom.go"
if i.Settings.Go.OutputCopyfromFileName != "" {
copyfromFileName = i.Settings.Go.OutputCopyfromFileName
}
batchFileName := "batch.go"
if i.Settings.Go.OutputBatchFileName != "" {
batchFileName = i.Settings.Go.OutputBatchFileName
Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ type SQLGo struct {
OutputDBFileName string `json:"output_db_file_name,omitempty" yaml:"output_db_file_name"`
OutputModelsFileName string `json:"output_models_file_name,omitempty" yaml:"output_models_file_name"`
OutputQuerierFileName string `json:"output_querier_file_name,omitempty" yaml:"output_querier_file_name"`
OutputCopyFromFileName string `json:"output_copyfrom_file_name,omitempty" yaml:"output_copyfrom_file_name"`
OutputFilesSuffix string `json:"output_files_suffix,omitempty" yaml:"output_files_suffix"`
InflectionExcludeTableNames []string `json:"inflection_exclude_table_names,omitempty" yaml:"inflection_exclude_table_names"`
QueryParameterLimit *int32 `json:"query_parameter_limit,omitempty" yaml:"query_parameter_limit"`
Expand Down
2 changes: 2 additions & 0 deletions internal/config/v_one.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type v1PackageSettings struct {
OutputDBFileName string `json:"output_db_file_name,omitempty" yaml:"output_db_file_name"`
OutputModelsFileName string `json:"output_models_file_name,omitempty" yaml:"output_models_file_name"`
OutputQuerierFileName string `json:"output_querier_file_name,omitempty" yaml:"output_querier_file_name"`
OutputCopyFromFileName string `json:"output_copyfrom_file_name,omitempty" yaml:"output_copyfrom_file_name"`
OutputFilesSuffix string `json:"output_files_suffix,omitempty" yaml:"output_files_suffix"`
StrictFunctionChecks bool `json:"strict_function_checks" yaml:"strict_function_checks"`
StrictOrderBy *bool `json:"strict_order_by" yaml:"strict_order_by"`
Expand Down Expand Up @@ -173,6 +174,7 @@ func (c *V1GenerateSettings) Translate() Config {
OutputDBFileName: pkg.OutputDBFileName,
OutputModelsFileName: pkg.OutputModelsFileName,
OutputQuerierFileName: pkg.OutputQuerierFileName,
OutputCopyFromFileName: pkg.OutputCopyFromFileName,
OutputFilesSuffix: pkg.OutputFilesSuffix,
QueryParameterLimit: pkg.QueryParameterLimit,
OmitUnusedStructs: pkg.OmitUnusedStructs,
Expand Down
3 changes: 3 additions & 0 deletions internal/config/v_one.json
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@
"output_querier_file_name": {
"type": "string"
},
"output_copyfrom_file_name": {
"type": "string"
},
"output_files_suffix": {
"type": "string"
},
Expand Down
3 changes: 3 additions & 0 deletions internal/config/v_two.json
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@
"output_querier_file_name": {
"type": "string"
},
"output_copyfrom_file_name": {
"type": "string"
},
"output_files_suffix": {
"type": "string"
},
Expand Down
1 change: 1 addition & 0 deletions internal/endtoend/testdata/codegen_json/gen/codegen.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"output_db_file_name": "",
"output_models_file_name": "",
"output_querier_file_name": "",
"output_copyfrom_file_name": "",
"output_files_suffix": "",
"emit_enum_valid_method": false,
"emit_all_enum_values": false,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions internal/endtoend/testdata/output_file_names/pgx/v4/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ SELECT "user".* FROM "user";
-- name: UsersB :batchmany
SELECT * FROM "user"
WHERE id = $1;

-- name: UsersC :copyfrom
INSERT INTO "user"
(id)
VALUES
($1);
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"output_batch_file_name": "batch_gen.go",
"output_db_file_name": "db_gen.go",
"output_models_file_name": "models_gen.go",
"output_querier_file_name": "querier_gen.go"
"output_querier_file_name": "querier_gen.go",
"output_copyfrom_file_name": "copyfrom_gen.go"
}
]
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions internal/endtoend/testdata/output_file_names/pgx/v5/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ SELECT "user".* FROM "user";
-- name: UsersB :batchmany
SELECT * FROM "user"
WHERE id = $1;

-- name: UsersC :copyfrom
INSERT INTO "user"
(id)
VALUES
($1);
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"output_batch_file_name": "batch_gen.go",
"output_db_file_name": "db_gen.go",
"output_models_file_name": "models_gen.go",
"output_querier_file_name": "querier_gen.go"
"output_querier_file_name": "querier_gen.go",
"output_copyfrom_file_name": "copyfrom_gen.go"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"emit_interface": true,
"output_db_file_name": "db_gen.go",
"output_models_file_name": "models_gen.go",
"output_querier_file_name": "querier_gen.go"
"output_querier_file_name": "querier_gen.go",
"output_copyfrom_file_name": "copyfrom_gen.go"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"output_db_file_name": "",
"output_models_file_name": "",
"output_querier_file_name": "",
"output_copyfrom_file_name": "",
"output_files_suffix": "",
"emit_enum_valid_method": false,
"emit_all_enum_values": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"output_db_file_name": "",
"output_models_file_name": "",
"output_querier_file_name": "",
"output_copyfrom_file_name": "",
"output_files_suffix": "",
"emit_enum_valid_method": false,
"emit_all_enum_values": false,
Expand Down
Loading

0 comments on commit 33398b7

Please sign in to comment.