Skip to content

Commit 800d51f

Browse files
committed
internal/codegen/golang: omit unused structs from output
1 parent 28696e5 commit 800d51f

File tree

16 files changed

+546
-5
lines changed

16 files changed

+546
-5
lines changed

internal/cmd/shim.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ func pluginGoCode(s config.SQLGo) *plugin.GoCode {
108108
OutputFilesSuffix: s.OutputFilesSuffix,
109109
InflectionExcludeTableNames: s.InflectionExcludeTableNames,
110110
QueryParameterLimit: s.QueryParameterLimit,
111+
OmitUnusedStructs: s.OmitUnusedStructs,
111112
}
112113
}
113114

internal/codegen/golang/gen.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ func Generate(ctx context.Context, req *plugin.CodeGenRequest) (*plugin.CodeGenR
108108
if err != nil {
109109
return nil, err
110110
}
111+
112+
if req.Settings.Go.OmitUnusedStructs {
113+
enums, structs = filterUnusedStructs(enums, structs, queries)
114+
}
115+
111116
return generate(req, enums, structs, queries)
112117
}
113118

@@ -288,3 +293,42 @@ func usesBatch(queries []Query) bool {
288293
}
289294
return false
290295
}
296+
297+
func filterUnusedStructs(enums []Enum, structs []Struct, queries []Query) ([]Enum, []Struct) {
298+
keepTypes := make(map[string]struct{})
299+
300+
for _, query := range queries {
301+
if !query.Arg.isEmpty() {
302+
keepTypes[strings.TrimPrefix(query.Arg.Type(), "Null")] = struct{}{}
303+
if query.Arg.IsStruct() {
304+
for _, field := range query.Arg.Struct.Fields {
305+
keepTypes[strings.TrimPrefix(field.Type, "Null")] = struct{}{}
306+
}
307+
}
308+
}
309+
if query.hasRetType() {
310+
keepTypes[strings.TrimPrefix(query.Ret.Type(), "Null")] = struct{}{}
311+
if query.Ret.IsStruct() {
312+
for _, field := range query.Ret.Struct.Fields {
313+
keepTypes[strings.TrimPrefix(field.Type, "Null")] = struct{}{}
314+
}
315+
}
316+
}
317+
}
318+
319+
keepEnums := make([]Enum, 0, len(enums))
320+
for _, enum := range enums {
321+
if _, ok := keepTypes[enum.Name]; ok {
322+
keepEnums = append(keepEnums, enum)
323+
}
324+
}
325+
326+
keepStructs := make([]Struct, 0, len(structs))
327+
for _, st := range structs {
328+
if _, ok := keepTypes[st.Name]; ok {
329+
keepStructs = append(keepStructs, st)
330+
}
331+
}
332+
333+
return keepEnums, keepStructs
334+
}

internal/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ type SQLGo struct {
145145
OutputFilesSuffix string `json:"output_files_suffix,omitempty" yaml:"output_files_suffix"`
146146
InflectionExcludeTableNames []string `json:"inflection_exclude_table_names,omitempty" yaml:"inflection_exclude_table_names"`
147147
QueryParameterLimit *int32 `json:"query_parameter_limit,omitempty" yaml:"query_parameter_limit"`
148+
OmitUnusedStructs bool `json:"omit_unused_structs,omitempty" yaml:"omit_unused_structs"`
148149
}
149150

150151
type SQLJSON struct {

internal/config/v_one.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type v1PackageSettings struct {
4949
StrictFunctionChecks bool `json:"strict_function_checks" yaml:"strict_function_checks"`
5050
StrictOrderBy *bool `json:"strict_order_by" yaml:"strict_order_by"`
5151
QueryParameterLimit *int32 `json:"query_parameter_limit,omitempty" yaml:"query_parameter_limit"`
52+
OmitUnusedStructs bool `json:"omit_unused_structs,omitempty" yaml:"omit_unused_structs"`
5253
}
5354

5455
func v1ParseConfig(rd io.Reader) (Config, error) {
@@ -168,6 +169,7 @@ func (c *V1GenerateSettings) Translate() Config {
168169
OutputQuerierFileName: pkg.OutputQuerierFileName,
169170
OutputFilesSuffix: pkg.OutputFilesSuffix,
170171
QueryParameterLimit: pkg.QueryParameterLimit,
172+
OmitUnusedStructs: pkg.OmitUnusedStructs,
171173
},
172174
},
173175
StrictFunctionChecks: pkg.StrictFunctionChecks,

internal/endtoend/testdata/codegen_json/gen/codegen.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
"emit_pointers_for_null_types": false,
4242
"query_parameter_limit": 1,
4343
"output_batch_file_name": "",
44-
"json_tags_id_uppercase": false
44+
"json_tags_id_uppercase": false,
45+
"omit_unused_structs": false
4546
},
4647
"json": {
4748
"out": "gen",

internal/endtoend/testdata/omit_unused_structs/postgresql/stdlib/go/db.go

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/omit_unused_structs/postgresql/stdlib/go/models.go

Lines changed: 225 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)