entc/gen: check edge-field on mutation-cleared calls for edges

Fixed #1445
This commit is contained in:
Ariel Mashraki
2021-04-09 18:02:54 +03:00
committed by Ariel Mashraki
parent 106dd53d23
commit 8d7bb7fad6
29 changed files with 178 additions and 160 deletions

File diff suppressed because one or more lines are too long

View File

@@ -218,7 +218,7 @@ func (m *{{ $mutation }}) ID() (id {{ $n.ID.Type }}, exists bool) {
{{ end }}
{{ if $f.Optional }}
{{ $func := print "Clear" $f.StructField }}
{{ $func := $f.MutationClear }}
// {{ $func }} clears the value of the "{{ $f.Name }}" field.
func (m *{{ $mutation }}) {{ $func }}() {
m.{{ $f.BuilderField }} = nil
@@ -228,7 +228,7 @@ func (m *{{ $mutation }}) ID() (id {{ $n.ID.Type }}, exists bool) {
m.clearedFields[{{ $const }}] = struct{}{}
}
{{ $func = print $f.StructField "Cleared" }}
{{ $func = $f.MutationCleared }}
// {{ $func }} returns if the "{{ $f.Name }}" field was cleared in this mutation.
func (m *{{ $mutation }}) {{ $func }}() bool {
_, ok := m.clearedFields[{{ $const }}]
@@ -277,9 +277,9 @@ func (m *{{ $mutation }}) ID() (id {{ $n.ID.Type }}, exists bool) {
}
{{ $func = $e.MutationCleared }}
// {{ $func }} returns if the "{{ $e.Name }}" edge to the {{ $e.Type.Name }} entity was cleared.
// {{ $func }} reports if the "{{ $e.Name }}" edge to the {{ $e.Type.Name }} entity was cleared.
func (m *{{ $mutation }}) {{ $func }}() bool {
return m.cleared{{ $e.BuilderField }}
return {{ with $e.Field }}{{ if .Optional }}m.{{ .MutationCleared }}() || {{ end }}{{ end }}m.cleared{{ $e.BuilderField }}
}
{{ if not $e.Unique }}

View File

@@ -128,7 +128,7 @@ func (c *Client) Close() error {
// Use adds the mutation hooks to all the entity clients.
// In order to add hooks to a specific client, call: `client.Node.Use(...)`.
func (c *Client) Use(hooks ...Hook) {
{{- range $_, $n := $.Nodes }}
{{- range $n := $.Nodes }}
c.{{ $n.Name }}.Use(hooks...)
{{- end }}
}

View File

@@ -956,6 +956,17 @@ func (f Field) MutationSet() string {
return name
}
// MutationClear returns the method name for clearing the field value.
func (f Field) MutationClear() string {
return "Clear" + f.StructField()
}
// MutationCleared returns the method name for indicating if the field
// was cleared in the mutation.
func (f Field) MutationCleared() string {
return f.StructField() + "Cleared"
}
// IsBool returns true if the field is a bool field.
func (f Field) IsBool() bool { return f.Type != nil && f.Type.Type == field.TypeBool }
@@ -1348,7 +1359,10 @@ func (e *Edge) ForeignKey() (*ForeignKey, error) {
//
// Note that the zero value is returned if no field was defined in the schema.
func (e Edge) Field() *Field {
if fk, err := e.ForeignKey(); err == nil {
if !e.OwnFK() {
return nil
}
if fk, err := e.ForeignKey(); err == nil && fk.Field.IsEdgeField() {
return fk.Field
}
return nil