mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
entc/gen: add fields checks for orderfuncs
This commit is contained in:
committed by
Ariel Mashraki
parent
9743213860
commit
6a7a5ef439
@@ -4,11 +4,20 @@ This source code is licensed under the Apache 2.0 license found
|
||||
in the LICENSE file in the root directory of this source tree.
|
||||
*/}}
|
||||
|
||||
{{ define "dialect/sql/order" -}}
|
||||
{{ define "dialect/sql/order/signature" -}}
|
||||
// OrderFunc applies an ordering on the sql selector.
|
||||
type OrderFunc func(*sql.Selector, func(string) bool)
|
||||
{{- end }}
|
||||
|
||||
{{ define "dialect/sql/order/func" -}}
|
||||
{{- $f := $.Scope.Func -}}
|
||||
func(s *sql.Selector) {
|
||||
func(s *sql.Selector, check func(string) bool) {
|
||||
for _, f := range fields {
|
||||
s.OrderBy(sql.{{ $f }}(f))
|
||||
if check(f) {
|
||||
s.OrderBy(sql.{{ $f }}(f))
|
||||
} else {
|
||||
s.AddError(&ValidationError{Name: f, err: fmt.Errorf("invalid field %q for ordering", f)})
|
||||
}
|
||||
}
|
||||
}
|
||||
{{- end }}
|
||||
|
||||
Reference in New Issue
Block a user