| |
|
|
| package hook |
|
|
| import ( |
| "context" |
| "fmt" |
|
|
| "github.com/openmeterio/openmeter/pkg/framework/entutils/testutils/ent2/db" |
| ) |
|
|
| |
| |
| type Example2Func func(context.Context, *db.Example2Mutation) (db.Value, error) |
|
|
| |
| func (f Example2Func) Mutate(ctx context.Context, m db.Mutation) (db.Value, error) { |
| if mv, ok := m.(*db.Example2Mutation); ok { |
| return f(ctx, mv) |
| } |
| return nil, fmt.Errorf("unexpected mutation type %T. expect *db.Example2Mutation", m) |
| } |
|
|
| |
| type Condition func(context.Context, db.Mutation) bool |
|
|
| |
| 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 |
| } |
| } |
|
|
| |
| 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 |
| } |
| } |
|
|
| |
| func Not(cond Condition) Condition { |
| return func(ctx context.Context, m db.Mutation) bool { |
| return !cond(ctx, m) |
| } |
| } |
|
|
| |
| func HasOp(op db.Op) Condition { |
| return func(_ context.Context, m db.Mutation) bool { |
| return m.Op().Is(op) |
| } |
| } |
|
|
| |
| 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 |
| } |
| } |
|
|
| |
| 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 |
| } |
| } |
|
|
| |
| 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 |
| } |
| } |
|
|
| |
| |
| |
| 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) |
| }) |
| } |
| } |
|
|
| |
| |
| |
| func On(hk db.Hook, op db.Op) db.Hook { |
| return If(hk, HasOp(op)) |
| } |
|
|
| |
| |
| |
| func Unless(hk db.Hook, op db.Op) db.Hook { |
| return If(hk, Not(HasOp(op))) |
| } |
|
|
| |
| 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 |
| }) |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| func Reject(op db.Op) db.Hook { |
| hk := FixedError(fmt.Errorf("%s operation is not allowed", op)) |
| return On(hk, op) |
| } |
|
|
| |
| |
| type Chain struct { |
| hooks []db.Hook |
| } |
|
|
| |
| func NewChain(hooks ...db.Hook) Chain { |
| return Chain{append([]db.Hook(nil), hooks...)} |
| } |
|
|
| |
| 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 |
| } |
| } |
|
|
| |
| |
| 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} |
| } |
|
|
| |
| |
| func (c Chain) Extend(chain Chain) Chain { |
| return c.Append(chain.hooks...) |
| } |
|
|