entc/gen: nillable update setters for non-optional fields (#3788)

This commit is contained in:
Ariel Mashraki
2023-10-12 21:26:07 +03:00
committed by GitHub
parent 5368e5c54e
commit 8a848a6475
78 changed files with 1831 additions and 2 deletions

View File

@@ -11,8 +11,10 @@ in the LICENSE file in the root directory of this source tree.
{{ $receiver := $.Scope.Receiver }}
{{ $fields := $.Fields }}
{{ $updater := false }}
{{ $creator := true }}
{{- if or (hasSuffix $builder "Update") (hasSuffix $builder "UpdateOne") }}
{{ $updater = true }}
{{ $creator = false }}
{{ $fields = $.MutableFields }}
{{- else if $.HasCompositeID }}
{{- else if $.ID.UserDefined }}
@@ -33,8 +35,11 @@ in the LICENSE file in the root directory of this source tree.
}
{{/* 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 }}
{{ $nillableC := and $creator (or $f.Optional $f.Default) }}
{{ $nillableU := and $updater (not $f.UpdateDefault) }}
{{ $nillableFunc := print "SetNillable" $f.StructField }}
{{ $skipNillable := false }}{{ range $fields }}{{ if and (ne .Name $f.Name) (eq .MutationSet $nillableFunc) }}{{ $skipNillable = true }}{{ break }}{{ end }}{{ end }}
{{ if and (not $f.Type.Nillable) (not $skipNillable) (or $nillableC $nillableU) }}
// {{ $nillableFunc }} sets the "{{ $f.Name }}" field if the given value is not nil.
func ({{ $receiver }} *{{ $builder }}) {{ $nillableFunc }}({{ $p }} *{{ $f.Type }}) *{{ $builder }} {
if {{ $p }} != nil {