-
Notifications
You must be signed in to change notification settings - Fork 0
/
setter.tmpl
114 lines (105 loc) · 4.59 KB
/
setter.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
{{/*
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 "setter" }}
{{ $builder := pascal $.Scope.Builder }}
{{ $receiver := receiver $builder }}
{{ $fields := $.Fields }}
{{ $updater := false }}
{{- if or (hasSuffix $builder "Update") (hasSuffix $builder "UpdateOne") }}
{{ $updater = true }}
{{ $fields = $.MutableFields }}
{{- else if $.HasCompositeID }}
{{- else if $.ID.UserDefined }}
{{ $fields = append $fields $.ID }}
{{- end }}
{{ range $f := $fields }}
{{ $p := receiver $f.Type.String }}{{ if eq $p $receiver }} {{ $p = "value" }} {{ end }}
{{ $func := print "Set" $f.StructField }}
// {{ $func }} sets the "{{ $f.Name }}" field.
func ({{ $receiver }} *{{ $builder }}) {{ $func }}({{ $p }} {{ $f.Type }}) *{{ $builder }} {
{{- /* setting numeric type override previous calls to Add. */}}
{{- if and $updater $f.SupportsMutationAdd }}
{{ $receiver }}.mutation.{{ print "Reset" $f.StructField }}()
{{- end }}
{{ $receiver }}.mutation.{{ $func }}({{ $p }})
return {{ $receiver }}
}
{{/* Avoid generating nillable setters for nillable types. */}}
{{ if and (not $f.Type.Nillable) (or $f.Optional $f.Default) (not (and $updater $f.UpdateDefault)) }}
{{ $nillableFunc := print "SetNillable" $f.StructField }}
// {{ $nillableFunc }} sets the "{{ $f.Name }}" field if the given value is not nil.
func ({{ $receiver }} *{{ $builder }}) {{ $nillableFunc }}({{ $p }} *{{ $f.Type }}) *{{ $builder }} {
if {{ $p }} != nil {
{{ $receiver }}.{{ $func }}(*{{ $p }})
}
return {{ $receiver }}
}
{{ end }}
{{ if and $updater $f.SupportsMutationAdd }}
{{ $func := print "Add" $f.StructField }}
// {{ $func }} adds {{ $p }} to the "{{ $f.Name }}" field.
func ({{ $receiver }} *{{ $builder }}) {{ $func }}({{ $p }} {{ $f.SignedType }}) *{{ $builder }} {
{{ $receiver }}.mutation.{{ $func }}({{ $p }})
return {{ $receiver }}
}
{{ end }}
{{ if and $f.Optional $updater }}
{{ $func := print "Clear" $f.StructField }}
// {{ $func }} clears the value of the "{{ $f.Name }}" field.
func ({{ $receiver }} *{{ $builder }}) {{ $func }}() *{{ $builder }} {
{{ $receiver }}.mutation.{{ $func }}()
return {{ $receiver }}
}
{{ end }}
{{ end }}
{{ range $e := $.EdgesWithID }}
{{ if and $updater $e.Immutable }}
{{/* Skip to the next one as immutable edges cannot be updated. */}}
{{continue}}
{{ end }}
{{ $op := "add" }}{{ $idsFunc := $e.MutationAdd }}{{ if $e.Unique }}{{ $op = "set" }}{{ $idsFunc = $e.MutationSet }}{{ end }}
{{/* Check if this setter was already defined by the field-setters (e.g. edge-field with the same name). */}}
{{ $withSetter := not $e.HasFieldSetter }}
{{ if $withSetter }}
// {{ $idsFunc }} {{ $op }}s the "{{ $e.Name }}" edge to the {{ $e.Type.Name }} entity by ID{{ if not $e.Unique }}s{{ end }}.
func ({{ $receiver }} *{{ $builder }}) {{ $idsFunc }}({{ if $e.Unique }}id{{ else }}ids ...{{ end }} {{ $e.Type.ID.Type }}) *{{ $builder }} {
{{ $receiver }}.mutation.{{ $idsFunc }}({{ if $e.Unique }}id{{ else }}ids ...{{ end }})
return {{ $receiver }}
}
{{ end }}
{{ if and $e.Unique $e.Optional $withSetter }}
{{ $nillableIDsFunc := print "SetNillable" $e.StructField "ID" }}
// {{ $nillableIDsFunc }} sets the "{{ $e.Name }}" edge to the {{ $e.Type.Name }} entity by ID if the given value is not nil.
func ({{ $receiver }} *{{ $builder }}) {{ $nillableIDsFunc }}(id *{{ $e.Type.ID.Type }}) *{{ $builder }} {
if id != nil {
{{ $receiver}} = {{ $receiver }}.{{ $idsFunc }}(*id)
}
return {{ $receiver }}
}
{{ end }}
{{ $p := lower (printf "%.1s" $e.Type.Name) }}
{{ if eq $p $receiver }} {{ $p = "v" }} {{ end }}
{{ $func := print (pascal $op) $e.StructField }}
// {{ $func }} {{ $op }}s the "{{ $e.Name }}" edge{{if not $e.Unique}}s{{ end }} to the {{ $e.Type.Name }} entity.
func ({{ $receiver }} *{{ $builder }}) {{ $func }}({{ $p }} {{ if not $e.Unique }}...{{ end }}*{{ $e.Type.Name}}) *{{ $builder }} {
{{ if $e.Unique -}}
return {{ $receiver }}.{{ $idsFunc }}({{ $p }}.ID)
{{- else -}}
ids := make([]{{ $e.Type.ID.Type }}, len({{ $p }}))
{{ $i := "i" }}{{ if eq $i $p }}{{ $i = "j" }}{{ end -}}
for {{ $i }} := range {{ $p }} {
ids[{{ $i }}] = {{ $p }}[{{ $i }}].ID
}
return {{ $receiver }}.{{ $idsFunc }}(ids...)
{{- end }}
}
{{ end }}
// Mutation returns the {{ $.MutationName }} object of the builder.
func ({{ $receiver }} *{{ $builder }}) Mutation() *{{ $.MutationName }} {
return {{ $receiver }}.mutation
}
{{ end }}