ent icon indicating copy to clipboard operation
ent copied to clipboard

Generated aggregate method's receiver name conflict with arg name

Open pot-code opened this issue 2 years ago β€’ 2 comments

  • [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

pot-code avatar Jul 31 '23 14:07 pot-code

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 avatar Jun 07 '24 06:06 aholstenson

@aholstenson glad you solved it! i have been using Gorm after that

pot-code avatar Jun 07 '24 07:06 pot-code

Hello, please upgrade to latest version of ent. This issue should be fixed with static receiver.

giautm avatar Mar 19 '25 08:03 giautm

Hello, please upgrade to latest version of ent. This issue should be fixed with static receiver.

thank you for you guy's great work😁

pot-code avatar Mar 19 '25 08:03 pot-code