Skip to content

Inflection Singular table names exclusion configuration #1531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 33 additions & 30 deletions internal/cmd/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,38 +79,40 @@ func pluginCodegen(s config.Codegen) *plugin.Codegen {

func pluginPythonCode(s config.SQLPython) *plugin.PythonCode {
return &plugin.PythonCode{
Out: s.Out,
Package: s.Package,
EmitExactTableNames: s.EmitExactTableNames,
EmitSyncQuerier: s.EmitSyncQuerier,
EmitAsyncQuerier: s.EmitAsyncQuerier,
EmitPydanticModels: s.EmitPydanticModels,
QueryParameterLimit: s.QueryParameterLimit,
Out: s.Out,
Package: s.Package,
EmitExactTableNames: s.EmitExactTableNames,
EmitSyncQuerier: s.EmitSyncQuerier,
EmitAsyncQuerier: s.EmitAsyncQuerier,
EmitPydanticModels: s.EmitPydanticModels,
QueryParameterLimit: s.QueryParameterLimit,
InflectionExcludeTableNames: s.InflectionExcludeTableNames,
}
}

func pluginGoCode(s config.SQLGo) *plugin.GoCode {
return &plugin.GoCode{
EmitInterface: s.EmitInterface,
EmitJsonTags: s.EmitJSONTags,
EmitDbTags: s.EmitDBTags,
EmitPreparedQueries: s.EmitPreparedQueries,
EmitExactTableNames: s.EmitExactTableNames,
EmitEmptySlices: s.EmitEmptySlices,
EmitExportedQueries: s.EmitExportedQueries,
EmitResultStructPointers: s.EmitResultStructPointers,
EmitParamsStructPointers: s.EmitParamsStructPointers,
EmitMethodsWithDbArgument: s.EmitMethodsWithDBArgument,
EmitEnumValidMethod: s.EmitEnumValidMethod,
EmitAllEnumValues: s.EmitAllEnumValues,
JsonTagsCaseStyle: s.JSONTagsCaseStyle,
Package: s.Package,
Out: s.Out,
SqlPackage: s.SQLPackage,
OutputDbFileName: s.OutputDBFileName,
OutputModelsFileName: s.OutputModelsFileName,
OutputQuerierFileName: s.OutputQuerierFileName,
OutputFilesSuffix: s.OutputFilesSuffix,
EmitInterface: s.EmitInterface,
EmitJsonTags: s.EmitJSONTags,
EmitDbTags: s.EmitDBTags,
EmitPreparedQueries: s.EmitPreparedQueries,
EmitExactTableNames: s.EmitExactTableNames,
EmitEmptySlices: s.EmitEmptySlices,
EmitExportedQueries: s.EmitExportedQueries,
EmitResultStructPointers: s.EmitResultStructPointers,
EmitParamsStructPointers: s.EmitParamsStructPointers,
EmitMethodsWithDbArgument: s.EmitMethodsWithDBArgument,
EmitEnumValidMethod: s.EmitEnumValidMethod,
EmitAllEnumValues: s.EmitAllEnumValues,
JsonTagsCaseStyle: s.JSONTagsCaseStyle,
Package: s.Package,
Out: s.Out,
SqlPackage: s.SQLPackage,
OutputDbFileName: s.OutputDBFileName,
OutputModelsFileName: s.OutputModelsFileName,
OutputQuerierFileName: s.OutputQuerierFileName,
OutputFilesSuffix: s.OutputFilesSuffix,
InflectionExcludeTableNames: s.InflectionExcludeTableNames,
}
}

Expand All @@ -137,9 +139,10 @@ func pluginPythonType(pt config.PythonType) *plugin.PythonType {

func pluginKotlinCode(s config.SQLKotlin) *plugin.KotlinCode {
return &plugin.KotlinCode{
Out: s.Out,
Package: s.Package,
EmitExactTableNames: s.EmitExactTableNames,
Out: s.Out,
Package: s.Package,
EmitExactTableNames: s.EmitExactTableNames,
InflectionExcludeTableNames: s.InflectionExcludeTableNames,
}
}

Expand Down
5 changes: 4 additions & 1 deletion internal/codegen/golang/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ func buildStructs(req *plugin.CodeGenRequest) []Struct {
}
structName := tableName
if !req.Settings.Go.EmitExactTableNames {
structName = inflection.Singular(structName)
structName = inflection.Singular(inflection.SingularParams{
Name: structName,
Exclusions: req.Settings.Go.InflectionExcludeTableNames,
})
}
s := Struct{
Table: plugin.Identifier{Schema: schema.Name, Name: table.Rel.Name},
Expand Down
5 changes: 4 additions & 1 deletion internal/codegen/kotlin/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,10 @@ func buildDataClasses(req *plugin.CodeGenRequest) []Struct {
}
structName := dataClassName(tableName, req.Settings)
if !req.Settings.Kotlin.EmitExactTableNames {
structName = inflection.Singular(structName)
structName = inflection.Singular(inflection.SingularParams{
Name: structName,
Exclusions: req.Settings.Kotlin.InflectionExcludeTableNames,
})
}
s := Struct{
Table: plugin.Identifier{Schema: schema.Name, Name: table.Rel.Name},
Expand Down
5 changes: 4 additions & 1 deletion internal/codegen/python/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,10 @@ func buildModels(req *plugin.CodeGenRequest) []Struct {
}
structName := tableName
if !req.Settings.Python.EmitExactTableNames {
structName = inflection.Singular(structName)
structName = inflection.Singular(inflection.SingularParams{
Name: structName,
Exclusions: req.Settings.Python.InflectionExcludeTableNames,
})
}
s := Struct{
Table: plugin.Identifier{Schema: schema.Name, Name: table.Rel.Name},
Expand Down
69 changes: 36 additions & 33 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,45 +131,48 @@ type SQLGen struct {
}

type SQLGo struct {
EmitInterface bool `json:"emit_interface" yaml:"emit_interface"`
EmitJSONTags bool `json:"emit_json_tags" yaml:"emit_json_tags"`
EmitDBTags bool `json:"emit_db_tags" yaml:"emit_db_tags"`
EmitPreparedQueries bool `json:"emit_prepared_queries" yaml:"emit_prepared_queries"`
EmitExactTableNames bool `json:"emit_exact_table_names,omitempty" yaml:"emit_exact_table_names"`
EmitEmptySlices bool `json:"emit_empty_slices,omitempty" yaml:"emit_empty_slices"`
EmitExportedQueries bool `json:"emit_exported_queries" yaml:"emit_exported_queries"`
EmitResultStructPointers bool `json:"emit_result_struct_pointers" yaml:"emit_result_struct_pointers"`
EmitParamsStructPointers bool `json:"emit_params_struct_pointers" yaml:"emit_params_struct_pointers"`
EmitMethodsWithDBArgument bool `json:"emit_methods_with_db_argument,omitempty" yaml:"emit_methods_with_db_argument"`
EmitEnumValidMethod bool `json:"emit_enum_valid_method,omitempty" yaml:"emit_enum_valid_method"`
EmitAllEnumValues bool `json:"emit_all_enum_values,omitempty" yaml:"emit_all_enum_values"`
JSONTagsCaseStyle string `json:"json_tags_case_style,omitempty" yaml:"json_tags_case_style"`
Package string `json:"package" yaml:"package"`
Out string `json:"out" yaml:"out"`
Overrides []Override `json:"overrides,omitempty" yaml:"overrides"`
Rename map[string]string `json:"rename,omitempty" yaml:"rename"`
SQLPackage string `json:"sql_package" yaml:"sql_package"`
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"`
OutputFilesSuffix string `json:"output_files_suffix,omitempty" yaml:"output_files_suffix"`
EmitInterface bool `json:"emit_interface" yaml:"emit_interface"`
EmitJSONTags bool `json:"emit_json_tags" yaml:"emit_json_tags"`
EmitDBTags bool `json:"emit_db_tags" yaml:"emit_db_tags"`
EmitPreparedQueries bool `json:"emit_prepared_queries" yaml:"emit_prepared_queries"`
EmitExactTableNames bool `json:"emit_exact_table_names,omitempty" yaml:"emit_exact_table_names"`
EmitEmptySlices bool `json:"emit_empty_slices,omitempty" yaml:"emit_empty_slices"`
EmitExportedQueries bool `json:"emit_exported_queries" yaml:"emit_exported_queries"`
EmitResultStructPointers bool `json:"emit_result_struct_pointers" yaml:"emit_result_struct_pointers"`
EmitParamsStructPointers bool `json:"emit_params_struct_pointers" yaml:"emit_params_struct_pointers"`
EmitMethodsWithDBArgument bool `json:"emit_methods_with_db_argument,omitempty" yaml:"emit_methods_with_db_argument"`
EmitEnumValidMethod bool `json:"emit_enum_valid_method,omitempty" yaml:"emit_enum_valid_method"`
EmitAllEnumValues bool `json:"emit_all_enum_values,omitempty" yaml:"emit_all_enum_values"`
JSONTagsCaseStyle string `json:"json_tags_case_style,omitempty" yaml:"json_tags_case_style"`
Package string `json:"package" yaml:"package"`
Out string `json:"out" yaml:"out"`
Overrides []Override `json:"overrides,omitempty" yaml:"overrides"`
Rename map[string]string `json:"rename,omitempty" yaml:"rename"`
SQLPackage string `json:"sql_package" yaml:"sql_package"`
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"`
OutputFilesSuffix string `json:"output_files_suffix,omitempty" yaml:"output_files_suffix"`
InflectionExcludeTableNames []string `json:"inflection_exclude_table_names,omitempty" yaml:"inflection_exclude_table_names"`
}

type SQLKotlin struct {
EmitExactTableNames bool `json:"emit_exact_table_names,omitempty" yaml:"emit_exact_table_names"`
Package string `json:"package" yaml:"package"`
Out string `json:"out" yaml:"out"`
EmitExactTableNames bool `json:"emit_exact_table_names,omitempty" yaml:"emit_exact_table_names"`
Package string `json:"package" yaml:"package"`
Out string `json:"out" yaml:"out"`
InflectionExcludeTableNames []string `json:"inflection_exclude_table_names,omitempty" yaml:"inflection_exclude_table_names"`
}

type SQLPython struct {
EmitExactTableNames bool `json:"emit_exact_table_names" yaml:"emit_exact_table_names"`
EmitSyncQuerier bool `json:"emit_sync_querier" yaml:"emit_sync_querier"`
EmitAsyncQuerier bool `json:"emit_async_querier" yaml:"emit_async_querier"`
Package string `json:"package" yaml:"package"`
Out string `json:"out" yaml:"out"`
Overrides []Override `json:"overrides,omitempty" yaml:"overrides"`
EmitPydanticModels bool `json:"emit_pydantic_models,omitempty" yaml:"emit_pydantic_models"`
QueryParameterLimit *int32 `json:"query_parameter_limit,omitempty" yaml:"query_parameter_limit"`
EmitExactTableNames bool `json:"emit_exact_table_names" yaml:"emit_exact_table_names"`
EmitSyncQuerier bool `json:"emit_sync_querier" yaml:"emit_sync_querier"`
EmitAsyncQuerier bool `json:"emit_async_querier" yaml:"emit_async_querier"`
Package string `json:"package" yaml:"package"`
Out string `json:"out" yaml:"out"`
Overrides []Override `json:"overrides,omitempty" yaml:"overrides"`
EmitPydanticModels bool `json:"emit_pydantic_models,omitempty" yaml:"emit_pydantic_models"`
QueryParameterLimit *int32 `json:"query_parameter_limit,omitempty" yaml:"query_parameter_limit"`
InflectionExcludeTableNames []string `json:"inflection_exclude_table_names,omitempty" yaml:"inflection_exclude_table_names"`
}

type SQLJSON struct {
Expand Down
9 changes: 6 additions & 3 deletions internal/endtoend/testdata/codegen_json/gen/codegen.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
"emit_async_querier": false,
"package": "",
"out": "",
"emit_pydantic_models": false
"emit_pydantic_models": false,
"inflection_exclude_table_names": []
},
"kotlin": {
"emit_exact_table_names": false,
"package": "",
"out": ""
"out": "",
"inflection_exclude_table_names": []
},
"go": {
"emit_interface": false,
Expand All @@ -48,7 +50,8 @@
"output_querier_file_name": "",
"output_files_suffix": "",
"emit_enum_valid_method": false,
"emit_all_enum_values": false
"emit_all_enum_values": false,
"inflection_exclude_table_names": []
},
"json": {
"out": "gen",
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.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE TABLE bars (id serial not null, name text not null, primary key (id));
CREATE TABLE my_data (id serial not null, name text not null, primary key (id));
CREATE TABLE exclusions (id serial not null, name text not null, primary key (id));

-- name: DeleteBarByID :one
DELETE FROM bars WHERE id = $1 RETURNING id, name;

-- name: DeleteMyDataByID :one
DELETE FROM my_data WHERE id = $1 RETURNING id, name;

-- name: DeleteExclusionByID :one
DELETE FROM exclusions WHERE id = $1 RETURNING id, name;
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"version": "2",
"sql": [
{
"engine": "postgresql",
"schema": "query.sql",
"queries": "query.sql",
"gen": {
"go": {
"package": "querytest",
"sql_package": "pgx/v4",
"out": "go",
"inflection_exclude_table_names": [
"my_data",
"exclusions"
]
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Code generated by sqlc. DO NOT EDIT.
# versions:
# sqlc v1.15.0
import dataclasses


@dataclasses.dataclass()
class Bar:
id: int
name: str


@dataclasses.dataclass()
class Exclusions:
id: int
name: str


@dataclasses.dataclass()
class MyData:
id: int
name: str
Loading