Skip to content

Commit

Permalink
entc/gen: avoid conflict between order by edge-count and fields end w…
Browse files Browse the repository at this point in the history
…ith _count (ent#3534)
  • Loading branch information
a8m authored May 12, 2023
1 parent dcb84d8 commit 36553bb
Show file tree
Hide file tree
Showing 16 changed files with 666 additions and 7 deletions.
15 changes: 14 additions & 1 deletion entc/gen/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,20 @@ func (f Field) DefaultValue() any { return f.def.DefaultValue }
func (f Field) DefaultFunc() bool { return f.def.DefaultKind == reflect.Func }

// OrderName returns the function/option name for ordering by this field.
func (f Field) OrderName() string { return "By" + pascal(f.Name) }
func (f Field) OrderName() string {
name := "By" + pascal(f.Name)
// Some users store associations count as a separate field.
// In this case, we suffix the order name with "Field".
if f.typ == nil || !strings.HasSuffix(name, "Count") {
return name
}
for _, e := range f.typ.Edges {
if nameE, err := e.OrderCountName(); err == nil && nameE == name {
return name + "Field"
}
}
return name
}

// BuilderField returns the struct member of the field in the builder.
func (f Field) BuilderField() string {
Expand Down
6 changes: 6 additions & 0 deletions entc/integration/ent/entql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions entc/integration/ent/migrate/schema.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

109 changes: 108 additions & 1 deletion entc/integration/ent/mutation.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions entc/integration/ent/schema/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ func (User) Fields() []ent.Field {
Default("Full-Time"),
field.String("SSOCert").
Optional(),
// Some users store the associations
// count as a separate field.
field.Int("files_count").
Optional(),
}
}

Expand Down
13 changes: 12 additions & 1 deletion entc/integration/ent/user.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions entc/integration/ent/user/user.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 36553bb

Please sign in to comment.