mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
* entc/hooks: initial work for mutations and hooks * ent/schema: adding policy to schema * ent: change op string to uint * entc: move entschema to runtime and enable smooth transition * entc/privacy: adding privacy template * all: goimports * intg/hooks: mutation client/tx and basic schema tests * ent/privacy: adding more verbose decisions * entc/gen: edge-ids getter and additional tests * all: regen assets * entc/gen: fix client hookd propagation * intg: add deletion example * intg/privacy: remove old entschema package * typed privacy * ent/privacy: hooks shouldn't be called on privacy deny * entc/gen: fix schema hooks invocation order * remove read policy from public api * update circleci go orb Co-authored-by: Ariel Mashraki <ariel@mashraki.co.il>
66 lines
1.7 KiB
Cheetah
66 lines
1.7 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 }}"
|
|
|
|
{{ range $n := $.Nodes }}
|
|
{{ $name := print $n.Name "Func" }}
|
|
{{ $type := print "*ent." $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 }}) (ent.Value, error)
|
|
|
|
// Mutate calls f(ctx, m).
|
|
func (f {{ $name }}) Mutate(ctx context.Context, m ent.Mutation) (ent.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, ent.Delete|ent.Create)
|
|
//
|
|
func On(hk ent.Hook, op ent.Op) ent.Hook {
|
|
return func(next ent.Mutator) ent.Mutator {
|
|
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.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() []ent.Hook {
|
|
// return []ent.Hook{
|
|
// Reject(ent.Delete|ent.Update),
|
|
// }
|
|
// }
|
|
//
|
|
func Reject(op ent.Op) ent.Hook {
|
|
return func(next ent.Mutator) ent.Mutator {
|
|
return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
|
|
if m.Op().Is(op) {
|
|
return nil, fmt.Errorf("%s operation is not allowed", m.Op())
|
|
}
|
|
return next.Mutate(ctx, m)
|
|
})
|
|
}
|
|
}
|
|
{{ end }} |