hook.go 7.2 KB

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