feat(golang): emit models to a different package#4421
Open
kyleconroy wants to merge 2 commits intomainfrom
Open
feat(golang): emit models to a different package#4421kyleconroy wants to merge 2 commits intomainfrom
kyleconroy wants to merge 2 commits intomainfrom
Conversation
Closes #835. Adds four Go codegen options that let users place table model structs and enums in a Go package distinct from the queries package, optionally shared across multiple SQL blocks: - output_models_path: directory for the models file - output_models_package: Go package name for the models file - output_models_import: import path for the models package - output_models_emit: when false, skip emitting models.go and only reference an externally-emitted models package When the models package differs from the queries package, query files import the models package and reference types as `model.User` instead of `User`. Embedded model structs in synthetic Row structs and enum types in synthetic Params structs are qualified consistently. End-to-end fixtures cover both shapes: a single block with models in a sibling directory, and two query blocks that share one models package. https://claude.ai/code/session_01Nj4LbGbqFBTU1EPKKNHtNn
Two refinements on top of the initial #835 work: 1. output_models_package now defaults to filepath.Base(output_models_path) when unset, matching how `package` defaults to base(out). When only output_models_emit: false is configured (no path), it falls back to filepath.Base(output_models_import). 2. Query files always import the models package under the alias `models` and reference types as `models.User`, regardless of the actual package name. This keeps generated query code readable without users having to think about import aliases. The actual `package X` declaration in models.go still uses the configured (or defaulted) package name. https://claude.ai/code/session_01Nj4LbGbqFBTU1EPKKNHtNn
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #835.
Adds four Go codegen options that let users place table model structs
and enums in a Go package distinct from the queries package, optionally
shared across multiple SQL blocks:
reference an externally-emitted models package
When the models package differs from the queries package, query files
import the models package and reference types as
model.Userinstead ofUser. Embedded model structs in synthetic Row structs and enum typesin synthetic Params structs are qualified consistently.
End-to-end fixtures cover both shapes: a single block with models in a
sibling directory, and two query blocks that share one models package.
https://claude.ai/code/session_01Nj4LbGbqFBTU1EPKKNHtNn