hook.go 8.1 KB

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