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

@@ -26,7 +26,7 @@ type CityQuery struct {
config
limit *int
offset *int
order []Order
order []OrderFunc
unique []string
predicates []predicate.City
// eager-loading edges.
@@ -55,7 +55,7 @@ func (cq *CityQuery) Offset(offset int) *CityQuery {
}
// Order adds an order step to the query.
func (cq *CityQuery) Order(o ...Order) *CityQuery {
func (cq *CityQuery) Order(o ...OrderFunc) *CityQuery {
cq.order = append(cq.order, o...)
return cq
}
@@ -248,7 +248,7 @@ func (cq *CityQuery) Clone() *CityQuery {
config: cq.config,
limit: cq.limit,
offset: cq.offset,
order: append([]Order{}, cq.order...),
order: append([]OrderFunc{}, cq.order...),
unique: append([]string{}, cq.unique...),
predicates: append([]predicate.City{}, cq.predicates...),
// clone intermediate query.
@@ -468,14 +468,14 @@ func (cq *CityQuery) sqlQuery() *sql.Selector {
type CityGroupBy 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 (cgb *CityGroupBy) Aggregate(fns ...Aggregate) *CityGroupBy {
func (cgb *CityGroupBy) Aggregate(fns ...AggregateFunc) *CityGroupBy {
cgb.fns = append(cgb.fns, fns...)
return cgb
}

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 StreetQuery struct {
config
limit *int
offset *int
order []Order
order []OrderFunc
unique []string
predicates []predicate.Street
// eager-loading edges.
@@ -55,7 +55,7 @@ func (sq *StreetQuery) Offset(offset int) *StreetQuery {
}
// Order adds an order step to the query.
func (sq *StreetQuery) Order(o ...Order) *StreetQuery {
func (sq *StreetQuery) Order(o ...OrderFunc) *StreetQuery {
sq.order = append(sq.order, o...)
return sq
}
@@ -248,7 +248,7 @@ func (sq *StreetQuery) Clone() *StreetQuery {
config: sq.config,
limit: sq.limit,
offset: sq.offset,
order: append([]Order{}, sq.order...),
order: append([]OrderFunc{}, sq.order...),
unique: append([]string{}, sq.unique...),
predicates: append([]predicate.Street{}, sq.predicates...),
// clone intermediate query.
@@ -475,14 +475,14 @@ func (sq *StreetQuery) sqlQuery() *sql.Selector {
type StreetGroupBy 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 (sgb *StreetGroupBy) Aggregate(fns ...Aggregate) *StreetGroupBy {
func (sgb *StreetGroupBy) Aggregate(fns ...AggregateFunc) *StreetGroupBy {
sgb.fns = append(sgb.fns, fns...)
return sgb
}