mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
entc/gen: make generated client to implement the ent.Mutator interface (#3161)
This commit is contained in:
@@ -411,6 +411,11 @@ func (m *{{ $mutation }}) Op() Op {
|
||||
return m.op
|
||||
}
|
||||
|
||||
// SetOp allows setting the mutation operation.
|
||||
func (m *{{ $mutation }}) SetOp(op Op) {
|
||||
m.op = op
|
||||
}
|
||||
|
||||
// Type returns the node type of this mutation ({{ $n.Name }}).
|
||||
func (m *{{ $mutation }}) Type() string {
|
||||
return m.typ
|
||||
@@ -479,7 +484,7 @@ func (m *{{ $mutation }}) SetField(name string, value ent.Value) error {
|
||||
if !ok {
|
||||
return fmt.Errorf("unexpected type %T for field %s", value, name)
|
||||
}
|
||||
m.Set{{ $f.StructField }}(v)
|
||||
m.{{ $f.MutationSet }}(v)
|
||||
return nil
|
||||
{{- end }}
|
||||
}
|
||||
|
||||
@@ -26,9 +26,9 @@ in the LICENSE file in the root directory of this source tree.
|
||||
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 }}()
|
||||
{{ $receiver }}.mutation.{{ $f.MutationReset }}()
|
||||
{{- end }}
|
||||
{{ $receiver }}.mutation.{{ $func }}({{ $p }})
|
||||
{{ $receiver }}.mutation.{{ $f.MutationSet }}({{ $p }})
|
||||
return {{ $receiver }}
|
||||
}
|
||||
|
||||
|
||||
@@ -143,6 +143,19 @@ func (c *Client) Use(hooks ...Hook) {
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
|
||||
|
||||
// Mutate implements the ent.Mutator interface.
|
||||
func (c *Client) Mutate(ctx context.Context, m Mutation) (Value, error) {
|
||||
switch m := m.(type) {
|
||||
{{- range $n := $.Nodes }}
|
||||
case *{{ $n.MutationName }}:
|
||||
return c.{{ $n.Name }}.mutate(ctx, m)
|
||||
{{- end }}
|
||||
default:
|
||||
return nil, fmt.Errorf("{{ $pkg }}: unknown mutation type %T", m)
|
||||
}
|
||||
}
|
||||
|
||||
{{ range $n := $.Nodes }}
|
||||
{{ $client := print $n.Name "Client" }}
|
||||
// {{ $client }} is a client for the {{ $n.Name }} schema.
|
||||
@@ -285,6 +298,20 @@ func (c *{{ $client }}) Hooks() []Hook {
|
||||
{{- end }}
|
||||
}
|
||||
|
||||
func (c *{{ $client }}) mutate(ctx context.Context, m *{{ $n.MutationName }}) (Value, error) {
|
||||
switch m.Op() {
|
||||
case OpCreate:
|
||||
return (&{{ $n.CreateName }}{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
|
||||
case OpUpdate:
|
||||
return (&{{ $n.UpdateName }}{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
|
||||
case OpUpdateOne:
|
||||
return (&{{ $n.UpdateOneName }}{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
|
||||
case OpDelete, OpDeleteOne:
|
||||
return (&{{ $n.DeleteName }}{config: c.config, hooks: c.Hooks(), mutation: m}).Exec(ctx)
|
||||
default:
|
||||
return nil, fmt.Errorf("{{ $pkg }}: unknown {{ $n.Name }} mutation op: %q", m.Op())
|
||||
}
|
||||
}
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user