mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
* entc/gen: simplify hook chain creation Signed-off-by: Alex Snast <alexsn@fb.com> * circleci: upgrade orb versions Signed-off-by: Alex Snast <alexsn@fb.com>
106 lines
3.0 KiB
Cheetah
106 lines
3.0 KiB
Cheetah
{{/*
|
|
Copyright 2019-present Facebook Inc. All rights reserved.
|
|
This source code is licensed under the Apache 2.0 license found
|
|
in the LICENSE file in the root directory of this source tree.
|
|
*/}}
|
|
|
|
{{ define "hook" }}
|
|
|
|
{{ with extend $ "Package" "hook" }}
|
|
{{ template "header" . }}
|
|
{{ end }}
|
|
|
|
import "{{ $.Config.Package }}"
|
|
|
|
{{ $pkg := base $.Config.Package }}
|
|
|
|
{{ range $n := $.Nodes }}
|
|
{{ $name := print $n.Name "Func" }}
|
|
{{ $type := printf "*%s.%s" $pkg $n.MutationName }}
|
|
|
|
// The {{ $name }} type is an adapter to allow the use of ordinary
|
|
// function as {{ $n.Name }} mutator.
|
|
type {{ $name }} func(context.Context, {{ $type }}) ({{ $pkg }}.Value, error)
|
|
|
|
// Mutate calls f(ctx, m).
|
|
func (f {{ $name }}) Mutate(ctx context.Context, m {{ $pkg }}.Mutation) ({{ $pkg }}.Value, error) {
|
|
mv, ok := m.({{ $type }})
|
|
if !ok {
|
|
return nil, fmt.Errorf("unexpected mutation type %T. expect {{ $type }}", m)
|
|
}
|
|
return f(ctx, mv)
|
|
}
|
|
{{ end }}
|
|
|
|
// On executes the given hook only of the given operation.
|
|
//
|
|
// hook.On(Log, {{ $pkg }}.Delete|{{ $pkg }}.Create)
|
|
//
|
|
func On(hk {{ $pkg }}.Hook, op {{ $pkg }}.Op) {{ $pkg }}.Hook {
|
|
return func(next {{ $pkg }}.Mutator) {{ $pkg }}.Mutator {
|
|
return {{ $pkg }}.MutateFunc(func(ctx context.Context, m {{ $pkg }}.Mutation) ({{ $pkg }}.Value, error) {
|
|
if m.Op().Is(op) {
|
|
return hk(next).Mutate(ctx, m)
|
|
}
|
|
return next.Mutate(ctx, m)
|
|
})
|
|
}
|
|
}
|
|
|
|
// Reject returns a hook that rejects all operations that match op.
|
|
//
|
|
// func (T) Hooks() []{{ $pkg }}.Hook {
|
|
// return []{{ $pkg }}.Hook{
|
|
// Reject({{ $pkg }}.Delete|{{ $pkg }}.Update),
|
|
// }
|
|
// }
|
|
//
|
|
func Reject(op {{ $pkg }}.Op) {{ $pkg }}.Hook {
|
|
return func(next {{ $pkg }}.Mutator) {{ $pkg }}.Mutator {
|
|
return {{ $pkg }}.MutateFunc(func(ctx context.Context, m {{ $pkg }}.Mutation) ({{ $pkg }}.Value, error) {
|
|
if m.Op().Is(op) {
|
|
return nil, fmt.Errorf("%s operation is not allowed", m.Op())
|
|
}
|
|
return next.Mutate(ctx, m)
|
|
})
|
|
}
|
|
}
|
|
|
|
// 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 []{{ $pkg }}.Hook
|
|
}
|
|
|
|
// NewChain creates a new chain of hooks.
|
|
func NewChain(hooks ...{{ $pkg }}.Hook) Chain {
|
|
return Chain{append([]{{ $pkg }}.Hook(nil), hooks...)}
|
|
}
|
|
|
|
// Hook chains the list of hooks and returns the final hook.
|
|
func (c Chain) Hook() {{ $pkg }}.Hook {
|
|
return func(mutator {{ $pkg }}.Mutator) {{ $pkg }}.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 ...{{ $pkg }}.Hook) Chain {
|
|
newHooks := make([]{{ $pkg }}.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...)
|
|
}
|
|
|
|
{{ end }}
|