-
Notifications
You must be signed in to change notification settings - Fork 892
/
where.tmpl
153 lines (137 loc) · 5.16 KB
/
where.tmpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
{{/*
Copyright 2019-present Facebook Inc. All rights reserved.
This source code is licensed under the Apache 2.0 license found
in the LICENSE file in the root directory of this source tree.
*/}}
{{/* gotype: entgo.io/ent/entc/gen.Type */}}
{{ define "where" }}
{{- with extend $ "Package" $.Package -}}
{{ template "header" . }}
{{ end }}
{{ template "import" $ }}
// ID filters vertices based on their ID field.
func ID(id {{ $.ID.Type }}) predicate.{{ $.Name }} {
return predicate.{{ $.Name }}(
{{- $tmpl := printf "dialect/%s/predicate/id" $.Storage }}
{{- xtemplate $tmpl $ -}}
)
}
{{ range $op := $.ID.Ops }}
{{ $arg := "id" }}{{ if $op.Variadic }}{{ $arg = "ids" }}{{ end }}
{{ $func := printf "ID%s" $op.Name }}
// {{ $func }} applies the {{ $op.Name }} predicate on the ID field.
func {{ $func }}({{ $arg }} {{ if $op.Variadic }}...{{ end }}{{ $.ID.Type }}) predicate.{{ $.Name }} {
return predicate.{{ $.Name }}(
{{- with extend $ "Arg" $arg "Op" $op "Storage" $.Storage -}}
{{ $tmpl := printf "dialect/%s/predicate/id/ops" $.Storage }}
{{- xtemplate $tmpl . }}
{{- end -}}
)
}
{{ end }}
{{ range $f := $.Fields }}
{{ $func := $f.StructField }}
{{/* JSON cannot be compared using "=" and Enum has a type defined with the field name */}}
{{ $hasP := not (or $f.IsJSON $f.IsEnum) }}
{{ $comparable := or $f.ConvertedToBasic $f.Type.Valuer }}
{{ $undeclared := (and (ne $func "Label") (ne $func "Hooks") (ne $func "Policy") (ne $func "Table")) }}
{{- if and $hasP $comparable $undeclared }}
{{ $arg := "v" }}
// {{ $func }} applies equality check predicate on the {{ quote $f.Name }} field. It's identical to {{ $func }}EQ.
func {{ $func }}({{ $arg }} {{ $f.Type }}) predicate.{{ $.Name }} {
{{- if and $f.HasGoType (not $f.Type.Valuer) }}
vc := {{ $f.BasicType "v" }}
{{- $arg = "vc" }}
{{- end }}
return predicate.{{ $.Name }}(
{{- with extend $ "Arg" $arg "Field" $f -}}
{{ $tmpl := printf "dialect/%s/predicate/field" $.Storage }}
{{- xtemplate $tmpl . }}
{{- end -}}
)
}
{{- end }}
{{ end }}
{{ range $f := $.Fields }}
{{ range $op := $f.Ops }}
{{ $arg := "v" }}{{ if $op.Variadic }}{{ $arg = "vs" }}{{ end }}
{{ $stringOp := eq $op.Name "EqualFold" "Contains" "ContainsFold" "HasPrefix" "HasSuffix" }}
{{ $func := print $f.StructField $op.Name }}
{{ $type := $f.Type.String }}{{ if $f.IsEnum }}{{ $type = trimPackage $type $.Package }}{{ end }}
// {{ $func }} applies the {{ $op.Name }} predicate on the {{ quote $f.Name }} field.
func {{ $func }}({{ if not $op.Niladic }}{{ $arg }} {{ if $op.Variadic }}...{{ end }}{{ $type }}{{ end }}) predicate.{{ $.Name }} {
{{- if $op.Variadic }}
v := make([]interface{}, len({{ $arg }}))
for i := range v {
{{- if and $f.HasGoType (not $f.Type.Valuer) }}
v[i] = {{ $f.BasicType (printf "%s[i]" $arg) }}
{{- else }}
v[i] = {{ $arg }}[i]
{{- end }}
}
{{- $arg = "v" }}
{{- else if and (not $op.Niladic) $f.HasGoType (or $stringOp (not $f.Type.Valuer)) }}
vc := {{ $f.BasicType "v" }}
{{- $arg = "vc" }}
{{- end }}
return predicate.{{ $.Name }}(
{{- with extend $ "Arg" $arg "Field" $f "Op" $op "Storage" $.Storage -}}
{{ $tmpl := printf "dialect/%s/predicate/field/ops" $.Storage }}
{{- xtemplate $tmpl . }}
{{- end -}}
)
}
{{ end }}
{{ end }}
{{ range $e := $.Edges }}
{{ $func := print "Has" $e.StructField }}
// {{ $func }} applies the HasEdge predicate on the {{ quote $e.Name }} edge.
func {{ $func }}() predicate.{{ $.Name }} {
return predicate.{{ $.Name }}(
{{- with extend $ "Edge" $e -}}
{{ $tmpl := printf "dialect/%s/predicate/edge/has" $.Storage }}
{{- xtemplate $tmpl . }}
{{- end -}}
)
}
{{ $func = printf "%sWith" $func }}
// {{ $func }} applies the HasEdge predicate on the {{ quote $e.Name }} edge with a given conditions (other predicates).
func {{ $func }}(preds ...predicate.{{ $e.Type.Name }}) predicate.{{ $.Name }} {
return predicate.{{ $.Name }}(
{{- with extend $ "Edge" $e -}}
{{ $tmpl := printf "dialect/%s/predicate/edge/haswith" $.Storage }}
{{- xtemplate $tmpl . }}
{{- end -}}
)
}
{{ end }}
// And groups predicates with the AND operator between them.
func And(predicates ...predicate.{{ $.Name }}) predicate.{{ $.Name }} {
return predicate.{{ $.Name }}(
{{- $tmpl = printf "dialect/%s/predicate/and" $.Storage }}
{{- xtemplate $tmpl . -}}
)
}
// Or groups predicates with the OR operator between them.
func Or(predicates ...predicate.{{ $.Name }}) predicate.{{ $.Name }} {
return predicate.{{ $.Name }}(
{{- $tmpl = printf "dialect/%s/predicate/or" $.Storage }}
{{- xtemplate $tmpl . -}}
)
}
// Not applies the not operator on the given predicate.
func Not(p predicate.{{ $.Name }}) predicate.{{ $.Name }} {
return predicate.{{ $.Name }}(
{{- $tmpl = printf "dialect/%s/predicate/not" $.Storage }}
{{- xtemplate $tmpl . -}}
)
}
{{ template "where/additional" $ }}
{{ with $tmpls := matchTemplate "where/additional/*" }}
{{ range $tmpl := $tmpls }}
{{ xtemplate $tmpl $ }}
{{ end }}
{{ end }}
{{ end }}
{{/* A template that can be overridden in order to add additional code for the type package. */}}
{{ define "where/additional" }}{{ end }}