hook.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // Code generated by entc, DO NOT EDIT.
  2. package hook
  3. import (
  4. "context"
  5. "fmt"
  6. "todo/ent"
  7. )
  8. // The TodoFunc type is an adapter to allow the use of ordinary
  9. // function as Todo mutator.
  10. type TodoFunc func(context.Context, *ent.TodoMutation) (ent.Value, error)
  11. // Mutate calls f(ctx, m).
  12. func (f TodoFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  13. mv, ok := m.(*ent.TodoMutation)
  14. if !ok {
  15. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.TodoMutation", m)
  16. }
  17. return f(ctx, mv)
  18. }
  19. // Condition is a hook condition function.
  20. type Condition func(context.Context, ent.Mutation) bool
  21. // And groups conditions with the AND operator.
  22. func And(first, second Condition, rest ...Condition) Condition {
  23. return func(ctx context.Context, m ent.Mutation) bool {
  24. if !first(ctx, m) || !second(ctx, m) {
  25. return false
  26. }
  27. for _, cond := range rest {
  28. if !cond(ctx, m) {
  29. return false
  30. }
  31. }
  32. return true
  33. }
  34. }
  35. // Or groups conditions with the OR operator.
  36. func Or(first, second Condition, rest ...Condition) Condition {
  37. return func(ctx context.Context, m ent.Mutation) bool {
  38. if first(ctx, m) || second(ctx, m) {
  39. return true
  40. }
  41. for _, cond := range rest {
  42. if cond(ctx, m) {
  43. return true
  44. }
  45. }
  46. return false
  47. }
  48. }
  49. // Not negates a given condition.
  50. func Not(cond Condition) Condition {
  51. return func(ctx context.Context, m ent.Mutation) bool {
  52. return !cond(ctx, m)
  53. }
  54. }
  55. // HasOp is a condition testing mutation operation.
  56. func HasOp(op ent.Op) Condition {
  57. return func(_ context.Context, m ent.Mutation) bool {
  58. return m.Op().Is(op)
  59. }
  60. }
  61. // HasAddedFields is a condition validating `.AddedField` on fields.
  62. func HasAddedFields(field string, fields ...string) Condition {
  63. return func(_ context.Context, m ent.Mutation) bool {
  64. if _, exists := m.AddedField(field); !exists {
  65. return false
  66. }
  67. for _, field := range fields {
  68. if _, exists := m.AddedField(field); !exists {
  69. return false
  70. }
  71. }
  72. return true
  73. }
  74. }
  75. // HasClearedFields is a condition validating `.FieldCleared` on fields.
  76. func HasClearedFields(field string, fields ...string) Condition {
  77. return func(_ context.Context, m ent.Mutation) bool {
  78. if exists := m.FieldCleared(field); !exists {
  79. return false
  80. }
  81. for _, field := range fields {
  82. if exists := m.FieldCleared(field); !exists {
  83. return false
  84. }
  85. }
  86. return true
  87. }
  88. }
  89. // HasFields is a condition validating `.Field` on fields.
  90. func HasFields(field string, fields ...string) Condition {
  91. return func(_ context.Context, m ent.Mutation) bool {
  92. if _, exists := m.Field(field); !exists {
  93. return false
  94. }
  95. for _, field := range fields {
  96. if _, exists := m.Field(field); !exists {
  97. return false
  98. }
  99. }
  100. return true
  101. }
  102. }
  103. // If executes the given hook under condition.
  104. //
  105. // hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...)))
  106. //
  107. func If(hk ent.Hook, cond Condition) ent.Hook {
  108. return func(next ent.Mutator) ent.Mutator {
  109. return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  110. if cond(ctx, m) {
  111. return hk(next).Mutate(ctx, m)
  112. }
  113. return next.Mutate(ctx, m)
  114. })
  115. }
  116. }
  117. // On executes the given hook only for the given operation.
  118. //
  119. // hook.On(Log, ent.Delete|ent.Create)
  120. //
  121. func On(hk ent.Hook, op ent.Op) ent.Hook {
  122. return If(hk, HasOp(op))
  123. }
  124. // Unless skips the given hook only for the given operation.
  125. //
  126. // hook.Unless(Log, ent.Update|ent.UpdateOne)
  127. //
  128. func Unless(hk ent.Hook, op ent.Op) ent.Hook {
  129. return If(hk, Not(HasOp(op)))
  130. }
  131. // FixedError is a hook returning a fixed error.
  132. func FixedError(err error) ent.Hook {
  133. return func(ent.Mutator) ent.Mutator {
  134. return ent.MutateFunc(func(context.Context, ent.Mutation) (ent.Value, error) {
  135. return nil, err
  136. })
  137. }
  138. }
  139. // Reject returns a hook that rejects all operations that match op.
  140. //
  141. // func (T) Hooks() []ent.Hook {
  142. // return []ent.Hook{
  143. // Reject(ent.Delete|ent.Update),
  144. // }
  145. // }
  146. //
  147. func Reject(op ent.Op) ent.Hook {
  148. hk := FixedError(fmt.Errorf("%s operation is not allowed", op))
  149. return On(hk, op)
  150. }
  151. // Chain acts as a list of hooks and is effectively immutable.
  152. // Once created, it will always hold the same set of hooks in the same order.
  153. type Chain struct {
  154. hooks []ent.Hook
  155. }
  156. // NewChain creates a new chain of hooks.
  157. func NewChain(hooks ...ent.Hook) Chain {
  158. return Chain{append([]ent.Hook(nil), hooks...)}
  159. }
  160. // Hook chains the list of hooks and returns the final hook.
  161. func (c Chain) Hook() ent.Hook {
  162. return func(mutator ent.Mutator) ent.Mutator {
  163. for i := len(c.hooks) - 1; i >= 0; i-- {
  164. mutator = c.hooks[i](mutator)
  165. }
  166. return mutator
  167. }
  168. }
  169. // Append extends a chain, adding the specified hook
  170. // as the last ones in the mutation flow.
  171. func (c Chain) Append(hooks ...ent.Hook) Chain {
  172. newHooks := make([]ent.Hook, 0, len(c.hooks)+len(hooks))
  173. newHooks = append(newHooks, c.hooks...)
  174. newHooks = append(newHooks, hooks...)
  175. return Chain{newHooks}
  176. }
  177. // Extend extends a chain, adding the specified chain
  178. // as the last ones in the mutation flow.
  179. func (c Chain) Extend(chain Chain) Chain {
  180. return c.Append(chain.hooks...)
  181. }