-
Notifications
You must be signed in to change notification settings - Fork 890
/
ent.tmpl
239 lines (211 loc) · 8.09 KB
/
ent.tmpl
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
{{/*
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.
*/}}
{{/* gotype: entgo.io/ent/entc/gen.Type */}}
{{ define "model" }}
{{ $pkg := base $.Config.Package }}
{{ template "header" $ }}
{{ template "import" $ }}
import (
{{- range $import := $.SiblingImports }}
{{ $import.Alias }} "{{ $import.Path }}"
{{- end }}
)
// {{ $.Name }} is the model entity for the {{ $.Name }} schema.
{{- with $tmpls := matchTemplate "model/comment/additional/*" }}
{{- range $tmpl := $tmpls }}
{{- xtemplate $tmpl $ }}
{{- end }}
{{- end }}
type {{ $.Name }} struct {
config {{ template "model/omittags" $ }}
// ID of the ent.
{{- if $.ID.Comment }}
{{- range $line := split $.ID.Comment "\n" }}
// {{ $line }}
{{- end }}
{{- end }}
ID {{ $.ID.Type }} {{ with $.Annotations.Fields.StructTag.id }}`{{ . }}`{{ else }}`{{ $.ID.StructTag }}`{{ end }}
{{- range $f := $.Fields }}
{{- $tag := $f.StructTag }}{{ with $tags := $.Annotations.Fields.StructTag }}{{ with index $tags $f.Name }}{{ $tag = . }}{{ end }}{{ end }}
{{- template "model/fieldcomment" $f }}
{{ $f.StructField }} {{ if $f.NillableValue }}*{{ end }}{{ $f.Type }} {{ if not $f.Sensitive }}`{{ $tag }}`{{ else }}{{ template "model/omittags" $ }}{{ end }}
{{- end }}
{{- with $.Edges }}
// Edges holds the relations/edges for other nodes in the graph.
// The values are being populated by the {{ $.Name }}Query when eager-loading is set.
Edges {{ $.Name }}Edges {{ template "model/edgetags" $ }}
{{- end -}}
{{- /* Additional fields to add by the storage driver. */}}
{{- $tmpl := printf "dialect/%s/model/fields" $.Storage }}
{{- if hasTemplate $tmpl }}
{{- xtemplate $tmpl . }}
{{- end }}
{{- /* Additional fields to add by the user. */}}
{{ template "model/fields/additional" $ }}
}
{{- with $.Edges }}
// {{ $.Name }}Edges holds the relations/edges for other nodes in the graph.
type {{ $.Name }}Edges struct {
{{- range $e := . }}
// {{ $e.StructField }} holds the value of the {{ $e.Name }} edge.
{{ $e.StructField }} {{ if not $e.Unique }}[]{{ end }}*{{ $e.Type.Name }} {{ with $e.StructTag }}`{{ . }}`{{ end }}
{{- end }}
// loadedTypes holds the information for reporting if a
// type was loaded (or requested) in eager-loading or not.
loadedTypes [{{ len . }}]bool
{{- /* Additional fields to add by the user. */}}
{{ template "model/edges/fields/additional" $ }}
}
{{- range $i, $e := . }}
// {{ $e.StructField }}OrErr returns the {{ $e.StructField }} value or an error if the edge
// was not loaded in eager-loading{{ if $e.Unique }}, or loaded but was not found{{ end }}.
func (e {{ $.Name }}Edges) {{ $e.StructField }}OrErr() ({{ if not $e.Unique }}[]{{ end }}*{{ $e.Type.Name }}, error) {
if e.loadedTypes[{{ $i }}] {
{{- if $e.Unique }}
if e.{{ $e.StructField }} == nil {
// The edge {{ $e.Name }} was loaded in eager-loading,
// but was not found.
return nil, &NotFoundError{label: {{ $e.Type.Package }}.Label}
}
{{- end }}
return e.{{ $e.StructField }}, nil
}
return nil, &NotLoadedError{edge: "{{ $e.Name }}"}
}
{{- end }}
{{- end }}
{{ $tmpl = printf "dialect/%s/decode/one" $.Storage }}
{{ xtemplate $tmpl $ }}
{{ $receiver := $.Receiver }}
{{ range $e := $.Edges }}
{{ $func := print "Query" $e.StructField }}
// {{ $func }} queries the "{{ $e.Name }}" edge of the {{ $.Name }} entity.
func ({{ $receiver }} *{{ $.Name }}) {{ $func }}() *{{ $e.Type.QueryName }} {
return (&{{ $.Name }}Client{config: {{ $receiver }}.config}).{{ $func }}({{ $receiver }})
}
{{ end }}
// Update returns a builder for updating this {{ $.Name }}.
// Note that you need to call {{ $.Name }}.Unwrap() before calling this method if this {{ $.Name }}
// was returned from a transaction, and the transaction was committed or rolled back.
func ({{ $receiver }} *{{ $.Name }}) Update() *{{ $.UpdateOneName }} {
return (&{{ $.Name }}Client{config: {{ $receiver }}.config}).UpdateOne({{ $receiver }})
}
// Unwrap unwraps the {{ $.Name }} entity that was returned from a transaction after it was closed,
// so that all future queries will be executed through the driver which created the transaction.
func ({{ $receiver }} *{{ $.Name }}) Unwrap() *{{ $.Name }} {
tx, ok := {{ $receiver }}.config.driver.(*txDriver)
if !ok {
panic("{{ $pkg }}: {{ $.Name }} is not a transactional entity")
}
{{ $receiver }}.config.driver = tx.drv
return {{ $receiver }}
}
{{ template "model/stringer" $ }}
{{ template "model/additional" $ }}
{{ $slice := plural $.Name }}
// {{ $slice }} is a parsable slice of {{ $.Name }}.
type {{ $slice }} []*{{ $.Name }}
{{ with extend $ "Slice" $slice }}
{{ $tmpl := printf "dialect/%s/decode/many" $.Storage }}
{{ xtemplate $tmpl . }}
{{ end }}
func ({{ $receiver }} {{ $slice }}) config(cfg config) {
for _i := range {{ $receiver }} {
{{ $receiver }}[_i].config = cfg
}
}
{{ end }}
{{/* A template to generate a fmt.Stringer implementation. */}}
{{ define "model/stringer" }}
{{ $receiver := $.Receiver }}
// String implements the fmt.Stringer.
func ({{ $receiver }} *{{ $.Name }}) String() string {
var builder strings.Builder
builder.WriteString("{{ $.Name }}(")
builder.WriteString(fmt.Sprintf("id=%v", {{ $receiver }}.ID))
{{- range $i, $f := $.Fields }}
{{- if $f.Sensitive }}
builder.WriteString(", {{ $f.Name }}={{ print "<sensitive>" }}")
{{- else }}
{{- $sf := printf "%s.%s" $receiver $f.StructField }}
{{- if $f.Nillable }}
if v := {{ $sf }}; v != nil {
builder.WriteString(", {{ $f.Name }}=")
{{- if and $f.IsTime (not $f.HasGoType) }}
builder.WriteString(v.Format(time.ANSIC))
{{- else if and $f.IsString (not $f.HasGoType) }}
builder.WriteString(*v)
{{- else }}
builder.WriteString(fmt.Sprintf("%v", *v))
{{- end }}
}
{{- else }}
builder.WriteString(", {{ $f.Name }}=")
{{- if and $f.IsTime (not $f.HasGoType) }}
builder.WriteString({{ $sf }}.Format(time.ANSIC))
{{- else if and $f.IsString (not $f.HasGoType) }}
builder.WriteString({{ $sf }})
{{- else }}
builder.WriteString(fmt.Sprintf("%v", {{ $sf }}))
{{- end }}
{{- end }}
{{- end }}
{{- end }}
builder.WriteByte(')')
return builder.String()
}
{{ end }}
{{/* A template for generating the tag of the Edges struct-field. */}}
{{- define "model/edgetags" }}
{{- $tag := `json:"edges"` }}
{{- with $.Annotations.Edges.StructTag }}
{{- if tagLookup . "json" }}
{{- $tag = . }}
{{- else }}
{{- $tag = print $tag " " . }}
{{- end }}
{{- end }}
{{- print "`" $tag "`" }}
{{- end }}
{{/* A template for omitting struct-tags. */}}
{{ define "model/omittags" }}{{ with $.TagTypes }}`{{ range $i, $t := . }}{{ if ne $i 0 }} {{ end }}{{ $t }}:"-"{{ end }}`{{ end }}{{ end }}
{{/* A template that can be overridden in order to add additional fields to the each type.*/}}
{{ define "model/fields/additional" }}{{ end }}
{{/* A template for setting the field comment. */}}
{{- define "model/fieldcomment" }}
// {{ $.StructField }} holds the value of the "{{ $.Name }}" field.
{{- if $.Comment }}
{{- range $line := split $.Comment "\n" }}
// {{ $line }}
{{- end }}
{{- end }}
{{- end }}
{{/* A template for adding additional methods or helpers for the generated model. */}}
{{ define "model/additional" }}
{{- with $tmpls := matchTemplate "model/additional/*" }}
{{- range $tmpl := $tmpls }}
{{- xtemplate $tmpl $ }}
{{- end }}
{{- end }}
{{- with $tmpls := matchTemplate (printf "dialect/%s/model/additional/*" $.Storage) }}
{{- range $tmpl := $tmpls }}
{{- xtemplate $tmpl $ }}
{{- end }}
{{- end }}
{{ end }}
{{/* A template for adding additional fields for the Edges object of the generated model. */}}
{{ define "model/edges/fields/additional" }}
{{- with $tmpls := matchTemplate "model/edges/fields/additional/*" }}
{{- range $tmpl := $tmpls }}
{{- xtemplate $tmpl $ }}
{{- end }}
{{- end }}
{{- with $tmpls := matchTemplate (printf "dialect/%s/model/edges/fields/additional/*" $.Storage) }}
{{- range $tmpl := $tmpls }}
{{- xtemplate $tmpl $ }}
{{- end }}
{{- end }}
{{ end }}