hook.go 9.9 KB

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