mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
Reviewed By: alexsn Differential Revision: D16757699 fbshipit-source-id: 6a0027b1f855721f1415b8c72b5b1be8bc2ce902
69 lines
2.1 KiB
Cheetah
69 lines
2.1 KiB
Cheetah
{{ define "meta" }}
|
|
|
|
{{- with extend $ "Package" $.Package -}}
|
|
{{ template "header" . }}
|
|
{{ end }}
|
|
|
|
{{ template "import" $ }}
|
|
|
|
const (
|
|
// Label holds the string label denoting the {{ lower $.Name }} type in the database.
|
|
Label = "{{ $.Label }}"
|
|
// {{ $.ID.Constant }} holds the string denoting the id field in the database.
|
|
{{ $.ID.Constant }} = "{{ snake $.ID.Name }}"
|
|
{{ range $_, $f := $.Fields -}}{{ $field := $f.Constant -}}
|
|
// {{ $field }} holds the string denoting the {{ lower $f.Name }} vertex property in the database.
|
|
{{ $field }} = "{{ snake $f.Name }}"
|
|
{{ end -}}
|
|
{{ range $_, $storage := $.Storage }}
|
|
{{ $tmpl := printf "dialect/%s/meta/constants" $storage }}
|
|
{{ xtemplate $tmpl $ }}
|
|
{{ end }}
|
|
)
|
|
|
|
{{ range $_, $storage := $.Storage }}
|
|
{{ $tmpl := printf "dialect/%s/meta/variables" $storage }}
|
|
{{ if hasTemplate $tmpl }}
|
|
{{ xtemplate $tmpl $ }}
|
|
{{ end }}
|
|
{{ end }}
|
|
|
|
{{ if or $.HasDefault $.HasValidators }}
|
|
var (
|
|
fields = {{ base $.Schema }}.{{ $.Name }}{}.Fields()
|
|
{{ range $i, $f := $.Fields -}}
|
|
{{ if $f.HasDefault }}
|
|
{{- $default := $f.DefaultConstant -}}
|
|
// {{ $default }} holds the default value for the {{ $f.Name }} field.
|
|
{{ $default }} = fields[{{ $i }}].Value().({{ $f.Type }})
|
|
{{ end -}}
|
|
{{ with $f.Validators -}}
|
|
{{ $name := $f.Validator -}}
|
|
{{ $type := printf "func (%s) error" $f.Type -}}
|
|
// {{ $name }} is a validator for the "{{ $f.Name }}" field. It is called by the builders before save.
|
|
{{ if eq $f.Validators 1 -}}
|
|
{{ $name }} = fields[{{ $i }}].Validators()[0].({{ $type }})
|
|
{{ else -}}
|
|
{{ $name }} = func() {{ $type }} {
|
|
validators := fields[{{ $i }}].Validators()
|
|
fns := [...]func({{ $f.Type }}) error {
|
|
{{- range $j, $n := xrange $f.Validators }}
|
|
validators[{{ $j }}].(func({{ $f.Type }}) error),
|
|
{{- end }}
|
|
}
|
|
return func({{ $f.Name }} {{ $f.Type }}) error {
|
|
for _, fn := range fns {
|
|
if err := fn({{ $f.Name }}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
}()
|
|
{{ end -}}
|
|
{{ end -}}
|
|
{{ end -}}
|
|
)
|
|
{{ end }}
|
|
|
|
{{ end }} |