Skip to content

Commit 3d3630a

Browse files
authored
Add LeavePlural config option (#474)
* Add EmitExactTableNames config option If set to true, the struct names (derived from table names) are not singularized. My table names are not English, stripping the "s" from the end is quite annoying. * Fix mysql/gen
1 parent 2307462 commit 3d3630a

File tree

5 files changed

+22
-6
lines changed

5 files changed

+22
-6
lines changed

internal/config/config.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,18 @@ type SQLGen struct {
114114
type SQLGo struct {
115115
EmitInterface bool `json:"emit_interface" yaml:"emit_interface"`
116116
EmitJSONTags bool `json:"emit_json_tags" yaml:"emit_json_tags"`
117-
EmitPreparedQueries bool `json:"emit_prepared_queries" yaml:"emit_prepared_queries":`
117+
EmitPreparedQueries bool `json:"emit_prepared_queries" yaml:"emit_prepared_queries"`
118+
EmitExactTableNames bool `json:"emit_exact_table_names,omitempty" yaml:"emit_exact_table_names"`
118119
Package string `json:"package" yaml:"package"`
119120
Out string `json:"out" yaml:"out"`
120121
Overrides []Override `json:"overrides,omitempty" yaml:"overrides"`
121122
Rename map[string]string `json:"rename,omitempty" yaml:"rename"`
122123
}
123124

124125
type SQLKotlin struct {
125-
Package string `json:"package" yaml:"package"`
126-
Out string `json:"out" yaml:"out"`
126+
EmitExactTableNames bool `json:"emit_exact_table_names,omitempty" yaml:"emit_exact_table_names"`
127+
Package string `json:"package" yaml:"package"`
128+
Out string `json:"out" yaml:"out"`
127129
}
128130

129131
type Override struct {

internal/config/v_one.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type v1PackageSettings struct {
2424
EmitInterface bool `json:"emit_interface" yaml:"emit_interface"`
2525
EmitJSONTags bool `json:"emit_json_tags" yaml:"emit_json_tags"`
2626
EmitPreparedQueries bool `json:"emit_prepared_queries" yaml:"emit_prepared_queries"`
27+
EmitExactTableNames bool `json:"emit_exact_table_names,omitempty" yaml:"emit_exact_table_names"`
2728
Overrides []Override `json:"overrides" yaml:"overrides"`
2829
}
2930

@@ -103,6 +104,7 @@ func (c *V1GenerateSettings) Translate() Config {
103104
EmitInterface: pkg.EmitInterface,
104105
EmitJSONTags: pkg.EmitJSONTags,
105106
EmitPreparedQueries: pkg.EmitPreparedQueries,
107+
EmitExactTableNames: pkg.EmitExactTableNames,
106108
Package: pkg.Name,
107109
Out: pkg.Path,
108110
Overrides: pkg.Overrides,

internal/dinosql/gen.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,9 +595,13 @@ func (r Result) Structs(settings config.CombinedSettings) []GoStruct {
595595
} else {
596596
tableName = name + "_" + table.Name
597597
}
598+
structName := tableName
599+
if !settings.Go.EmitExactTableNames {
600+
structName = inflection.Singular(structName)
601+
}
598602
s := GoStruct{
599603
Table: core.FQN{Schema: name, Rel: table.Name},
600-
Name: StructName(inflection.Singular(tableName), settings),
604+
Name: StructName(structName, settings),
601605
Comment: table.Comment,
602606
}
603607
for _, column := range table.Columns {

internal/dinosql/kotlin/gen.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,9 +446,13 @@ func (r Result) KtDataClasses(settings config.CombinedSettings) []KtStruct {
446446
} else {
447447
tableName = name + "_" + table.Name
448448
}
449+
structName := KtDataClassName(tableName, settings)
450+
if !settings.Go.EmitExactTableNames {
451+
structName = inflection.Singular(structName)
452+
}
449453
s := KtStruct{
450454
Table: core.FQN{Schema: name, Rel: table.Name},
451-
Name: inflection.Singular(KtDataClassName(tableName, settings)),
455+
Name: structName,
452456
Comment: table.Comment,
453457
}
454458
for _, column := range table.Columns {

internal/mysql/gen.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,12 @@ func (pGen PackageGenerator) enumNameFromColDef(col *sqlparser.ColumnDefinition)
6767
func (r *Result) Structs(settings config.CombinedSettings) []dinosql.GoStruct {
6868
var structs []dinosql.GoStruct
6969
for tableName, cols := range r.Schema.tables {
70+
structName := dinosql.StructName(tableName, settings)
71+
if !(settings.Go.EmitExactTableNames || settings.Kotlin.EmitExactTableNames) {
72+
structName = inflection.Singular(structName)
73+
}
7074
s := dinosql.GoStruct{
71-
Name: inflection.Singular(dinosql.StructName(tableName, settings)),
75+
Name: structName,
7276
Table: core.FQN{tableName, "", ""}, // TODO: Complete hack. Only need for equality check to see if struct can be reused between queries
7377
}
7478

0 commit comments

Comments
 (0)