entc/gen: adding hook.Unless helper (#550)

This commit is contained in:
Alex Snast
2020-06-16 16:16:02 +03:00
committed by GitHub
parent 47f1a203aa
commit 1f553bc145
28 changed files with 326 additions and 179 deletions

View File

@@ -39,7 +39,7 @@ func (f StreetFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, erro
return f(ctx, mv)
}
// On executes the given hook only of the given operation.
// On executes the given hook only for the given operation.
//
// hook.On(Log, ent.Delete|ent.Create)
//
@@ -54,6 +54,14 @@ func On(hk ent.Hook, op ent.Op) ent.Hook {
}
}
// Unless skips the given hook only for the given operation.
//
// hook.Unless(Log, ent.Update|ent.UpdateOne)
//
func Unless(hk ent.Hook, op ent.Op) ent.Hook {
return On(hk, ^op)
}
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []ent.Hook {
@@ -63,14 +71,12 @@ func On(hk ent.Hook, op ent.Op) ent.Hook {
// }
//
func Reject(op ent.Op) ent.Hook {
return func(next ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if m.Op().Is(op) {
return nil, fmt.Errorf("%s operation is not allowed", m.Op())
}
return next.Mutate(ctx, m)
hk := func(ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(_ context.Context, m ent.Mutation) (ent.Value, error) {
return nil, fmt.Errorf("%s operation is not allowed", m.Op())
})
}
return On(hk, op)
}
// Chain acts as a list of hooks and is effectively immutable.

View File

@@ -26,7 +26,7 @@ func (f UserFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error)
return f(ctx, mv)
}
// On executes the given hook only of the given operation.
// On executes the given hook only for the given operation.
//
// hook.On(Log, ent.Delete|ent.Create)
//
@@ -41,6 +41,14 @@ func On(hk ent.Hook, op ent.Op) ent.Hook {
}
}
// Unless skips the given hook only for the given operation.
//
// hook.Unless(Log, ent.Update|ent.UpdateOne)
//
func Unless(hk ent.Hook, op ent.Op) ent.Hook {
return On(hk, ^op)
}
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []ent.Hook {
@@ -50,14 +58,12 @@ func On(hk ent.Hook, op ent.Op) ent.Hook {
// }
//
func Reject(op ent.Op) ent.Hook {
return func(next ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if m.Op().Is(op) {
return nil, fmt.Errorf("%s operation is not allowed", m.Op())
}
return next.Mutate(ctx, m)
hk := func(ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(_ context.Context, m ent.Mutation) (ent.Value, error) {
return nil, fmt.Errorf("%s operation is not allowed", m.Op())
})
}
return On(hk, op)
}
// Chain acts as a list of hooks and is effectively immutable.

View File

@@ -39,7 +39,7 @@ func (f UserFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error)
return f(ctx, mv)
}
// On executes the given hook only of the given operation.
// On executes the given hook only for the given operation.
//
// hook.On(Log, ent.Delete|ent.Create)
//
@@ -54,6 +54,14 @@ func On(hk ent.Hook, op ent.Op) ent.Hook {
}
}
// Unless skips the given hook only for the given operation.
//
// hook.Unless(Log, ent.Update|ent.UpdateOne)
//
func Unless(hk ent.Hook, op ent.Op) ent.Hook {
return On(hk, ^op)
}
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []ent.Hook {
@@ -63,14 +71,12 @@ func On(hk ent.Hook, op ent.Op) ent.Hook {
// }
//
func Reject(op ent.Op) ent.Hook {
return func(next ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if m.Op().Is(op) {
return nil, fmt.Errorf("%s operation is not allowed", m.Op())
}
return next.Mutate(ctx, m)
hk := func(ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(_ context.Context, m ent.Mutation) (ent.Value, error) {
return nil, fmt.Errorf("%s operation is not allowed", m.Op())
})
}
return On(hk, op)
}
// Chain acts as a list of hooks and is effectively immutable.

View File

@@ -26,7 +26,7 @@ func (f UserFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error)
return f(ctx, mv)
}
// On executes the given hook only of the given operation.
// On executes the given hook only for the given operation.
//
// hook.On(Log, ent.Delete|ent.Create)
//
@@ -41,6 +41,14 @@ func On(hk ent.Hook, op ent.Op) ent.Hook {
}
}
// Unless skips the given hook only for the given operation.
//
// hook.Unless(Log, ent.Update|ent.UpdateOne)
//
func Unless(hk ent.Hook, op ent.Op) ent.Hook {
return On(hk, ^op)
}
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []ent.Hook {
@@ -50,14 +58,12 @@ func On(hk ent.Hook, op ent.Op) ent.Hook {
// }
//
func Reject(op ent.Op) ent.Hook {
return func(next ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if m.Op().Is(op) {
return nil, fmt.Errorf("%s operation is not allowed", m.Op())
}
return next.Mutate(ctx, m)
hk := func(ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(_ context.Context, m ent.Mutation) (ent.Value, error) {
return nil, fmt.Errorf("%s operation is not allowed", m.Op())
})
}
return On(hk, op)
}
// Chain acts as a list of hooks and is effectively immutable.

View File

@@ -26,7 +26,7 @@ func (f UserFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error)
return f(ctx, mv)
}
// On executes the given hook only of the given operation.
// On executes the given hook only for the given operation.
//
// hook.On(Log, ent.Delete|ent.Create)
//
@@ -41,6 +41,14 @@ func On(hk ent.Hook, op ent.Op) ent.Hook {
}
}
// Unless skips the given hook only for the given operation.
//
// hook.Unless(Log, ent.Update|ent.UpdateOne)
//
func Unless(hk ent.Hook, op ent.Op) ent.Hook {
return On(hk, ^op)
}
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []ent.Hook {
@@ -50,14 +58,12 @@ func On(hk ent.Hook, op ent.Op) ent.Hook {
// }
//
func Reject(op ent.Op) ent.Hook {
return func(next ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if m.Op().Is(op) {
return nil, fmt.Errorf("%s operation is not allowed", m.Op())
}
return next.Mutate(ctx, m)
hk := func(ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(_ context.Context, m ent.Mutation) (ent.Value, error) {
return nil, fmt.Errorf("%s operation is not allowed", m.Op())
})
}
return On(hk, op)
}
// Chain acts as a list of hooks and is effectively immutable.

View File

@@ -39,7 +39,7 @@ func (f UserFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error)
return f(ctx, mv)
}
// On executes the given hook only of the given operation.
// On executes the given hook only for the given operation.
//
// hook.On(Log, ent.Delete|ent.Create)
//
@@ -54,6 +54,14 @@ func On(hk ent.Hook, op ent.Op) ent.Hook {
}
}
// Unless skips the given hook only for the given operation.
//
// hook.Unless(Log, ent.Update|ent.UpdateOne)
//
func Unless(hk ent.Hook, op ent.Op) ent.Hook {
return On(hk, ^op)
}
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []ent.Hook {
@@ -63,14 +71,12 @@ func On(hk ent.Hook, op ent.Op) ent.Hook {
// }
//
func Reject(op ent.Op) ent.Hook {
return func(next ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if m.Op().Is(op) {
return nil, fmt.Errorf("%s operation is not allowed", m.Op())
}
return next.Mutate(ctx, m)
hk := func(ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(_ context.Context, m ent.Mutation) (ent.Value, error) {
return nil, fmt.Errorf("%s operation is not allowed", m.Op())
})
}
return On(hk, op)
}
// Chain acts as a list of hooks and is effectively immutable.

View File

@@ -26,7 +26,7 @@ func (f NodeFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error)
return f(ctx, mv)
}
// On executes the given hook only of the given operation.
// On executes the given hook only for the given operation.
//
// hook.On(Log, ent.Delete|ent.Create)
//
@@ -41,6 +41,14 @@ func On(hk ent.Hook, op ent.Op) ent.Hook {
}
}
// Unless skips the given hook only for the given operation.
//
// hook.Unless(Log, ent.Update|ent.UpdateOne)
//
func Unless(hk ent.Hook, op ent.Op) ent.Hook {
return On(hk, ^op)
}
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []ent.Hook {
@@ -50,14 +58,12 @@ func On(hk ent.Hook, op ent.Op) ent.Hook {
// }
//
func Reject(op ent.Op) ent.Hook {
return func(next ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if m.Op().Is(op) {
return nil, fmt.Errorf("%s operation is not allowed", m.Op())
}
return next.Mutate(ctx, m)
hk := func(ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(_ context.Context, m ent.Mutation) (ent.Value, error) {
return nil, fmt.Errorf("%s operation is not allowed", m.Op())
})
}
return On(hk, op)
}
// Chain acts as a list of hooks and is effectively immutable.

View File

@@ -39,7 +39,7 @@ func (f UserFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error)
return f(ctx, mv)
}
// On executes the given hook only of the given operation.
// On executes the given hook only for the given operation.
//
// hook.On(Log, ent.Delete|ent.Create)
//
@@ -54,6 +54,14 @@ func On(hk ent.Hook, op ent.Op) ent.Hook {
}
}
// Unless skips the given hook only for the given operation.
//
// hook.Unless(Log, ent.Update|ent.UpdateOne)
//
func Unless(hk ent.Hook, op ent.Op) ent.Hook {
return On(hk, ^op)
}
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []ent.Hook {
@@ -63,14 +71,12 @@ func On(hk ent.Hook, op ent.Op) ent.Hook {
// }
//
func Reject(op ent.Op) ent.Hook {
return func(next ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if m.Op().Is(op) {
return nil, fmt.Errorf("%s operation is not allowed", m.Op())
}
return next.Mutate(ctx, m)
hk := func(ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(_ context.Context, m ent.Mutation) (ent.Value, error) {
return nil, fmt.Errorf("%s operation is not allowed", m.Op())
})
}
return On(hk, op)
}
// Chain acts as a list of hooks and is effectively immutable.

View File

@@ -26,7 +26,7 @@ func (f UserFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error)
return f(ctx, mv)
}
// On executes the given hook only of the given operation.
// On executes the given hook only for the given operation.
//
// hook.On(Log, ent.Delete|ent.Create)
//
@@ -41,6 +41,14 @@ func On(hk ent.Hook, op ent.Op) ent.Hook {
}
}
// Unless skips the given hook only for the given operation.
//
// hook.Unless(Log, ent.Update|ent.UpdateOne)
//
func Unless(hk ent.Hook, op ent.Op) ent.Hook {
return On(hk, ^op)
}
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []ent.Hook {
@@ -50,14 +58,12 @@ func On(hk ent.Hook, op ent.Op) ent.Hook {
// }
//
func Reject(op ent.Op) ent.Hook {
return func(next ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if m.Op().Is(op) {
return nil, fmt.Errorf("%s operation is not allowed", m.Op())
}
return next.Mutate(ctx, m)
hk := func(ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(_ context.Context, m ent.Mutation) (ent.Value, error) {
return nil, fmt.Errorf("%s operation is not allowed", m.Op())
})
}
return On(hk, op)
}
// Chain acts as a list of hooks and is effectively immutable.

View File

@@ -26,7 +26,7 @@ func (f NodeFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error)
return f(ctx, mv)
}
// On executes the given hook only of the given operation.
// On executes the given hook only for the given operation.
//
// hook.On(Log, ent.Delete|ent.Create)
//
@@ -41,6 +41,14 @@ func On(hk ent.Hook, op ent.Op) ent.Hook {
}
}
// Unless skips the given hook only for the given operation.
//
// hook.Unless(Log, ent.Update|ent.UpdateOne)
//
func Unless(hk ent.Hook, op ent.Op) ent.Hook {
return On(hk, ^op)
}
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []ent.Hook {
@@ -50,14 +58,12 @@ func On(hk ent.Hook, op ent.Op) ent.Hook {
// }
//
func Reject(op ent.Op) ent.Hook {
return func(next ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if m.Op().Is(op) {
return nil, fmt.Errorf("%s operation is not allowed", m.Op())
}
return next.Mutate(ctx, m)
hk := func(ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(_ context.Context, m ent.Mutation) (ent.Value, error) {
return nil, fmt.Errorf("%s operation is not allowed", m.Op())
})
}
return On(hk, op)
}
// Chain acts as a list of hooks and is effectively immutable.

View File

@@ -52,7 +52,7 @@ func (f UserFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error)
return f(ctx, mv)
}
// On executes the given hook only of the given operation.
// On executes the given hook only for the given operation.
//
// hook.On(Log, ent.Delete|ent.Create)
//
@@ -67,6 +67,14 @@ func On(hk ent.Hook, op ent.Op) ent.Hook {
}
}
// Unless skips the given hook only for the given operation.
//
// hook.Unless(Log, ent.Update|ent.UpdateOne)
//
func Unless(hk ent.Hook, op ent.Op) ent.Hook {
return On(hk, ^op)
}
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []ent.Hook {
@@ -76,14 +84,12 @@ func On(hk ent.Hook, op ent.Op) ent.Hook {
// }
//
func Reject(op ent.Op) ent.Hook {
return func(next ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if m.Op().Is(op) {
return nil, fmt.Errorf("%s operation is not allowed", m.Op())
}
return next.Mutate(ctx, m)
hk := func(ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(_ context.Context, m ent.Mutation) (ent.Value, error) {
return nil, fmt.Errorf("%s operation is not allowed", m.Op())
})
}
return On(hk, op)
}
// Chain acts as a list of hooks and is effectively immutable.

View File

@@ -52,7 +52,7 @@ func (f UserFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error)
return f(ctx, mv)
}
// On executes the given hook only of the given operation.
// On executes the given hook only for the given operation.
//
// hook.On(Log, ent.Delete|ent.Create)
//
@@ -67,6 +67,14 @@ func On(hk ent.Hook, op ent.Op) ent.Hook {
}
}
// Unless skips the given hook only for the given operation.
//
// hook.Unless(Log, ent.Update|ent.UpdateOne)
//
func Unless(hk ent.Hook, op ent.Op) ent.Hook {
return On(hk, ^op)
}
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []ent.Hook {
@@ -76,14 +84,12 @@ func On(hk ent.Hook, op ent.Op) ent.Hook {
// }
//
func Reject(op ent.Op) ent.Hook {
return func(next ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if m.Op().Is(op) {
return nil, fmt.Errorf("%s operation is not allowed", m.Op())
}
return next.Mutate(ctx, m)
hk := func(ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(_ context.Context, m ent.Mutation) (ent.Value, error) {
return nil, fmt.Errorf("%s operation is not allowed", m.Op())
})
}
return On(hk, op)
}
// Chain acts as a list of hooks and is effectively immutable.