github.com/crowdsecurity/crowdsec@v1.6.1/pkg/database/ent/hook/hook.go (about)

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