entc/gen: add fields checks for orderfuncs

This commit is contained in:
Ariel Mashraki
2020-09-10 22:12:57 +03:00
committed by Ariel Mashraki
parent 9743213860
commit 6a7a5ef439
20 changed files with 217 additions and 79 deletions

View File

@@ -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 }}