Skip to content

Commit eddb736

Browse files
committed
allow renaming function Params structs
1 parent a18363b commit eddb736

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

‎docs/howto/rename.md‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ sql:
2121
queries: "postgresql/query.sql"
2222
engine: "postgresql"
2323
gen:
24-
go:
24+
go:
2525
package: "authors"
2626
out: "postgresql"
2727
rename:
@@ -116,6 +116,28 @@ type Publisher struct {
116116
}
117117
```
118118

119+
## Parameter Structure Names
120+
121+
It is possible to rename the arguments a generated function would use.
122+
For example, if you had a generated function called `FindWriter`
123+
which used a generated name of `FindWriterParams`, you can choose to rename
124+
this:
125+
126+
```yaml
127+
version: "2"
128+
sql:
129+
- engine: postgresql
130+
queries: query.sql
131+
schema: query.sql
132+
overrides:
133+
go:
134+
rename:
135+
FindWriterParams: SomeOtherNameParams
136+
```
137+
138+
The target name must be unique, and even if multiple structures would
139+
have the same fields, there will be a conflict.
140+
119141
## Limitations
120142

121143
Rename mappings apply to an entire package. Therefore, a column named `foo` and

‎internal/codegen/golang/result.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ func putOutColumns(query *plugin.Query) bool {
353353
// This is unlikely to happen, so don't fix it yet
354354
func columnsToStruct(req *plugin.GenerateRequest, options *opts.Options, name string, columns []goColumn, useID bool) (*Struct, error) {
355355
gs := Struct{
356-
Name: name,
356+
Name: CheckRename(name, options),
357357
}
358358
seen := map[string][]int{}
359359
suffixes := map[int]int{}

‎internal/codegen/golang/struct.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ type Struct struct {
1616
Comment string
1717
}
1818

19+
func CheckRename(name string, options *opts.Options) string {
20+
if rename := options.Rename[name]; rename != "" {
21+
return rename
22+
}
23+
return name
24+
}
25+
1926
func StructName(name string, options *opts.Options) string {
2027
if rename := options.Rename[name]; rename != "" {
2128
return rename

0 commit comments

Comments
 (0)