Skip to content

Conversation

@uptutu
Copy link
Contributor

@uptutu uptutu commented Sep 16, 2025

support query interface in where is models define


Support for Query Interfaces Defined in Model Packages and Code Generation Improvements

This PR introduces enhanced support for query interfaces such as UserQuery and generic Query[T any] defined directly within model packages, allowing the code generation system to produce type-safe, concrete implementations for these interfaces. Key modifications include updates to the type resolution logic in internal/gen/generator.go, addition of new and updated model/interface files, refreshed generated code for query/update methods, and corrections to SQL template documentation and example usage in README.md. These changes collectively improve ergonomics and correctness when binding SQL templates to interfaces within model code.

Key Changes

• Added UserQuery and generic Query[T any] interfaces in examples/models/user_query.go and examples/models/query.go
• Generated new query implementation files: examples/output/models/user_query.go and examples/output/models/query.go
• Implemented concrete methods (QueryWith, UpdateWith, GetByID, FilterWithColumn) for these interfaces
• Enhanced parseFieldType and type/package path resolution logic in internal/gen/generator.go
• Fixed initialization of filesWithCfg in internal/gen/generator.go
• Updated documentation and SQL template examples in README.md
• Minor output formatting fix in internal/gen/generator_test.go

Affected Areas

examples/models/user_query.go
examples/models/query.go
examples/output/models/user_query.go
examples/output/models/query.go
internal/gen/generator.go
README.md
internal/gen/generator_test.go


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

Copilot AI review requested due to automatic review settings September 16, 2025 08:54
@propel-code-bot propel-code-bot bot changed the title fix: support query interface in where is models define Fix support for query interfaces defined in model packages Sep 16, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds support for query interfaces where models are defined, addressing type resolution issues in the code generator. The main change improves how the generator handles exported types by properly qualifying them with package names when necessary.

  • Enhanced type resolution logic in the parser to handle exported types in the same package
  • Generated query interface implementations for User and Query models
  • Updated documentation to correct template syntax examples

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
internal/gen/generator.go Improved variable declaration style and added logic to handle exported type resolution with package qualification
examples/output/models/user_query.go Generated query interface implementation for User model with QueryWith and UpdateWith methods
examples/output/models/query.go Generated generic query interface implementation with GetByID and FilterWithColumn methods
examples/models/user_query.go Added source interface definition for User-specific query operations
examples/models/query.go Added source interface definition for generic query operations
README.md Fixed template syntax in documentation examples by removing @ prefix from conditionals

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@jinzhu jinzhu requested a review from Copilot September 16, 2025 13:36
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.


// files contains config
filesWithCfg := []string{}
var filesWithCfg []string
Copy link

Copilot AI Sep 16, 2025

Choose a reason for hiding this comment

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

[nitpick] While var filesWithCfg []string is more idiomatic than filesWithCfg := []string{}, this change appears cosmetic and doesn't address any functional issue. Consider whether this change is necessary for the PR's primary objective.

Copilot uses AI. Check for mistakes.
Comment on lines +68 to +81
var tmp strings.Builder
if user.Name != "" {
tmp.WriteString(" name=?,")
params = append(params, user.Name)
}
if user.Age > 0 {
tmp.WriteString(" age=?")
params = append(params, user.Age)
}
c := strings.TrimSpace(tmp.String())
if c != "" {
c = strings.Trim(c, ", ")
sb.WriteString(" SET ")
sb.WriteString(c)
Copy link

Copilot AI Sep 16, 2025

Choose a reason for hiding this comment

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

The UpdateWith method has inconsistent comma handling. Line 70 adds a comma after name=? but line 74 doesn't add a comma after age=?. This could result in malformed SQL when both fields are present.

Suggested change
var tmp strings.Builder
if user.Name != "" {
tmp.WriteString(" name=?,")
params = append(params, user.Name)
}
if user.Age > 0 {
tmp.WriteString(" age=?")
params = append(params, user.Age)
}
c := strings.TrimSpace(tmp.String())
if c != "" {
c = strings.Trim(c, ", ")
sb.WriteString(" SET ")
sb.WriteString(c)
assignments := make([]string, 0, 2)
if user.Name != "" {
assignments = append(assignments, "name=?")
params = append(params, user.Name)
}
if user.Age > 0 {
assignments = append(assignments, "age=?")
params = append(params, user.Age)
}
if len(assignments) > 0 {
sb.WriteString(" SET ")
sb.WriteString(strings.Join(assignments, ", "))

Copilot uses AI. Check for mistakes.
@jinzhu jinzhu merged commit d5e7794 into go-gorm:master Sep 16, 2025
1 check passed
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.

2 participants