entc/gen: suffix Order and Aggregate with Func (#449)

This commit is contained in:
Ariel Mashraki
2020-04-23 13:22:21 +03:00
committed by GitHub
parent 6e584d8efc
commit 7cfcb97694
94 changed files with 624 additions and 624 deletions

View File

@@ -30,11 +30,11 @@ type (
MutateFunc = ent.MutateFunc
)
// Order applies an ordering on either graph traversal or sql selector.
type Order func(*sql.Selector)
// OrderFunc applies an ordering on either graph traversal or sql selector.
type OrderFunc func(*sql.Selector)
// Asc applies the given fields in ASC order.
func Asc(fields ...string) Order {
func Asc(fields ...string) OrderFunc {
return func(s *sql.Selector) {
for _, f := range fields {
s.OrderBy(sql.Asc(f))
@@ -43,7 +43,7 @@ func Asc(fields ...string) Order {
}
// Desc applies the given fields in DESC order.
func Desc(fields ...string) Order {
func Desc(fields ...string) OrderFunc {
return func(s *sql.Selector) {
for _, f := range fields {
s.OrderBy(sql.Desc(f))
@@ -51,8 +51,8 @@ func Desc(fields ...string) Order {
}
}
// Aggregate applies an aggregation step on the group-by traversal/selector.
type Aggregate func(*sql.Selector) string
// AggregateFunc applies an aggregation step on the group-by traversal/selector.
type AggregateFunc func(*sql.Selector) string
// As is a pseudo aggregation function for renaming another other functions with custom names. For example:
//
@@ -60,42 +60,42 @@ type Aggregate func(*sql.Selector) string
// Aggregate(ent.As(ent.Sum(field1), "sum_field1"), (ent.As(ent.Sum(field2), "sum_field2")).
// Scan(ctx, &v)
//
func As(fn Aggregate, end string) Aggregate {
func As(fn AggregateFunc, end string) AggregateFunc {
return func(s *sql.Selector) string {
return sql.As(fn(s), end)
}
}
// Count applies the "count" aggregation function on each group.
func Count() Aggregate {
func Count() AggregateFunc {
return func(s *sql.Selector) string {
return sql.Count("*")
}
}
// Max applies the "max" aggregation function on the given field of each group.
func Max(field string) Aggregate {
func Max(field string) AggregateFunc {
return func(s *sql.Selector) string {
return sql.Max(s.C(field))
}
}
// Mean applies the "mean" aggregation function on the given field of each group.
func Mean(field string) Aggregate {
func Mean(field string) AggregateFunc {
return func(s *sql.Selector) string {
return sql.Avg(s.C(field))
}
}
// Min applies the "min" aggregation function on the given field of each group.
func Min(field string) Aggregate {
func Min(field string) AggregateFunc {
return func(s *sql.Selector) string {
return sql.Min(s.C(field))
}
}
// Sum applies the "sum" aggregation function on the given field of each group.
func Sum(field string) Aggregate {
func Sum(field string) AggregateFunc {
return func(s *sql.Selector) string {
return sql.Sum(s.C(field))
}

View File

@@ -25,7 +25,7 @@ type NodeQuery struct {
config
limit *int
offset *int
order []Order
order []OrderFunc
unique []string
predicates []predicate.Node
// eager-loading edges.
@@ -56,7 +56,7 @@ func (nq *NodeQuery) Offset(offset int) *NodeQuery {
}
// Order adds an order step to the query.
func (nq *NodeQuery) Order(o ...Order) *NodeQuery {
func (nq *NodeQuery) Order(o ...OrderFunc) *NodeQuery {
nq.order = append(nq.order, o...)
return nq
}
@@ -267,7 +267,7 @@ func (nq *NodeQuery) Clone() *NodeQuery {
config: nq.config,
limit: nq.limit,
offset: nq.offset,
order: append([]Order{}, nq.order...),
order: append([]OrderFunc{}, nq.order...),
unique: append([]string{}, nq.unique...),
predicates: append([]predicate.Node{}, nq.predicates...),
// clone intermediate query.
@@ -534,14 +534,14 @@ func (nq *NodeQuery) sqlQuery() *sql.Selector {
type NodeGroupBy struct {
config
fields []string
fns []Aggregate
fns []AggregateFunc
// intermediate query (i.e. traversal path).
sql *sql.Selector
path func(context.Context) (*sql.Selector, error)
}
// Aggregate adds the given aggregation functions to the group-by query.
func (ngb *NodeGroupBy) Aggregate(fns ...Aggregate) *NodeGroupBy {
func (ngb *NodeGroupBy) Aggregate(fns ...AggregateFunc) *NodeGroupBy {
ngb.fns = append(ngb.fns, fns...)
return ngb
}