mirror of
https://github.com/ent/ent.git
synced 2026-04-28 05:30:56 +03:00
132 lines
4.1 KiB
Cheetah
132 lines
4.1 KiB
Cheetah
{{/*
|
|
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.
|
|
*/}}
|
|
|
|
{{ 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 }} = "{{ $.ID.StorageKey }}"
|
|
{{- range $f := $.Fields -}}{{ $field := $f.Constant -}}
|
|
// {{ $field }} holds the string denoting the {{ lower $f.Name }} vertex property in the database.
|
|
{{ $field }} = "{{ $f.StorageKey }}"
|
|
{{- end }}
|
|
|
|
{{ range $e := $.Edges }}
|
|
{{- $edge := $e.Constant }}
|
|
// {{ $edge }} holds the string denoting the {{ lower $e.Name }} edge name in mutations.
|
|
{{ $edge }} = "{{ $e.Name }}"
|
|
{{- end }}
|
|
|
|
{{ $tmpl := printf "dialect/%s/meta/constants" $.Storage }}
|
|
{{ xtemplate $tmpl $ }}
|
|
)
|
|
|
|
{{ $tmpl = printf "dialect/%s/meta/variables" $.Storage }}
|
|
{{ if hasTemplate $tmpl }}
|
|
{{ xtemplate $tmpl $ }}
|
|
{{ end }}
|
|
|
|
{{ if or $.HasDefault $.HasValidators $.NumHooks $.HasPolicy }}
|
|
{{- $numHooks := $.NumHooks }}
|
|
{{- if $.HasPolicy }}
|
|
{{- $numHooks = add $numHooks 1 }}
|
|
{{- end }}
|
|
{{- if $numHooks }}
|
|
// Note that the variables below are initialized by the runtime
|
|
// package on the initialization of the application. Therefore,
|
|
// it should be imported in the main as follows:
|
|
//
|
|
// import _ "{{ $.Config.Package }}/runtime"
|
|
//
|
|
{{- end }}
|
|
var (
|
|
{{- if $numHooks }}
|
|
Hooks [{{ $numHooks }}]ent.Hook
|
|
{{- end }}
|
|
{{- if $.HasPolicy }}
|
|
Policy ent.Policy
|
|
{{- end }}
|
|
{{- $fields := $.Fields }}{{ if $.ID.UserDefined }}{{ $fields = append $fields $.ID }}{{ end }}
|
|
{{- range $f := $fields }}
|
|
{{- if and $f.Default (not $f.IsEnum) }}
|
|
{{- $default := $f.DefaultName }}
|
|
// {{ $default }} holds the default value on creation for the {{ $f.Name }} field.
|
|
{{ $default }} {{ if or $f.IsTime $f.IsUUID }}func() {{ end }}{{ $f.Type }}
|
|
{{- end }}
|
|
{{- if $f.UpdateDefault }}
|
|
{{- $default := $f.UpdateDefaultName }}
|
|
// {{ $default }} holds the default value on update for the {{ $f.Name }} field.
|
|
{{ $default }} {{ 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.
|
|
{{ $name }} {{ $type }}
|
|
{{- end }}
|
|
{{- end }}
|
|
)
|
|
{{ end }}
|
|
|
|
{{/* define custom type for enum fields */}}
|
|
{{ range $f := $.Fields -}}
|
|
{{ if $f.IsEnum }}
|
|
{{/* omit the package name from the type. */}}
|
|
{{ $enum := trimPackage $f.Type.String $.Package }}
|
|
// {{ $enum }} defines the type for the {{ $f.Name }} enum field.
|
|
type {{ $enum }} string
|
|
|
|
{{- if $f.Default }}
|
|
{{- /* find the enum that holds the default value. */ -}}
|
|
{{- range $_, $e := $f.Enums }}
|
|
{{- if eq $e $f.DefaultValue }}
|
|
{{- $name := $f.EnumName $e }}
|
|
// {{ $name }} is the default {{ $enum }}.
|
|
const {{ $f.DefaultName }} = {{ $name }}
|
|
{{- end }}
|
|
{{- end }}
|
|
{{- end }}
|
|
|
|
// {{ $enum }} values.
|
|
const (
|
|
{{- range $_, $e := $f.Enums }}
|
|
{{ $f.EnumName $e }} {{ $enum }} = "{{ $e }}"
|
|
{{- end }}
|
|
)
|
|
|
|
func (s {{ $enum }}) String() string {
|
|
return string(s)
|
|
}
|
|
|
|
{{ $name := $f.Validator -}}
|
|
{{ $arg := receiver $f.BuilderField }}
|
|
// {{ $name }} is a validator for the "{{ $arg }}" field enum values. It is called by the builders before save.
|
|
func {{ $name }}({{ $arg }} {{ $enum }}) error {
|
|
switch {{ $arg }} {
|
|
case {{ range $i, $e := $f.Enums }}{{ if ne $i 0 }},{{ end }}{{ $f.StructField }}{{ pascal $e }}{{ end }}:
|
|
return nil
|
|
default:
|
|
return fmt.Errorf("{{ $.Package }}: invalid enum value for {{ $f.Name }} field: %q", {{ $arg }})
|
|
}
|
|
}
|
|
{{ end }}
|
|
{{ end }}
|
|
|
|
{{ template "meta/additional" $ }}
|
|
|
|
{{ end }}
|
|
|
|
{{/* A template that can be overrided in order to add additional code for the type package. */}}
|
|
{{ define "meta/additional" }}{{ end }}
|