hook.go 5.4 KB

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