Generated aggregate method's receiver name conflict with arg name
- [x] The issue is present in the latest release.
- [x] I have searched the issues of this repository and believe that this is not a duplicate.
Current Behavior π―
The schema is:
type FlowNode struct {
ent.Schema
}
Generated query:
// FlowNodeSelect is the builder for selecting fields of FlowNode entities.
type FlowNodeSelect struct {
*FlowNodeQuery
selector
}
// Aggregate adds the given aggregation functions to the selector query.
func (fns *FlowNodeSelect) Aggregate(fns ...AggregateFunc) *FlowNodeSelect {
fns.fns = append(fns.fns, fns...)
return fns
}
the receiver name is the same as the arg name so the compile won't pass
Expected Behavior π€
the receiver name and the arg name should be different
Steps to Reproduce πΉ
Steps:
Your Environment π
| Tech | Version |
|---|---|
| Go | 1.20.4 |
| Ent | 0.12.3 |
| Database | MySQL |
| Driver | https://github.com/go-sql-driver/mysql |
Encountered this exact same issue today, ended up working around it by switching to using entc as package and adding a small utility that replaces the function with one that doesn't have a naming conflict:
func main() {
// ... entc generation goes here
if err := fixFlowNodeSelectError(); err != nil {
log.Fatalf("fixing FlowNodeSelect error: %v", err)
}
}
// fixFlowNodeSelect works around a compile error that Ent generates in
// flownode_query.go, where it creates a receiver named fns for the
// FlowNodeSelect struct which is also used as an arg name in Aggregate.
func fixFlowNodeSelectError() error {
filename := "./flownode_query.go"
content, err := os.ReadFile(filename)
if err != nil {
return err
}
source := `func (fns *FlowNodeSelect) Aggregate(fns ...AggregateFunc) *FlowNodeSelect {
fns.fns = append(fns.fns, fns...)
return fns`
replacement := `func (s *FlowNodeSelect) Aggregate(fns ...AggregateFunc) *FlowNodeSelect {
s.fns = append(s.fns, fns...)
return s`
content = []byte(strings.Replace(string(content), source, replacement, 1))
return os.WriteFile(filename, content, 0)
}
It's a fragile solution but lets me keep the name FlowNode for now.
@aholstenson glad you solved it! i have been using Gorm after that
Hello, please upgrade to latest version of ent. This issue should be fixed with static receiver.
Hello, please upgrade to latest version of ent. This issue should be fixed with static receiver.
thank you for you guy's great workπ