Skip to content

Add same-package code generation with configurable suffix support#46

Open
abc-valera wants to merge 1 commit into
go-gorm:masterfrom
abc-valera:master
Open

Add same-package code generation with configurable suffix support#46
abc-valera wants to merge 1 commit into
go-gorm:masterfrom
abc-valera:master

Conversation

@abc-valera

@abc-valera abc-valera commented Apr 25, 2026

Copy link
Copy Markdown
  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

Added support for generating GORM query code in the same package as the source file, instead of always writing output to a separate directory.

Added the Config.IsSamePackage bool and Config.SamePackageSuffix string fields to indicate if the same-package generation should be applied.

User Case Description

The new Config option allows generation is the same package as the data models as queries and can be useful for some architectural styles (e.g. feature-first instead of layer-first).

Example

Imagine such folder structure:

  features/
  ├── user/
  │   ├── model.go
  │   └── query.go                                                                                                      
  └── product/
      ├── model.go                                                                                                      
      └── query.go

Adding a //go:generate directive to each package:

// features/user/query.go
package user

//go:generate gorm gen -i . --is-same-package --same-package-suffix=helper                                              

Running go generate ./features/... produces the query helpers in-place:

  features/
  ├── user/
  │   ├── model.go
  │   ├── model_helper.go                                                                                  
  │   ├── query.go
  │   └── query_helper.go                                                                                  
  └── product/
      ├── model.go                                                                                                      
      ├── model_helper.go
      ├── query.go
      └── query_helper.go

<!-- Summary by @propel-code-bot -->

---

In addition to enabling same-package generation via config, this change also supports controlling the behavior through CLI flags, with CLI values taking precedence over in-code configuration. It further refines generation behavior to be safe and consistent in same-package mode, including default suffix handling when none is provided.

<details>
<summary><strong>Affected Areas</strong></summary>

• `internal/gen/gen.go`
• `internal/gen/generator.go`
• `internal/gen/generator_test.go`
• `genconfig/config.go`
• `examples/samepackage/config.go`
• `examples/samepackage/query.go`
• `examples/samepackage/struct.go`
• `internal/gen/template.go`

</details>

---
*This summary was automatically generated by @propel-code-bot*

@propel-code-bot propel-code-bot Bot changed the title Added support for same package generation Add same-package code generation with configurable suffix support Apr 25, 2026

@propel-code-bot propel-code-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues were identified; changes appear cohesive, well-tested, and low risk.

Status: No Issues Found | Risk: Low

Review Details

📁 8 files reviewed | 💬 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant