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