hook.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. // Code generated by ent, DO NOT EDIT.
  2. package hook
  3. import (
  4. "context"
  5. "fmt"
  6. "github.com/Wei-Shaw/sub2api/ent"
  7. )
  8. // The APIKeyFunc type is an adapter to allow the use of ordinary
  9. // function as APIKey mutator.
  10. type APIKeyFunc func(context.Context, *ent.APIKeyMutation) (ent.Value, error)
  11. // Mutate calls f(ctx, m).
  12. func (f APIKeyFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  13. if mv, ok := m.(*ent.APIKeyMutation); ok {
  14. return f(ctx, mv)
  15. }
  16. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.APIKeyMutation", m)
  17. }
  18. // The AccountFunc type is an adapter to allow the use of ordinary
  19. // function as Account mutator.
  20. type AccountFunc func(context.Context, *ent.AccountMutation) (ent.Value, error)
  21. // Mutate calls f(ctx, m).
  22. func (f AccountFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  23. if mv, ok := m.(*ent.AccountMutation); ok {
  24. return f(ctx, mv)
  25. }
  26. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.AccountMutation", m)
  27. }
  28. // The AccountGroupFunc type is an adapter to allow the use of ordinary
  29. // function as AccountGroup mutator.
  30. type AccountGroupFunc func(context.Context, *ent.AccountGroupMutation) (ent.Value, error)
  31. // Mutate calls f(ctx, m).
  32. func (f AccountGroupFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  33. if mv, ok := m.(*ent.AccountGroupMutation); ok {
  34. return f(ctx, mv)
  35. }
  36. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.AccountGroupMutation", m)
  37. }
  38. // The AnnouncementFunc type is an adapter to allow the use of ordinary
  39. // function as Announcement mutator.
  40. type AnnouncementFunc func(context.Context, *ent.AnnouncementMutation) (ent.Value, error)
  41. // Mutate calls f(ctx, m).
  42. func (f AnnouncementFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  43. if mv, ok := m.(*ent.AnnouncementMutation); ok {
  44. return f(ctx, mv)
  45. }
  46. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.AnnouncementMutation", m)
  47. }
  48. // The AnnouncementReadFunc type is an adapter to allow the use of ordinary
  49. // function as AnnouncementRead mutator.
  50. type AnnouncementReadFunc func(context.Context, *ent.AnnouncementReadMutation) (ent.Value, error)
  51. // Mutate calls f(ctx, m).
  52. func (f AnnouncementReadFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  53. if mv, ok := m.(*ent.AnnouncementReadMutation); ok {
  54. return f(ctx, mv)
  55. }
  56. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.AnnouncementReadMutation", m)
  57. }
  58. // The AuthIdentityFunc type is an adapter to allow the use of ordinary
  59. // function as AuthIdentity mutator.
  60. type AuthIdentityFunc func(context.Context, *ent.AuthIdentityMutation) (ent.Value, error)
  61. // Mutate calls f(ctx, m).
  62. func (f AuthIdentityFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  63. if mv, ok := m.(*ent.AuthIdentityMutation); ok {
  64. return f(ctx, mv)
  65. }
  66. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.AuthIdentityMutation", m)
  67. }
  68. // The AuthIdentityChannelFunc type is an adapter to allow the use of ordinary
  69. // function as AuthIdentityChannel mutator.
  70. type AuthIdentityChannelFunc func(context.Context, *ent.AuthIdentityChannelMutation) (ent.Value, error)
  71. // Mutate calls f(ctx, m).
  72. func (f AuthIdentityChannelFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  73. if mv, ok := m.(*ent.AuthIdentityChannelMutation); ok {
  74. return f(ctx, mv)
  75. }
  76. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.AuthIdentityChannelMutation", m)
  77. }
  78. // The BatchImageEventFunc type is an adapter to allow the use of ordinary
  79. // function as BatchImageEvent mutator.
  80. type BatchImageEventFunc func(context.Context, *ent.BatchImageEventMutation) (ent.Value, error)
  81. // Mutate calls f(ctx, m).
  82. func (f BatchImageEventFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  83. if mv, ok := m.(*ent.BatchImageEventMutation); ok {
  84. return f(ctx, mv)
  85. }
  86. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.BatchImageEventMutation", m)
  87. }
  88. // The BatchImageItemFunc type is an adapter to allow the use of ordinary
  89. // function as BatchImageItem mutator.
  90. type BatchImageItemFunc func(context.Context, *ent.BatchImageItemMutation) (ent.Value, error)
  91. // Mutate calls f(ctx, m).
  92. func (f BatchImageItemFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  93. if mv, ok := m.(*ent.BatchImageItemMutation); ok {
  94. return f(ctx, mv)
  95. }
  96. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.BatchImageItemMutation", m)
  97. }
  98. // The BatchImageJobFunc type is an adapter to allow the use of ordinary
  99. // function as BatchImageJob mutator.
  100. type BatchImageJobFunc func(context.Context, *ent.BatchImageJobMutation) (ent.Value, error)
  101. // Mutate calls f(ctx, m).
  102. func (f BatchImageJobFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  103. if mv, ok := m.(*ent.BatchImageJobMutation); ok {
  104. return f(ctx, mv)
  105. }
  106. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.BatchImageJobMutation", m)
  107. }
  108. // The ChannelMonitorFunc type is an adapter to allow the use of ordinary
  109. // function as ChannelMonitor mutator.
  110. type ChannelMonitorFunc func(context.Context, *ent.ChannelMonitorMutation) (ent.Value, error)
  111. // Mutate calls f(ctx, m).
  112. func (f ChannelMonitorFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  113. if mv, ok := m.(*ent.ChannelMonitorMutation); ok {
  114. return f(ctx, mv)
  115. }
  116. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.ChannelMonitorMutation", m)
  117. }
  118. // The ChannelMonitorDailyRollupFunc type is an adapter to allow the use of ordinary
  119. // function as ChannelMonitorDailyRollup mutator.
  120. type ChannelMonitorDailyRollupFunc func(context.Context, *ent.ChannelMonitorDailyRollupMutation) (ent.Value, error)
  121. // Mutate calls f(ctx, m).
  122. func (f ChannelMonitorDailyRollupFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  123. if mv, ok := m.(*ent.ChannelMonitorDailyRollupMutation); ok {
  124. return f(ctx, mv)
  125. }
  126. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.ChannelMonitorDailyRollupMutation", m)
  127. }
  128. // The ChannelMonitorHistoryFunc type is an adapter to allow the use of ordinary
  129. // function as ChannelMonitorHistory mutator.
  130. type ChannelMonitorHistoryFunc func(context.Context, *ent.ChannelMonitorHistoryMutation) (ent.Value, error)
  131. // Mutate calls f(ctx, m).
  132. func (f ChannelMonitorHistoryFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  133. if mv, ok := m.(*ent.ChannelMonitorHistoryMutation); ok {
  134. return f(ctx, mv)
  135. }
  136. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.ChannelMonitorHistoryMutation", m)
  137. }
  138. // The ChannelMonitorRequestTemplateFunc type is an adapter to allow the use of ordinary
  139. // function as ChannelMonitorRequestTemplate mutator.
  140. type ChannelMonitorRequestTemplateFunc func(context.Context, *ent.ChannelMonitorRequestTemplateMutation) (ent.Value, error)
  141. // Mutate calls f(ctx, m).
  142. func (f ChannelMonitorRequestTemplateFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  143. if mv, ok := m.(*ent.ChannelMonitorRequestTemplateMutation); ok {
  144. return f(ctx, mv)
  145. }
  146. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.ChannelMonitorRequestTemplateMutation", m)
  147. }
  148. // The CompositeModelRouteFunc type is an adapter to allow the use of ordinary
  149. // function as CompositeModelRoute mutator.
  150. type CompositeModelRouteFunc func(context.Context, *ent.CompositeModelRouteMutation) (ent.Value, error)
  151. // Mutate calls f(ctx, m).
  152. func (f CompositeModelRouteFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  153. if mv, ok := m.(*ent.CompositeModelRouteMutation); ok {
  154. return f(ctx, mv)
  155. }
  156. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.CompositeModelRouteMutation", m)
  157. }
  158. // The ErrorPassthroughRuleFunc type is an adapter to allow the use of ordinary
  159. // function as ErrorPassthroughRule mutator.
  160. type ErrorPassthroughRuleFunc func(context.Context, *ent.ErrorPassthroughRuleMutation) (ent.Value, error)
  161. // Mutate calls f(ctx, m).
  162. func (f ErrorPassthroughRuleFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  163. if mv, ok := m.(*ent.ErrorPassthroughRuleMutation); ok {
  164. return f(ctx, mv)
  165. }
  166. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.ErrorPassthroughRuleMutation", m)
  167. }
  168. // The GroupFunc type is an adapter to allow the use of ordinary
  169. // function as Group mutator.
  170. type GroupFunc func(context.Context, *ent.GroupMutation) (ent.Value, error)
  171. // Mutate calls f(ctx, m).
  172. func (f GroupFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  173. if mv, ok := m.(*ent.GroupMutation); ok {
  174. return f(ctx, mv)
  175. }
  176. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.GroupMutation", m)
  177. }
  178. // The IdempotencyRecordFunc type is an adapter to allow the use of ordinary
  179. // function as IdempotencyRecord mutator.
  180. type IdempotencyRecordFunc func(context.Context, *ent.IdempotencyRecordMutation) (ent.Value, error)
  181. // Mutate calls f(ctx, m).
  182. func (f IdempotencyRecordFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  183. if mv, ok := m.(*ent.IdempotencyRecordMutation); ok {
  184. return f(ctx, mv)
  185. }
  186. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.IdempotencyRecordMutation", m)
  187. }
  188. // The IdentityAdoptionDecisionFunc type is an adapter to allow the use of ordinary
  189. // function as IdentityAdoptionDecision mutator.
  190. type IdentityAdoptionDecisionFunc func(context.Context, *ent.IdentityAdoptionDecisionMutation) (ent.Value, error)
  191. // Mutate calls f(ctx, m).
  192. func (f IdentityAdoptionDecisionFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  193. if mv, ok := m.(*ent.IdentityAdoptionDecisionMutation); ok {
  194. return f(ctx, mv)
  195. }
  196. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.IdentityAdoptionDecisionMutation", m)
  197. }
  198. // The PaymentAuditLogFunc type is an adapter to allow the use of ordinary
  199. // function as PaymentAuditLog mutator.
  200. type PaymentAuditLogFunc func(context.Context, *ent.PaymentAuditLogMutation) (ent.Value, error)
  201. // Mutate calls f(ctx, m).
  202. func (f PaymentAuditLogFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  203. if mv, ok := m.(*ent.PaymentAuditLogMutation); ok {
  204. return f(ctx, mv)
  205. }
  206. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.PaymentAuditLogMutation", m)
  207. }
  208. // The PaymentOrderFunc type is an adapter to allow the use of ordinary
  209. // function as PaymentOrder mutator.
  210. type PaymentOrderFunc func(context.Context, *ent.PaymentOrderMutation) (ent.Value, error)
  211. // Mutate calls f(ctx, m).
  212. func (f PaymentOrderFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  213. if mv, ok := m.(*ent.PaymentOrderMutation); ok {
  214. return f(ctx, mv)
  215. }
  216. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.PaymentOrderMutation", m)
  217. }
  218. // The PaymentProviderInstanceFunc type is an adapter to allow the use of ordinary
  219. // function as PaymentProviderInstance mutator.
  220. type PaymentProviderInstanceFunc func(context.Context, *ent.PaymentProviderInstanceMutation) (ent.Value, error)
  221. // Mutate calls f(ctx, m).
  222. func (f PaymentProviderInstanceFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  223. if mv, ok := m.(*ent.PaymentProviderInstanceMutation); ok {
  224. return f(ctx, mv)
  225. }
  226. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.PaymentProviderInstanceMutation", m)
  227. }
  228. // The PendingAuthSessionFunc type is an adapter to allow the use of ordinary
  229. // function as PendingAuthSession mutator.
  230. type PendingAuthSessionFunc func(context.Context, *ent.PendingAuthSessionMutation) (ent.Value, error)
  231. // Mutate calls f(ctx, m).
  232. func (f PendingAuthSessionFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  233. if mv, ok := m.(*ent.PendingAuthSessionMutation); ok {
  234. return f(ctx, mv)
  235. }
  236. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.PendingAuthSessionMutation", m)
  237. }
  238. // The PromoCodeFunc type is an adapter to allow the use of ordinary
  239. // function as PromoCode mutator.
  240. type PromoCodeFunc func(context.Context, *ent.PromoCodeMutation) (ent.Value, error)
  241. // Mutate calls f(ctx, m).
  242. func (f PromoCodeFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  243. if mv, ok := m.(*ent.PromoCodeMutation); ok {
  244. return f(ctx, mv)
  245. }
  246. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.PromoCodeMutation", m)
  247. }
  248. // The PromoCodeUsageFunc type is an adapter to allow the use of ordinary
  249. // function as PromoCodeUsage mutator.
  250. type PromoCodeUsageFunc func(context.Context, *ent.PromoCodeUsageMutation) (ent.Value, error)
  251. // Mutate calls f(ctx, m).
  252. func (f PromoCodeUsageFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  253. if mv, ok := m.(*ent.PromoCodeUsageMutation); ok {
  254. return f(ctx, mv)
  255. }
  256. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.PromoCodeUsageMutation", m)
  257. }
  258. // The ProxyFunc type is an adapter to allow the use of ordinary
  259. // function as Proxy mutator.
  260. type ProxyFunc func(context.Context, *ent.ProxyMutation) (ent.Value, error)
  261. // Mutate calls f(ctx, m).
  262. func (f ProxyFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  263. if mv, ok := m.(*ent.ProxyMutation); ok {
  264. return f(ctx, mv)
  265. }
  266. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.ProxyMutation", m)
  267. }
  268. // The RedeemCodeFunc type is an adapter to allow the use of ordinary
  269. // function as RedeemCode mutator.
  270. type RedeemCodeFunc func(context.Context, *ent.RedeemCodeMutation) (ent.Value, error)
  271. // Mutate calls f(ctx, m).
  272. func (f RedeemCodeFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  273. if mv, ok := m.(*ent.RedeemCodeMutation); ok {
  274. return f(ctx, mv)
  275. }
  276. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.RedeemCodeMutation", m)
  277. }
  278. // The SecurityPolicyKeywordFunc type is an adapter to allow the use of ordinary
  279. // function as SecurityPolicyKeyword mutator.
  280. type SecurityPolicyKeywordFunc func(context.Context, *ent.SecurityPolicyKeywordMutation) (ent.Value, error)
  281. // Mutate calls f(ctx, m).
  282. func (f SecurityPolicyKeywordFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  283. if mv, ok := m.(*ent.SecurityPolicyKeywordMutation); ok {
  284. return f(ctx, mv)
  285. }
  286. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.SecurityPolicyKeywordMutation", m)
  287. }
  288. // The SecuritySecretFunc type is an adapter to allow the use of ordinary
  289. // function as SecuritySecret mutator.
  290. type SecuritySecretFunc func(context.Context, *ent.SecuritySecretMutation) (ent.Value, error)
  291. // Mutate calls f(ctx, m).
  292. func (f SecuritySecretFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  293. if mv, ok := m.(*ent.SecuritySecretMutation); ok {
  294. return f(ctx, mv)
  295. }
  296. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.SecuritySecretMutation", m)
  297. }
  298. // The SettingFunc type is an adapter to allow the use of ordinary
  299. // function as Setting mutator.
  300. type SettingFunc func(context.Context, *ent.SettingMutation) (ent.Value, error)
  301. // Mutate calls f(ctx, m).
  302. func (f SettingFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  303. if mv, ok := m.(*ent.SettingMutation); ok {
  304. return f(ctx, mv)
  305. }
  306. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.SettingMutation", m)
  307. }
  308. // The SubscriptionPlanFunc type is an adapter to allow the use of ordinary
  309. // function as SubscriptionPlan mutator.
  310. type SubscriptionPlanFunc func(context.Context, *ent.SubscriptionPlanMutation) (ent.Value, error)
  311. // Mutate calls f(ctx, m).
  312. func (f SubscriptionPlanFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  313. if mv, ok := m.(*ent.SubscriptionPlanMutation); ok {
  314. return f(ctx, mv)
  315. }
  316. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.SubscriptionPlanMutation", m)
  317. }
  318. // The TLSFingerprintProfileFunc type is an adapter to allow the use of ordinary
  319. // function as TLSFingerprintProfile mutator.
  320. type TLSFingerprintProfileFunc func(context.Context, *ent.TLSFingerprintProfileMutation) (ent.Value, error)
  321. // Mutate calls f(ctx, m).
  322. func (f TLSFingerprintProfileFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  323. if mv, ok := m.(*ent.TLSFingerprintProfileMutation); ok {
  324. return f(ctx, mv)
  325. }
  326. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.TLSFingerprintProfileMutation", m)
  327. }
  328. // The UsageCleanupTaskFunc type is an adapter to allow the use of ordinary
  329. // function as UsageCleanupTask mutator.
  330. type UsageCleanupTaskFunc func(context.Context, *ent.UsageCleanupTaskMutation) (ent.Value, error)
  331. // Mutate calls f(ctx, m).
  332. func (f UsageCleanupTaskFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  333. if mv, ok := m.(*ent.UsageCleanupTaskMutation); ok {
  334. return f(ctx, mv)
  335. }
  336. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.UsageCleanupTaskMutation", m)
  337. }
  338. // The UsageLogFunc type is an adapter to allow the use of ordinary
  339. // function as UsageLog mutator.
  340. type UsageLogFunc func(context.Context, *ent.UsageLogMutation) (ent.Value, error)
  341. // Mutate calls f(ctx, m).
  342. func (f UsageLogFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  343. if mv, ok := m.(*ent.UsageLogMutation); ok {
  344. return f(ctx, mv)
  345. }
  346. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.UsageLogMutation", m)
  347. }
  348. // The UserFunc type is an adapter to allow the use of ordinary
  349. // function as User mutator.
  350. type UserFunc func(context.Context, *ent.UserMutation) (ent.Value, error)
  351. // Mutate calls f(ctx, m).
  352. func (f UserFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  353. if mv, ok := m.(*ent.UserMutation); ok {
  354. return f(ctx, mv)
  355. }
  356. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.UserMutation", m)
  357. }
  358. // The UserAllowedGroupFunc type is an adapter to allow the use of ordinary
  359. // function as UserAllowedGroup mutator.
  360. type UserAllowedGroupFunc func(context.Context, *ent.UserAllowedGroupMutation) (ent.Value, error)
  361. // Mutate calls f(ctx, m).
  362. func (f UserAllowedGroupFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  363. if mv, ok := m.(*ent.UserAllowedGroupMutation); ok {
  364. return f(ctx, mv)
  365. }
  366. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.UserAllowedGroupMutation", m)
  367. }
  368. // The UserAttributeDefinitionFunc type is an adapter to allow the use of ordinary
  369. // function as UserAttributeDefinition mutator.
  370. type UserAttributeDefinitionFunc func(context.Context, *ent.UserAttributeDefinitionMutation) (ent.Value, error)
  371. // Mutate calls f(ctx, m).
  372. func (f UserAttributeDefinitionFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  373. if mv, ok := m.(*ent.UserAttributeDefinitionMutation); ok {
  374. return f(ctx, mv)
  375. }
  376. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.UserAttributeDefinitionMutation", m)
  377. }
  378. // The UserAttributeValueFunc type is an adapter to allow the use of ordinary
  379. // function as UserAttributeValue mutator.
  380. type UserAttributeValueFunc func(context.Context, *ent.UserAttributeValueMutation) (ent.Value, error)
  381. // Mutate calls f(ctx, m).
  382. func (f UserAttributeValueFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  383. if mv, ok := m.(*ent.UserAttributeValueMutation); ok {
  384. return f(ctx, mv)
  385. }
  386. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.UserAttributeValueMutation", m)
  387. }
  388. // The UserPlatformQuotaFunc type is an adapter to allow the use of ordinary
  389. // function as UserPlatformQuota mutator.
  390. type UserPlatformQuotaFunc func(context.Context, *ent.UserPlatformQuotaMutation) (ent.Value, error)
  391. // Mutate calls f(ctx, m).
  392. func (f UserPlatformQuotaFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  393. if mv, ok := m.(*ent.UserPlatformQuotaMutation); ok {
  394. return f(ctx, mv)
  395. }
  396. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.UserPlatformQuotaMutation", m)
  397. }
  398. // The UserSubscriptionFunc type is an adapter to allow the use of ordinary
  399. // function as UserSubscription mutator.
  400. type UserSubscriptionFunc func(context.Context, *ent.UserSubscriptionMutation) (ent.Value, error)
  401. // Mutate calls f(ctx, m).
  402. func (f UserSubscriptionFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  403. if mv, ok := m.(*ent.UserSubscriptionMutation); ok {
  404. return f(ctx, mv)
  405. }
  406. return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.UserSubscriptionMutation", m)
  407. }
  408. // Condition is a hook condition function.
  409. type Condition func(context.Context, ent.Mutation) bool
  410. // And groups conditions with the AND operator.
  411. func And(first, second Condition, rest ...Condition) Condition {
  412. return func(ctx context.Context, m ent.Mutation) bool {
  413. if !first(ctx, m) || !second(ctx, m) {
  414. return false
  415. }
  416. for _, cond := range rest {
  417. if !cond(ctx, m) {
  418. return false
  419. }
  420. }
  421. return true
  422. }
  423. }
  424. // Or groups conditions with the OR operator.
  425. func Or(first, second Condition, rest ...Condition) Condition {
  426. return func(ctx context.Context, m ent.Mutation) bool {
  427. if first(ctx, m) || second(ctx, m) {
  428. return true
  429. }
  430. for _, cond := range rest {
  431. if cond(ctx, m) {
  432. return true
  433. }
  434. }
  435. return false
  436. }
  437. }
  438. // Not negates a given condition.
  439. func Not(cond Condition) Condition {
  440. return func(ctx context.Context, m ent.Mutation) bool {
  441. return !cond(ctx, m)
  442. }
  443. }
  444. // HasOp is a condition testing mutation operation.
  445. func HasOp(op ent.Op) Condition {
  446. return func(_ context.Context, m ent.Mutation) bool {
  447. return m.Op().Is(op)
  448. }
  449. }
  450. // HasAddedFields is a condition validating `.AddedField` on fields.
  451. func HasAddedFields(field string, fields ...string) Condition {
  452. return func(_ context.Context, m ent.Mutation) bool {
  453. if _, exists := m.AddedField(field); !exists {
  454. return false
  455. }
  456. for _, field := range fields {
  457. if _, exists := m.AddedField(field); !exists {
  458. return false
  459. }
  460. }
  461. return true
  462. }
  463. }
  464. // HasClearedFields is a condition validating `.FieldCleared` on fields.
  465. func HasClearedFields(field string, fields ...string) Condition {
  466. return func(_ context.Context, m ent.Mutation) bool {
  467. if exists := m.FieldCleared(field); !exists {
  468. return false
  469. }
  470. for _, field := range fields {
  471. if exists := m.FieldCleared(field); !exists {
  472. return false
  473. }
  474. }
  475. return true
  476. }
  477. }
  478. // HasFields is a condition validating `.Field` on fields.
  479. func HasFields(field string, fields ...string) Condition {
  480. return func(_ context.Context, m ent.Mutation) bool {
  481. if _, exists := m.Field(field); !exists {
  482. return false
  483. }
  484. for _, field := range fields {
  485. if _, exists := m.Field(field); !exists {
  486. return false
  487. }
  488. }
  489. return true
  490. }
  491. }
  492. // If executes the given hook under condition.
  493. //
  494. // hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...)))
  495. func If(hk ent.Hook, cond Condition) ent.Hook {
  496. return func(next ent.Mutator) ent.Mutator {
  497. return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
  498. if cond(ctx, m) {
  499. return hk(next).Mutate(ctx, m)
  500. }
  501. return next.Mutate(ctx, m)
  502. })
  503. }
  504. }
  505. // On executes the given hook only for the given operation.
  506. //
  507. // hook.On(Log, ent.Delete|ent.Create)
  508. func On(hk ent.Hook, op ent.Op) ent.Hook {
  509. return If(hk, HasOp(op))
  510. }
  511. // Unless skips the given hook only for the given operation.
  512. //
  513. // hook.Unless(Log, ent.Update|ent.UpdateOne)
  514. func Unless(hk ent.Hook, op ent.Op) ent.Hook {
  515. return If(hk, Not(HasOp(op)))
  516. }
  517. // FixedError is a hook returning a fixed error.
  518. func FixedError(err error) ent.Hook {
  519. return func(ent.Mutator) ent.Mutator {
  520. return ent.MutateFunc(func(context.Context, ent.Mutation) (ent.Value, error) {
  521. return nil, err
  522. })
  523. }
  524. }
  525. // Reject returns a hook that rejects all operations that match op.
  526. //
  527. // func (T) Hooks() []ent.Hook {
  528. // return []ent.Hook{
  529. // Reject(ent.Delete|ent.Update),
  530. // }
  531. // }
  532. func Reject(op ent.Op) ent.Hook {
  533. hk := FixedError(fmt.Errorf("%s operation is not allowed", op))
  534. return On(hk, op)
  535. }
  536. // Chain acts as a list of hooks and is effectively immutable.
  537. // Once created, it will always hold the same set of hooks in the same order.
  538. type Chain struct {
  539. hooks []ent.Hook
  540. }
  541. // NewChain creates a new chain of hooks.
  542. func NewChain(hooks ...ent.Hook) Chain {
  543. return Chain{append([]ent.Hook(nil), hooks...)}
  544. }
  545. // Hook chains the list of hooks and returns the final hook.
  546. func (c Chain) Hook() ent.Hook {
  547. return func(mutator ent.Mutator) ent.Mutator {
  548. for i := len(c.hooks) - 1; i >= 0; i-- {
  549. mutator = c.hooks[i](mutator)
  550. }
  551. return mutator
  552. }
  553. }
  554. // Append extends a chain, adding the specified hook
  555. // as the last ones in the mutation flow.
  556. func (c Chain) Append(hooks ...ent.Hook) Chain {
  557. newHooks := make([]ent.Hook, 0, len(c.hooks)+len(hooks))
  558. newHooks = append(newHooks, c.hooks...)
  559. newHooks = append(newHooks, hooks...)
  560. return Chain{newHooks}
  561. }
  562. // Extend extends a chain, adding the specified chain
  563. // as the last ones in the mutation flow.
  564. func (c Chain) Extend(chain Chain) Chain {
  565. return c.Append(chain.hooks...)
  566. }