File: meta.tmpl

package info (click to toggle)
golang-github-facebook-ent 0.5.4-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 14,284 kB
  • sloc: javascript: 349; makefile: 8
file content (153 lines) | stat: -rw-r--r-- 5,046 bytes parent folder | download | duplicates (2)
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.
*/}}

{{ 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 }} field 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 }}

{{ $tmpl = printf "dialect/%s/meta/functions" $.Storage }}
{{ if hasTemplate $tmpl }}
	{{ xtemplate $tmpl $ }}
{{ end }}

{{/* Has at least one field (not enum) with default value */}}
{{ $fields := $.Fields }}{{ if $.ID.UserDefined }}{{ $fields = append $fields $.ID }}{{ end }}
{{ $hasDefault := false }}{{ range $f := $fields }}{{ if and $f.Default (not $f.IsEnum) }}{{ $hasDefault = true }}{{ end }}{{ end }}

{{/* Generate global variables for hooks, validators and policy checkers */}}
{{ if or $hasDefault $.HasValidators $.NumHooks $.NumPolicy }}
	{{- $numHooks := $.NumHooks }}
	{{- if $.NumPolicy }}
		{{- $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 $.NumPolicy }}
			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 $f.DefaultFunc }}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.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 := $.EnumFields }}
	{{ $enum := $f.Type }}
	{{ $receiver := receiver $f.BuilderField }}
	{{ if $f.HasGoType }}
		{{ if $f.Default }}
			const {{ $f.DefaultName }} {{ $enum }} = "{{ $f.DefaultValue }}"
		{{ end }}
	{{ else }}
		{{/* 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.Value $f.DefaultValue }}
					// {{ $e.Name }} is the default value of the {{ $enum }} enum.
					const {{ $f.DefaultName }} = {{ $e.Name }}
				{{- end }}
			{{- end }}
		{{- end }}

		// {{ $enum }} values.
		const (
			{{- range $e := $f.Enums }}
				{{ $e.Name }} {{ $enum }} = "{{ $e.Value }}"
			{{-  end }}
		)

		func ({{ $receiver }} {{ $enum }}) String() string {
			return string({{ $receiver }})
		}
	{{ end }}


	{{ $name := $f.Validator }}
	// {{ $name }} is a validator for the "{{ $f.Name }}" field enum values. It is called by the builders before save.
	func {{ $name }}({{ $receiver }} {{ $enum }}) error {
		switch {{ $receiver }} {
			case {{ range $i, $e := $f.Enums }}{{ if ne $i 0 }},{{ end }}{{ if $f.HasGoType }}"{{ $e.Value }}"{{ else }}{{ $e.Name }}{{ end }}{{ end }}:
				return nil
			default:
				return fmt.Errorf("{{ $.Package }}: invalid enum value for {{ $f.Name }} field: %q", {{ $receiver }})
		}
	}
{{ end }}

{{ template "meta/additional" $ }}

{{ with $tmpls := matchTemplate "meta/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 "meta/additional" }}{{ end }}