Files
ent/entc/gen/template/meta.tmpl
Ariel Mashraki 772b8a33f8 ent/field: add update_default option time field
Reviewed By: alexsn

Differential Revision: D17070907

fbshipit-source-id: 63c9ce75c58e524044c38f9461cb04e8e45c8017
2019-08-27 06:54:49 -07:00

79 lines
2.6 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 -}}
{{- $desc := print "desc" (pascal $f.Name) -}}
{{ if or $f.Default $f.UpdateDefault $f.Validators -}}
// {{ $desc }} is the schema descriptor for {{ $f.Name }} field.
{{ $desc }} = fields[{{ $i }}].Descriptor()
{{ end -}}
{{ if $f.Default }}
{{- $default := $f.DefaultName -}}
// {{ $default }} holds the default value on creation for the {{ $f.Name }} field.
{{ $default }} = {{ $desc }}.Default.({{ if $f.IsTime }}func() {{ end }}{{ $f.Type }})
{{ end -}}
{{ if $f.UpdateDefault }}
{{- $default := $f.UpdateDefaultName -}}
// {{ $default }} holds the default value on update for the {{ $f.Name }} field.
{{ $default }} = {{ $desc }}.UpdateDefault.({{ if $f.IsTime }}func() {{ end }}{{ $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 }} = {{ $desc }}.Validators[0].({{ $type }})
{{ else -}}
{{ $name }} = func() {{ $type }} {
validators := {{ $desc }}.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 }}