123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678 |
- package hook
- import (
- "context"
- "fmt"
- "wechat-api/ent"
- )
- type AgentFunc func(context.Context, *ent.AgentMutation) (ent.Value, error)
- func (f AgentFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.AgentMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.AgentMutation", m)
- }
- type AgentBaseFunc func(context.Context, *ent.AgentBaseMutation) (ent.Value, error)
- func (f AgentBaseFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.AgentBaseMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.AgentBaseMutation", m)
- }
- type AliyunAvatarFunc func(context.Context, *ent.AliyunAvatarMutation) (ent.Value, error)
- func (f AliyunAvatarFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.AliyunAvatarMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.AliyunAvatarMutation", m)
- }
- type AllocAgentFunc func(context.Context, *ent.AllocAgentMutation) (ent.Value, error)
- func (f AllocAgentFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.AllocAgentMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.AllocAgentMutation", m)
- }
- type ApiKeyFunc func(context.Context, *ent.ApiKeyMutation) (ent.Value, error)
- func (f ApiKeyFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.ApiKeyMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.ApiKeyMutation", m)
- }
- type BatchMsgFunc func(context.Context, *ent.BatchMsgMutation) (ent.Value, error)
- func (f BatchMsgFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.BatchMsgMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.BatchMsgMutation", m)
- }
- type CategoryFunc func(context.Context, *ent.CategoryMutation) (ent.Value, error)
- func (f CategoryFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.CategoryMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.CategoryMutation", m)
- }
- type ChatRecordsFunc func(context.Context, *ent.ChatRecordsMutation) (ent.Value, error)
- func (f ChatRecordsFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.ChatRecordsMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.ChatRecordsMutation", m)
- }
- type ChatSessionFunc func(context.Context, *ent.ChatSessionMutation) (ent.Value, error)
- func (f ChatSessionFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.ChatSessionMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.ChatSessionMutation", m)
- }
- type ContactFunc func(context.Context, *ent.ContactMutation) (ent.Value, error)
- func (f ContactFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.ContactMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.ContactMutation", m)
- }
- type CreditBalanceFunc func(context.Context, *ent.CreditBalanceMutation) (ent.Value, error)
- func (f CreditBalanceFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.CreditBalanceMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.CreditBalanceMutation", m)
- }
- type CreditUsageFunc func(context.Context, *ent.CreditUsageMutation) (ent.Value, error)
- func (f CreditUsageFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.CreditUsageMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.CreditUsageMutation", m)
- }
- type EmployeeFunc func(context.Context, *ent.EmployeeMutation) (ent.Value, error)
- func (f EmployeeFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.EmployeeMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.EmployeeMutation", m)
- }
- type EmployeeConfigFunc func(context.Context, *ent.EmployeeConfigMutation) (ent.Value, error)
- func (f EmployeeConfigFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.EmployeeConfigMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.EmployeeConfigMutation", m)
- }
- type LabelFunc func(context.Context, *ent.LabelMutation) (ent.Value, error)
- func (f LabelFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.LabelMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.LabelMutation", m)
- }
- type LabelRelationshipFunc func(context.Context, *ent.LabelRelationshipMutation) (ent.Value, error)
- func (f LabelRelationshipFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.LabelRelationshipMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.LabelRelationshipMutation", m)
- }
- type LabelTaggingFunc func(context.Context, *ent.LabelTaggingMutation) (ent.Value, error)
- func (f LabelTaggingFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.LabelTaggingMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.LabelTaggingMutation", m)
- }
- type MessageFunc func(context.Context, *ent.MessageMutation) (ent.Value, error)
- func (f MessageFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.MessageMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.MessageMutation", m)
- }
- type MessageRecordsFunc func(context.Context, *ent.MessageRecordsMutation) (ent.Value, error)
- func (f MessageRecordsFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.MessageRecordsMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.MessageRecordsMutation", m)
- }
- type MsgFunc func(context.Context, *ent.MsgMutation) (ent.Value, error)
- func (f MsgFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.MsgMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.MsgMutation", m)
- }
- type PayRechargeFunc func(context.Context, *ent.PayRechargeMutation) (ent.Value, error)
- func (f PayRechargeFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.PayRechargeMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.PayRechargeMutation", m)
- }
- type ServerFunc func(context.Context, *ent.ServerMutation) (ent.Value, error)
- func (f ServerFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.ServerMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.ServerMutation", m)
- }
- type SopNodeFunc func(context.Context, *ent.SopNodeMutation) (ent.Value, error)
- func (f SopNodeFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.SopNodeMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.SopNodeMutation", m)
- }
- type SopStageFunc func(context.Context, *ent.SopStageMutation) (ent.Value, error)
- func (f SopStageFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.SopStageMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.SopStageMutation", m)
- }
- type SopTaskFunc func(context.Context, *ent.SopTaskMutation) (ent.Value, error)
- func (f SopTaskFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.SopTaskMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.SopTaskMutation", m)
- }
- type TokenFunc func(context.Context, *ent.TokenMutation) (ent.Value, error)
- func (f TokenFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.TokenMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.TokenMutation", m)
- }
- type TutorialFunc func(context.Context, *ent.TutorialMutation) (ent.Value, error)
- func (f TutorialFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.TutorialMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.TutorialMutation", m)
- }
- type UsageDetailFunc func(context.Context, *ent.UsageDetailMutation) (ent.Value, error)
- func (f UsageDetailFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.UsageDetailMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.UsageDetailMutation", m)
- }
- type UsageStatisticDayFunc func(context.Context, *ent.UsageStatisticDayMutation) (ent.Value, error)
- func (f UsageStatisticDayFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.UsageStatisticDayMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.UsageStatisticDayMutation", m)
- }
- type UsageStatisticHourFunc func(context.Context, *ent.UsageStatisticHourMutation) (ent.Value, error)
- func (f UsageStatisticHourFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.UsageStatisticHourMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.UsageStatisticHourMutation", m)
- }
- type UsageStatisticMonthFunc func(context.Context, *ent.UsageStatisticMonthMutation) (ent.Value, error)
- func (f UsageStatisticMonthFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.UsageStatisticMonthMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.UsageStatisticMonthMutation", m)
- }
- type UsageTotalFunc func(context.Context, *ent.UsageTotalMutation) (ent.Value, error)
- func (f UsageTotalFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.UsageTotalMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.UsageTotalMutation", m)
- }
- type WhatsappFunc func(context.Context, *ent.WhatsappMutation) (ent.Value, error)
- func (f WhatsappFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.WhatsappMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.WhatsappMutation", m)
- }
- type WhatsappChannelFunc func(context.Context, *ent.WhatsappChannelMutation) (ent.Value, error)
- func (f WhatsappChannelFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.WhatsappChannelMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.WhatsappChannelMutation", m)
- }
- type WorkExperienceFunc func(context.Context, *ent.WorkExperienceMutation) (ent.Value, error)
- func (f WorkExperienceFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.WorkExperienceMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.WorkExperienceMutation", m)
- }
- type WpChatroomFunc func(context.Context, *ent.WpChatroomMutation) (ent.Value, error)
- func (f WpChatroomFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.WpChatroomMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.WpChatroomMutation", m)
- }
- type WpChatroomMemberFunc func(context.Context, *ent.WpChatroomMemberMutation) (ent.Value, error)
- func (f WpChatroomMemberFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.WpChatroomMemberMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.WpChatroomMemberMutation", m)
- }
- type WxFunc func(context.Context, *ent.WxMutation) (ent.Value, error)
- func (f WxFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.WxMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.WxMutation", m)
- }
- type WxCardFunc func(context.Context, *ent.WxCardMutation) (ent.Value, error)
- func (f WxCardFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.WxCardMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.WxCardMutation", m)
- }
- type WxCardUserFunc func(context.Context, *ent.WxCardUserMutation) (ent.Value, error)
- func (f WxCardUserFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.WxCardUserMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.WxCardUserMutation", m)
- }
- type WxCardVisitFunc func(context.Context, *ent.WxCardVisitMutation) (ent.Value, error)
- func (f WxCardVisitFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if mv, ok := m.(*ent.WxCardVisitMutation); ok {
- return f(ctx, mv)
- }
- return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.WxCardVisitMutation", m)
- }
- type Condition func(context.Context, ent.Mutation) bool
- func And(first, second Condition, rest ...Condition) Condition {
- return func(ctx context.Context, m ent.Mutation) bool {
- if !first(ctx, m) || !second(ctx, m) {
- return false
- }
- for _, cond := range rest {
- if !cond(ctx, m) {
- return false
- }
- }
- return true
- }
- }
- func Or(first, second Condition, rest ...Condition) Condition {
- return func(ctx context.Context, m ent.Mutation) bool {
- if first(ctx, m) || second(ctx, m) {
- return true
- }
- for _, cond := range rest {
- if cond(ctx, m) {
- return true
- }
- }
- return false
- }
- }
- func Not(cond Condition) Condition {
- return func(ctx context.Context, m ent.Mutation) bool {
- return !cond(ctx, m)
- }
- }
- func HasOp(op ent.Op) Condition {
- return func(_ context.Context, m ent.Mutation) bool {
- return m.Op().Is(op)
- }
- }
- func HasAddedFields(field string, fields ...string) Condition {
- return func(_ context.Context, m ent.Mutation) bool {
- if _, exists := m.AddedField(field); !exists {
- return false
- }
- for _, field := range fields {
- if _, exists := m.AddedField(field); !exists {
- return false
- }
- }
- return true
- }
- }
- func HasClearedFields(field string, fields ...string) Condition {
- return func(_ context.Context, m ent.Mutation) bool {
- if exists := m.FieldCleared(field); !exists {
- return false
- }
- for _, field := range fields {
- if exists := m.FieldCleared(field); !exists {
- return false
- }
- }
- return true
- }
- }
- func HasFields(field string, fields ...string) Condition {
- return func(_ context.Context, m ent.Mutation) bool {
- if _, exists := m.Field(field); !exists {
- return false
- }
- for _, field := range fields {
- if _, exists := m.Field(field); !exists {
- return false
- }
- }
- return true
- }
- }
- func If(hk ent.Hook, cond Condition) ent.Hook {
- return func(next ent.Mutator) ent.Mutator {
- return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
- if cond(ctx, m) {
- return hk(next).Mutate(ctx, m)
- }
- return next.Mutate(ctx, m)
- })
- }
- }
- func On(hk ent.Hook, op ent.Op) ent.Hook {
- return If(hk, HasOp(op))
- }
- func Unless(hk ent.Hook, op ent.Op) ent.Hook {
- return If(hk, Not(HasOp(op)))
- }
- 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
- })
- }
- }
- func Reject(op ent.Op) ent.Hook {
- hk := FixedError(fmt.Errorf("%s operation is not allowed", op))
- return On(hk, op)
- }
- type Chain struct {
- hooks []ent.Hook
- }
- func NewChain(hooks ...ent.Hook) Chain {
- return Chain{append([]ent.Hook(nil), hooks...)}
- }
- func (c Chain) Hook() ent.Hook {
- return func(mutator ent.Mutator) ent.Mutator {
- for i := len(c.hooks) - 1; i >= 0; i-- {
- mutator = c.hooks[i](mutator)
- }
- return mutator
- }
- }
- func (c Chain) Append(hooks ...ent.Hook) Chain {
- newHooks := make([]ent.Hook, 0, len(c.hooks)+len(hooks))
- newHooks = append(newHooks, c.hooks...)
- newHooks = append(newHooks, hooks...)
- return Chain{newHooks}
- }
- func (c Chain) Extend(chain Chain) Chain {
- return c.Append(chain.hooks...)
- }
|