mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
entc/gen: move mutation hooks logic to a shared generic function (#3180)
This commit is contained in:
@@ -473,6 +473,40 @@ func (s *selector) BoolX(ctx context.Context) bool {
|
||||
return v
|
||||
}
|
||||
|
||||
// withHooks invokes the builder operation with the given hooks, if any.
|
||||
func withHooks[V Value, M any, PM interface {
|
||||
*M
|
||||
Mutation
|
||||
}](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) {
|
||||
if len(hooks) == 0 {
|
||||
return exec(ctx)
|
||||
}
|
||||
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
||||
mutationT, ok := m.(PM)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
||||
}
|
||||
// Set the mutation to the builder.
|
||||
*mutation = *mutationT
|
||||
return exec(ctx)
|
||||
})
|
||||
for i := len(hooks) - 1; i >= 0; i-- {
|
||||
if hooks[i] == nil {
|
||||
return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
|
||||
}
|
||||
mut = hooks[i](mut)
|
||||
}
|
||||
v, err := mut.Mutate(ctx, mutation)
|
||||
if err != nil {
|
||||
return value, err
|
||||
}
|
||||
nv, ok := v.(V)
|
||||
if !ok {
|
||||
return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation)
|
||||
}
|
||||
return nv, nil
|
||||
}
|
||||
|
||||
// newQueryContext returns a new context with the given QueryContext attached in case it does not exist.
|
||||
func newQueryContext(ctx context.Context, typ, op string) context.Context {
|
||||
if ent.QueryFromContext(ctx) == nil {
|
||||
|
||||
@@ -74,49 +74,7 @@ func (nc *NodeCreate) Mutation() *NodeMutation {
|
||||
|
||||
// Save creates the Node in the database.
|
||||
func (nc *NodeCreate) Save(ctx context.Context) (*Node, error) {
|
||||
var (
|
||||
err error
|
||||
node *Node
|
||||
)
|
||||
if len(nc.hooks) == 0 {
|
||||
if err = nc.check(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
node, err = nc.sqlSave(ctx)
|
||||
} else {
|
||||
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
||||
mutation, ok := m.(*NodeMutation)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
||||
}
|
||||
if err = nc.check(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
nc.mutation = mutation
|
||||
if node, err = nc.sqlSave(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
mutation.id = &node.ID
|
||||
mutation.done = true
|
||||
return node, err
|
||||
})
|
||||
for i := len(nc.hooks) - 1; i >= 0; i-- {
|
||||
if nc.hooks[i] == nil {
|
||||
return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
|
||||
}
|
||||
mut = nc.hooks[i](mut)
|
||||
}
|
||||
v, err := mut.Mutate(ctx, nc.mutation)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
nv, ok := v.(*Node)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected node type %T returned from NodeMutation", v)
|
||||
}
|
||||
node = nv
|
||||
}
|
||||
return node, err
|
||||
return withHooks[*Node, NodeMutation](ctx, nc.sqlSave, nc.mutation, nc.hooks)
|
||||
}
|
||||
|
||||
// SaveX calls Save and panics if Save returns an error.
|
||||
@@ -150,6 +108,9 @@ func (nc *NodeCreate) check() error {
|
||||
}
|
||||
|
||||
func (nc *NodeCreate) sqlSave(ctx context.Context) (*Node, error) {
|
||||
if err := nc.check(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_node, _spec := nc.createSpec()
|
||||
if err := sqlgraph.CreateNode(ctx, nc.driver, _spec); err != nil {
|
||||
if sqlgraph.IsConstraintError(err) {
|
||||
@@ -159,6 +120,8 @@ func (nc *NodeCreate) sqlSave(ctx context.Context) (*Node, error) {
|
||||
}
|
||||
id := _spec.ID.Value.(int64)
|
||||
_node.ID = int(id)
|
||||
nc.mutation.id = &_node.ID
|
||||
nc.mutation.done = true
|
||||
return _node, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,6 @@ package ent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"entgo.io/ent/dialect/sql/sqlgraph"
|
||||
@@ -32,40 +31,7 @@ func (nd *NodeDelete) Where(ps ...predicate.Node) *NodeDelete {
|
||||
|
||||
// Exec executes the deletion query and returns how many vertices were deleted.
|
||||
func (nd *NodeDelete) Exec(ctx context.Context) (int, error) {
|
||||
var (
|
||||
err error
|
||||
affected int
|
||||
)
|
||||
if len(nd.hooks) == 0 {
|
||||
affected, err = nd.sqlExec(ctx)
|
||||
} else {
|
||||
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
||||
mutation, ok := m.(*NodeMutation)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
||||
}
|
||||
nd.mutation = mutation
|
||||
affected, err = nd.sqlExec(ctx)
|
||||
mutation.done = true
|
||||
return affected, err
|
||||
})
|
||||
for i := len(nd.hooks) - 1; i >= 0; i-- {
|
||||
if nd.hooks[i] == nil {
|
||||
return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
|
||||
}
|
||||
mut = nd.hooks[i](mut)
|
||||
}
|
||||
n, err := mut.Mutate(ctx, nd.mutation)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
nv, ok := n.(int)
|
||||
if !ok {
|
||||
return 0, fmt.Errorf("unexpected type %T returned from mutation. expected type: int", n)
|
||||
}
|
||||
affected = nv
|
||||
}
|
||||
return affected, err
|
||||
return withHooks[int, NodeMutation](ctx, nd.sqlExec, nd.mutation, nd.hooks)
|
||||
}
|
||||
|
||||
// ExecX is like Exec, but panics if an error occurs.
|
||||
@@ -98,6 +64,7 @@ func (nd *NodeDelete) sqlExec(ctx context.Context) (int, error) {
|
||||
if err != nil && sqlgraph.IsConstraintError(err) {
|
||||
err = &ConstraintError{msg: err.Error(), wrap: err}
|
||||
}
|
||||
nd.mutation.done = true
|
||||
return affected, err
|
||||
}
|
||||
|
||||
|
||||
@@ -101,34 +101,7 @@ func (nu *NodeUpdate) ClearNext() *NodeUpdate {
|
||||
|
||||
// Save executes the query and returns the number of nodes affected by the update operation.
|
||||
func (nu *NodeUpdate) Save(ctx context.Context) (int, error) {
|
||||
var (
|
||||
err error
|
||||
affected int
|
||||
)
|
||||
if len(nu.hooks) == 0 {
|
||||
affected, err = nu.sqlSave(ctx)
|
||||
} else {
|
||||
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
||||
mutation, ok := m.(*NodeMutation)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
||||
}
|
||||
nu.mutation = mutation
|
||||
affected, err = nu.sqlSave(ctx)
|
||||
mutation.done = true
|
||||
return affected, err
|
||||
})
|
||||
for i := len(nu.hooks) - 1; i >= 0; i-- {
|
||||
if nu.hooks[i] == nil {
|
||||
return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
|
||||
}
|
||||
mut = nu.hooks[i](mut)
|
||||
}
|
||||
if _, err := mut.Mutate(ctx, nu.mutation); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
return affected, err
|
||||
return withHooks[int, NodeMutation](ctx, nu.sqlSave, nu.mutation, nu.hooks)
|
||||
}
|
||||
|
||||
// SaveX is like Save, but panics if an error occurs.
|
||||
@@ -255,6 +228,7 @@ func (nu *NodeUpdate) sqlSave(ctx context.Context) (n int, err error) {
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
nu.mutation.done = true
|
||||
return n, nil
|
||||
}
|
||||
|
||||
@@ -343,40 +317,7 @@ func (nuo *NodeUpdateOne) Select(field string, fields ...string) *NodeUpdateOne
|
||||
|
||||
// Save executes the query and returns the updated Node entity.
|
||||
func (nuo *NodeUpdateOne) Save(ctx context.Context) (*Node, error) {
|
||||
var (
|
||||
err error
|
||||
node *Node
|
||||
)
|
||||
if len(nuo.hooks) == 0 {
|
||||
node, err = nuo.sqlSave(ctx)
|
||||
} else {
|
||||
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
||||
mutation, ok := m.(*NodeMutation)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
||||
}
|
||||
nuo.mutation = mutation
|
||||
node, err = nuo.sqlSave(ctx)
|
||||
mutation.done = true
|
||||
return node, err
|
||||
})
|
||||
for i := len(nuo.hooks) - 1; i >= 0; i-- {
|
||||
if nuo.hooks[i] == nil {
|
||||
return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
|
||||
}
|
||||
mut = nuo.hooks[i](mut)
|
||||
}
|
||||
v, err := mut.Mutate(ctx, nuo.mutation)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
nv, ok := v.(*Node)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected node type %T returned from NodeMutation", v)
|
||||
}
|
||||
node = nv
|
||||
}
|
||||
return node, err
|
||||
return withHooks[*Node, NodeMutation](ctx, nuo.sqlSave, nuo.mutation, nuo.hooks)
|
||||
}
|
||||
|
||||
// SaveX is like Save, but panics if an error occurs.
|
||||
@@ -523,5 +464,6 @@ func (nuo *NodeUpdateOne) sqlSave(ctx context.Context) (_node *Node, err error)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
nuo.mutation.done = true
|
||||
return _node, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user