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