Skip to content

Conversation

zhouyusd
Copy link
Contributor

@zhouyusd zhouyusd commented Oct 5, 2025

让动态SQL支持泛型参数

  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

有的时候参数作为查询条件,零值是有意义的,需要实现nil的时候忽略该查询条件,尝试了泛型
原先gen版本只能解析到类型名,不带泛型参数

User Case Description

wrapper.go

package wrapper

type Wrapper[T any] struct {
	Value T
}
type UserQuerier interface {
  // SELECT * FROM users
  // {{ where }}
  //   {{ if status != nil }}status = @status.Value{{ end }}
  // {{ end }}
  GetUsers(status *wrapper.Wrapper[int]) ([]*gen.T, error)
}

原先生成效果

type IUserDo interface {
    ...

    GetUsers(status *Wrapper) ([]*model.User, error)
}

现在生成效果

type IUserDo interface {
    ...

    GetUsers(status *wrapper.Wrapper[int]) ([]*model.User, error)
}

Copy link

propel-code-bot bot commented Oct 5, 2025

Add Generic Parameter Support for Dynamic SQL Type Parsing

This pull request extends type parsing in internal/parser/parser.go to support Go generics for parameters used in dynamic SQL code generation. It addresses previous limitations where only the type name was parsed, now enabling recognition and use of generic type parameters (e.g., Wrapper[int]) in generated interfaces and functions. Changes affect how the parser processes AST nodes representing generic (indexed) types and adds functions to handle generic type extraction, falling back to interface{} for unknown types to maintain robustness.

Key Changes

• Enhanced type extraction logic in astGetEltType and astGetParamType to handle ast.IndexExpr and ast.IndexListExpr AST nodes representing generic types
• Added new helper function astGetGenericType to extract and format type parameters for generics
• Ensured that parsing of generics defaults type arguments to interface{} if their type cannot be resolved
• Adjusted how the Type and Package fields of Param are assigned for generic types to support correct code generation

Affected Areas

internal/parser/parser.go - specifically the parameter type analysis and AST parsing logic

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

Co-authored-by: propel-code-bot[bot] <203372662+propel-code-bot[bot]@users.noreply.github.com>
Copy link

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

Choose a reason for hiding this comment

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

LGTM, ship it! 🚢


Why was this auto-approved?
AI analysis completed with no actionable comments or suggestions.

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