hook.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Code generated by entc, DO NOT EDIT.
  2. package hook
  3. import (
  4. "context"
  5. "fmt"
  6. "code.osinet.fr/fgm/entdemo/ent"
  7. )
  8. // The CarFunc type is an adapter to allow the use of ordinary
  9. // function as Car mutator.
  10. type CarFunc func(context.Context, *ent.CarMutation) (ent.Value, error)
  11. // Mutate calls f(ctx, m).
  12. func (f CarFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  13. mv, ok := m.(*ent.CarMutation)
  14. if !ok {
  15. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.CarMutation", m)
  16. }
  17. return f(ctx, mv)
  18. }
  19. // The GroupFunc type is an adapter to allow the use of ordinary
  20. // function as Group mutator.
  21. type GroupFunc func(context.Context, *ent.GroupMutation) (ent.Value, error)
  22. // Mutate calls f(ctx, m).
  23. func (f GroupFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  24. mv, ok := m.(*ent.GroupMutation)
  25. if !ok {
  26. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.GroupMutation", m)
  27. }
  28. return f(ctx, mv)
  29. }
  30. // The UserFunc type is an adapter to allow the use of ordinary
  31. // function as User mutator.
  32. type UserFunc func(context.Context, *ent.UserMutation) (ent.Value, error)
  33. // Mutate calls f(ctx, m).
  34. func (f UserFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  35. mv, ok := m.(*ent.UserMutation)
  36. if !ok {
  37. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.UserMutation", m)
  38. }
  39. return f(ctx, mv)
  40. }
  41. // On executes the given hook only of the given operation.
  42. //
  43. // hook.On(Log, ent.Delete|ent.Create)
  44. //
  45. func On(hk ent.Hook, op ent.Op) ent.Hook {
  46. return func(next ent.Mutator) ent.Mutator {
  47. return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  48. if m.Op().Is(op) {
  49. return hk(next).Mutate(ctx, m)
  50. }
  51. return next.Mutate(ctx, m)
  52. })
  53. }
  54. }
  55. // Reject returns a hook that rejects all operations that match op.
  56. //
  57. // func (T) Hooks() []ent.Hook {
  58. // return []ent.Hook{
  59. // Reject(ent.Delete|ent.Update),
  60. // }
  61. // }
  62. //
  63. func Reject(op ent.Op) ent.Hook {
  64. return func(next ent.Mutator) ent.Mutator {
  65. return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  66. if m.Op().Is(op) {
  67. return nil, fmt.Errorf("%s operation is not allowed", m.Op())
  68. }
  69. return next.Mutate(ctx, m)
  70. })
  71. }
  72. }