hook.go•18.6 kB
// Code generated by ent, DO NOT EDIT.
package hook
import (
"context"
"fmt"
"github.com/Southclaws/storyden/internal/ent"
)
// The AccountFunc type is an adapter to allow the use of ordinary
// function as Account mutator.
type AccountFunc func(context.Context, *ent.AccountMutation) (ent.Value, error)
// Mutate calls f(ctx, m).
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)
}
// The AccountFollowFunc type is an adapter to allow the use of ordinary
// function as AccountFollow mutator.
type AccountFollowFunc func(context.Context, *ent.AccountFollowMutation) (ent.Value, error)
// Mutate calls f(ctx, m).
func (f AccountFollowFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if mv, ok := m.(*ent.AccountFollowMutation); ok {
return f(ctx, mv)
}
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.AccountFollowMutation", m)
}
// The AccountRolesFunc type is an adapter to allow the use of ordinary
// function as AccountRoles mutator.
type AccountRolesFunc func(context.Context, *ent.AccountRolesMutation) (ent.Value, error)
// Mutate calls f(ctx, m).
func (f AccountRolesFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if mv, ok := m.(*ent.AccountRolesMutation); ok {
return f(ctx, mv)
}
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.AccountRolesMutation", m)
}
// The AssetFunc type is an adapter to allow the use of ordinary
// function as Asset mutator.
type AssetFunc func(context.Context, *ent.AssetMutation) (ent.Value, error)
// Mutate calls f(ctx, m).
func (f AssetFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if mv, ok := m.(*ent.AssetMutation); ok {
return f(ctx, mv)
}
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.AssetMutation", m)
}
// The AuthenticationFunc type is an adapter to allow the use of ordinary
// function as Authentication mutator.
type AuthenticationFunc func(context.Context, *ent.AuthenticationMutation) (ent.Value, error)
// Mutate calls f(ctx, m).
func (f AuthenticationFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if mv, ok := m.(*ent.AuthenticationMutation); ok {
return f(ctx, mv)
}
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.AuthenticationMutation", m)
}
// The CategoryFunc type is an adapter to allow the use of ordinary
// function as Category mutator.
type CategoryFunc func(context.Context, *ent.CategoryMutation) (ent.Value, error)
// Mutate calls f(ctx, m).
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)
}
// The CollectionFunc type is an adapter to allow the use of ordinary
// function as Collection mutator.
type CollectionFunc func(context.Context, *ent.CollectionMutation) (ent.Value, error)
// Mutate calls f(ctx, m).
func (f CollectionFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if mv, ok := m.(*ent.CollectionMutation); ok {
return f(ctx, mv)
}
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.CollectionMutation", m)
}
// The CollectionNodeFunc type is an adapter to allow the use of ordinary
// function as CollectionNode mutator.
type CollectionNodeFunc func(context.Context, *ent.CollectionNodeMutation) (ent.Value, error)
// Mutate calls f(ctx, m).
func (f CollectionNodeFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if mv, ok := m.(*ent.CollectionNodeMutation); ok {
return f(ctx, mv)
}
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.CollectionNodeMutation", m)
}
// The CollectionPostFunc type is an adapter to allow the use of ordinary
// function as CollectionPost mutator.
type CollectionPostFunc func(context.Context, *ent.CollectionPostMutation) (ent.Value, error)
// Mutate calls f(ctx, m).
func (f CollectionPostFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if mv, ok := m.(*ent.CollectionPostMutation); ok {
return f(ctx, mv)
}
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.CollectionPostMutation", m)
}
// The EmailFunc type is an adapter to allow the use of ordinary
// function as Email mutator.
type EmailFunc func(context.Context, *ent.EmailMutation) (ent.Value, error)
// Mutate calls f(ctx, m).
func (f EmailFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if mv, ok := m.(*ent.EmailMutation); ok {
return f(ctx, mv)
}
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.EmailMutation", m)
}
// The EventFunc type is an adapter to allow the use of ordinary
// function as Event mutator.
type EventFunc func(context.Context, *ent.EventMutation) (ent.Value, error)
// Mutate calls f(ctx, m).
func (f EventFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if mv, ok := m.(*ent.EventMutation); ok {
return f(ctx, mv)
}
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.EventMutation", m)
}
// The EventParticipantFunc type is an adapter to allow the use of ordinary
// function as EventParticipant mutator.
type EventParticipantFunc func(context.Context, *ent.EventParticipantMutation) (ent.Value, error)
// Mutate calls f(ctx, m).
func (f EventParticipantFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if mv, ok := m.(*ent.EventParticipantMutation); ok {
return f(ctx, mv)
}
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.EventParticipantMutation", m)
}
// The InvitationFunc type is an adapter to allow the use of ordinary
// function as Invitation mutator.
type InvitationFunc func(context.Context, *ent.InvitationMutation) (ent.Value, error)
// Mutate calls f(ctx, m).
func (f InvitationFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if mv, ok := m.(*ent.InvitationMutation); ok {
return f(ctx, mv)
}
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.InvitationMutation", m)
}
// The LikePostFunc type is an adapter to allow the use of ordinary
// function as LikePost mutator.
type LikePostFunc func(context.Context, *ent.LikePostMutation) (ent.Value, error)
// Mutate calls f(ctx, m).
func (f LikePostFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if mv, ok := m.(*ent.LikePostMutation); ok {
return f(ctx, mv)
}
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.LikePostMutation", m)
}
// The LinkFunc type is an adapter to allow the use of ordinary
// function as Link mutator.
type LinkFunc func(context.Context, *ent.LinkMutation) (ent.Value, error)
// Mutate calls f(ctx, m).
func (f LinkFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if mv, ok := m.(*ent.LinkMutation); ok {
return f(ctx, mv)
}
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.LinkMutation", m)
}
// The MentionProfileFunc type is an adapter to allow the use of ordinary
// function as MentionProfile mutator.
type MentionProfileFunc func(context.Context, *ent.MentionProfileMutation) (ent.Value, error)
// Mutate calls f(ctx, m).
func (f MentionProfileFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if mv, ok := m.(*ent.MentionProfileMutation); ok {
return f(ctx, mv)
}
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.MentionProfileMutation", m)
}
// The NodeFunc type is an adapter to allow the use of ordinary
// function as Node mutator.
type NodeFunc func(context.Context, *ent.NodeMutation) (ent.Value, error)
// Mutate calls f(ctx, m).
func (f NodeFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if mv, ok := m.(*ent.NodeMutation); ok {
return f(ctx, mv)
}
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.NodeMutation", m)
}
// The NotificationFunc type is an adapter to allow the use of ordinary
// function as Notification mutator.
type NotificationFunc func(context.Context, *ent.NotificationMutation) (ent.Value, error)
// Mutate calls f(ctx, m).
func (f NotificationFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if mv, ok := m.(*ent.NotificationMutation); ok {
return f(ctx, mv)
}
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.NotificationMutation", m)
}
// The PostFunc type is an adapter to allow the use of ordinary
// function as Post mutator.
type PostFunc func(context.Context, *ent.PostMutation) (ent.Value, error)
// Mutate calls f(ctx, m).
func (f PostFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if mv, ok := m.(*ent.PostMutation); ok {
return f(ctx, mv)
}
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.PostMutation", m)
}
// The PostReadFunc type is an adapter to allow the use of ordinary
// function as PostRead mutator.
type PostReadFunc func(context.Context, *ent.PostReadMutation) (ent.Value, error)
// Mutate calls f(ctx, m).
func (f PostReadFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if mv, ok := m.(*ent.PostReadMutation); ok {
return f(ctx, mv)
}
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.PostReadMutation", m)
}
// The PropertyFunc type is an adapter to allow the use of ordinary
// function as Property mutator.
type PropertyFunc func(context.Context, *ent.PropertyMutation) (ent.Value, error)
// Mutate calls f(ctx, m).
func (f PropertyFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if mv, ok := m.(*ent.PropertyMutation); ok {
return f(ctx, mv)
}
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.PropertyMutation", m)
}
// The PropertySchemaFunc type is an adapter to allow the use of ordinary
// function as PropertySchema mutator.
type PropertySchemaFunc func(context.Context, *ent.PropertySchemaMutation) (ent.Value, error)
// Mutate calls f(ctx, m).
func (f PropertySchemaFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if mv, ok := m.(*ent.PropertySchemaMutation); ok {
return f(ctx, mv)
}
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.PropertySchemaMutation", m)
}
// The PropertySchemaFieldFunc type is an adapter to allow the use of ordinary
// function as PropertySchemaField mutator.
type PropertySchemaFieldFunc func(context.Context, *ent.PropertySchemaFieldMutation) (ent.Value, error)
// Mutate calls f(ctx, m).
func (f PropertySchemaFieldFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if mv, ok := m.(*ent.PropertySchemaFieldMutation); ok {
return f(ctx, mv)
}
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.PropertySchemaFieldMutation", m)
}
// The QuestionFunc type is an adapter to allow the use of ordinary
// function as Question mutator.
type QuestionFunc func(context.Context, *ent.QuestionMutation) (ent.Value, error)
// Mutate calls f(ctx, m).
func (f QuestionFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if mv, ok := m.(*ent.QuestionMutation); ok {
return f(ctx, mv)
}
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.QuestionMutation", m)
}
// The ReactFunc type is an adapter to allow the use of ordinary
// function as React mutator.
type ReactFunc func(context.Context, *ent.ReactMutation) (ent.Value, error)
// Mutate calls f(ctx, m).
func (f ReactFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if mv, ok := m.(*ent.ReactMutation); ok {
return f(ctx, mv)
}
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.ReactMutation", m)
}
// The ReportFunc type is an adapter to allow the use of ordinary
// function as Report mutator.
type ReportFunc func(context.Context, *ent.ReportMutation) (ent.Value, error)
// Mutate calls f(ctx, m).
func (f ReportFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if mv, ok := m.(*ent.ReportMutation); ok {
return f(ctx, mv)
}
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.ReportMutation", m)
}
// The RoleFunc type is an adapter to allow the use of ordinary
// function as Role mutator.
type RoleFunc func(context.Context, *ent.RoleMutation) (ent.Value, error)
// Mutate calls f(ctx, m).
func (f RoleFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if mv, ok := m.(*ent.RoleMutation); ok {
return f(ctx, mv)
}
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.RoleMutation", m)
}
// The SessionFunc type is an adapter to allow the use of ordinary
// function as Session mutator.
type SessionFunc func(context.Context, *ent.SessionMutation) (ent.Value, error)
// Mutate calls f(ctx, m).
func (f SessionFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if mv, ok := m.(*ent.SessionMutation); ok {
return f(ctx, mv)
}
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.SessionMutation", m)
}
// The SettingFunc type is an adapter to allow the use of ordinary
// function as Setting mutator.
type SettingFunc func(context.Context, *ent.SettingMutation) (ent.Value, error)
// Mutate calls f(ctx, m).
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)
}
// The TagFunc type is an adapter to allow the use of ordinary
// function as Tag mutator.
type TagFunc func(context.Context, *ent.TagMutation) (ent.Value, error)
// Mutate calls f(ctx, m).
func (f TagFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) {
if mv, ok := m.(*ent.TagMutation); ok {
return f(ctx, mv)
}
return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.TagMutation", m)
}
// Condition is a hook condition function.
type Condition func(context.Context, ent.Mutation) bool
// And groups conditions with the AND operator.
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
}
}
// Or groups conditions with the OR operator.
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
}
}
// Not negates a given condition.
func Not(cond Condition) Condition {
return func(ctx context.Context, m ent.Mutation) bool {
return !cond(ctx, m)
}
}
// HasOp is a condition testing mutation operation.
func HasOp(op ent.Op) Condition {
return func(_ context.Context, m ent.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 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
}
}
// HasClearedFields is a condition validating `.FieldCleared` on fields.
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
}
}
// HasFields is a condition validating `.Field` on fields.
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
}
}
// If executes the given hook under condition.
//
// hook.If(ComputeAverage, And(HasFields(...), HasAddedFields(...)))
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)
})
}
}
// On executes the given hook only for the given operation.
//
// hook.On(Log, ent.Delete|ent.Create)
func On(hk ent.Hook, op ent.Op) ent.Hook {
return If(hk, HasOp(op))
}
// 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 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 {
// return []ent.Hook{
// Reject(ent.Delete|ent.Update),
// }
// }
func Reject(op ent.Op) ent.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 []ent.Hook
}
// NewChain creates a new chain of hooks.
func NewChain(hooks ...ent.Hook) Chain {
return Chain{append([]ent.Hook(nil), hooks...)}
}
// Hook chains the list of hooks and returns the final hook.
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
}
}
// Append extends a chain, adding the specified hook
// as the last ones in the mutation flow.
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}
}
// 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...)
}