Skip to content

allow renaming function Params structs #3878

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
26 changes: 24 additions & 2 deletions docs/howto/rename.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ sql:
queries: "postgresql/query.sql"
engine: "postgresql"
gen:
go:
go:
package: "authors"
out: "postgresql"
rename:
Expand Down Expand Up @@ -93,7 +93,29 @@ type Writer struct {
}
```

## Parameter Structure Names

It is possible to rename the arguments a generated function would use.
For example, if you had a generated function called `FindWriter`
which used a generated name of `FindWriterParams`, you can choose to rename
this:

```yaml
version: "2"
sql:
- engine: postgresql
queries: query.sql
schema: query.sql
overrides:
go:
rename:
FindWriterParams: SomeOtherNameParams
```

The target name must be unique, and even if multiple structures would
have the same fields, there will be a conflict.

## Limitations

Rename mappings apply to an entire package. Therefore, a column named `foo` and
a table name `foo` can't map to different rename values.
a table name `foo` can't map to different rename values.
2 changes: 1 addition & 1 deletion internal/codegen/golang/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ func putOutColumns(query *plugin.Query) bool {
// This is unlikely to happen, so don't fix it yet
func columnsToStruct(req *plugin.GenerateRequest, options *opts.Options, name string, columns []goColumn, useID bool) (*Struct, error) {
gs := Struct{
Name: name,
Name: CheckRename(name, options),
}
seen := map[string][]int{}
suffixes := map[int]int{}
Expand Down
7 changes: 7 additions & 0 deletions internal/codegen/golang/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ type Struct struct {
Comment string
}

func CheckRename(name string, options *opts.Options) string {
if rename := options.Rename[name]; rename != "" {
return rename
}
return name
}

func StructName(name string, options *opts.Options) string {
if rename := options.Rename[name]; rename != "" {
return rename
Expand Down
Loading