hook.go 8.1 KB

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