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
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
1 change: 1 addition & 0 deletions internal/cmd/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func pluginGoCode(s config.SQLGo) *plugin.GoCode {
OutputFilesSuffix: s.OutputFilesSuffix,
InflectionExcludeTableNames: s.InflectionExcludeTableNames,
QueryParameterLimit: s.QueryParameterLimit,
OmitUnusedStructs: s.OmitUnusedStructs,
}
}

Expand Down
47 changes: 47 additions & 0 deletions internal/codegen/golang/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ func Generate(ctx context.Context, req *plugin.CodeGenRequest) (*plugin.CodeGenR
if err != nil {
return nil, err
}

if req.Settings.Go.OmitUnusedStructs {
enums, structs = filterUnusedStructs(enums, structs, queries)
}

return generate(req, enums, structs, queries)
}

Expand Down Expand Up @@ -288,3 +293,45 @@ func usesBatch(queries []Query) bool {
}
return false
}

func filterUnusedStructs(enums []Enum, structs []Struct, queries []Query) ([]Enum, []Struct) {
keepTypes := make(map[string]struct{})

for _, query := range queries {
if !query.Arg.isEmpty() {
keepTypes[query.Arg.Type()] = struct{}{}
if query.Arg.IsStruct() {
for _, field := range query.Arg.Struct.Fields {
keepTypes[field.Type] = struct{}{}
}
}
}
if query.hasRetType() {
keepTypes[query.Ret.Type()] = struct{}{}
if query.Ret.IsStruct() {
for _, field := range query.Ret.Struct.Fields {
keepTypes[field.Type] = struct{}{}
}
}
}
}

keepEnums := make([]Enum, 0, len(enums))
for _, enum := range enums {
if _, ok := keepTypes[enum.Name]; ok {
keepEnums = append(keepEnums, enum)
}
if _, ok := keepTypes["Null"+enum.Name]; ok {
keepEnums = append(keepEnums, enum)
}
}

keepStructs := make([]Struct, 0, len(structs))
for _, st := range structs {
if _, ok := keepTypes[st.Name]; ok {
keepStructs = append(keepStructs, st)
}
}

return keepEnums, keepStructs
}
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ type SQLGo struct {
OutputFilesSuffix string `json:"output_files_suffix,omitempty" yaml:"output_files_suffix"`
InflectionExcludeTableNames []string `json:"inflection_exclude_table_names,omitempty" yaml:"inflection_exclude_table_names"`
QueryParameterLimit *int32 `json:"query_parameter_limit,omitempty" yaml:"query_parameter_limit"`
OmitUnusedStructs bool `json:"omit_unused_structs,omitempty" yaml:"omit_unused_structs"`
}

type SQLJSON struct {
Expand Down
2 changes: 2 additions & 0 deletions internal/config/v_one.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type v1PackageSettings struct {
StrictFunctionChecks bool `json:"strict_function_checks" yaml:"strict_function_checks"`
StrictOrderBy *bool `json:"strict_order_by" yaml:"strict_order_by"`
QueryParameterLimit *int32 `json:"query_parameter_limit,omitempty" yaml:"query_parameter_limit"`
OmitUnusedStructs bool `json:"omit_unused_structs,omitempty" yaml:"omit_unused_structs"`
}

func v1ParseConfig(rd io.Reader) (Config, error) {
Expand Down Expand Up @@ -168,6 +169,7 @@ func (c *V1GenerateSettings) Translate() Config {
OutputQuerierFileName: pkg.OutputQuerierFileName,
OutputFilesSuffix: pkg.OutputFilesSuffix,
QueryParameterLimit: pkg.QueryParameterLimit,
OmitUnusedStructs: pkg.OmitUnusedStructs,
},
},
StrictFunctionChecks: pkg.StrictFunctionChecks,
Expand Down
3 changes: 2 additions & 1 deletion internal/endtoend/testdata/codegen_json/gen/codegen.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"emit_pointers_for_null_types": false,
"query_parameter_limit": 1,
"output_batch_file_name": "",
"json_tags_id_uppercase": false
"json_tags_id_uppercase": false,
"omit_unused_structs": false
},
"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.

Loading