| |
|
|
| package hook |
|
|
| import ( |
| "context" |
| "fmt" |
|
|
| "github.com/Wei-Shaw/sub2api/ent" |
| ) |
|
|
| |
| |
| 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 AccountFunc func(context.Context, *ent.AccountMutation) (ent.Value, error) |
|
|
| |
| func (f AccountFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { |
| if mv, ok := m.(*ent.AccountMutation); ok { |
| return f(ctx, mv) |
| } |
| return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.AccountMutation", m) |
| } |
|
|
| |
| |
| type AccountGroupFunc func(context.Context, *ent.AccountGroupMutation) (ent.Value, error) |
|
|
| |
| func (f AccountGroupFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { |
| if mv, ok := m.(*ent.AccountGroupMutation); ok { |
| return f(ctx, mv) |
| } |
| return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.AccountGroupMutation", m) |
| } |
|
|
| |
| |
| type AnnouncementFunc func(context.Context, *ent.AnnouncementMutation) (ent.Value, error) |
|
|
| |
| func (f AnnouncementFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { |
| if mv, ok := m.(*ent.AnnouncementMutation); ok { |
| return f(ctx, mv) |
| } |
| return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.AnnouncementMutation", m) |
| } |
|
|
| |
| |
| type AnnouncementReadFunc func(context.Context, *ent.AnnouncementReadMutation) (ent.Value, error) |
|
|
| |
| func (f AnnouncementReadFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { |
| if mv, ok := m.(*ent.AnnouncementReadMutation); ok { |
| return f(ctx, mv) |
| } |
| return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.AnnouncementReadMutation", m) |
| } |
|
|
| |
| |
| type ErrorPassthroughRuleFunc func(context.Context, *ent.ErrorPassthroughRuleMutation) (ent.Value, error) |
|
|
| |
| func (f ErrorPassthroughRuleFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { |
| if mv, ok := m.(*ent.ErrorPassthroughRuleMutation); ok { |
| return f(ctx, mv) |
| } |
| return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.ErrorPassthroughRuleMutation", m) |
| } |
|
|
| |
| |
| type GroupFunc func(context.Context, *ent.GroupMutation) (ent.Value, error) |
|
|
| |
| func (f GroupFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { |
| if mv, ok := m.(*ent.GroupMutation); ok { |
| return f(ctx, mv) |
| } |
| return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.GroupMutation", m) |
| } |
|
|
| |
| |
| type IdempotencyRecordFunc func(context.Context, *ent.IdempotencyRecordMutation) (ent.Value, error) |
|
|
| |
| func (f IdempotencyRecordFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { |
| if mv, ok := m.(*ent.IdempotencyRecordMutation); ok { |
| return f(ctx, mv) |
| } |
| return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.IdempotencyRecordMutation", m) |
| } |
|
|
| |
| |
| type PromoCodeFunc func(context.Context, *ent.PromoCodeMutation) (ent.Value, error) |
|
|
| |
| func (f PromoCodeFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { |
| if mv, ok := m.(*ent.PromoCodeMutation); ok { |
| return f(ctx, mv) |
| } |
| return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.PromoCodeMutation", m) |
| } |
|
|
| |
| |
| type PromoCodeUsageFunc func(context.Context, *ent.PromoCodeUsageMutation) (ent.Value, error) |
|
|
| |
| func (f PromoCodeUsageFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { |
| if mv, ok := m.(*ent.PromoCodeUsageMutation); ok { |
| return f(ctx, mv) |
| } |
| return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.PromoCodeUsageMutation", m) |
| } |
|
|
| |
| |
| type ProxyFunc func(context.Context, *ent.ProxyMutation) (ent.Value, error) |
|
|
| |
| func (f ProxyFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { |
| if mv, ok := m.(*ent.ProxyMutation); ok { |
| return f(ctx, mv) |
| } |
| return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.ProxyMutation", m) |
| } |
|
|
| |
| |
| type RedeemCodeFunc func(context.Context, *ent.RedeemCodeMutation) (ent.Value, error) |
|
|
| |
| func (f RedeemCodeFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { |
| if mv, ok := m.(*ent.RedeemCodeMutation); ok { |
| return f(ctx, mv) |
| } |
| return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.RedeemCodeMutation", m) |
| } |
|
|
| |
| |
| type SecuritySecretFunc func(context.Context, *ent.SecuritySecretMutation) (ent.Value, error) |
|
|
| |
| func (f SecuritySecretFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { |
| if mv, ok := m.(*ent.SecuritySecretMutation); ok { |
| return f(ctx, mv) |
| } |
| return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.SecuritySecretMutation", m) |
| } |
|
|
| |
| |
| type SettingFunc func(context.Context, *ent.SettingMutation) (ent.Value, error) |
|
|
| |
| func (f SettingFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { |
| if mv, ok := m.(*ent.SettingMutation); ok { |
| return f(ctx, mv) |
| } |
| return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.SettingMutation", m) |
| } |
|
|
| |
| |
| type UsageCleanupTaskFunc func(context.Context, *ent.UsageCleanupTaskMutation) (ent.Value, error) |
|
|
| |
| func (f UsageCleanupTaskFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { |
| if mv, ok := m.(*ent.UsageCleanupTaskMutation); ok { |
| return f(ctx, mv) |
| } |
| return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.UsageCleanupTaskMutation", m) |
| } |
|
|
| |
| |
| type UsageLogFunc func(context.Context, *ent.UsageLogMutation) (ent.Value, error) |
|
|
| |
| func (f UsageLogFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { |
| if mv, ok := m.(*ent.UsageLogMutation); ok { |
| return f(ctx, mv) |
| } |
| return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.UsageLogMutation", m) |
| } |
|
|
| |
| |
| type UserFunc func(context.Context, *ent.UserMutation) (ent.Value, error) |
|
|
| |
| func (f UserFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { |
| if mv, ok := m.(*ent.UserMutation); ok { |
| return f(ctx, mv) |
| } |
| return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.UserMutation", m) |
| } |
|
|
| |
| |
| type UserAllowedGroupFunc func(context.Context, *ent.UserAllowedGroupMutation) (ent.Value, error) |
|
|
| |
| func (f UserAllowedGroupFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { |
| if mv, ok := m.(*ent.UserAllowedGroupMutation); ok { |
| return f(ctx, mv) |
| } |
| return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.UserAllowedGroupMutation", m) |
| } |
|
|
| |
| |
| type UserAttributeDefinitionFunc func(context.Context, *ent.UserAttributeDefinitionMutation) (ent.Value, error) |
|
|
| |
| func (f UserAttributeDefinitionFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { |
| if mv, ok := m.(*ent.UserAttributeDefinitionMutation); ok { |
| return f(ctx, mv) |
| } |
| return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.UserAttributeDefinitionMutation", m) |
| } |
|
|
| |
| |
| type UserAttributeValueFunc func(context.Context, *ent.UserAttributeValueMutation) (ent.Value, error) |
|
|
| |
| func (f UserAttributeValueFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { |
| if mv, ok := m.(*ent.UserAttributeValueMutation); ok { |
| return f(ctx, mv) |
| } |
| return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.UserAttributeValueMutation", m) |
| } |
|
|
| |
| |
| type UserSubscriptionFunc func(context.Context, *ent.UserSubscriptionMutation) (ent.Value, error) |
|
|
| |
| func (f UserSubscriptionFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { |
| if mv, ok := m.(*ent.UserSubscriptionMutation); ok { |
| return f(ctx, mv) |
| } |
| return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.UserSubscriptionMutation", 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...) |
| } |
|
|