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

@@ -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

@@ -78,7 +78,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)
//
@@ -93,6 +93,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 {
@@ -102,14 +110,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

@@ -169,7 +169,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)
//
@@ -184,6 +184,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 {
@@ -193,14 +201,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

@@ -169,7 +169,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)
//
@@ -184,6 +184,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 {
@@ -193,14 +201,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

@@ -207,7 +207,7 @@ func TestOldValues(t *testing.T) {
})
}, ent.OpUpdateOne))
// A generic hook (executed on all types).
client.Use(hook.On(func(next ent.Mutator) ent.Mutator {
client.Use(hook.Unless(func(next ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
namer, ok := m.(interface {
OldName(context.Context) (string, error)
@@ -230,7 +230,7 @@ func TestOldValues(t *testing.T) {
require.NoError(t, err)
return value, nil
})
}, ent.OpUpdateOne))
}, ^ent.OpUpdateOne))
a8m := client.User.Create().SetName("a8m").SaveX(ctx)
require.Equal(t, "a8m", a8m.Name)
_, err := client.User.UpdateOne(a8m).SetName("Ariel").SetVersion(a8m.Version).Save(ctx)

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 entv1.Mutation) (entv1.Value, er
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, entv1.Delete|entv1.Create)
//
@@ -54,6 +54,14 @@ func On(hk entv1.Hook, op entv1.Op) entv1.Hook {
}
}
// Unless skips the given hook only for the given operation.
//
// hook.Unless(Log, entv1.Update|entv1.UpdateOne)
//
func Unless(hk entv1.Hook, op entv1.Op) entv1.Hook {
return On(hk, ^op)
}
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []entv1.Hook {
@@ -63,14 +71,12 @@ func On(hk entv1.Hook, op entv1.Op) entv1.Hook {
// }
//
func Reject(op entv1.Op) entv1.Hook {
return func(next entv1.Mutator) entv1.Mutator {
return entv1.MutateFunc(func(ctx context.Context, m entv1.Mutation) (entv1.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(entv1.Mutator) entv1.Mutator {
return entv1.MutateFunc(func(_ context.Context, m entv1.Mutation) (entv1.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

@@ -65,7 +65,7 @@ func (f UserFunc) Mutate(ctx context.Context, m entv2.Mutation) (entv2.Value, er
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, entv2.Delete|entv2.Create)
//
@@ -80,6 +80,14 @@ func On(hk entv2.Hook, op entv2.Op) entv2.Hook {
}
}
// Unless skips the given hook only for the given operation.
//
// hook.Unless(Log, entv2.Update|entv2.UpdateOne)
//
func Unless(hk entv2.Hook, op entv2.Op) entv2.Hook {
return On(hk, ^op)
}
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []entv2.Hook {
@@ -89,14 +97,12 @@ func On(hk entv2.Hook, op entv2.Op) entv2.Hook {
// }
//
func Reject(op entv2.Op) entv2.Hook {
return func(next entv2.Mutator) entv2.Mutator {
return entv2.MutateFunc(func(ctx context.Context, m entv2.Mutation) (entv2.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(entv2.Mutator) entv2.Mutator {
return entv2.MutateFunc(func(_ context.Context, m entv2.Mutation) (entv2.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 PlanetFunc) 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

@@ -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.