Files
ent/entc/gen/template/where.tmpl
Ariel Mashraki 56656dfcb6 ent/entc: configure storage driver in codegen
Summary: Pull Request resolved: https://github.com/facebookexternal/fbc/pull/1229

Reviewed By: alexsn

Differential Revision: D16539934

fbshipit-source-id: b3a8bf1f1be6f65ad3f649cd921ea20fc24182bf
2019-07-30 02:49:22 -07:00

121 lines
4.1 KiB
Cheetah

{{ define "where" }}
{{ template "header" $.Package }}
{{ template "import" $ }}
// ID filters vertices based on their identifier.
func ID(id {{ $.ID.Type }}) predicate.{{ $.Name }} {
return predicate.{{ $.Name }}{{ if gt (len $.Storage) 1 }}PerDialect{{ end }}(
{{ range $_, $storage := $.Storage -}}
{{ $tmpl := printf "dialect/%s/predicate/id" $storage }}
{{- xtemplate $tmpl $ }},
{{ end -}}
)
}
{{ range $_, $op := ops $.ID }}
{{ $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 }}{{ if gt (len $.Storage) 1 }}PerDialect{{ end }}(
{{ range $_, $storage := $.Storage -}}
{{- with extend $ "Arg" $arg "Op" $op -}}
{{ $tmpl := printf "dialect/%s/predicate/id/ops" $storage }}
{{- xtemplate $tmpl . }},
{{ end -}}
{{ end -}}
)
}
{{ end }}
{{ range $_, $f := $.Fields }}
{{ $func := pascal $f.Name }}
// {{ $func }} applies equality check predicate on the {{ quote $f.Name }} field. It's identical to {{ $func }}EQ.
func {{ $func }}(v {{ $f.Type }}) predicate.{{ $.Name }} {
return predicate.{{ $.Name }}{{ if gt (len $.Storage) 1 }}PerDialect{{ end }}(
{{ range $_, $storage := $.Storage -}}
{{- with extend $ "Field" $f -}}
{{ $tmpl := printf "dialect/%s/predicate/field" $storage }}
{{- xtemplate $tmpl . }},
{{ end -}}
{{ end -}}
)
}
{{ end }}
{{ range $_, $f := $.Fields }}
{{ range $_, $op := (ops $f) }}
{{ $arg := "v" }}{{ if $op.Variadic }}{{ $arg = "vs" }}{{ end }}
{{ $func := print (pascal $f.Name) ($op.Name) }}
// {{ $func }} applies the {{ $op.Name }} predicate on the {{ quote $f.Name }} field.
func {{ $func }}({{ $arg }} {{ if $op.Variadic }}...{{ end }}{{ $f.Type }}) predicate.{{ $.Name }} {
{{- if $op.Variadic }}
v := make([]interface{}, len({{ $arg }}))
for i := range v {
v[i] = {{ $arg }}[i]
}
{{- end }}
return predicate.{{ $.Name }}{{ if gt (len $.Storage) 1 }}PerDialect{{ end }}(
{{ range $_, $storage := $.Storage -}}
{{- with extend $ "Arg" $arg "Field" $f "Op" $op -}}
{{ $tmpl := printf "dialect/%s/predicate/field/ops" $storage }}
{{- xtemplate $tmpl . }},
{{ end -}}
{{ end -}}
)
}
{{ end }}
{{ end }}
{{ range $_, $e := $.Edges }}
{{ $func := pascal $e.Name | printf "Has%s" }}
// {{ $func }} applies the HasEdge predicate on the {{ quote $e.Name }} edge.
func {{ $func }}() predicate.{{ $.Name }} {
return predicate.{{ $.Name }}{{ if gt (len $.Storage) 1 }}PerDialect{{ end }}(
{{ range $_, $storage := $.Storage -}}
{{- with extend $ "Edge" $e -}}
{{ $tmpl := printf "dialect/%s/predicate/edge/has" $storage }}
{{- xtemplate $tmpl . }},
{{ end -}}
{{ 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 }}{{ if gt (len $.Storage) 1 }}PerDialect{{ end }}(
{{ range $_, $storage := $.Storage -}}
{{- with extend $ "Edge" $e -}}
{{ $tmpl := printf "dialect/%s/predicate/edge/haswith" $storage }}
{{- xtemplate $tmpl . }},
{{ end -}}
{{ end -}}
)
}
{{ end }}
// Or groups list of predicates with the or operator between them.
func Or(predicates ...predicate.{{ $.Name }}) predicate.{{ $.Name }} {
return predicate.{{ $.Name }}{{ if gt (len $.Storage) 1 }}PerDialect{{ end }}(
{{ range $_, $storage := $.Storage -}}
{{ $tmpl := printf "dialect/%s/predicate/or" $storage }}
{{- xtemplate $tmpl . }},
{{ end -}}
)
}
// Not applies the not operator on the given predicate.
func Not(p predicate.{{ $.Name }}) predicate.{{ $.Name }} {
return predicate.{{ $.Name }}{{ if gt (len $.Storage) 1 }}PerDialect{{ end }}(
{{ range $_, $storage := $.Storage -}}
{{ $tmpl := printf "dialect/%s/predicate/not" $storage }}
{{- xtemplate $tmpl . }},
{{ end -}}
)
}
{{ end }}