Skip to content
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

internal/codegen/golang: omit unused structs from output #2369

Merged
merged 2 commits into from
Jun 27, 2023

Conversation

andrewmbenton
Copy link
Collaborator

This is a feature that has been requested from time to time, most-recently in #2336.

This implementation introduces a new configuration parameter omit_unused_structs which, when set to true, prevents inclusion of table and enum structs that aren't used in queries for a given package.

Closes #710

Comment on lines 300 to 317
for _, query := range queries {
if !query.Arg.isEmpty() {
keepTypes[strings.TrimPrefix(query.Arg.Type(), "Null")] = struct{}{}
if query.Arg.IsStruct() {
for _, field := range query.Arg.Struct.Fields {
keepTypes[strings.TrimPrefix(field.Type, "Null")] = struct{}{}
}
}
}
if query.hasRetType() {
keepTypes[strings.TrimPrefix(query.Ret.Type(), "Null")] = struct{}{}
if query.Ret.IsStruct() {
for _, field := range query.Ret.Struct.Fields {
keepTypes[strings.TrimPrefix(field.Type, "Null")] = struct{}{}
}
}
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The trimming behavior here has a subtle bug. Type names come from user-defined table names. A table named null_foo will have a struct named NullFoo. Instead of trimming the null prefix, The enum code below should check for any use of the enum struct or the nullable enum struct.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, will update

@andrewmbenton andrewmbenton merged commit eceac40 into main Jun 27, 2023
8 checks passed
@andrewmbenton andrewmbenton deleted the andrew/omit-unused-structs branch June 27, 2023 22:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unused struct is being generated into models.go
2 participants