mirror of
https://github.com/chaitin/MonkeyCode.git
synced 2026-11-22 19:39:31 +08:00
- 新增 git_bots, git_bot_users, git_bot_tasks, project_git_bots 表 - 添加 user, host, project 到 gitbot 的关联边 - 使用 soft delete 和 created_at 游标
572 lines
19 KiB
Go
572 lines
19 KiB
Go
// Code generated by ent, DO NOT EDIT.
|
|
|
|
package hook
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/chaitin/MonkeyCode/backend/db"
|
|
)
|
|
|
|
// The AuditFunc type is an adapter to allow the use of ordinary
|
|
// function as Audit mutator.
|
|
type AuditFunc func(context.Context, *db.AuditMutation) (db.Value, error)
|
|
|
|
// Mutate calls f(ctx, m).
|
|
func (f AuditFunc) Mutate(ctx context.Context, m db.Mutation) (db.Value, error) {
|
|
if mv, ok := m.(*db.AuditMutation); ok {
|
|
return f(ctx, mv)
|
|
}
|
|
return nil, fmt.Errorf("unexpected mutation type %T. expect *db.AuditMutation", m)
|
|
}
|
|
|
|
// The GitBotFunc type is an adapter to allow the use of ordinary
|
|
// function as GitBot mutator.
|
|
type GitBotFunc func(context.Context, *db.GitBotMutation) (db.Value, error)
|
|
|
|
// Mutate calls f(ctx, m).
|
|
func (f GitBotFunc) Mutate(ctx context.Context, m db.Mutation) (db.Value, error) {
|
|
if mv, ok := m.(*db.GitBotMutation); ok {
|
|
return f(ctx, mv)
|
|
}
|
|
return nil, fmt.Errorf("unexpected mutation type %T. expect *db.GitBotMutation", m)
|
|
}
|
|
|
|
// The GitBotTaskFunc type is an adapter to allow the use of ordinary
|
|
// function as GitBotTask mutator.
|
|
type GitBotTaskFunc func(context.Context, *db.GitBotTaskMutation) (db.Value, error)
|
|
|
|
// Mutate calls f(ctx, m).
|
|
func (f GitBotTaskFunc) Mutate(ctx context.Context, m db.Mutation) (db.Value, error) {
|
|
if mv, ok := m.(*db.GitBotTaskMutation); ok {
|
|
return f(ctx, mv)
|
|
}
|
|
return nil, fmt.Errorf("unexpected mutation type %T. expect *db.GitBotTaskMutation", m)
|
|
}
|
|
|
|
// The GitBotUserFunc type is an adapter to allow the use of ordinary
|
|
// function as GitBotUser mutator.
|
|
type GitBotUserFunc func(context.Context, *db.GitBotUserMutation) (db.Value, error)
|
|
|
|
// Mutate calls f(ctx, m).
|
|
func (f GitBotUserFunc) Mutate(ctx context.Context, m db.Mutation) (db.Value, error) {
|
|
if mv, ok := m.(*db.GitBotUserMutation); ok {
|
|
return f(ctx, mv)
|
|
}
|
|
return nil, fmt.Errorf("unexpected mutation type %T. expect *db.GitBotUserMutation", m)
|
|
}
|
|
|
|
// The GitIdentityFunc type is an adapter to allow the use of ordinary
|
|
// function as GitIdentity mutator.
|
|
type GitIdentityFunc func(context.Context, *db.GitIdentityMutation) (db.Value, error)
|
|
|
|
// Mutate calls f(ctx, m).
|
|
func (f GitIdentityFunc) Mutate(ctx context.Context, m db.Mutation) (db.Value, error) {
|
|
if mv, ok := m.(*db.GitIdentityMutation); ok {
|
|
return f(ctx, mv)
|
|
}
|
|
return nil, fmt.Errorf("unexpected mutation type %T. expect *db.GitIdentityMutation", m)
|
|
}
|
|
|
|
// The HostFunc type is an adapter to allow the use of ordinary
|
|
// function as Host mutator.
|
|
type HostFunc func(context.Context, *db.HostMutation) (db.Value, error)
|
|
|
|
// Mutate calls f(ctx, m).
|
|
func (f HostFunc) Mutate(ctx context.Context, m db.Mutation) (db.Value, error) {
|
|
if mv, ok := m.(*db.HostMutation); ok {
|
|
return f(ctx, mv)
|
|
}
|
|
return nil, fmt.Errorf("unexpected mutation type %T. expect *db.HostMutation", m)
|
|
}
|
|
|
|
// The ImageFunc type is an adapter to allow the use of ordinary
|
|
// function as Image mutator.
|
|
type ImageFunc func(context.Context, *db.ImageMutation) (db.Value, error)
|
|
|
|
// Mutate calls f(ctx, m).
|
|
func (f ImageFunc) Mutate(ctx context.Context, m db.Mutation) (db.Value, error) {
|
|
if mv, ok := m.(*db.ImageMutation); ok {
|
|
return f(ctx, mv)
|
|
}
|
|
return nil, fmt.Errorf("unexpected mutation type %T. expect *db.ImageMutation", m)
|
|
}
|
|
|
|
// The ModelFunc type is an adapter to allow the use of ordinary
|
|
// function as Model mutator.
|
|
type ModelFunc func(context.Context, *db.ModelMutation) (db.Value, error)
|
|
|
|
// Mutate calls f(ctx, m).
|
|
func (f ModelFunc) Mutate(ctx context.Context, m db.Mutation) (db.Value, error) {
|
|
if mv, ok := m.(*db.ModelMutation); ok {
|
|
return f(ctx, mv)
|
|
}
|
|
return nil, fmt.Errorf("unexpected mutation type %T. expect *db.ModelMutation", m)
|
|
}
|
|
|
|
// The NotifyChannelFunc type is an adapter to allow the use of ordinary
|
|
// function as NotifyChannel mutator.
|
|
type NotifyChannelFunc func(context.Context, *db.NotifyChannelMutation) (db.Value, error)
|
|
|
|
// Mutate calls f(ctx, m).
|
|
func (f NotifyChannelFunc) Mutate(ctx context.Context, m db.Mutation) (db.Value, error) {
|
|
if mv, ok := m.(*db.NotifyChannelMutation); ok {
|
|
return f(ctx, mv)
|
|
}
|
|
return nil, fmt.Errorf("unexpected mutation type %T. expect *db.NotifyChannelMutation", m)
|
|
}
|
|
|
|
// The NotifySendLogFunc type is an adapter to allow the use of ordinary
|
|
// function as NotifySendLog mutator.
|
|
type NotifySendLogFunc func(context.Context, *db.NotifySendLogMutation) (db.Value, error)
|
|
|
|
// Mutate calls f(ctx, m).
|
|
func (f NotifySendLogFunc) Mutate(ctx context.Context, m db.Mutation) (db.Value, error) {
|
|
if mv, ok := m.(*db.NotifySendLogMutation); ok {
|
|
return f(ctx, mv)
|
|
}
|
|
return nil, fmt.Errorf("unexpected mutation type %T. expect *db.NotifySendLogMutation", m)
|
|
}
|
|
|
|
// The NotifySubscriptionFunc type is an adapter to allow the use of ordinary
|
|
// function as NotifySubscription mutator.
|
|
type NotifySubscriptionFunc func(context.Context, *db.NotifySubscriptionMutation) (db.Value, error)
|
|
|
|
// Mutate calls f(ctx, m).
|
|
func (f NotifySubscriptionFunc) Mutate(ctx context.Context, m db.Mutation) (db.Value, error) {
|
|
if mv, ok := m.(*db.NotifySubscriptionMutation); ok {
|
|
return f(ctx, mv)
|
|
}
|
|
return nil, fmt.Errorf("unexpected mutation type %T. expect *db.NotifySubscriptionMutation", m)
|
|
}
|
|
|
|
// The ProjectFunc type is an adapter to allow the use of ordinary
|
|
// function as Project mutator.
|
|
type ProjectFunc func(context.Context, *db.ProjectMutation) (db.Value, error)
|
|
|
|
// Mutate calls f(ctx, m).
|
|
func (f ProjectFunc) Mutate(ctx context.Context, m db.Mutation) (db.Value, error) {
|
|
if mv, ok := m.(*db.ProjectMutation); ok {
|
|
return f(ctx, mv)
|
|
}
|
|
return nil, fmt.Errorf("unexpected mutation type %T. expect *db.ProjectMutation", m)
|
|
}
|
|
|
|
// The ProjectCollaboratorFunc type is an adapter to allow the use of ordinary
|
|
// function as ProjectCollaborator mutator.
|
|
type ProjectCollaboratorFunc func(context.Context, *db.ProjectCollaboratorMutation) (db.Value, error)
|
|
|
|
// Mutate calls f(ctx, m).
|
|
func (f ProjectCollaboratorFunc) Mutate(ctx context.Context, m db.Mutation) (db.Value, error) {
|
|
if mv, ok := m.(*db.ProjectCollaboratorMutation); ok {
|
|
return f(ctx, mv)
|
|
}
|
|
return nil, fmt.Errorf("unexpected mutation type %T. expect *db.ProjectCollaboratorMutation", m)
|
|
}
|
|
|
|
// The ProjectGitBotFunc type is an adapter to allow the use of ordinary
|
|
// function as ProjectGitBot mutator.
|
|
type ProjectGitBotFunc func(context.Context, *db.ProjectGitBotMutation) (db.Value, error)
|
|
|
|
// Mutate calls f(ctx, m).
|
|
func (f ProjectGitBotFunc) Mutate(ctx context.Context, m db.Mutation) (db.Value, error) {
|
|
if mv, ok := m.(*db.ProjectGitBotMutation); ok {
|
|
return f(ctx, mv)
|
|
}
|
|
return nil, fmt.Errorf("unexpected mutation type %T. expect *db.ProjectGitBotMutation", m)
|
|
}
|
|
|
|
// The ProjectIssueFunc type is an adapter to allow the use of ordinary
|
|
// function as ProjectIssue mutator.
|
|
type ProjectIssueFunc func(context.Context, *db.ProjectIssueMutation) (db.Value, error)
|
|
|
|
// Mutate calls f(ctx, m).
|
|
func (f ProjectIssueFunc) Mutate(ctx context.Context, m db.Mutation) (db.Value, error) {
|
|
if mv, ok := m.(*db.ProjectIssueMutation); ok {
|
|
return f(ctx, mv)
|
|
}
|
|
return nil, fmt.Errorf("unexpected mutation type %T. expect *db.ProjectIssueMutation", m)
|
|
}
|
|
|
|
// The ProjectIssueCommentFunc type is an adapter to allow the use of ordinary
|
|
// function as ProjectIssueComment mutator.
|
|
type ProjectIssueCommentFunc func(context.Context, *db.ProjectIssueCommentMutation) (db.Value, error)
|
|
|
|
// Mutate calls f(ctx, m).
|
|
func (f ProjectIssueCommentFunc) Mutate(ctx context.Context, m db.Mutation) (db.Value, error) {
|
|
if mv, ok := m.(*db.ProjectIssueCommentMutation); ok {
|
|
return f(ctx, mv)
|
|
}
|
|
return nil, fmt.Errorf("unexpected mutation type %T. expect *db.ProjectIssueCommentMutation", m)
|
|
}
|
|
|
|
// The ProjectTaskFunc type is an adapter to allow the use of ordinary
|
|
// function as ProjectTask mutator.
|
|
type ProjectTaskFunc func(context.Context, *db.ProjectTaskMutation) (db.Value, error)
|
|
|
|
// Mutate calls f(ctx, m).
|
|
func (f ProjectTaskFunc) Mutate(ctx context.Context, m db.Mutation) (db.Value, error) {
|
|
if mv, ok := m.(*db.ProjectTaskMutation); ok {
|
|
return f(ctx, mv)
|
|
}
|
|
return nil, fmt.Errorf("unexpected mutation type %T. expect *db.ProjectTaskMutation", m)
|
|
}
|
|
|
|
// The TaskFunc type is an adapter to allow the use of ordinary
|
|
// function as Task mutator.
|
|
type TaskFunc func(context.Context, *db.TaskMutation) (db.Value, error)
|
|
|
|
// Mutate calls f(ctx, m).
|
|
func (f TaskFunc) Mutate(ctx context.Context, m db.Mutation) (db.Value, error) {
|
|
if mv, ok := m.(*db.TaskMutation); ok {
|
|
return f(ctx, mv)
|
|
}
|
|
return nil, fmt.Errorf("unexpected mutation type %T. expect *db.TaskMutation", m)
|
|
}
|
|
|
|
// The TaskVirtualMachineFunc type is an adapter to allow the use of ordinary
|
|
// function as TaskVirtualMachine mutator.
|
|
type TaskVirtualMachineFunc func(context.Context, *db.TaskVirtualMachineMutation) (db.Value, error)
|
|
|
|
// Mutate calls f(ctx, m).
|
|
func (f TaskVirtualMachineFunc) Mutate(ctx context.Context, m db.Mutation) (db.Value, error) {
|
|
if mv, ok := m.(*db.TaskVirtualMachineMutation); ok {
|
|
return f(ctx, mv)
|
|
}
|
|
return nil, fmt.Errorf("unexpected mutation type %T. expect *db.TaskVirtualMachineMutation", m)
|
|
}
|
|
|
|
// The TeamFunc type is an adapter to allow the use of ordinary
|
|
// function as Team mutator.
|
|
type TeamFunc func(context.Context, *db.TeamMutation) (db.Value, error)
|
|
|
|
// Mutate calls f(ctx, m).
|
|
func (f TeamFunc) Mutate(ctx context.Context, m db.Mutation) (db.Value, error) {
|
|
if mv, ok := m.(*db.TeamMutation); ok {
|
|
return f(ctx, mv)
|
|
}
|
|
return nil, fmt.Errorf("unexpected mutation type %T. expect *db.TeamMutation", m)
|
|
}
|
|
|
|
// The TeamGroupFunc type is an adapter to allow the use of ordinary
|
|
// function as TeamGroup mutator.
|
|
type TeamGroupFunc func(context.Context, *db.TeamGroupMutation) (db.Value, error)
|
|
|
|
// Mutate calls f(ctx, m).
|
|
func (f TeamGroupFunc) Mutate(ctx context.Context, m db.Mutation) (db.Value, error) {
|
|
if mv, ok := m.(*db.TeamGroupMutation); ok {
|
|
return f(ctx, mv)
|
|
}
|
|
return nil, fmt.Errorf("unexpected mutation type %T. expect *db.TeamGroupMutation", m)
|
|
}
|
|
|
|
// The TeamGroupHostFunc type is an adapter to allow the use of ordinary
|
|
// function as TeamGroupHost mutator.
|
|
type TeamGroupHostFunc func(context.Context, *db.TeamGroupHostMutation) (db.Value, error)
|
|
|
|
// Mutate calls f(ctx, m).
|
|
func (f TeamGroupHostFunc) Mutate(ctx context.Context, m db.Mutation) (db.Value, error) {
|
|
if mv, ok := m.(*db.TeamGroupHostMutation); ok {
|
|
return f(ctx, mv)
|
|
}
|
|
return nil, fmt.Errorf("unexpected mutation type %T. expect *db.TeamGroupHostMutation", m)
|
|
}
|
|
|
|
// The TeamGroupImageFunc type is an adapter to allow the use of ordinary
|
|
// function as TeamGroupImage mutator.
|
|
type TeamGroupImageFunc func(context.Context, *db.TeamGroupImageMutation) (db.Value, error)
|
|
|
|
// Mutate calls f(ctx, m).
|
|
func (f TeamGroupImageFunc) Mutate(ctx context.Context, m db.Mutation) (db.Value, error) {
|
|
if mv, ok := m.(*db.TeamGroupImageMutation); ok {
|
|
return f(ctx, mv)
|
|
}
|
|
return nil, fmt.Errorf("unexpected mutation type %T. expect *db.TeamGroupImageMutation", m)
|
|
}
|
|
|
|
// The TeamGroupMemberFunc type is an adapter to allow the use of ordinary
|
|
// function as TeamGroupMember mutator.
|
|
type TeamGroupMemberFunc func(context.Context, *db.TeamGroupMemberMutation) (db.Value, error)
|
|
|
|
// Mutate calls f(ctx, m).
|
|
func (f TeamGroupMemberFunc) Mutate(ctx context.Context, m db.Mutation) (db.Value, error) {
|
|
if mv, ok := m.(*db.TeamGroupMemberMutation); ok {
|
|
return f(ctx, mv)
|
|
}
|
|
return nil, fmt.Errorf("unexpected mutation type %T. expect *db.TeamGroupMemberMutation", m)
|
|
}
|
|
|
|
// The TeamGroupModelFunc type is an adapter to allow the use of ordinary
|
|
// function as TeamGroupModel mutator.
|
|
type TeamGroupModelFunc func(context.Context, *db.TeamGroupModelMutation) (db.Value, error)
|
|
|
|
// Mutate calls f(ctx, m).
|
|
func (f TeamGroupModelFunc) Mutate(ctx context.Context, m db.Mutation) (db.Value, error) {
|
|
if mv, ok := m.(*db.TeamGroupModelMutation); ok {
|
|
return f(ctx, mv)
|
|
}
|
|
return nil, fmt.Errorf("unexpected mutation type %T. expect *db.TeamGroupModelMutation", m)
|
|
}
|
|
|
|
// The TeamHostFunc type is an adapter to allow the use of ordinary
|
|
// function as TeamHost mutator.
|
|
type TeamHostFunc func(context.Context, *db.TeamHostMutation) (db.Value, error)
|
|
|
|
// Mutate calls f(ctx, m).
|
|
func (f TeamHostFunc) Mutate(ctx context.Context, m db.Mutation) (db.Value, error) {
|
|
if mv, ok := m.(*db.TeamHostMutation); ok {
|
|
return f(ctx, mv)
|
|
}
|
|
return nil, fmt.Errorf("unexpected mutation type %T. expect *db.TeamHostMutation", m)
|
|
}
|
|
|
|
// The TeamImageFunc type is an adapter to allow the use of ordinary
|
|
// function as TeamImage mutator.
|
|
type TeamImageFunc func(context.Context, *db.TeamImageMutation) (db.Value, error)
|
|
|
|
// Mutate calls f(ctx, m).
|
|
func (f TeamImageFunc) Mutate(ctx context.Context, m db.Mutation) (db.Value, error) {
|
|
if mv, ok := m.(*db.TeamImageMutation); ok {
|
|
return f(ctx, mv)
|
|
}
|
|
return nil, fmt.Errorf("unexpected mutation type %T. expect *db.TeamImageMutation", m)
|
|
}
|
|
|
|
// The TeamMemberFunc type is an adapter to allow the use of ordinary
|
|
// function as TeamMember mutator.
|
|
type TeamMemberFunc func(context.Context, *db.TeamMemberMutation) (db.Value, error)
|
|
|
|
// Mutate calls f(ctx, m).
|
|
func (f TeamMemberFunc) Mutate(ctx context.Context, m db.Mutation) (db.Value, error) {
|
|
if mv, ok := m.(*db.TeamMemberMutation); ok {
|
|
return f(ctx, mv)
|
|
}
|
|
return nil, fmt.Errorf("unexpected mutation type %T. expect *db.TeamMemberMutation", m)
|
|
}
|
|
|
|
// The TeamModelFunc type is an adapter to allow the use of ordinary
|
|
// function as TeamModel mutator.
|
|
type TeamModelFunc func(context.Context, *db.TeamModelMutation) (db.Value, error)
|
|
|
|
// Mutate calls f(ctx, m).
|
|
func (f TeamModelFunc) Mutate(ctx context.Context, m db.Mutation) (db.Value, error) {
|
|
if mv, ok := m.(*db.TeamModelMutation); ok {
|
|
return f(ctx, mv)
|
|
}
|
|
return nil, fmt.Errorf("unexpected mutation type %T. expect *db.TeamModelMutation", m)
|
|
}
|
|
|
|
// The UserFunc type is an adapter to allow the use of ordinary
|
|
// function as User mutator.
|
|
type UserFunc func(context.Context, *db.UserMutation) (db.Value, error)
|
|
|
|
// Mutate calls f(ctx, m).
|
|
func (f UserFunc) Mutate(ctx context.Context, m db.Mutation) (db.Value, error) {
|
|
if mv, ok := m.(*db.UserMutation); ok {
|
|
return f(ctx, mv)
|
|
}
|
|
return nil, fmt.Errorf("unexpected mutation type %T. expect *db.UserMutation", m)
|
|
}
|
|
|
|
// The UserIdentityFunc type is an adapter to allow the use of ordinary
|
|
// function as UserIdentity mutator.
|
|
type UserIdentityFunc func(context.Context, *db.UserIdentityMutation) (db.Value, error)
|
|
|
|
// Mutate calls f(ctx, m).
|
|
func (f UserIdentityFunc) Mutate(ctx context.Context, m db.Mutation) (db.Value, error) {
|
|
if mv, ok := m.(*db.UserIdentityMutation); ok {
|
|
return f(ctx, mv)
|
|
}
|
|
return nil, fmt.Errorf("unexpected mutation type %T. expect *db.UserIdentityMutation", m)
|
|
}
|
|
|
|
// The VirtualMachineFunc type is an adapter to allow the use of ordinary
|
|
// function as VirtualMachine mutator.
|
|
type VirtualMachineFunc func(context.Context, *db.VirtualMachineMutation) (db.Value, error)
|
|
|
|
// Mutate calls f(ctx, m).
|
|
func (f VirtualMachineFunc) Mutate(ctx context.Context, m db.Mutation) (db.Value, error) {
|
|
if mv, ok := m.(*db.VirtualMachineMutation); ok {
|
|
return f(ctx, mv)
|
|
}
|
|
return nil, fmt.Errorf("unexpected mutation type %T. expect *db.VirtualMachineMutation", m)
|
|
}
|
|
|
|
// Condition is a hook condition function.
|
|
type Condition func(context.Context, db.Mutation) bool
|
|
|
|
// And groups conditions with the AND operator.
|
|
func And(first, second Condition, rest ...Condition) Condition {
|
|
return func(ctx context.Context, m db.Mutation) bool {
|
|
if !first(ctx, m) || !second(ctx, m) {
|
|
return false
|
|
}
|
|
for _, cond := range rest {
|
|
if !cond(ctx, m) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
}
|
|
|
|
// Or groups conditions with the OR operator.
|
|
func Or(first, second Condition, rest ...Condition) Condition {
|
|
return func(ctx context.Context, m db.Mutation) bool {
|
|
if first(ctx, m) || second(ctx, m) {
|
|
return true
|
|
}
|
|
for _, cond := range rest {
|
|
if cond(ctx, m) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
}
|
|
|
|
// Not negates a given condition.
|
|
func Not(cond Condition) Condition {
|
|
return func(ctx context.Context, m db.Mutation) bool {
|
|
return !cond(ctx, m)
|
|
}
|
|
}
|
|
|
|
// HasOp is a condition testing mutation operation.
|
|
func HasOp(op db.Op) Condition {
|
|
return func(_ context.Context, m db.Mutation) bool {
|
|
return m.Op().Is(op)
|
|
}
|
|
}
|
|
|
|
// HasAddedFields is a condition validating `.AddedField` on fields.
|
|
func HasAddedFields(field string, fields ...string) Condition {
|
|
return func(_ context.Context, m db.Mutation) bool {
|
|
if _, exists := m.AddedField(field); !exists {
|
|
return false
|
|
}
|
|
for _, field := range fields {
|
|
if _, exists := m.AddedField(field); !exists {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
}
|
|
|
|
// HasClearedFields is a condition validating `.FieldCleared` on fields.
|
|
func HasClearedFields(field string, fields ...string) Condition {
|
|
return func(_ context.Context, m db.Mutation) bool {
|
|
if exists := m.FieldCleared(field); !exists {
|
|
return false
|
|
}
|
|
for _, field := range fields {
|
|
if exists := m.FieldCleared(field); !exists {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
}
|
|
|
|
// HasFields is a condition validating `.Field` on fields.
|
|
func HasFields(field string, fields ...string) Condition {
|
|
return func(_ context.Context, m db.Mutation) bool {
|
|
if _, exists := m.Field(field); !exists {
|
|
return false
|
|
}
|
|
for _, field := range fields {
|
|
if _, exists := m.Field(field); !exists {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
}
|
|
|
|
// If executes the given hook under condition.
|
|
//
|
|
// hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...)))
|
|
func If(hk db.Hook, cond Condition) db.Hook {
|
|
return func(next db.Mutator) db.Mutator {
|
|
return db.MutateFunc(func(ctx context.Context, m db.Mutation) (db.Value, error) {
|
|
if cond(ctx, m) {
|
|
return hk(next).Mutate(ctx, m)
|
|
}
|
|
return next.Mutate(ctx, m)
|
|
})
|
|
}
|
|
}
|
|
|
|
// On executes the given hook only for the given operation.
|
|
//
|
|
// hook.On(Log, db.Delete|db.Create)
|
|
func On(hk db.Hook, op db.Op) db.Hook {
|
|
return If(hk, HasOp(op))
|
|
}
|
|
|
|
// Unless skips the given hook only for the given operation.
|
|
//
|
|
// hook.Unless(Log, db.Update|db.UpdateOne)
|
|
func Unless(hk db.Hook, op db.Op) db.Hook {
|
|
return If(hk, Not(HasOp(op)))
|
|
}
|
|
|
|
// FixedError is a hook returning a fixed error.
|
|
func FixedError(err error) db.Hook {
|
|
return func(db.Mutator) db.Mutator {
|
|
return db.MutateFunc(func(context.Context, db.Mutation) (db.Value, error) {
|
|
return nil, err
|
|
})
|
|
}
|
|
}
|
|
|
|
// Reject returns a hook that rejects all operations that match op.
|
|
//
|
|
// func (T) Hooks() []db.Hook {
|
|
// return []db.Hook{
|
|
// Reject(db.Delete|db.Update),
|
|
// }
|
|
// }
|
|
func Reject(op db.Op) db.Hook {
|
|
hk := FixedError(fmt.Errorf("%s operation is not allowed", op))
|
|
return On(hk, op)
|
|
}
|
|
|
|
// Chain acts as a list of hooks and is effectively immutable.
|
|
// Once created, it will always hold the same set of hooks in the same order.
|
|
type Chain struct {
|
|
hooks []db.Hook
|
|
}
|
|
|
|
// NewChain creates a new chain of hooks.
|
|
func NewChain(hooks ...db.Hook) Chain {
|
|
return Chain{append([]db.Hook(nil), hooks...)}
|
|
}
|
|
|
|
// Hook chains the list of hooks and returns the final hook.
|
|
func (c Chain) Hook() db.Hook {
|
|
return func(mutator db.Mutator) db.Mutator {
|
|
for i := len(c.hooks) - 1; i >= 0; i-- {
|
|
mutator = c.hooks[i](mutator)
|
|
}
|
|
return mutator
|
|
}
|
|
}
|
|
|
|
// Append extends a chain, adding the specified hook
|
|
// as the last ones in the mutation flow.
|
|
func (c Chain) Append(hooks ...db.Hook) Chain {
|
|
newHooks := make([]db.Hook, 0, len(c.hooks)+len(hooks))
|
|
newHooks = append(newHooks, c.hooks...)
|
|
newHooks = append(newHooks, hooks...)
|
|
return Chain{newHooks}
|
|
}
|
|
|
|
// Extend extends a chain, adding the specified chain
|
|
// as the last ones in the mutation flow.
|
|
func (c Chain) Extend(chain Chain) Chain {
|
|
return c.Append(chain.hooks...)
|
|
}
|