{{/*
Copyright 2022-present Ryan SU (github.com/suyuan32). 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.Graph */}}


{{ define "set_not_nil" }}

    {{/* Add the base header for the generated file */}}
    {{ $pkg := base $.Config.Package }}
    {{ template "header" $ }}

    {{/* Loop over all updaters and implement the "SetNotNil" method for all optional fields */}}
    {{ range $n := $.Nodes }}
        {{ range $f := $n.MutableFields }}
            {{ $set := print "Set" $f.StructField }}

            {{ range $updater := list $n.UpdateName $n.UpdateOneName $n.CreateName}}
                // set field if value's pointer is not nil.
                func ({{ $n.Receiver }} *{{ $updater }}) SetNotNil{{ $f.StructField }}(value {{if not (hasPrefix $f.Type.String "[]") }}*{{end}}{{ $f.Type }}) *{{ $updater }} {
                if value != nil  {
                return {{ $n.Receiver }}.{{ $set }}({{if not (hasPrefix $f.Type.String "[]") }}*{{end}}value)
                }
                return {{ $n.Receiver }}
                }
            {{ end }}
        {{ end }}
    {{ end }}
{{ end }}