feat(codegen/golang): Allow exporting models to a different package
Fixes #835
This PR adds exporting models to a different package feature to Go code generator.
I also added a how-to page for this feature, you can check the docs/howto/separate-models-file.md file.
Main code changes made in this PR
- Added optional
output_models_package,models_package_import_pathandoutput_query_files_directoryoptions to gen/go configuration. (internal/codegen/golang/opts/options.go) - Added
Packagefield toStructtype to specify the correct type of the Struct since they can be in different package now. (internal/codegen/golang/struct.goandinternal/codegen/golang/result.go) Type()method ofQueryValuewill return{Package}.{Name}forStructtypes if thePackagefield of theStructis not empty. (internal/codegen/golang/query.go)- Added
{Package}.prefix to enum types if configuration specifies separate models package. (changed onlyinternal/codegen/golang/postgresql_type.gosince only postgresql implementation supports typed enum values) - Deciding whether the generated file needs to import the separated models file package. (
internal/codegen/golang/imports.go)
Note for sqlc users
You can simply try this feature using my sqlc-gen-go plugin fork. I will try to keep that plugin and this PR in sync. Surely sqlc-gen-go plugin configuration is not same as the sqlc gen configuration but the feature implementation code is the same as this PR.
Any plans to merge this PR? It’s a really useful feature for large projects using sqlc.
Would love to see some movement on this one, pretty handy feature. What's keeping this from being integrated?
@kyleconroy hey! I’m working on a big project using SQLC, and it keeps growing. This PR would have a huge positive impact on my project as well as many others. From my perspective, this is the only weak spot in SQLC, and it could be resolved with this PR. I already tested this PR on our codebase, and it works like a charm. Please consider reviewing it when you have a chance, and please let me know if I can help in any way. Thank you!
I need this feature. Can't use without. Please.
patiently waiting as well :)
@kyleconroy any plans to merge this?