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