Skip to content

feat(codegen/golang): Allow exporting models to a different package #3874

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

berk-karaal
Copy link

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_path and output_query_files_directory options to gen/go configuration. (internal/codegen/golang/opts/options.go)
  • Added Package field to Struct type to specify the correct type of the Struct since they can be in different package now. (internal/codegen/golang/struct.go and internal/codegen/golang/result.go)
  • Type() method of QueryValue will return {Package}.{Name} for Struct types if the Package field of the Struct is not empty. (internal/codegen/golang/query.go)
  • Added {Package}. prefix to enum types if configuration specifies separate models package. (changed only internal/codegen/golang/postgresql_type.go since 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.

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. 🔧 golang labels Mar 6, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
size:L This PR changes 100-499 lines, ignoring generated files. 🔧 golang
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Export models to a different package
1 participant