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