hook.go 13 KB

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