withHooks invokes the builder operation with the given hooks, if any.
(ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook)
| 528 | |
| 529 | // withHooks invokes the builder operation with the given hooks, if any. |
| 530 | func withHooks[V Value, M any, PM interface { |
| 531 | *M |
| 532 | Mutation |
| 533 | }](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) { |
| 534 | if len(hooks) == 0 { |
| 535 | return exec(ctx) |
| 536 | } |
| 537 | var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { |
| 538 | mutationT, ok := any(m).(PM) |
| 539 | if !ok { |
| 540 | return nil, fmt.Errorf("unexpected mutation type %T", m) |
| 541 | } |
| 542 | // Set the mutation to the builder. |
| 543 | *mutation = *mutationT |
| 544 | return exec(ctx) |
| 545 | }) |
| 546 | for i := len(hooks) - 1; i >= 0; i-- { |
| 547 | if hooks[i] == nil { |
| 548 | return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)") |
| 549 | } |
| 550 | mut = hooks[i](mut) |
| 551 | } |
| 552 | v, err := mut.Mutate(ctx, mutation) |
| 553 | if err != nil { |
| 554 | return value, err |
| 555 | } |
| 556 | nv, ok := v.(V) |
| 557 | if !ok { |
| 558 | return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation) |
| 559 | } |
| 560 | return nv, nil |
| 561 | } |
| 562 | |
| 563 | // setContextOp returns a new context with the given QueryContext attached (including its op) in case it does not exist. |
| 564 | func setContextOp(ctx context.Context, qc *QueryContext, op string) context.Context { |