entc/gen: add hook.FixedError helper (#895)

This commit is contained in:
Alex Snast
2020-10-28 14:25:09 +02:00
committed by GitHub
parent 967c10819a
commit c5fae0e751
27 changed files with 263 additions and 132 deletions

File diff suppressed because one or more lines are too long

View File

@@ -129,6 +129,16 @@ func Unless(hk {{ $pkg }}.Hook, op {{ $pkg }}.Op) {{ $pkg }}.Hook {
return If(hk, Not(HasOp(op)))
}
// FixedError is a hook returning a fixed error.
func FixedError(err error) {{ $pkg }}.Hook {
return func({{ $pkg }}.Mutator) {{ $pkg }}.Mutator {
return {{ $pkg }}.MutateFunc(func(context.Context, {{ $pkg }}.Mutation) ({{ $pkg }}.Value, error) {
return nil, err
})
}
}
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []{{ $pkg }}.Hook {
@@ -138,11 +148,7 @@ func Unless(hk {{ $pkg }}.Hook, op {{ $pkg }}.Op) {{ $pkg }}.Hook {
// }
//
func Reject(op {{ $pkg }}.Op) {{ $pkg }}.Hook {
hk := func({{ $pkg }}.Mutator) {{ $pkg }}.Mutator {
return {{ $pkg }}.MutateFunc(func(_ context.Context, m {{ $pkg }}.Mutation) ({{ $pkg }}.Value, error) {
return nil, fmt.Errorf("%s operation is not allowed", m.Op())
})
}
hk := FixedError(fmt.Errorf("%s operation is not allowed", op))
return On(hk, op)
}

View File

@@ -149,6 +149,15 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
return If(hk, Not(HasOp(op)))
}
// FixedError is a hook returning a fixed error.
func FixedError(err error) ent.Hook {
return func(ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(context.Context, ent.Mutation) (ent.Value, error) {
return nil, err
})
}
}
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []ent.Hook {
@@ -158,11 +167,7 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
// }
//
func Reject(op ent.Op) ent.Hook {
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())
})
}
hk := FixedError(fmt.Errorf("%s operation is not allowed", op))
return On(hk, op)
}

View File

@@ -201,6 +201,15 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
return If(hk, Not(HasOp(op)))
}
// FixedError is a hook returning a fixed error.
func FixedError(err error) ent.Hook {
return func(ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(context.Context, ent.Mutation) (ent.Value, error) {
return nil, err
})
}
}
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []ent.Hook {
@@ -210,11 +219,7 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
// }
//
func Reject(op ent.Op) ent.Hook {
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())
})
}
hk := FixedError(fmt.Errorf("%s operation is not allowed", op))
return On(hk, op)
}

View File

@@ -318,6 +318,15 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
return If(hk, Not(HasOp(op)))
}
// FixedError is a hook returning a fixed error.
func FixedError(err error) ent.Hook {
return func(ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(context.Context, ent.Mutation) (ent.Value, error) {
return nil, err
})
}
}
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []ent.Hook {
@@ -327,11 +336,7 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
// }
//
func Reject(op ent.Op) ent.Hook {
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())
})
}
hk := FixedError(fmt.Errorf("%s operation is not allowed", op))
return On(hk, op)
}

View File

@@ -318,6 +318,15 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
return If(hk, Not(HasOp(op)))
}
// FixedError is a hook returning a fixed error.
func FixedError(err error) ent.Hook {
return func(ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(context.Context, ent.Mutation) (ent.Value, error) {
return nil, err
})
}
}
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []ent.Hook {
@@ -327,11 +336,7 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
// }
//
func Reject(op ent.Op) ent.Hook {
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())
})
}
hk := FixedError(fmt.Errorf("%s operation is not allowed", op))
return On(hk, op)
}

View File

@@ -162,6 +162,15 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
return If(hk, Not(HasOp(op)))
}
// FixedError is a hook returning a fixed error.
func FixedError(err error) ent.Hook {
return func(ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(context.Context, ent.Mutation) (ent.Value, error) {
return nil, err
})
}
}
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []ent.Hook {
@@ -171,11 +180,7 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
// }
//
func Reject(op ent.Op) ent.Hook {
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())
})
}
hk := FixedError(fmt.Errorf("%s operation is not allowed", op))
return On(hk, op)
}

View File

@@ -149,6 +149,15 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
return If(hk, Not(HasOp(op)))
}
// FixedError is a hook returning a fixed error.
func FixedError(err error) ent.Hook {
return func(ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(context.Context, ent.Mutation) (ent.Value, error) {
return nil, err
})
}
}
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []ent.Hook {
@@ -158,11 +167,7 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
// }
//
func Reject(op ent.Op) ent.Hook {
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())
})
}
hk := FixedError(fmt.Errorf("%s operation is not allowed", op))
return On(hk, op)
}

View File

@@ -149,6 +149,15 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
return If(hk, Not(HasOp(op)))
}
// FixedError is a hook returning a fixed error.
func FixedError(err error) ent.Hook {
return func(ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(context.Context, ent.Mutation) (ent.Value, error) {
return nil, err
})
}
}
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []ent.Hook {
@@ -158,11 +167,7 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
// }
//
func Reject(op ent.Op) ent.Hook {
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())
})
}
hk := FixedError(fmt.Errorf("%s operation is not allowed", op))
return On(hk, op)
}

View File

@@ -162,6 +162,15 @@ func Unless(hk entv1.Hook, op entv1.Op) entv1.Hook {
return If(hk, Not(HasOp(op)))
}
// FixedError is a hook returning a fixed error.
func FixedError(err error) entv1.Hook {
return func(entv1.Mutator) entv1.Mutator {
return entv1.MutateFunc(func(context.Context, entv1.Mutation) (entv1.Value, error) {
return nil, err
})
}
}
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []entv1.Hook {
@@ -171,11 +180,7 @@ func Unless(hk entv1.Hook, op entv1.Op) entv1.Hook {
// }
//
func Reject(op entv1.Op) entv1.Hook {
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())
})
}
hk := FixedError(fmt.Errorf("%s operation is not allowed", op))
return On(hk, op)
}

View File

@@ -201,6 +201,15 @@ func Unless(hk entv2.Hook, op entv2.Op) entv2.Hook {
return If(hk, Not(HasOp(op)))
}
// FixedError is a hook returning a fixed error.
func FixedError(err error) entv2.Hook {
return func(entv2.Mutator) entv2.Mutator {
return entv2.MutateFunc(func(context.Context, entv2.Mutation) (entv2.Value, error) {
return nil, err
})
}
}
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []entv2.Hook {
@@ -210,11 +219,7 @@ func Unless(hk entv2.Hook, op entv2.Op) entv2.Hook {
// }
//
func Reject(op entv2.Op) entv2.Hook {
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())
})
}
hk := FixedError(fmt.Errorf("%s operation is not allowed", op))
return On(hk, op)
}

View File

@@ -175,6 +175,15 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
return If(hk, Not(HasOp(op)))
}
// FixedError is a hook returning a fixed error.
func FixedError(err error) ent.Hook {
return func(ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(context.Context, ent.Mutation) (ent.Value, error) {
return nil, err
})
}
}
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []ent.Hook {
@@ -184,11 +193,7 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
// }
//
func Reject(op ent.Op) ent.Hook {
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())
})
}
hk := FixedError(fmt.Errorf("%s operation is not allowed", op))
return On(hk, op)
}

View File

@@ -175,6 +175,15 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
return If(hk, Not(HasOp(op)))
}
// FixedError is a hook returning a fixed error.
func FixedError(err error) ent.Hook {
return func(ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(context.Context, ent.Mutation) (ent.Value, error) {
return nil, err
})
}
}
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []ent.Hook {
@@ -184,11 +193,7 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
// }
//
func Reject(op ent.Op) ent.Hook {
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())
})
}
hk := FixedError(fmt.Errorf("%s operation is not allowed", op))
return On(hk, op)
}

View File

@@ -162,6 +162,15 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
return If(hk, Not(HasOp(op)))
}
// FixedError is a hook returning a fixed error.
func FixedError(err error) ent.Hook {
return func(ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(context.Context, ent.Mutation) (ent.Value, error) {
return nil, err
})
}
}
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []ent.Hook {
@@ -171,11 +180,7 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
// }
//
func Reject(op ent.Op) ent.Hook {
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())
})
}
hk := FixedError(fmt.Errorf("%s operation is not allowed", op))
return On(hk, op)
}

View File

@@ -149,6 +149,15 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
return If(hk, Not(HasOp(op)))
}
// FixedError is a hook returning a fixed error.
func FixedError(err error) ent.Hook {
return func(ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(context.Context, ent.Mutation) (ent.Value, error) {
return nil, err
})
}
}
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []ent.Hook {
@@ -158,11 +167,7 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
// }
//
func Reject(op ent.Op) ent.Hook {
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())
})
}
hk := FixedError(fmt.Errorf("%s operation is not allowed", op))
return On(hk, op)
}

View File

@@ -162,6 +162,15 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
return If(hk, Not(HasOp(op)))
}
// FixedError is a hook returning a fixed error.
func FixedError(err error) ent.Hook {
return func(ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(context.Context, ent.Mutation) (ent.Value, error) {
return nil, err
})
}
}
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []ent.Hook {
@@ -171,11 +180,7 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
// }
//
func Reject(op ent.Op) ent.Hook {
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())
})
}
hk := FixedError(fmt.Errorf("%s operation is not allowed", op))
return On(hk, op)
}

View File

@@ -149,6 +149,15 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
return If(hk, Not(HasOp(op)))
}
// FixedError is a hook returning a fixed error.
func FixedError(err error) ent.Hook {
return func(ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(context.Context, ent.Mutation) (ent.Value, error) {
return nil, err
})
}
}
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []ent.Hook {
@@ -158,11 +167,7 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
// }
//
func Reject(op ent.Op) ent.Hook {
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())
})
}
hk := FixedError(fmt.Errorf("%s operation is not allowed", op))
return On(hk, op)
}

View File

@@ -149,6 +149,15 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
return If(hk, Not(HasOp(op)))
}
// FixedError is a hook returning a fixed error.
func FixedError(err error) ent.Hook {
return func(ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(context.Context, ent.Mutation) (ent.Value, error) {
return nil, err
})
}
}
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []ent.Hook {
@@ -158,11 +167,7 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
// }
//
func Reject(op ent.Op) ent.Hook {
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())
})
}
hk := FixedError(fmt.Errorf("%s operation is not allowed", op))
return On(hk, op)
}

View File

@@ -162,6 +162,15 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
return If(hk, Not(HasOp(op)))
}
// FixedError is a hook returning a fixed error.
func FixedError(err error) ent.Hook {
return func(ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(context.Context, ent.Mutation) (ent.Value, error) {
return nil, err
})
}
}
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []ent.Hook {
@@ -171,11 +180,7 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
// }
//
func Reject(op ent.Op) ent.Hook {
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())
})
}
hk := FixedError(fmt.Errorf("%s operation is not allowed", op))
return On(hk, op)
}

View File

@@ -149,6 +149,15 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
return If(hk, Not(HasOp(op)))
}
// FixedError is a hook returning a fixed error.
func FixedError(err error) ent.Hook {
return func(ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(context.Context, ent.Mutation) (ent.Value, error) {
return nil, err
})
}
}
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []ent.Hook {
@@ -158,11 +167,7 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
// }
//
func Reject(op ent.Op) ent.Hook {
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())
})
}
hk := FixedError(fmt.Errorf("%s operation is not allowed", op))
return On(hk, op)
}

View File

@@ -162,6 +162,15 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
return If(hk, Not(HasOp(op)))
}
// FixedError is a hook returning a fixed error.
func FixedError(err error) ent.Hook {
return func(ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(context.Context, ent.Mutation) (ent.Value, error) {
return nil, err
})
}
}
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []ent.Hook {
@@ -171,11 +180,7 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
// }
//
func Reject(op ent.Op) ent.Hook {
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())
})
}
hk := FixedError(fmt.Errorf("%s operation is not allowed", op))
return On(hk, op)
}

View File

@@ -149,6 +149,15 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
return If(hk, Not(HasOp(op)))
}
// FixedError is a hook returning a fixed error.
func FixedError(err error) ent.Hook {
return func(ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(context.Context, ent.Mutation) (ent.Value, error) {
return nil, err
})
}
}
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []ent.Hook {
@@ -158,11 +167,7 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
// }
//
func Reject(op ent.Op) ent.Hook {
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())
})
}
hk := FixedError(fmt.Errorf("%s operation is not allowed", op))
return On(hk, op)
}

View File

@@ -149,6 +149,15 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
return If(hk, Not(HasOp(op)))
}
// FixedError is a hook returning a fixed error.
func FixedError(err error) ent.Hook {
return func(ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(context.Context, ent.Mutation) (ent.Value, error) {
return nil, err
})
}
}
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []ent.Hook {
@@ -158,11 +167,7 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
// }
//
func Reject(op ent.Op) ent.Hook {
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())
})
}
hk := FixedError(fmt.Errorf("%s operation is not allowed", op))
return On(hk, op)
}

View File

@@ -149,6 +149,15 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
return If(hk, Not(HasOp(op)))
}
// FixedError is a hook returning a fixed error.
func FixedError(err error) ent.Hook {
return func(ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(context.Context, ent.Mutation) (ent.Value, error) {
return nil, err
})
}
}
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []ent.Hook {
@@ -158,11 +167,7 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
// }
//
func Reject(op ent.Op) ent.Hook {
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())
})
}
hk := FixedError(fmt.Errorf("%s operation is not allowed", op))
return On(hk, op)
}

View File

@@ -175,6 +175,15 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
return If(hk, Not(HasOp(op)))
}
// FixedError is a hook returning a fixed error.
func FixedError(err error) ent.Hook {
return func(ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(context.Context, ent.Mutation) (ent.Value, error) {
return nil, err
})
}
}
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []ent.Hook {
@@ -184,11 +193,7 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
// }
//
func Reject(op ent.Op) ent.Hook {
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())
})
}
hk := FixedError(fmt.Errorf("%s operation is not allowed", op))
return On(hk, op)
}

View File

@@ -175,6 +175,15 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
return If(hk, Not(HasOp(op)))
}
// FixedError is a hook returning a fixed error.
func FixedError(err error) ent.Hook {
return func(ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(context.Context, ent.Mutation) (ent.Value, error) {
return nil, err
})
}
}
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []ent.Hook {
@@ -184,11 +193,7 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
// }
//
func Reject(op ent.Op) ent.Hook {
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())
})
}
hk := FixedError(fmt.Errorf("%s operation is not allowed", op))
return On(hk, op)
}

View File

@@ -175,6 +175,15 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
return If(hk, Not(HasOp(op)))
}
// FixedError is a hook returning a fixed error.
func FixedError(err error) ent.Hook {
return func(ent.Mutator) ent.Mutator {
return ent.MutateFunc(func(context.Context, ent.Mutation) (ent.Value, error) {
return nil, err
})
}
}
// Reject returns a hook that rejects all operations that match op.
//
// func (T) Hooks() []ent.Hook {
@@ -184,11 +193,7 @@ func Unless(hk ent.Hook, op ent.Op) ent.Hook {
// }
//
func Reject(op ent.Op) ent.Hook {
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())
})
}
hk := FixedError(fmt.Errorf("%s operation is not allowed", op))
return On(hk, op)
}