hook.go 7.7 KB

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