hook.go 5.9 KB

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