diff --git a/cmd/entc/entc.go b/cmd/entc/entc.go index 1524af360..e8ac2a1c9 100644 --- a/cmd/entc/entc.go +++ b/cmd/entc/entc.go @@ -75,6 +75,7 @@ func main() { var ( cfg gen.Config storage string + features []string templates []string idtype = idType(field.TypeInt) cmd = &cobra.Command{ @@ -86,7 +87,10 @@ func main() { ), Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, path []string) { - opts := []entc.Option{entc.Storage(storage)} + opts := []entc.Option{ + entc.Storage(storage), + entc.FeatureNames(features...), + } for _, tmpl := range templates { typ := "dir" if parts := strings.SplitN(tmpl, "=", 2); len(parts) > 1 { @@ -123,6 +127,7 @@ func main() { cmd.Flags().StringVar(&storage, "storage", "sql", "storage driver to support in codegen") cmd.Flags().StringVar(&cfg.Header, "header", "", "override codegen header") cmd.Flags().StringVar(&cfg.Target, "target", "", "target directory for codegen") + cmd.Flags().StringSliceVarP(&features, "feature", "", nil, "extend codegen with additional features") cmd.Flags().StringSliceVarP(&templates, "template", "", nil, "external templates to execute") return cmd }(), diff --git a/entc/entc.go b/entc/entc.go index 4a6eff0ac..6d37aec5a 100644 --- a/entc/entc.go +++ b/entc/entc.go @@ -119,6 +119,20 @@ func Storage(typ string) Option { } } +// FeatureNames enables sets of features by their names. +func FeatureNames(names ...string) Option { + return func(cfg *gen.Config) error { + for _, name := range names { + for _, feat := range gen.AllFeatures { + if name == feat.Name { + cfg.Features = append(cfg.Features, feat) + } + } + } + return nil + } +} + // Funcs specifies external functions to add to the template execution. func Funcs(funcMap template.FuncMap) Option { return func(cfg *gen.Config) error { diff --git a/entc/gen/feature.go b/entc/gen/feature.go new file mode 100644 index 000000000..cc08ed2db --- /dev/null +++ b/entc/gen/feature.go @@ -0,0 +1,70 @@ +// 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. + +package gen + +import ( + "os" + "path/filepath" +) + +var ( + // FeaturePrivacy provides a feature-flag for the privacy extension for ent. + FeaturePrivacy = Feature{ + Name: "privacy", + Stage: Alpha, + Default: false, + Description: "Privacy provides a privacy layer for ent through the schema configuration.", + cleanup: func(c *Config) error { + return os.RemoveAll(filepath.Join(c.Target, "privacy")) + }, + } + + // AllFeatures holds a list of all feature-flags. + AllFeatures = []Feature{ + FeaturePrivacy, + } +) + +// FeatureStage describes the stage of the codegen feature. +type FeatureStage int + +const ( + _ FeatureStage = iota + + // An Experimental feature is one that is in development, + // and it's actively tested in the integration environment. + Experimental + + // An Alpha feature is one that its initial development was + // finished, it's tested on the infra of the ent team, but + // we expect breaking-changes to its API. + Alpha + + // A Beta feature is an Alpha feature that was added to the entgo.io + // documentation, and no breaking-changes are expected for it. + Beta + + // A Stable feature is a Beta feature that was running a while on ent infra. + Stable +) + +// A Feature of the ent codegen. +type Feature struct { + // Name of the feature. + Name string + + // Stage of the feature. + Stage FeatureStage + + // Default values indicates if this feature is enabled by default. + Default bool + + // A Description of this feature. + Description string + + // cleanup used to cleanup all changes when a feature-flag is removed. + // e.g. delete files from previous codegen runs. + cleanup func(*Config) error +} diff --git a/entc/gen/graph.go b/entc/gen/graph.go index 0d9feef40..3277eb608 100644 --- a/entc/gen/graph.go +++ b/entc/gen/graph.go @@ -62,6 +62,10 @@ type ( // templates will need to provide the same FuncMap that was used for parsing // the template. Funcs template.FuncMap + + // Features defines a list of additional features to add to the codegen phase. + // For example, the PrivacyFeature. + Features []Feature } // Graph holds the nodes/entities of the loaded graph schema. Note that, it doesn't // hold the edges of the graph. Instead, each Type holds the edges for other Types. @@ -130,6 +134,13 @@ func (g *Graph) Gen() (err error) { check(ioutil.WriteFile(target, b.Bytes(), 0644), "write file %s", target) written = append(written, target) } + for _, f := range AllFeatures { + if f.cleanup == nil || g.featureEnabled(f) { + continue + } + err := f.cleanup(g.Config) + check(err, "cleanup %q feature assets", f.Name) + } // We can't run "imports" on files when the state is not completed. // Because, "goimports" will drop undefined package. Therefore, it's // suspended to the end of the writing. @@ -455,6 +466,16 @@ func (Config) ModuleInfo() (m debug.Module) { return } +// featureEnabled indicates if the given feature-flag is enabled. +func (c Config) featureEnabled(f Feature) bool { + for i := range c.Features { + if f.Name == c.Features[i].Name { + return true + } + } + return false +} + // PrepareEnv makes sure the generated directory (environment) // is suitable for loading the `ent` package (avoid cyclic imports). func PrepareEnv(c *Config) (undo func() error, err error) { diff --git a/entc/gen/template.go b/entc/gen/template.go index 13a84686b..5f6d84a27 100644 --- a/entc/gen/template.go +++ b/entc/gen/template.go @@ -30,7 +30,7 @@ type ( // the Graph object. GraphTemplate struct { Name string // template name. - Skip func(*Graph) bool // skip condition. + Skip func(*Graph) bool // skip condition (storage constraints or gated by a feature-flag). Format string // file name format. } ) @@ -118,6 +118,9 @@ var ( { Name: "privacy", Format: "privacy/privacy.go", + Skip: func(g *Graph) bool { + return !g.featureEnabled(FeaturePrivacy) + }, }, { Name: "runtime/ent", diff --git a/entc/integration/config/ent/privacy/privacy.go b/entc/integration/config/ent/privacy/privacy.go deleted file mode 100644 index bf7ef9c0d..000000000 --- a/entc/integration/config/ent/privacy/privacy.go +++ /dev/null @@ -1,239 +0,0 @@ -// 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. - -// Code generated by entc, DO NOT EDIT. - -package privacy - -import ( - "context" - "errors" - "fmt" - - "github.com/facebook/ent/entc/integration/config/ent" -) - -var ( - // Allow may be returned by rules to indicate that the policy - // evaluation should terminate with an allow decision. - Allow = errors.New("ent/privacy: allow rule") - - // Deny may be returned by rules to indicate that the policy - // evaluation should terminate with an deny decision. - Deny = errors.New("ent/privacy: deny rule") - - // Skip may be returned by rules to indicate that the policy - // evaluation should continue to the next rule. - Skip = errors.New("ent/privacy: skip rule") -) - -// Allowf returns an formatted wrapped Allow decision. -func Allowf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Allow)...) -} - -// Denyf returns an formatted wrapped Deny decision. -func Denyf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Deny)...) -} - -// Skipf returns an formatted wrapped Skip decision. -func Skipf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Skip)...) -} - -type decisionCtxKey struct{} - -// DecisionContext creates a decision context. -func DecisionContext(parent context.Context, decision error) context.Context { - if decision == nil || errors.Is(decision, Skip) { - return parent - } - return context.WithValue(parent, decisionCtxKey{}, decision) -} - -func decisionFromContext(ctx context.Context) (error, bool) { - decision, ok := ctx.Value(decisionCtxKey{}).(error) - if ok && errors.Is(decision, Allow) { - decision = nil - } - return decision, ok -} - -type ( - // QueryPolicy combines multiple query rules into a single policy. - QueryPolicy []QueryRule - - // QueryRule defines the interface deciding whether a - // query is allowed and optionally modify it. - QueryRule interface { - EvalQuery(context.Context, ent.Query) error - } -) - -// EvalQuery evaluates a query against a query policy. -func (policy QueryPolicy) EvalQuery(ctx context.Context, q ent.Query) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalQuery(ctx, q); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// QueryRuleFunc type is an adapter to allow the use of -// ordinary functions as query rules. -type QueryRuleFunc func(context.Context, ent.Query) error - -// Eval returns f(ctx, q). -func (f QueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - return f(ctx, q) -} - -type ( - // MutationPolicy combines multiple mutation rules into a single policy. - MutationPolicy []MutationRule - - // MutationRule defines the interface deciding whether a - // mutation is allowed and optionally modify it. - MutationRule interface { - EvalMutation(context.Context, ent.Mutation) error - } -) - -// EvalMutation evaluates a mutation against a mutation policy. -func (policy MutationPolicy) EvalMutation(ctx context.Context, m ent.Mutation) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalMutation(ctx, m); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// MutationRuleFunc type is an adapter to allow the use of -// ordinary functions as mutation rules. -type MutationRuleFunc func(context.Context, ent.Mutation) error - -// EvalMutation returns f(ctx, m). -func (f MutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - return f(ctx, m) -} - -// Policy groups query and mutation policies. -type Policy struct { - Query QueryPolicy - Mutation MutationPolicy -} - -// EvalQuery forwards evaluation to query policy. -func (policy Policy) EvalQuery(ctx context.Context, q ent.Query) error { - return policy.Query.EvalQuery(ctx, q) -} - -// EvalMutation forwards evaluation to mutation policy. -func (policy Policy) EvalMutation(ctx context.Context, m ent.Mutation) error { - return policy.Mutation.EvalMutation(ctx, m) -} - -// QueryMutationRule is the interface that groups query and mutation rules. -type QueryMutationRule interface { - QueryRule - MutationRule -} - -// AlwaysAllowRule returns a rule that returns an allow decision. -func AlwaysAllowRule() QueryMutationRule { - return fixedDecision{Allow} -} - -// AlwaysDenyRule returns a rule that returns a deny decision. -func AlwaysDenyRule() QueryMutationRule { - return fixedDecision{Deny} -} - -type fixedDecision struct { - decision error -} - -func (f fixedDecision) EvalQuery(context.Context, ent.Query) error { - return f.decision -} - -func (f fixedDecision) EvalMutation(context.Context, ent.Mutation) error { - return f.decision -} - -type contextDecision struct { - eval func(context.Context) error -} - -// ContextQueryMutationRule creates a query/mutation rule from a context eval func. -func ContextQueryMutationRule(eval func(context.Context) error) QueryMutationRule { - return contextDecision{eval} -} - -func (c contextDecision) EvalQuery(ctx context.Context, _ ent.Query) error { - return c.eval(ctx) -} - -func (c contextDecision) EvalMutation(ctx context.Context, _ ent.Mutation) error { - return c.eval(ctx) -} - -// OnMutationOperation evaluates the given rule only on a given mutation operation. -func OnMutationOperation(rule MutationRule, op ent.Op) MutationRule { - return MutationRuleFunc(func(ctx context.Context, m ent.Mutation) error { - if m.Op().Is(op) { - return rule.EvalMutation(ctx, m) - } - return Skip - }) -} - -// DenyMutationOperationRule returns a rule denying specified mutation operation. -func DenyMutationOperationRule(op ent.Op) MutationRule { - rule := MutationRuleFunc(func(_ context.Context, m ent.Mutation) error { - return Denyf("ent/privacy: operation %s is not allowed", m.Op()) - }) - return OnMutationOperation(rule, op) -} - -// The UserQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type UserQueryRuleFunc func(context.Context, *ent.UserQuery) error - -// EvalQuery return f(ctx, q). -func (f UserQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.UserQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.UserQuery", q) -} - -// The UserMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type UserMutationRuleFunc func(context.Context, *ent.UserMutation) error - -// EvalMutation calls f(ctx, m). -func (f UserMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.UserMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.UserMutation", m) -} diff --git a/entc/integration/customid/ent/privacy/privacy.go b/entc/integration/customid/ent/privacy/privacy.go deleted file mode 100644 index c0330799b..000000000 --- a/entc/integration/customid/ent/privacy/privacy.go +++ /dev/null @@ -1,335 +0,0 @@ -// 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. - -// Code generated by entc, DO NOT EDIT. - -package privacy - -import ( - "context" - "errors" - "fmt" - - "github.com/facebook/ent/entc/integration/customid/ent" -) - -var ( - // Allow may be returned by rules to indicate that the policy - // evaluation should terminate with an allow decision. - Allow = errors.New("ent/privacy: allow rule") - - // Deny may be returned by rules to indicate that the policy - // evaluation should terminate with an deny decision. - Deny = errors.New("ent/privacy: deny rule") - - // Skip may be returned by rules to indicate that the policy - // evaluation should continue to the next rule. - Skip = errors.New("ent/privacy: skip rule") -) - -// Allowf returns an formatted wrapped Allow decision. -func Allowf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Allow)...) -} - -// Denyf returns an formatted wrapped Deny decision. -func Denyf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Deny)...) -} - -// Skipf returns an formatted wrapped Skip decision. -func Skipf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Skip)...) -} - -type decisionCtxKey struct{} - -// DecisionContext creates a decision context. -func DecisionContext(parent context.Context, decision error) context.Context { - if decision == nil || errors.Is(decision, Skip) { - return parent - } - return context.WithValue(parent, decisionCtxKey{}, decision) -} - -func decisionFromContext(ctx context.Context) (error, bool) { - decision, ok := ctx.Value(decisionCtxKey{}).(error) - if ok && errors.Is(decision, Allow) { - decision = nil - } - return decision, ok -} - -type ( - // QueryPolicy combines multiple query rules into a single policy. - QueryPolicy []QueryRule - - // QueryRule defines the interface deciding whether a - // query is allowed and optionally modify it. - QueryRule interface { - EvalQuery(context.Context, ent.Query) error - } -) - -// EvalQuery evaluates a query against a query policy. -func (policy QueryPolicy) EvalQuery(ctx context.Context, q ent.Query) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalQuery(ctx, q); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// QueryRuleFunc type is an adapter to allow the use of -// ordinary functions as query rules. -type QueryRuleFunc func(context.Context, ent.Query) error - -// Eval returns f(ctx, q). -func (f QueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - return f(ctx, q) -} - -type ( - // MutationPolicy combines multiple mutation rules into a single policy. - MutationPolicy []MutationRule - - // MutationRule defines the interface deciding whether a - // mutation is allowed and optionally modify it. - MutationRule interface { - EvalMutation(context.Context, ent.Mutation) error - } -) - -// EvalMutation evaluates a mutation against a mutation policy. -func (policy MutationPolicy) EvalMutation(ctx context.Context, m ent.Mutation) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalMutation(ctx, m); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// MutationRuleFunc type is an adapter to allow the use of -// ordinary functions as mutation rules. -type MutationRuleFunc func(context.Context, ent.Mutation) error - -// EvalMutation returns f(ctx, m). -func (f MutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - return f(ctx, m) -} - -// Policy groups query and mutation policies. -type Policy struct { - Query QueryPolicy - Mutation MutationPolicy -} - -// EvalQuery forwards evaluation to query policy. -func (policy Policy) EvalQuery(ctx context.Context, q ent.Query) error { - return policy.Query.EvalQuery(ctx, q) -} - -// EvalMutation forwards evaluation to mutation policy. -func (policy Policy) EvalMutation(ctx context.Context, m ent.Mutation) error { - return policy.Mutation.EvalMutation(ctx, m) -} - -// QueryMutationRule is the interface that groups query and mutation rules. -type QueryMutationRule interface { - QueryRule - MutationRule -} - -// AlwaysAllowRule returns a rule that returns an allow decision. -func AlwaysAllowRule() QueryMutationRule { - return fixedDecision{Allow} -} - -// AlwaysDenyRule returns a rule that returns a deny decision. -func AlwaysDenyRule() QueryMutationRule { - return fixedDecision{Deny} -} - -type fixedDecision struct { - decision error -} - -func (f fixedDecision) EvalQuery(context.Context, ent.Query) error { - return f.decision -} - -func (f fixedDecision) EvalMutation(context.Context, ent.Mutation) error { - return f.decision -} - -type contextDecision struct { - eval func(context.Context) error -} - -// ContextQueryMutationRule creates a query/mutation rule from a context eval func. -func ContextQueryMutationRule(eval func(context.Context) error) QueryMutationRule { - return contextDecision{eval} -} - -func (c contextDecision) EvalQuery(ctx context.Context, _ ent.Query) error { - return c.eval(ctx) -} - -func (c contextDecision) EvalMutation(ctx context.Context, _ ent.Mutation) error { - return c.eval(ctx) -} - -// OnMutationOperation evaluates the given rule only on a given mutation operation. -func OnMutationOperation(rule MutationRule, op ent.Op) MutationRule { - return MutationRuleFunc(func(ctx context.Context, m ent.Mutation) error { - if m.Op().Is(op) { - return rule.EvalMutation(ctx, m) - } - return Skip - }) -} - -// DenyMutationOperationRule returns a rule denying specified mutation operation. -func DenyMutationOperationRule(op ent.Op) MutationRule { - rule := MutationRuleFunc(func(_ context.Context, m ent.Mutation) error { - return Denyf("ent/privacy: operation %s is not allowed", m.Op()) - }) - return OnMutationOperation(rule, op) -} - -// The BlobQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type BlobQueryRuleFunc func(context.Context, *ent.BlobQuery) error - -// EvalQuery return f(ctx, q). -func (f BlobQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.BlobQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.BlobQuery", q) -} - -// The BlobMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type BlobMutationRuleFunc func(context.Context, *ent.BlobMutation) error - -// EvalMutation calls f(ctx, m). -func (f BlobMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.BlobMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.BlobMutation", m) -} - -// The CarQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type CarQueryRuleFunc func(context.Context, *ent.CarQuery) error - -// EvalQuery return f(ctx, q). -func (f CarQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.CarQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.CarQuery", q) -} - -// The CarMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type CarMutationRuleFunc func(context.Context, *ent.CarMutation) error - -// EvalMutation calls f(ctx, m). -func (f CarMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.CarMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.CarMutation", m) -} - -// The GroupQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type GroupQueryRuleFunc func(context.Context, *ent.GroupQuery) error - -// EvalQuery return f(ctx, q). -func (f GroupQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.GroupQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.GroupQuery", q) -} - -// The GroupMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type GroupMutationRuleFunc func(context.Context, *ent.GroupMutation) error - -// EvalMutation calls f(ctx, m). -func (f GroupMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.GroupMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.GroupMutation", m) -} - -// The PetQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type PetQueryRuleFunc func(context.Context, *ent.PetQuery) error - -// EvalQuery return f(ctx, q). -func (f PetQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.PetQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.PetQuery", q) -} - -// The PetMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type PetMutationRuleFunc func(context.Context, *ent.PetMutation) error - -// EvalMutation calls f(ctx, m). -func (f PetMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.PetMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.PetMutation", m) -} - -// The UserQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type UserQueryRuleFunc func(context.Context, *ent.UserQuery) error - -// EvalQuery return f(ctx, q). -func (f UserQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.UserQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.UserQuery", q) -} - -// The UserMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type UserMutationRuleFunc func(context.Context, *ent.UserMutation) error - -// EvalMutation calls f(ctx, m). -func (f UserMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.UserMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.UserMutation", m) -} diff --git a/entc/integration/ent/privacy/privacy.go b/entc/integration/ent/privacy/privacy.go deleted file mode 100644 index 4417b5c5c..000000000 --- a/entc/integration/ent/privacy/privacy.go +++ /dev/null @@ -1,551 +0,0 @@ -// 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. - -// Code generated by entc, DO NOT EDIT. - -package privacy - -import ( - "context" - "errors" - "fmt" - - "github.com/facebook/ent/entc/integration/ent" -) - -var ( - // Allow may be returned by rules to indicate that the policy - // evaluation should terminate with an allow decision. - Allow = errors.New("ent/privacy: allow rule") - - // Deny may be returned by rules to indicate that the policy - // evaluation should terminate with an deny decision. - Deny = errors.New("ent/privacy: deny rule") - - // Skip may be returned by rules to indicate that the policy - // evaluation should continue to the next rule. - Skip = errors.New("ent/privacy: skip rule") -) - -// Allowf returns an formatted wrapped Allow decision. -func Allowf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Allow)...) -} - -// Denyf returns an formatted wrapped Deny decision. -func Denyf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Deny)...) -} - -// Skipf returns an formatted wrapped Skip decision. -func Skipf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Skip)...) -} - -type decisionCtxKey struct{} - -// DecisionContext creates a decision context. -func DecisionContext(parent context.Context, decision error) context.Context { - if decision == nil || errors.Is(decision, Skip) { - return parent - } - return context.WithValue(parent, decisionCtxKey{}, decision) -} - -func decisionFromContext(ctx context.Context) (error, bool) { - decision, ok := ctx.Value(decisionCtxKey{}).(error) - if ok && errors.Is(decision, Allow) { - decision = nil - } - return decision, ok -} - -type ( - // QueryPolicy combines multiple query rules into a single policy. - QueryPolicy []QueryRule - - // QueryRule defines the interface deciding whether a - // query is allowed and optionally modify it. - QueryRule interface { - EvalQuery(context.Context, ent.Query) error - } -) - -// EvalQuery evaluates a query against a query policy. -func (policy QueryPolicy) EvalQuery(ctx context.Context, q ent.Query) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalQuery(ctx, q); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// QueryRuleFunc type is an adapter to allow the use of -// ordinary functions as query rules. -type QueryRuleFunc func(context.Context, ent.Query) error - -// Eval returns f(ctx, q). -func (f QueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - return f(ctx, q) -} - -type ( - // MutationPolicy combines multiple mutation rules into a single policy. - MutationPolicy []MutationRule - - // MutationRule defines the interface deciding whether a - // mutation is allowed and optionally modify it. - MutationRule interface { - EvalMutation(context.Context, ent.Mutation) error - } -) - -// EvalMutation evaluates a mutation against a mutation policy. -func (policy MutationPolicy) EvalMutation(ctx context.Context, m ent.Mutation) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalMutation(ctx, m); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// MutationRuleFunc type is an adapter to allow the use of -// ordinary functions as mutation rules. -type MutationRuleFunc func(context.Context, ent.Mutation) error - -// EvalMutation returns f(ctx, m). -func (f MutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - return f(ctx, m) -} - -// Policy groups query and mutation policies. -type Policy struct { - Query QueryPolicy - Mutation MutationPolicy -} - -// EvalQuery forwards evaluation to query policy. -func (policy Policy) EvalQuery(ctx context.Context, q ent.Query) error { - return policy.Query.EvalQuery(ctx, q) -} - -// EvalMutation forwards evaluation to mutation policy. -func (policy Policy) EvalMutation(ctx context.Context, m ent.Mutation) error { - return policy.Mutation.EvalMutation(ctx, m) -} - -// QueryMutationRule is the interface that groups query and mutation rules. -type QueryMutationRule interface { - QueryRule - MutationRule -} - -// AlwaysAllowRule returns a rule that returns an allow decision. -func AlwaysAllowRule() QueryMutationRule { - return fixedDecision{Allow} -} - -// AlwaysDenyRule returns a rule that returns a deny decision. -func AlwaysDenyRule() QueryMutationRule { - return fixedDecision{Deny} -} - -type fixedDecision struct { - decision error -} - -func (f fixedDecision) EvalQuery(context.Context, ent.Query) error { - return f.decision -} - -func (f fixedDecision) EvalMutation(context.Context, ent.Mutation) error { - return f.decision -} - -type contextDecision struct { - eval func(context.Context) error -} - -// ContextQueryMutationRule creates a query/mutation rule from a context eval func. -func ContextQueryMutationRule(eval func(context.Context) error) QueryMutationRule { - return contextDecision{eval} -} - -func (c contextDecision) EvalQuery(ctx context.Context, _ ent.Query) error { - return c.eval(ctx) -} - -func (c contextDecision) EvalMutation(ctx context.Context, _ ent.Mutation) error { - return c.eval(ctx) -} - -// OnMutationOperation evaluates the given rule only on a given mutation operation. -func OnMutationOperation(rule MutationRule, op ent.Op) MutationRule { - return MutationRuleFunc(func(ctx context.Context, m ent.Mutation) error { - if m.Op().Is(op) { - return rule.EvalMutation(ctx, m) - } - return Skip - }) -} - -// DenyMutationOperationRule returns a rule denying specified mutation operation. -func DenyMutationOperationRule(op ent.Op) MutationRule { - rule := MutationRuleFunc(func(_ context.Context, m ent.Mutation) error { - return Denyf("ent/privacy: operation %s is not allowed", m.Op()) - }) - return OnMutationOperation(rule, op) -} - -// The CardQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type CardQueryRuleFunc func(context.Context, *ent.CardQuery) error - -// EvalQuery return f(ctx, q). -func (f CardQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.CardQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.CardQuery", q) -} - -// The CardMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type CardMutationRuleFunc func(context.Context, *ent.CardMutation) error - -// EvalMutation calls f(ctx, m). -func (f CardMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.CardMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.CardMutation", m) -} - -// The CommentQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type CommentQueryRuleFunc func(context.Context, *ent.CommentQuery) error - -// EvalQuery return f(ctx, q). -func (f CommentQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.CommentQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.CommentQuery", q) -} - -// The CommentMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type CommentMutationRuleFunc func(context.Context, *ent.CommentMutation) error - -// EvalMutation calls f(ctx, m). -func (f CommentMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.CommentMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.CommentMutation", m) -} - -// The FieldTypeQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type FieldTypeQueryRuleFunc func(context.Context, *ent.FieldTypeQuery) error - -// EvalQuery return f(ctx, q). -func (f FieldTypeQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.FieldTypeQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.FieldTypeQuery", q) -} - -// The FieldTypeMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type FieldTypeMutationRuleFunc func(context.Context, *ent.FieldTypeMutation) error - -// EvalMutation calls f(ctx, m). -func (f FieldTypeMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.FieldTypeMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.FieldTypeMutation", m) -} - -// The FileQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type FileQueryRuleFunc func(context.Context, *ent.FileQuery) error - -// EvalQuery return f(ctx, q). -func (f FileQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.FileQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.FileQuery", q) -} - -// The FileMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type FileMutationRuleFunc func(context.Context, *ent.FileMutation) error - -// EvalMutation calls f(ctx, m). -func (f FileMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.FileMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.FileMutation", m) -} - -// The FileTypeQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type FileTypeQueryRuleFunc func(context.Context, *ent.FileTypeQuery) error - -// EvalQuery return f(ctx, q). -func (f FileTypeQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.FileTypeQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.FileTypeQuery", q) -} - -// The FileTypeMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type FileTypeMutationRuleFunc func(context.Context, *ent.FileTypeMutation) error - -// EvalMutation calls f(ctx, m). -func (f FileTypeMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.FileTypeMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.FileTypeMutation", m) -} - -// The GoodsQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type GoodsQueryRuleFunc func(context.Context, *ent.GoodsQuery) error - -// EvalQuery return f(ctx, q). -func (f GoodsQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.GoodsQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.GoodsQuery", q) -} - -// The GoodsMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type GoodsMutationRuleFunc func(context.Context, *ent.GoodsMutation) error - -// EvalMutation calls f(ctx, m). -func (f GoodsMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.GoodsMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.GoodsMutation", m) -} - -// The GroupQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type GroupQueryRuleFunc func(context.Context, *ent.GroupQuery) error - -// EvalQuery return f(ctx, q). -func (f GroupQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.GroupQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.GroupQuery", q) -} - -// The GroupMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type GroupMutationRuleFunc func(context.Context, *ent.GroupMutation) error - -// EvalMutation calls f(ctx, m). -func (f GroupMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.GroupMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.GroupMutation", m) -} - -// The GroupInfoQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type GroupInfoQueryRuleFunc func(context.Context, *ent.GroupInfoQuery) error - -// EvalQuery return f(ctx, q). -func (f GroupInfoQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.GroupInfoQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.GroupInfoQuery", q) -} - -// The GroupInfoMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type GroupInfoMutationRuleFunc func(context.Context, *ent.GroupInfoMutation) error - -// EvalMutation calls f(ctx, m). -func (f GroupInfoMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.GroupInfoMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.GroupInfoMutation", m) -} - -// The ItemQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type ItemQueryRuleFunc func(context.Context, *ent.ItemQuery) error - -// EvalQuery return f(ctx, q). -func (f ItemQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.ItemQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.ItemQuery", q) -} - -// The ItemMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type ItemMutationRuleFunc func(context.Context, *ent.ItemMutation) error - -// EvalMutation calls f(ctx, m). -func (f ItemMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.ItemMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.ItemMutation", m) -} - -// The NodeQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type NodeQueryRuleFunc func(context.Context, *ent.NodeQuery) error - -// EvalQuery return f(ctx, q). -func (f NodeQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.NodeQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.NodeQuery", q) -} - -// The NodeMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type NodeMutationRuleFunc func(context.Context, *ent.NodeMutation) error - -// EvalMutation calls f(ctx, m). -func (f NodeMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.NodeMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.NodeMutation", m) -} - -// The PetQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type PetQueryRuleFunc func(context.Context, *ent.PetQuery) error - -// EvalQuery return f(ctx, q). -func (f PetQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.PetQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.PetQuery", q) -} - -// The PetMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type PetMutationRuleFunc func(context.Context, *ent.PetMutation) error - -// EvalMutation calls f(ctx, m). -func (f PetMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.PetMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.PetMutation", m) -} - -// The SpecQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type SpecQueryRuleFunc func(context.Context, *ent.SpecQuery) error - -// EvalQuery return f(ctx, q). -func (f SpecQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.SpecQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.SpecQuery", q) -} - -// The SpecMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type SpecMutationRuleFunc func(context.Context, *ent.SpecMutation) error - -// EvalMutation calls f(ctx, m). -func (f SpecMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.SpecMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.SpecMutation", m) -} - -// The TaskQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type TaskQueryRuleFunc func(context.Context, *ent.TaskQuery) error - -// EvalQuery return f(ctx, q). -func (f TaskQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.TaskQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.TaskQuery", q) -} - -// The TaskMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type TaskMutationRuleFunc func(context.Context, *ent.TaskMutation) error - -// EvalMutation calls f(ctx, m). -func (f TaskMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.TaskMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.TaskMutation", m) -} - -// The UserQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type UserQueryRuleFunc func(context.Context, *ent.UserQuery) error - -// EvalQuery return f(ctx, q). -func (f UserQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.UserQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.UserQuery", q) -} - -// The UserMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type UserMutationRuleFunc func(context.Context, *ent.UserMutation) error - -// EvalMutation calls f(ctx, m). -func (f UserMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.UserMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.UserMutation", m) -} diff --git a/entc/integration/gremlin/ent/privacy/privacy.go b/entc/integration/gremlin/ent/privacy/privacy.go deleted file mode 100644 index 351364736..000000000 --- a/entc/integration/gremlin/ent/privacy/privacy.go +++ /dev/null @@ -1,551 +0,0 @@ -// 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. - -// Code generated by entc, DO NOT EDIT. - -package privacy - -import ( - "context" - "errors" - "fmt" - - "github.com/facebook/ent/entc/integration/gremlin/ent" -) - -var ( - // Allow may be returned by rules to indicate that the policy - // evaluation should terminate with an allow decision. - Allow = errors.New("ent/privacy: allow rule") - - // Deny may be returned by rules to indicate that the policy - // evaluation should terminate with an deny decision. - Deny = errors.New("ent/privacy: deny rule") - - // Skip may be returned by rules to indicate that the policy - // evaluation should continue to the next rule. - Skip = errors.New("ent/privacy: skip rule") -) - -// Allowf returns an formatted wrapped Allow decision. -func Allowf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Allow)...) -} - -// Denyf returns an formatted wrapped Deny decision. -func Denyf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Deny)...) -} - -// Skipf returns an formatted wrapped Skip decision. -func Skipf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Skip)...) -} - -type decisionCtxKey struct{} - -// DecisionContext creates a decision context. -func DecisionContext(parent context.Context, decision error) context.Context { - if decision == nil || errors.Is(decision, Skip) { - return parent - } - return context.WithValue(parent, decisionCtxKey{}, decision) -} - -func decisionFromContext(ctx context.Context) (error, bool) { - decision, ok := ctx.Value(decisionCtxKey{}).(error) - if ok && errors.Is(decision, Allow) { - decision = nil - } - return decision, ok -} - -type ( - // QueryPolicy combines multiple query rules into a single policy. - QueryPolicy []QueryRule - - // QueryRule defines the interface deciding whether a - // query is allowed and optionally modify it. - QueryRule interface { - EvalQuery(context.Context, ent.Query) error - } -) - -// EvalQuery evaluates a query against a query policy. -func (policy QueryPolicy) EvalQuery(ctx context.Context, q ent.Query) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalQuery(ctx, q); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// QueryRuleFunc type is an adapter to allow the use of -// ordinary functions as query rules. -type QueryRuleFunc func(context.Context, ent.Query) error - -// Eval returns f(ctx, q). -func (f QueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - return f(ctx, q) -} - -type ( - // MutationPolicy combines multiple mutation rules into a single policy. - MutationPolicy []MutationRule - - // MutationRule defines the interface deciding whether a - // mutation is allowed and optionally modify it. - MutationRule interface { - EvalMutation(context.Context, ent.Mutation) error - } -) - -// EvalMutation evaluates a mutation against a mutation policy. -func (policy MutationPolicy) EvalMutation(ctx context.Context, m ent.Mutation) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalMutation(ctx, m); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// MutationRuleFunc type is an adapter to allow the use of -// ordinary functions as mutation rules. -type MutationRuleFunc func(context.Context, ent.Mutation) error - -// EvalMutation returns f(ctx, m). -func (f MutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - return f(ctx, m) -} - -// Policy groups query and mutation policies. -type Policy struct { - Query QueryPolicy - Mutation MutationPolicy -} - -// EvalQuery forwards evaluation to query policy. -func (policy Policy) EvalQuery(ctx context.Context, q ent.Query) error { - return policy.Query.EvalQuery(ctx, q) -} - -// EvalMutation forwards evaluation to mutation policy. -func (policy Policy) EvalMutation(ctx context.Context, m ent.Mutation) error { - return policy.Mutation.EvalMutation(ctx, m) -} - -// QueryMutationRule is the interface that groups query and mutation rules. -type QueryMutationRule interface { - QueryRule - MutationRule -} - -// AlwaysAllowRule returns a rule that returns an allow decision. -func AlwaysAllowRule() QueryMutationRule { - return fixedDecision{Allow} -} - -// AlwaysDenyRule returns a rule that returns a deny decision. -func AlwaysDenyRule() QueryMutationRule { - return fixedDecision{Deny} -} - -type fixedDecision struct { - decision error -} - -func (f fixedDecision) EvalQuery(context.Context, ent.Query) error { - return f.decision -} - -func (f fixedDecision) EvalMutation(context.Context, ent.Mutation) error { - return f.decision -} - -type contextDecision struct { - eval func(context.Context) error -} - -// ContextQueryMutationRule creates a query/mutation rule from a context eval func. -func ContextQueryMutationRule(eval func(context.Context) error) QueryMutationRule { - return contextDecision{eval} -} - -func (c contextDecision) EvalQuery(ctx context.Context, _ ent.Query) error { - return c.eval(ctx) -} - -func (c contextDecision) EvalMutation(ctx context.Context, _ ent.Mutation) error { - return c.eval(ctx) -} - -// OnMutationOperation evaluates the given rule only on a given mutation operation. -func OnMutationOperation(rule MutationRule, op ent.Op) MutationRule { - return MutationRuleFunc(func(ctx context.Context, m ent.Mutation) error { - if m.Op().Is(op) { - return rule.EvalMutation(ctx, m) - } - return Skip - }) -} - -// DenyMutationOperationRule returns a rule denying specified mutation operation. -func DenyMutationOperationRule(op ent.Op) MutationRule { - rule := MutationRuleFunc(func(_ context.Context, m ent.Mutation) error { - return Denyf("ent/privacy: operation %s is not allowed", m.Op()) - }) - return OnMutationOperation(rule, op) -} - -// The CardQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type CardQueryRuleFunc func(context.Context, *ent.CardQuery) error - -// EvalQuery return f(ctx, q). -func (f CardQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.CardQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.CardQuery", q) -} - -// The CardMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type CardMutationRuleFunc func(context.Context, *ent.CardMutation) error - -// EvalMutation calls f(ctx, m). -func (f CardMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.CardMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.CardMutation", m) -} - -// The CommentQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type CommentQueryRuleFunc func(context.Context, *ent.CommentQuery) error - -// EvalQuery return f(ctx, q). -func (f CommentQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.CommentQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.CommentQuery", q) -} - -// The CommentMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type CommentMutationRuleFunc func(context.Context, *ent.CommentMutation) error - -// EvalMutation calls f(ctx, m). -func (f CommentMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.CommentMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.CommentMutation", m) -} - -// The FieldTypeQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type FieldTypeQueryRuleFunc func(context.Context, *ent.FieldTypeQuery) error - -// EvalQuery return f(ctx, q). -func (f FieldTypeQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.FieldTypeQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.FieldTypeQuery", q) -} - -// The FieldTypeMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type FieldTypeMutationRuleFunc func(context.Context, *ent.FieldTypeMutation) error - -// EvalMutation calls f(ctx, m). -func (f FieldTypeMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.FieldTypeMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.FieldTypeMutation", m) -} - -// The FileQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type FileQueryRuleFunc func(context.Context, *ent.FileQuery) error - -// EvalQuery return f(ctx, q). -func (f FileQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.FileQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.FileQuery", q) -} - -// The FileMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type FileMutationRuleFunc func(context.Context, *ent.FileMutation) error - -// EvalMutation calls f(ctx, m). -func (f FileMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.FileMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.FileMutation", m) -} - -// The FileTypeQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type FileTypeQueryRuleFunc func(context.Context, *ent.FileTypeQuery) error - -// EvalQuery return f(ctx, q). -func (f FileTypeQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.FileTypeQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.FileTypeQuery", q) -} - -// The FileTypeMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type FileTypeMutationRuleFunc func(context.Context, *ent.FileTypeMutation) error - -// EvalMutation calls f(ctx, m). -func (f FileTypeMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.FileTypeMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.FileTypeMutation", m) -} - -// The GoodsQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type GoodsQueryRuleFunc func(context.Context, *ent.GoodsQuery) error - -// EvalQuery return f(ctx, q). -func (f GoodsQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.GoodsQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.GoodsQuery", q) -} - -// The GoodsMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type GoodsMutationRuleFunc func(context.Context, *ent.GoodsMutation) error - -// EvalMutation calls f(ctx, m). -func (f GoodsMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.GoodsMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.GoodsMutation", m) -} - -// The GroupQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type GroupQueryRuleFunc func(context.Context, *ent.GroupQuery) error - -// EvalQuery return f(ctx, q). -func (f GroupQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.GroupQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.GroupQuery", q) -} - -// The GroupMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type GroupMutationRuleFunc func(context.Context, *ent.GroupMutation) error - -// EvalMutation calls f(ctx, m). -func (f GroupMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.GroupMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.GroupMutation", m) -} - -// The GroupInfoQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type GroupInfoQueryRuleFunc func(context.Context, *ent.GroupInfoQuery) error - -// EvalQuery return f(ctx, q). -func (f GroupInfoQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.GroupInfoQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.GroupInfoQuery", q) -} - -// The GroupInfoMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type GroupInfoMutationRuleFunc func(context.Context, *ent.GroupInfoMutation) error - -// EvalMutation calls f(ctx, m). -func (f GroupInfoMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.GroupInfoMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.GroupInfoMutation", m) -} - -// The ItemQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type ItemQueryRuleFunc func(context.Context, *ent.ItemQuery) error - -// EvalQuery return f(ctx, q). -func (f ItemQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.ItemQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.ItemQuery", q) -} - -// The ItemMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type ItemMutationRuleFunc func(context.Context, *ent.ItemMutation) error - -// EvalMutation calls f(ctx, m). -func (f ItemMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.ItemMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.ItemMutation", m) -} - -// The NodeQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type NodeQueryRuleFunc func(context.Context, *ent.NodeQuery) error - -// EvalQuery return f(ctx, q). -func (f NodeQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.NodeQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.NodeQuery", q) -} - -// The NodeMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type NodeMutationRuleFunc func(context.Context, *ent.NodeMutation) error - -// EvalMutation calls f(ctx, m). -func (f NodeMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.NodeMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.NodeMutation", m) -} - -// The PetQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type PetQueryRuleFunc func(context.Context, *ent.PetQuery) error - -// EvalQuery return f(ctx, q). -func (f PetQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.PetQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.PetQuery", q) -} - -// The PetMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type PetMutationRuleFunc func(context.Context, *ent.PetMutation) error - -// EvalMutation calls f(ctx, m). -func (f PetMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.PetMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.PetMutation", m) -} - -// The SpecQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type SpecQueryRuleFunc func(context.Context, *ent.SpecQuery) error - -// EvalQuery return f(ctx, q). -func (f SpecQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.SpecQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.SpecQuery", q) -} - -// The SpecMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type SpecMutationRuleFunc func(context.Context, *ent.SpecMutation) error - -// EvalMutation calls f(ctx, m). -func (f SpecMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.SpecMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.SpecMutation", m) -} - -// The TaskQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type TaskQueryRuleFunc func(context.Context, *ent.TaskQuery) error - -// EvalQuery return f(ctx, q). -func (f TaskQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.TaskQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.TaskQuery", q) -} - -// The TaskMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type TaskMutationRuleFunc func(context.Context, *ent.TaskMutation) error - -// EvalMutation calls f(ctx, m). -func (f TaskMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.TaskMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.TaskMutation", m) -} - -// The UserQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type UserQueryRuleFunc func(context.Context, *ent.UserQuery) error - -// EvalQuery return f(ctx, q). -func (f UserQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.UserQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.UserQuery", q) -} - -// The UserMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type UserMutationRuleFunc func(context.Context, *ent.UserMutation) error - -// EvalMutation calls f(ctx, m). -func (f UserMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.UserMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.UserMutation", m) -} diff --git a/entc/integration/hooks/ent/privacy/privacy.go b/entc/integration/hooks/ent/privacy/privacy.go deleted file mode 100644 index 0bbd0037c..000000000 --- a/entc/integration/hooks/ent/privacy/privacy.go +++ /dev/null @@ -1,263 +0,0 @@ -// 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. - -// Code generated by entc, DO NOT EDIT. - -package privacy - -import ( - "context" - "errors" - "fmt" - - "github.com/facebook/ent/entc/integration/hooks/ent" -) - -var ( - // Allow may be returned by rules to indicate that the policy - // evaluation should terminate with an allow decision. - Allow = errors.New("ent/privacy: allow rule") - - // Deny may be returned by rules to indicate that the policy - // evaluation should terminate with an deny decision. - Deny = errors.New("ent/privacy: deny rule") - - // Skip may be returned by rules to indicate that the policy - // evaluation should continue to the next rule. - Skip = errors.New("ent/privacy: skip rule") -) - -// Allowf returns an formatted wrapped Allow decision. -func Allowf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Allow)...) -} - -// Denyf returns an formatted wrapped Deny decision. -func Denyf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Deny)...) -} - -// Skipf returns an formatted wrapped Skip decision. -func Skipf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Skip)...) -} - -type decisionCtxKey struct{} - -// DecisionContext creates a decision context. -func DecisionContext(parent context.Context, decision error) context.Context { - if decision == nil || errors.Is(decision, Skip) { - return parent - } - return context.WithValue(parent, decisionCtxKey{}, decision) -} - -func decisionFromContext(ctx context.Context) (error, bool) { - decision, ok := ctx.Value(decisionCtxKey{}).(error) - if ok && errors.Is(decision, Allow) { - decision = nil - } - return decision, ok -} - -type ( - // QueryPolicy combines multiple query rules into a single policy. - QueryPolicy []QueryRule - - // QueryRule defines the interface deciding whether a - // query is allowed and optionally modify it. - QueryRule interface { - EvalQuery(context.Context, ent.Query) error - } -) - -// EvalQuery evaluates a query against a query policy. -func (policy QueryPolicy) EvalQuery(ctx context.Context, q ent.Query) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalQuery(ctx, q); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// QueryRuleFunc type is an adapter to allow the use of -// ordinary functions as query rules. -type QueryRuleFunc func(context.Context, ent.Query) error - -// Eval returns f(ctx, q). -func (f QueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - return f(ctx, q) -} - -type ( - // MutationPolicy combines multiple mutation rules into a single policy. - MutationPolicy []MutationRule - - // MutationRule defines the interface deciding whether a - // mutation is allowed and optionally modify it. - MutationRule interface { - EvalMutation(context.Context, ent.Mutation) error - } -) - -// EvalMutation evaluates a mutation against a mutation policy. -func (policy MutationPolicy) EvalMutation(ctx context.Context, m ent.Mutation) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalMutation(ctx, m); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// MutationRuleFunc type is an adapter to allow the use of -// ordinary functions as mutation rules. -type MutationRuleFunc func(context.Context, ent.Mutation) error - -// EvalMutation returns f(ctx, m). -func (f MutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - return f(ctx, m) -} - -// Policy groups query and mutation policies. -type Policy struct { - Query QueryPolicy - Mutation MutationPolicy -} - -// EvalQuery forwards evaluation to query policy. -func (policy Policy) EvalQuery(ctx context.Context, q ent.Query) error { - return policy.Query.EvalQuery(ctx, q) -} - -// EvalMutation forwards evaluation to mutation policy. -func (policy Policy) EvalMutation(ctx context.Context, m ent.Mutation) error { - return policy.Mutation.EvalMutation(ctx, m) -} - -// QueryMutationRule is the interface that groups query and mutation rules. -type QueryMutationRule interface { - QueryRule - MutationRule -} - -// AlwaysAllowRule returns a rule that returns an allow decision. -func AlwaysAllowRule() QueryMutationRule { - return fixedDecision{Allow} -} - -// AlwaysDenyRule returns a rule that returns a deny decision. -func AlwaysDenyRule() QueryMutationRule { - return fixedDecision{Deny} -} - -type fixedDecision struct { - decision error -} - -func (f fixedDecision) EvalQuery(context.Context, ent.Query) error { - return f.decision -} - -func (f fixedDecision) EvalMutation(context.Context, ent.Mutation) error { - return f.decision -} - -type contextDecision struct { - eval func(context.Context) error -} - -// ContextQueryMutationRule creates a query/mutation rule from a context eval func. -func ContextQueryMutationRule(eval func(context.Context) error) QueryMutationRule { - return contextDecision{eval} -} - -func (c contextDecision) EvalQuery(ctx context.Context, _ ent.Query) error { - return c.eval(ctx) -} - -func (c contextDecision) EvalMutation(ctx context.Context, _ ent.Mutation) error { - return c.eval(ctx) -} - -// OnMutationOperation evaluates the given rule only on a given mutation operation. -func OnMutationOperation(rule MutationRule, op ent.Op) MutationRule { - return MutationRuleFunc(func(ctx context.Context, m ent.Mutation) error { - if m.Op().Is(op) { - return rule.EvalMutation(ctx, m) - } - return Skip - }) -} - -// DenyMutationOperationRule returns a rule denying specified mutation operation. -func DenyMutationOperationRule(op ent.Op) MutationRule { - rule := MutationRuleFunc(func(_ context.Context, m ent.Mutation) error { - return Denyf("ent/privacy: operation %s is not allowed", m.Op()) - }) - return OnMutationOperation(rule, op) -} - -// The CardQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type CardQueryRuleFunc func(context.Context, *ent.CardQuery) error - -// EvalQuery return f(ctx, q). -func (f CardQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.CardQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.CardQuery", q) -} - -// The CardMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type CardMutationRuleFunc func(context.Context, *ent.CardMutation) error - -// EvalMutation calls f(ctx, m). -func (f CardMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.CardMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.CardMutation", m) -} - -// The UserQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type UserQueryRuleFunc func(context.Context, *ent.UserQuery) error - -// EvalQuery return f(ctx, q). -func (f UserQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.UserQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.UserQuery", q) -} - -// The UserMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type UserMutationRuleFunc func(context.Context, *ent.UserMutation) error - -// EvalMutation calls f(ctx, m). -func (f UserMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.UserMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.UserMutation", m) -} diff --git a/entc/integration/idtype/ent/privacy/privacy.go b/entc/integration/idtype/ent/privacy/privacy.go deleted file mode 100644 index b014b10a7..000000000 --- a/entc/integration/idtype/ent/privacy/privacy.go +++ /dev/null @@ -1,239 +0,0 @@ -// 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. - -// Code generated by entc, DO NOT EDIT. - -package privacy - -import ( - "context" - "errors" - "fmt" - - "github.com/facebook/ent/entc/integration/idtype/ent" -) - -var ( - // Allow may be returned by rules to indicate that the policy - // evaluation should terminate with an allow decision. - Allow = errors.New("ent/privacy: allow rule") - - // Deny may be returned by rules to indicate that the policy - // evaluation should terminate with an deny decision. - Deny = errors.New("ent/privacy: deny rule") - - // Skip may be returned by rules to indicate that the policy - // evaluation should continue to the next rule. - Skip = errors.New("ent/privacy: skip rule") -) - -// Allowf returns an formatted wrapped Allow decision. -func Allowf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Allow)...) -} - -// Denyf returns an formatted wrapped Deny decision. -func Denyf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Deny)...) -} - -// Skipf returns an formatted wrapped Skip decision. -func Skipf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Skip)...) -} - -type decisionCtxKey struct{} - -// DecisionContext creates a decision context. -func DecisionContext(parent context.Context, decision error) context.Context { - if decision == nil || errors.Is(decision, Skip) { - return parent - } - return context.WithValue(parent, decisionCtxKey{}, decision) -} - -func decisionFromContext(ctx context.Context) (error, bool) { - decision, ok := ctx.Value(decisionCtxKey{}).(error) - if ok && errors.Is(decision, Allow) { - decision = nil - } - return decision, ok -} - -type ( - // QueryPolicy combines multiple query rules into a single policy. - QueryPolicy []QueryRule - - // QueryRule defines the interface deciding whether a - // query is allowed and optionally modify it. - QueryRule interface { - EvalQuery(context.Context, ent.Query) error - } -) - -// EvalQuery evaluates a query against a query policy. -func (policy QueryPolicy) EvalQuery(ctx context.Context, q ent.Query) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalQuery(ctx, q); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// QueryRuleFunc type is an adapter to allow the use of -// ordinary functions as query rules. -type QueryRuleFunc func(context.Context, ent.Query) error - -// Eval returns f(ctx, q). -func (f QueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - return f(ctx, q) -} - -type ( - // MutationPolicy combines multiple mutation rules into a single policy. - MutationPolicy []MutationRule - - // MutationRule defines the interface deciding whether a - // mutation is allowed and optionally modify it. - MutationRule interface { - EvalMutation(context.Context, ent.Mutation) error - } -) - -// EvalMutation evaluates a mutation against a mutation policy. -func (policy MutationPolicy) EvalMutation(ctx context.Context, m ent.Mutation) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalMutation(ctx, m); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// MutationRuleFunc type is an adapter to allow the use of -// ordinary functions as mutation rules. -type MutationRuleFunc func(context.Context, ent.Mutation) error - -// EvalMutation returns f(ctx, m). -func (f MutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - return f(ctx, m) -} - -// Policy groups query and mutation policies. -type Policy struct { - Query QueryPolicy - Mutation MutationPolicy -} - -// EvalQuery forwards evaluation to query policy. -func (policy Policy) EvalQuery(ctx context.Context, q ent.Query) error { - return policy.Query.EvalQuery(ctx, q) -} - -// EvalMutation forwards evaluation to mutation policy. -func (policy Policy) EvalMutation(ctx context.Context, m ent.Mutation) error { - return policy.Mutation.EvalMutation(ctx, m) -} - -// QueryMutationRule is the interface that groups query and mutation rules. -type QueryMutationRule interface { - QueryRule - MutationRule -} - -// AlwaysAllowRule returns a rule that returns an allow decision. -func AlwaysAllowRule() QueryMutationRule { - return fixedDecision{Allow} -} - -// AlwaysDenyRule returns a rule that returns a deny decision. -func AlwaysDenyRule() QueryMutationRule { - return fixedDecision{Deny} -} - -type fixedDecision struct { - decision error -} - -func (f fixedDecision) EvalQuery(context.Context, ent.Query) error { - return f.decision -} - -func (f fixedDecision) EvalMutation(context.Context, ent.Mutation) error { - return f.decision -} - -type contextDecision struct { - eval func(context.Context) error -} - -// ContextQueryMutationRule creates a query/mutation rule from a context eval func. -func ContextQueryMutationRule(eval func(context.Context) error) QueryMutationRule { - return contextDecision{eval} -} - -func (c contextDecision) EvalQuery(ctx context.Context, _ ent.Query) error { - return c.eval(ctx) -} - -func (c contextDecision) EvalMutation(ctx context.Context, _ ent.Mutation) error { - return c.eval(ctx) -} - -// OnMutationOperation evaluates the given rule only on a given mutation operation. -func OnMutationOperation(rule MutationRule, op ent.Op) MutationRule { - return MutationRuleFunc(func(ctx context.Context, m ent.Mutation) error { - if m.Op().Is(op) { - return rule.EvalMutation(ctx, m) - } - return Skip - }) -} - -// DenyMutationOperationRule returns a rule denying specified mutation operation. -func DenyMutationOperationRule(op ent.Op) MutationRule { - rule := MutationRuleFunc(func(_ context.Context, m ent.Mutation) error { - return Denyf("ent/privacy: operation %s is not allowed", m.Op()) - }) - return OnMutationOperation(rule, op) -} - -// The UserQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type UserQueryRuleFunc func(context.Context, *ent.UserQuery) error - -// EvalQuery return f(ctx, q). -func (f UserQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.UserQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.UserQuery", q) -} - -// The UserMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type UserMutationRuleFunc func(context.Context, *ent.UserMutation) error - -// EvalMutation calls f(ctx, m). -func (f UserMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.UserMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.UserMutation", m) -} diff --git a/entc/integration/json/ent/privacy/privacy.go b/entc/integration/json/ent/privacy/privacy.go deleted file mode 100644 index 92d06a8ee..000000000 --- a/entc/integration/json/ent/privacy/privacy.go +++ /dev/null @@ -1,239 +0,0 @@ -// 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. - -// Code generated by entc, DO NOT EDIT. - -package privacy - -import ( - "context" - "errors" - "fmt" - - "github.com/facebook/ent/entc/integration/json/ent" -) - -var ( - // Allow may be returned by rules to indicate that the policy - // evaluation should terminate with an allow decision. - Allow = errors.New("ent/privacy: allow rule") - - // Deny may be returned by rules to indicate that the policy - // evaluation should terminate with an deny decision. - Deny = errors.New("ent/privacy: deny rule") - - // Skip may be returned by rules to indicate that the policy - // evaluation should continue to the next rule. - Skip = errors.New("ent/privacy: skip rule") -) - -// Allowf returns an formatted wrapped Allow decision. -func Allowf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Allow)...) -} - -// Denyf returns an formatted wrapped Deny decision. -func Denyf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Deny)...) -} - -// Skipf returns an formatted wrapped Skip decision. -func Skipf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Skip)...) -} - -type decisionCtxKey struct{} - -// DecisionContext creates a decision context. -func DecisionContext(parent context.Context, decision error) context.Context { - if decision == nil || errors.Is(decision, Skip) { - return parent - } - return context.WithValue(parent, decisionCtxKey{}, decision) -} - -func decisionFromContext(ctx context.Context) (error, bool) { - decision, ok := ctx.Value(decisionCtxKey{}).(error) - if ok && errors.Is(decision, Allow) { - decision = nil - } - return decision, ok -} - -type ( - // QueryPolicy combines multiple query rules into a single policy. - QueryPolicy []QueryRule - - // QueryRule defines the interface deciding whether a - // query is allowed and optionally modify it. - QueryRule interface { - EvalQuery(context.Context, ent.Query) error - } -) - -// EvalQuery evaluates a query against a query policy. -func (policy QueryPolicy) EvalQuery(ctx context.Context, q ent.Query) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalQuery(ctx, q); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// QueryRuleFunc type is an adapter to allow the use of -// ordinary functions as query rules. -type QueryRuleFunc func(context.Context, ent.Query) error - -// Eval returns f(ctx, q). -func (f QueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - return f(ctx, q) -} - -type ( - // MutationPolicy combines multiple mutation rules into a single policy. - MutationPolicy []MutationRule - - // MutationRule defines the interface deciding whether a - // mutation is allowed and optionally modify it. - MutationRule interface { - EvalMutation(context.Context, ent.Mutation) error - } -) - -// EvalMutation evaluates a mutation against a mutation policy. -func (policy MutationPolicy) EvalMutation(ctx context.Context, m ent.Mutation) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalMutation(ctx, m); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// MutationRuleFunc type is an adapter to allow the use of -// ordinary functions as mutation rules. -type MutationRuleFunc func(context.Context, ent.Mutation) error - -// EvalMutation returns f(ctx, m). -func (f MutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - return f(ctx, m) -} - -// Policy groups query and mutation policies. -type Policy struct { - Query QueryPolicy - Mutation MutationPolicy -} - -// EvalQuery forwards evaluation to query policy. -func (policy Policy) EvalQuery(ctx context.Context, q ent.Query) error { - return policy.Query.EvalQuery(ctx, q) -} - -// EvalMutation forwards evaluation to mutation policy. -func (policy Policy) EvalMutation(ctx context.Context, m ent.Mutation) error { - return policy.Mutation.EvalMutation(ctx, m) -} - -// QueryMutationRule is the interface that groups query and mutation rules. -type QueryMutationRule interface { - QueryRule - MutationRule -} - -// AlwaysAllowRule returns a rule that returns an allow decision. -func AlwaysAllowRule() QueryMutationRule { - return fixedDecision{Allow} -} - -// AlwaysDenyRule returns a rule that returns a deny decision. -func AlwaysDenyRule() QueryMutationRule { - return fixedDecision{Deny} -} - -type fixedDecision struct { - decision error -} - -func (f fixedDecision) EvalQuery(context.Context, ent.Query) error { - return f.decision -} - -func (f fixedDecision) EvalMutation(context.Context, ent.Mutation) error { - return f.decision -} - -type contextDecision struct { - eval func(context.Context) error -} - -// ContextQueryMutationRule creates a query/mutation rule from a context eval func. -func ContextQueryMutationRule(eval func(context.Context) error) QueryMutationRule { - return contextDecision{eval} -} - -func (c contextDecision) EvalQuery(ctx context.Context, _ ent.Query) error { - return c.eval(ctx) -} - -func (c contextDecision) EvalMutation(ctx context.Context, _ ent.Mutation) error { - return c.eval(ctx) -} - -// OnMutationOperation evaluates the given rule only on a given mutation operation. -func OnMutationOperation(rule MutationRule, op ent.Op) MutationRule { - return MutationRuleFunc(func(ctx context.Context, m ent.Mutation) error { - if m.Op().Is(op) { - return rule.EvalMutation(ctx, m) - } - return Skip - }) -} - -// DenyMutationOperationRule returns a rule denying specified mutation operation. -func DenyMutationOperationRule(op ent.Op) MutationRule { - rule := MutationRuleFunc(func(_ context.Context, m ent.Mutation) error { - return Denyf("ent/privacy: operation %s is not allowed", m.Op()) - }) - return OnMutationOperation(rule, op) -} - -// The UserQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type UserQueryRuleFunc func(context.Context, *ent.UserQuery) error - -// EvalQuery return f(ctx, q). -func (f UserQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.UserQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.UserQuery", q) -} - -// The UserMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type UserMutationRuleFunc func(context.Context, *ent.UserMutation) error - -// EvalMutation calls f(ctx, m). -func (f UserMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.UserMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.UserMutation", m) -} diff --git a/entc/integration/migrate/entv1/privacy/privacy.go b/entc/integration/migrate/entv1/privacy/privacy.go deleted file mode 100644 index 80e30c6ac..000000000 --- a/entc/integration/migrate/entv1/privacy/privacy.go +++ /dev/null @@ -1,263 +0,0 @@ -// 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. - -// Code generated by entc, DO NOT EDIT. - -package privacy - -import ( - "context" - "errors" - "fmt" - - "github.com/facebook/ent/entc/integration/migrate/entv1" -) - -var ( - // Allow may be returned by rules to indicate that the policy - // evaluation should terminate with an allow decision. - Allow = errors.New("ent/privacy: allow rule") - - // Deny may be returned by rules to indicate that the policy - // evaluation should terminate with an deny decision. - Deny = errors.New("ent/privacy: deny rule") - - // Skip may be returned by rules to indicate that the policy - // evaluation should continue to the next rule. - Skip = errors.New("ent/privacy: skip rule") -) - -// Allowf returns an formatted wrapped Allow decision. -func Allowf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Allow)...) -} - -// Denyf returns an formatted wrapped Deny decision. -func Denyf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Deny)...) -} - -// Skipf returns an formatted wrapped Skip decision. -func Skipf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Skip)...) -} - -type decisionCtxKey struct{} - -// DecisionContext creates a decision context. -func DecisionContext(parent context.Context, decision error) context.Context { - if decision == nil || errors.Is(decision, Skip) { - return parent - } - return context.WithValue(parent, decisionCtxKey{}, decision) -} - -func decisionFromContext(ctx context.Context) (error, bool) { - decision, ok := ctx.Value(decisionCtxKey{}).(error) - if ok && errors.Is(decision, Allow) { - decision = nil - } - return decision, ok -} - -type ( - // QueryPolicy combines multiple query rules into a single policy. - QueryPolicy []QueryRule - - // QueryRule defines the interface deciding whether a - // query is allowed and optionally modify it. - QueryRule interface { - EvalQuery(context.Context, entv1.Query) error - } -) - -// EvalQuery evaluates a query against a query policy. -func (policy QueryPolicy) EvalQuery(ctx context.Context, q entv1.Query) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalQuery(ctx, q); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// QueryRuleFunc type is an adapter to allow the use of -// ordinary functions as query rules. -type QueryRuleFunc func(context.Context, entv1.Query) error - -// Eval returns f(ctx, q). -func (f QueryRuleFunc) EvalQuery(ctx context.Context, q entv1.Query) error { - return f(ctx, q) -} - -type ( - // MutationPolicy combines multiple mutation rules into a single policy. - MutationPolicy []MutationRule - - // MutationRule defines the interface deciding whether a - // mutation is allowed and optionally modify it. - MutationRule interface { - EvalMutation(context.Context, entv1.Mutation) error - } -) - -// EvalMutation evaluates a mutation against a mutation policy. -func (policy MutationPolicy) EvalMutation(ctx context.Context, m entv1.Mutation) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalMutation(ctx, m); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// MutationRuleFunc type is an adapter to allow the use of -// ordinary functions as mutation rules. -type MutationRuleFunc func(context.Context, entv1.Mutation) error - -// EvalMutation returns f(ctx, m). -func (f MutationRuleFunc) EvalMutation(ctx context.Context, m entv1.Mutation) error { - return f(ctx, m) -} - -// Policy groups query and mutation policies. -type Policy struct { - Query QueryPolicy - Mutation MutationPolicy -} - -// EvalQuery forwards evaluation to query policy. -func (policy Policy) EvalQuery(ctx context.Context, q entv1.Query) error { - return policy.Query.EvalQuery(ctx, q) -} - -// EvalMutation forwards evaluation to mutation policy. -func (policy Policy) EvalMutation(ctx context.Context, m entv1.Mutation) error { - return policy.Mutation.EvalMutation(ctx, m) -} - -// QueryMutationRule is the interface that groups query and mutation rules. -type QueryMutationRule interface { - QueryRule - MutationRule -} - -// AlwaysAllowRule returns a rule that returns an allow decision. -func AlwaysAllowRule() QueryMutationRule { - return fixedDecision{Allow} -} - -// AlwaysDenyRule returns a rule that returns a deny decision. -func AlwaysDenyRule() QueryMutationRule { - return fixedDecision{Deny} -} - -type fixedDecision struct { - decision error -} - -func (f fixedDecision) EvalQuery(context.Context, entv1.Query) error { - return f.decision -} - -func (f fixedDecision) EvalMutation(context.Context, entv1.Mutation) error { - return f.decision -} - -type contextDecision struct { - eval func(context.Context) error -} - -// ContextQueryMutationRule creates a query/mutation rule from a context eval func. -func ContextQueryMutationRule(eval func(context.Context) error) QueryMutationRule { - return contextDecision{eval} -} - -func (c contextDecision) EvalQuery(ctx context.Context, _ entv1.Query) error { - return c.eval(ctx) -} - -func (c contextDecision) EvalMutation(ctx context.Context, _ entv1.Mutation) error { - return c.eval(ctx) -} - -// OnMutationOperation evaluates the given rule only on a given mutation operation. -func OnMutationOperation(rule MutationRule, op entv1.Op) MutationRule { - return MutationRuleFunc(func(ctx context.Context, m entv1.Mutation) error { - if m.Op().Is(op) { - return rule.EvalMutation(ctx, m) - } - return Skip - }) -} - -// DenyMutationOperationRule returns a rule denying specified mutation operation. -func DenyMutationOperationRule(op entv1.Op) MutationRule { - rule := MutationRuleFunc(func(_ context.Context, m entv1.Mutation) error { - return Denyf("ent/privacy: operation %s is not allowed", m.Op()) - }) - return OnMutationOperation(rule, op) -} - -// The CarQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type CarQueryRuleFunc func(context.Context, *entv1.CarQuery) error - -// EvalQuery return f(ctx, q). -func (f CarQueryRuleFunc) EvalQuery(ctx context.Context, q entv1.Query) error { - if q, ok := q.(*entv1.CarQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *entv1.CarQuery", q) -} - -// The CarMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type CarMutationRuleFunc func(context.Context, *entv1.CarMutation) error - -// EvalMutation calls f(ctx, m). -func (f CarMutationRuleFunc) EvalMutation(ctx context.Context, m entv1.Mutation) error { - if m, ok := m.(*entv1.CarMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *entv1.CarMutation", m) -} - -// The UserQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type UserQueryRuleFunc func(context.Context, *entv1.UserQuery) error - -// EvalQuery return f(ctx, q). -func (f UserQueryRuleFunc) EvalQuery(ctx context.Context, q entv1.Query) error { - if q, ok := q.(*entv1.UserQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *entv1.UserQuery", q) -} - -// The UserMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type UserMutationRuleFunc func(context.Context, *entv1.UserMutation) error - -// EvalMutation calls f(ctx, m). -func (f UserMutationRuleFunc) EvalMutation(ctx context.Context, m entv1.Mutation) error { - if m, ok := m.(*entv1.UserMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *entv1.UserMutation", m) -} diff --git a/entc/integration/migrate/entv2/privacy/privacy.go b/entc/integration/migrate/entv2/privacy/privacy.go deleted file mode 100644 index 4bd6b0f3b..000000000 --- a/entc/integration/migrate/entv2/privacy/privacy.go +++ /dev/null @@ -1,311 +0,0 @@ -// 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. - -// Code generated by entc, DO NOT EDIT. - -package privacy - -import ( - "context" - "errors" - "fmt" - - "github.com/facebook/ent/entc/integration/migrate/entv2" -) - -var ( - // Allow may be returned by rules to indicate that the policy - // evaluation should terminate with an allow decision. - Allow = errors.New("ent/privacy: allow rule") - - // Deny may be returned by rules to indicate that the policy - // evaluation should terminate with an deny decision. - Deny = errors.New("ent/privacy: deny rule") - - // Skip may be returned by rules to indicate that the policy - // evaluation should continue to the next rule. - Skip = errors.New("ent/privacy: skip rule") -) - -// Allowf returns an formatted wrapped Allow decision. -func Allowf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Allow)...) -} - -// Denyf returns an formatted wrapped Deny decision. -func Denyf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Deny)...) -} - -// Skipf returns an formatted wrapped Skip decision. -func Skipf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Skip)...) -} - -type decisionCtxKey struct{} - -// DecisionContext creates a decision context. -func DecisionContext(parent context.Context, decision error) context.Context { - if decision == nil || errors.Is(decision, Skip) { - return parent - } - return context.WithValue(parent, decisionCtxKey{}, decision) -} - -func decisionFromContext(ctx context.Context) (error, bool) { - decision, ok := ctx.Value(decisionCtxKey{}).(error) - if ok && errors.Is(decision, Allow) { - decision = nil - } - return decision, ok -} - -type ( - // QueryPolicy combines multiple query rules into a single policy. - QueryPolicy []QueryRule - - // QueryRule defines the interface deciding whether a - // query is allowed and optionally modify it. - QueryRule interface { - EvalQuery(context.Context, entv2.Query) error - } -) - -// EvalQuery evaluates a query against a query policy. -func (policy QueryPolicy) EvalQuery(ctx context.Context, q entv2.Query) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalQuery(ctx, q); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// QueryRuleFunc type is an adapter to allow the use of -// ordinary functions as query rules. -type QueryRuleFunc func(context.Context, entv2.Query) error - -// Eval returns f(ctx, q). -func (f QueryRuleFunc) EvalQuery(ctx context.Context, q entv2.Query) error { - return f(ctx, q) -} - -type ( - // MutationPolicy combines multiple mutation rules into a single policy. - MutationPolicy []MutationRule - - // MutationRule defines the interface deciding whether a - // mutation is allowed and optionally modify it. - MutationRule interface { - EvalMutation(context.Context, entv2.Mutation) error - } -) - -// EvalMutation evaluates a mutation against a mutation policy. -func (policy MutationPolicy) EvalMutation(ctx context.Context, m entv2.Mutation) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalMutation(ctx, m); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// MutationRuleFunc type is an adapter to allow the use of -// ordinary functions as mutation rules. -type MutationRuleFunc func(context.Context, entv2.Mutation) error - -// EvalMutation returns f(ctx, m). -func (f MutationRuleFunc) EvalMutation(ctx context.Context, m entv2.Mutation) error { - return f(ctx, m) -} - -// Policy groups query and mutation policies. -type Policy struct { - Query QueryPolicy - Mutation MutationPolicy -} - -// EvalQuery forwards evaluation to query policy. -func (policy Policy) EvalQuery(ctx context.Context, q entv2.Query) error { - return policy.Query.EvalQuery(ctx, q) -} - -// EvalMutation forwards evaluation to mutation policy. -func (policy Policy) EvalMutation(ctx context.Context, m entv2.Mutation) error { - return policy.Mutation.EvalMutation(ctx, m) -} - -// QueryMutationRule is the interface that groups query and mutation rules. -type QueryMutationRule interface { - QueryRule - MutationRule -} - -// AlwaysAllowRule returns a rule that returns an allow decision. -func AlwaysAllowRule() QueryMutationRule { - return fixedDecision{Allow} -} - -// AlwaysDenyRule returns a rule that returns a deny decision. -func AlwaysDenyRule() QueryMutationRule { - return fixedDecision{Deny} -} - -type fixedDecision struct { - decision error -} - -func (f fixedDecision) EvalQuery(context.Context, entv2.Query) error { - return f.decision -} - -func (f fixedDecision) EvalMutation(context.Context, entv2.Mutation) error { - return f.decision -} - -type contextDecision struct { - eval func(context.Context) error -} - -// ContextQueryMutationRule creates a query/mutation rule from a context eval func. -func ContextQueryMutationRule(eval func(context.Context) error) QueryMutationRule { - return contextDecision{eval} -} - -func (c contextDecision) EvalQuery(ctx context.Context, _ entv2.Query) error { - return c.eval(ctx) -} - -func (c contextDecision) EvalMutation(ctx context.Context, _ entv2.Mutation) error { - return c.eval(ctx) -} - -// OnMutationOperation evaluates the given rule only on a given mutation operation. -func OnMutationOperation(rule MutationRule, op entv2.Op) MutationRule { - return MutationRuleFunc(func(ctx context.Context, m entv2.Mutation) error { - if m.Op().Is(op) { - return rule.EvalMutation(ctx, m) - } - return Skip - }) -} - -// DenyMutationOperationRule returns a rule denying specified mutation operation. -func DenyMutationOperationRule(op entv2.Op) MutationRule { - rule := MutationRuleFunc(func(_ context.Context, m entv2.Mutation) error { - return Denyf("ent/privacy: operation %s is not allowed", m.Op()) - }) - return OnMutationOperation(rule, op) -} - -// The CarQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type CarQueryRuleFunc func(context.Context, *entv2.CarQuery) error - -// EvalQuery return f(ctx, q). -func (f CarQueryRuleFunc) EvalQuery(ctx context.Context, q entv2.Query) error { - if q, ok := q.(*entv2.CarQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *entv2.CarQuery", q) -} - -// The CarMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type CarMutationRuleFunc func(context.Context, *entv2.CarMutation) error - -// EvalMutation calls f(ctx, m). -func (f CarMutationRuleFunc) EvalMutation(ctx context.Context, m entv2.Mutation) error { - if m, ok := m.(*entv2.CarMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *entv2.CarMutation", m) -} - -// The GroupQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type GroupQueryRuleFunc func(context.Context, *entv2.GroupQuery) error - -// EvalQuery return f(ctx, q). -func (f GroupQueryRuleFunc) EvalQuery(ctx context.Context, q entv2.Query) error { - if q, ok := q.(*entv2.GroupQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *entv2.GroupQuery", q) -} - -// The GroupMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type GroupMutationRuleFunc func(context.Context, *entv2.GroupMutation) error - -// EvalMutation calls f(ctx, m). -func (f GroupMutationRuleFunc) EvalMutation(ctx context.Context, m entv2.Mutation) error { - if m, ok := m.(*entv2.GroupMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *entv2.GroupMutation", m) -} - -// The PetQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type PetQueryRuleFunc func(context.Context, *entv2.PetQuery) error - -// EvalQuery return f(ctx, q). -func (f PetQueryRuleFunc) EvalQuery(ctx context.Context, q entv2.Query) error { - if q, ok := q.(*entv2.PetQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *entv2.PetQuery", q) -} - -// The PetMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type PetMutationRuleFunc func(context.Context, *entv2.PetMutation) error - -// EvalMutation calls f(ctx, m). -func (f PetMutationRuleFunc) EvalMutation(ctx context.Context, m entv2.Mutation) error { - if m, ok := m.(*entv2.PetMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *entv2.PetMutation", m) -} - -// The UserQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type UserQueryRuleFunc func(context.Context, *entv2.UserQuery) error - -// EvalQuery return f(ctx, q). -func (f UserQueryRuleFunc) EvalQuery(ctx context.Context, q entv2.Query) error { - if q, ok := q.(*entv2.UserQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *entv2.UserQuery", q) -} - -// The UserMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type UserMutationRuleFunc func(context.Context, *entv2.UserMutation) error - -// EvalMutation calls f(ctx, m). -func (f UserMutationRuleFunc) EvalMutation(ctx context.Context, m entv2.Mutation) error { - if m, ok := m.(*entv2.UserMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *entv2.UserMutation", m) -} diff --git a/entc/integration/privacy/ent/generate.go b/entc/integration/privacy/ent/generate.go index f6c2769de..e3d65fb5d 100644 --- a/entc/integration/privacy/ent/generate.go +++ b/entc/integration/privacy/ent/generate.go @@ -4,4 +4,4 @@ package ent -//go:generate go run github.com/facebook/ent/cmd/entc generate --header "// Copyright 2019-present Facebook Inc. All rights reserved.\n// This source code is licensed under the Apache 2.0 license found\n// in the LICENSE file in the root directory of this source tree.\n\n// Code generated by entc, DO NOT EDIT." ./schema +//go:generate go run github.com/facebook/ent/cmd/entc generate --feature privacy --header "// Copyright 2019-present Facebook Inc. All rights reserved.\n// This source code is licensed under the Apache 2.0 license found\n// in the LICENSE file in the root directory of this source tree.\n\n// Code generated by entc, DO NOT EDIT." ./schema diff --git a/entc/integration/template/ent/privacy/privacy.go b/entc/integration/template/ent/privacy/privacy.go deleted file mode 100644 index 44f4766c1..000000000 --- a/entc/integration/template/ent/privacy/privacy.go +++ /dev/null @@ -1,287 +0,0 @@ -// 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. - -// Code generated by entc, DO NOT EDIT. - -package privacy - -import ( - "context" - "errors" - "fmt" - - "github.com/facebook/ent/entc/integration/template/ent" -) - -var ( - // Allow may be returned by rules to indicate that the policy - // evaluation should terminate with an allow decision. - Allow = errors.New("ent/privacy: allow rule") - - // Deny may be returned by rules to indicate that the policy - // evaluation should terminate with an deny decision. - Deny = errors.New("ent/privacy: deny rule") - - // Skip may be returned by rules to indicate that the policy - // evaluation should continue to the next rule. - Skip = errors.New("ent/privacy: skip rule") -) - -// Allowf returns an formatted wrapped Allow decision. -func Allowf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Allow)...) -} - -// Denyf returns an formatted wrapped Deny decision. -func Denyf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Deny)...) -} - -// Skipf returns an formatted wrapped Skip decision. -func Skipf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Skip)...) -} - -type decisionCtxKey struct{} - -// DecisionContext creates a decision context. -func DecisionContext(parent context.Context, decision error) context.Context { - if decision == nil || errors.Is(decision, Skip) { - return parent - } - return context.WithValue(parent, decisionCtxKey{}, decision) -} - -func decisionFromContext(ctx context.Context) (error, bool) { - decision, ok := ctx.Value(decisionCtxKey{}).(error) - if ok && errors.Is(decision, Allow) { - decision = nil - } - return decision, ok -} - -type ( - // QueryPolicy combines multiple query rules into a single policy. - QueryPolicy []QueryRule - - // QueryRule defines the interface deciding whether a - // query is allowed and optionally modify it. - QueryRule interface { - EvalQuery(context.Context, ent.Query) error - } -) - -// EvalQuery evaluates a query against a query policy. -func (policy QueryPolicy) EvalQuery(ctx context.Context, q ent.Query) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalQuery(ctx, q); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// QueryRuleFunc type is an adapter to allow the use of -// ordinary functions as query rules. -type QueryRuleFunc func(context.Context, ent.Query) error - -// Eval returns f(ctx, q). -func (f QueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - return f(ctx, q) -} - -type ( - // MutationPolicy combines multiple mutation rules into a single policy. - MutationPolicy []MutationRule - - // MutationRule defines the interface deciding whether a - // mutation is allowed and optionally modify it. - MutationRule interface { - EvalMutation(context.Context, ent.Mutation) error - } -) - -// EvalMutation evaluates a mutation against a mutation policy. -func (policy MutationPolicy) EvalMutation(ctx context.Context, m ent.Mutation) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalMutation(ctx, m); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// MutationRuleFunc type is an adapter to allow the use of -// ordinary functions as mutation rules. -type MutationRuleFunc func(context.Context, ent.Mutation) error - -// EvalMutation returns f(ctx, m). -func (f MutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - return f(ctx, m) -} - -// Policy groups query and mutation policies. -type Policy struct { - Query QueryPolicy - Mutation MutationPolicy -} - -// EvalQuery forwards evaluation to query policy. -func (policy Policy) EvalQuery(ctx context.Context, q ent.Query) error { - return policy.Query.EvalQuery(ctx, q) -} - -// EvalMutation forwards evaluation to mutation policy. -func (policy Policy) EvalMutation(ctx context.Context, m ent.Mutation) error { - return policy.Mutation.EvalMutation(ctx, m) -} - -// QueryMutationRule is the interface that groups query and mutation rules. -type QueryMutationRule interface { - QueryRule - MutationRule -} - -// AlwaysAllowRule returns a rule that returns an allow decision. -func AlwaysAllowRule() QueryMutationRule { - return fixedDecision{Allow} -} - -// AlwaysDenyRule returns a rule that returns a deny decision. -func AlwaysDenyRule() QueryMutationRule { - return fixedDecision{Deny} -} - -type fixedDecision struct { - decision error -} - -func (f fixedDecision) EvalQuery(context.Context, ent.Query) error { - return f.decision -} - -func (f fixedDecision) EvalMutation(context.Context, ent.Mutation) error { - return f.decision -} - -type contextDecision struct { - eval func(context.Context) error -} - -// ContextQueryMutationRule creates a query/mutation rule from a context eval func. -func ContextQueryMutationRule(eval func(context.Context) error) QueryMutationRule { - return contextDecision{eval} -} - -func (c contextDecision) EvalQuery(ctx context.Context, _ ent.Query) error { - return c.eval(ctx) -} - -func (c contextDecision) EvalMutation(ctx context.Context, _ ent.Mutation) error { - return c.eval(ctx) -} - -// OnMutationOperation evaluates the given rule only on a given mutation operation. -func OnMutationOperation(rule MutationRule, op ent.Op) MutationRule { - return MutationRuleFunc(func(ctx context.Context, m ent.Mutation) error { - if m.Op().Is(op) { - return rule.EvalMutation(ctx, m) - } - return Skip - }) -} - -// DenyMutationOperationRule returns a rule denying specified mutation operation. -func DenyMutationOperationRule(op ent.Op) MutationRule { - rule := MutationRuleFunc(func(_ context.Context, m ent.Mutation) error { - return Denyf("ent/privacy: operation %s is not allowed", m.Op()) - }) - return OnMutationOperation(rule, op) -} - -// The GroupQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type GroupQueryRuleFunc func(context.Context, *ent.GroupQuery) error - -// EvalQuery return f(ctx, q). -func (f GroupQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.GroupQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.GroupQuery", q) -} - -// The GroupMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type GroupMutationRuleFunc func(context.Context, *ent.GroupMutation) error - -// EvalMutation calls f(ctx, m). -func (f GroupMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.GroupMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.GroupMutation", m) -} - -// The PetQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type PetQueryRuleFunc func(context.Context, *ent.PetQuery) error - -// EvalQuery return f(ctx, q). -func (f PetQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.PetQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.PetQuery", q) -} - -// The PetMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type PetMutationRuleFunc func(context.Context, *ent.PetMutation) error - -// EvalMutation calls f(ctx, m). -func (f PetMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.PetMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.PetMutation", m) -} - -// The UserQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type UserQueryRuleFunc func(context.Context, *ent.UserQuery) error - -// EvalQuery return f(ctx, q). -func (f UserQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.UserQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.UserQuery", q) -} - -// The UserMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type UserMutationRuleFunc func(context.Context, *ent.UserMutation) error - -// EvalMutation calls f(ctx, m). -func (f UserMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.UserMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.UserMutation", m) -} diff --git a/examples/edgeindex/ent/privacy/privacy.go b/examples/edgeindex/ent/privacy/privacy.go deleted file mode 100644 index 45842e7ff..000000000 --- a/examples/edgeindex/ent/privacy/privacy.go +++ /dev/null @@ -1,263 +0,0 @@ -// 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. - -// Code generated by entc, DO NOT EDIT. - -package privacy - -import ( - "context" - "errors" - "fmt" - - "github.com/facebook/ent/examples/edgeindex/ent" -) - -var ( - // Allow may be returned by rules to indicate that the policy - // evaluation should terminate with an allow decision. - Allow = errors.New("ent/privacy: allow rule") - - // Deny may be returned by rules to indicate that the policy - // evaluation should terminate with an deny decision. - Deny = errors.New("ent/privacy: deny rule") - - // Skip may be returned by rules to indicate that the policy - // evaluation should continue to the next rule. - Skip = errors.New("ent/privacy: skip rule") -) - -// Allowf returns an formatted wrapped Allow decision. -func Allowf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Allow)...) -} - -// Denyf returns an formatted wrapped Deny decision. -func Denyf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Deny)...) -} - -// Skipf returns an formatted wrapped Skip decision. -func Skipf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Skip)...) -} - -type decisionCtxKey struct{} - -// DecisionContext creates a decision context. -func DecisionContext(parent context.Context, decision error) context.Context { - if decision == nil || errors.Is(decision, Skip) { - return parent - } - return context.WithValue(parent, decisionCtxKey{}, decision) -} - -func decisionFromContext(ctx context.Context) (error, bool) { - decision, ok := ctx.Value(decisionCtxKey{}).(error) - if ok && errors.Is(decision, Allow) { - decision = nil - } - return decision, ok -} - -type ( - // QueryPolicy combines multiple query rules into a single policy. - QueryPolicy []QueryRule - - // QueryRule defines the interface deciding whether a - // query is allowed and optionally modify it. - QueryRule interface { - EvalQuery(context.Context, ent.Query) error - } -) - -// EvalQuery evaluates a query against a query policy. -func (policy QueryPolicy) EvalQuery(ctx context.Context, q ent.Query) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalQuery(ctx, q); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// QueryRuleFunc type is an adapter to allow the use of -// ordinary functions as query rules. -type QueryRuleFunc func(context.Context, ent.Query) error - -// Eval returns f(ctx, q). -func (f QueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - return f(ctx, q) -} - -type ( - // MutationPolicy combines multiple mutation rules into a single policy. - MutationPolicy []MutationRule - - // MutationRule defines the interface deciding whether a - // mutation is allowed and optionally modify it. - MutationRule interface { - EvalMutation(context.Context, ent.Mutation) error - } -) - -// EvalMutation evaluates a mutation against a mutation policy. -func (policy MutationPolicy) EvalMutation(ctx context.Context, m ent.Mutation) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalMutation(ctx, m); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// MutationRuleFunc type is an adapter to allow the use of -// ordinary functions as mutation rules. -type MutationRuleFunc func(context.Context, ent.Mutation) error - -// EvalMutation returns f(ctx, m). -func (f MutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - return f(ctx, m) -} - -// Policy groups query and mutation policies. -type Policy struct { - Query QueryPolicy - Mutation MutationPolicy -} - -// EvalQuery forwards evaluation to query policy. -func (policy Policy) EvalQuery(ctx context.Context, q ent.Query) error { - return policy.Query.EvalQuery(ctx, q) -} - -// EvalMutation forwards evaluation to mutation policy. -func (policy Policy) EvalMutation(ctx context.Context, m ent.Mutation) error { - return policy.Mutation.EvalMutation(ctx, m) -} - -// QueryMutationRule is the interface that groups query and mutation rules. -type QueryMutationRule interface { - QueryRule - MutationRule -} - -// AlwaysAllowRule returns a rule that returns an allow decision. -func AlwaysAllowRule() QueryMutationRule { - return fixedDecision{Allow} -} - -// AlwaysDenyRule returns a rule that returns a deny decision. -func AlwaysDenyRule() QueryMutationRule { - return fixedDecision{Deny} -} - -type fixedDecision struct { - decision error -} - -func (f fixedDecision) EvalQuery(context.Context, ent.Query) error { - return f.decision -} - -func (f fixedDecision) EvalMutation(context.Context, ent.Mutation) error { - return f.decision -} - -type contextDecision struct { - eval func(context.Context) error -} - -// ContextQueryMutationRule creates a query/mutation rule from a context eval func. -func ContextQueryMutationRule(eval func(context.Context) error) QueryMutationRule { - return contextDecision{eval} -} - -func (c contextDecision) EvalQuery(ctx context.Context, _ ent.Query) error { - return c.eval(ctx) -} - -func (c contextDecision) EvalMutation(ctx context.Context, _ ent.Mutation) error { - return c.eval(ctx) -} - -// OnMutationOperation evaluates the given rule only on a given mutation operation. -func OnMutationOperation(rule MutationRule, op ent.Op) MutationRule { - return MutationRuleFunc(func(ctx context.Context, m ent.Mutation) error { - if m.Op().Is(op) { - return rule.EvalMutation(ctx, m) - } - return Skip - }) -} - -// DenyMutationOperationRule returns a rule denying specified mutation operation. -func DenyMutationOperationRule(op ent.Op) MutationRule { - rule := MutationRuleFunc(func(_ context.Context, m ent.Mutation) error { - return Denyf("ent/privacy: operation %s is not allowed", m.Op()) - }) - return OnMutationOperation(rule, op) -} - -// The CityQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type CityQueryRuleFunc func(context.Context, *ent.CityQuery) error - -// EvalQuery return f(ctx, q). -func (f CityQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.CityQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.CityQuery", q) -} - -// The CityMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type CityMutationRuleFunc func(context.Context, *ent.CityMutation) error - -// EvalMutation calls f(ctx, m). -func (f CityMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.CityMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.CityMutation", m) -} - -// The StreetQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type StreetQueryRuleFunc func(context.Context, *ent.StreetQuery) error - -// EvalQuery return f(ctx, q). -func (f StreetQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.StreetQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.StreetQuery", q) -} - -// The StreetMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type StreetMutationRuleFunc func(context.Context, *ent.StreetMutation) error - -// EvalMutation calls f(ctx, m). -func (f StreetMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.StreetMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.StreetMutation", m) -} diff --git a/examples/entcpkg/ent/privacy/privacy.go b/examples/entcpkg/ent/privacy/privacy.go deleted file mode 100644 index f9f96379e..000000000 --- a/examples/entcpkg/ent/privacy/privacy.go +++ /dev/null @@ -1,239 +0,0 @@ -// 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. - -// Code generated by entc, DO NOT EDIT. - -package privacy - -import ( - "context" - "errors" - "fmt" - - "github.com/facebook/ent/examples/entcpkg/ent" -) - -var ( - // Allow may be returned by rules to indicate that the policy - // evaluation should terminate with an allow decision. - Allow = errors.New("ent/privacy: allow rule") - - // Deny may be returned by rules to indicate that the policy - // evaluation should terminate with an deny decision. - Deny = errors.New("ent/privacy: deny rule") - - // Skip may be returned by rules to indicate that the policy - // evaluation should continue to the next rule. - Skip = errors.New("ent/privacy: skip rule") -) - -// Allowf returns an formatted wrapped Allow decision. -func Allowf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Allow)...) -} - -// Denyf returns an formatted wrapped Deny decision. -func Denyf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Deny)...) -} - -// Skipf returns an formatted wrapped Skip decision. -func Skipf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Skip)...) -} - -type decisionCtxKey struct{} - -// DecisionContext creates a decision context. -func DecisionContext(parent context.Context, decision error) context.Context { - if decision == nil || errors.Is(decision, Skip) { - return parent - } - return context.WithValue(parent, decisionCtxKey{}, decision) -} - -func decisionFromContext(ctx context.Context) (error, bool) { - decision, ok := ctx.Value(decisionCtxKey{}).(error) - if ok && errors.Is(decision, Allow) { - decision = nil - } - return decision, ok -} - -type ( - // QueryPolicy combines multiple query rules into a single policy. - QueryPolicy []QueryRule - - // QueryRule defines the interface deciding whether a - // query is allowed and optionally modify it. - QueryRule interface { - EvalQuery(context.Context, ent.Query) error - } -) - -// EvalQuery evaluates a query against a query policy. -func (policy QueryPolicy) EvalQuery(ctx context.Context, q ent.Query) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalQuery(ctx, q); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// QueryRuleFunc type is an adapter to allow the use of -// ordinary functions as query rules. -type QueryRuleFunc func(context.Context, ent.Query) error - -// Eval returns f(ctx, q). -func (f QueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - return f(ctx, q) -} - -type ( - // MutationPolicy combines multiple mutation rules into a single policy. - MutationPolicy []MutationRule - - // MutationRule defines the interface deciding whether a - // mutation is allowed and optionally modify it. - MutationRule interface { - EvalMutation(context.Context, ent.Mutation) error - } -) - -// EvalMutation evaluates a mutation against a mutation policy. -func (policy MutationPolicy) EvalMutation(ctx context.Context, m ent.Mutation) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalMutation(ctx, m); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// MutationRuleFunc type is an adapter to allow the use of -// ordinary functions as mutation rules. -type MutationRuleFunc func(context.Context, ent.Mutation) error - -// EvalMutation returns f(ctx, m). -func (f MutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - return f(ctx, m) -} - -// Policy groups query and mutation policies. -type Policy struct { - Query QueryPolicy - Mutation MutationPolicy -} - -// EvalQuery forwards evaluation to query policy. -func (policy Policy) EvalQuery(ctx context.Context, q ent.Query) error { - return policy.Query.EvalQuery(ctx, q) -} - -// EvalMutation forwards evaluation to mutation policy. -func (policy Policy) EvalMutation(ctx context.Context, m ent.Mutation) error { - return policy.Mutation.EvalMutation(ctx, m) -} - -// QueryMutationRule is the interface that groups query and mutation rules. -type QueryMutationRule interface { - QueryRule - MutationRule -} - -// AlwaysAllowRule returns a rule that returns an allow decision. -func AlwaysAllowRule() QueryMutationRule { - return fixedDecision{Allow} -} - -// AlwaysDenyRule returns a rule that returns a deny decision. -func AlwaysDenyRule() QueryMutationRule { - return fixedDecision{Deny} -} - -type fixedDecision struct { - decision error -} - -func (f fixedDecision) EvalQuery(context.Context, ent.Query) error { - return f.decision -} - -func (f fixedDecision) EvalMutation(context.Context, ent.Mutation) error { - return f.decision -} - -type contextDecision struct { - eval func(context.Context) error -} - -// ContextQueryMutationRule creates a query/mutation rule from a context eval func. -func ContextQueryMutationRule(eval func(context.Context) error) QueryMutationRule { - return contextDecision{eval} -} - -func (c contextDecision) EvalQuery(ctx context.Context, _ ent.Query) error { - return c.eval(ctx) -} - -func (c contextDecision) EvalMutation(ctx context.Context, _ ent.Mutation) error { - return c.eval(ctx) -} - -// OnMutationOperation evaluates the given rule only on a given mutation operation. -func OnMutationOperation(rule MutationRule, op ent.Op) MutationRule { - return MutationRuleFunc(func(ctx context.Context, m ent.Mutation) error { - if m.Op().Is(op) { - return rule.EvalMutation(ctx, m) - } - return Skip - }) -} - -// DenyMutationOperationRule returns a rule denying specified mutation operation. -func DenyMutationOperationRule(op ent.Op) MutationRule { - rule := MutationRuleFunc(func(_ context.Context, m ent.Mutation) error { - return Denyf("ent/privacy: operation %s is not allowed", m.Op()) - }) - return OnMutationOperation(rule, op) -} - -// The UserQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type UserQueryRuleFunc func(context.Context, *ent.UserQuery) error - -// EvalQuery return f(ctx, q). -func (f UserQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.UserQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.UserQuery", q) -} - -// The UserMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type UserMutationRuleFunc func(context.Context, *ent.UserMutation) error - -// EvalMutation calls f(ctx, m). -func (f UserMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.UserMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.UserMutation", m) -} diff --git a/examples/m2m2types/ent/privacy/privacy.go b/examples/m2m2types/ent/privacy/privacy.go deleted file mode 100644 index 22bfd0eec..000000000 --- a/examples/m2m2types/ent/privacy/privacy.go +++ /dev/null @@ -1,263 +0,0 @@ -// 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. - -// Code generated by entc, DO NOT EDIT. - -package privacy - -import ( - "context" - "errors" - "fmt" - - "github.com/facebook/ent/examples/m2m2types/ent" -) - -var ( - // Allow may be returned by rules to indicate that the policy - // evaluation should terminate with an allow decision. - Allow = errors.New("ent/privacy: allow rule") - - // Deny may be returned by rules to indicate that the policy - // evaluation should terminate with an deny decision. - Deny = errors.New("ent/privacy: deny rule") - - // Skip may be returned by rules to indicate that the policy - // evaluation should continue to the next rule. - Skip = errors.New("ent/privacy: skip rule") -) - -// Allowf returns an formatted wrapped Allow decision. -func Allowf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Allow)...) -} - -// Denyf returns an formatted wrapped Deny decision. -func Denyf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Deny)...) -} - -// Skipf returns an formatted wrapped Skip decision. -func Skipf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Skip)...) -} - -type decisionCtxKey struct{} - -// DecisionContext creates a decision context. -func DecisionContext(parent context.Context, decision error) context.Context { - if decision == nil || errors.Is(decision, Skip) { - return parent - } - return context.WithValue(parent, decisionCtxKey{}, decision) -} - -func decisionFromContext(ctx context.Context) (error, bool) { - decision, ok := ctx.Value(decisionCtxKey{}).(error) - if ok && errors.Is(decision, Allow) { - decision = nil - } - return decision, ok -} - -type ( - // QueryPolicy combines multiple query rules into a single policy. - QueryPolicy []QueryRule - - // QueryRule defines the interface deciding whether a - // query is allowed and optionally modify it. - QueryRule interface { - EvalQuery(context.Context, ent.Query) error - } -) - -// EvalQuery evaluates a query against a query policy. -func (policy QueryPolicy) EvalQuery(ctx context.Context, q ent.Query) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalQuery(ctx, q); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// QueryRuleFunc type is an adapter to allow the use of -// ordinary functions as query rules. -type QueryRuleFunc func(context.Context, ent.Query) error - -// Eval returns f(ctx, q). -func (f QueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - return f(ctx, q) -} - -type ( - // MutationPolicy combines multiple mutation rules into a single policy. - MutationPolicy []MutationRule - - // MutationRule defines the interface deciding whether a - // mutation is allowed and optionally modify it. - MutationRule interface { - EvalMutation(context.Context, ent.Mutation) error - } -) - -// EvalMutation evaluates a mutation against a mutation policy. -func (policy MutationPolicy) EvalMutation(ctx context.Context, m ent.Mutation) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalMutation(ctx, m); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// MutationRuleFunc type is an adapter to allow the use of -// ordinary functions as mutation rules. -type MutationRuleFunc func(context.Context, ent.Mutation) error - -// EvalMutation returns f(ctx, m). -func (f MutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - return f(ctx, m) -} - -// Policy groups query and mutation policies. -type Policy struct { - Query QueryPolicy - Mutation MutationPolicy -} - -// EvalQuery forwards evaluation to query policy. -func (policy Policy) EvalQuery(ctx context.Context, q ent.Query) error { - return policy.Query.EvalQuery(ctx, q) -} - -// EvalMutation forwards evaluation to mutation policy. -func (policy Policy) EvalMutation(ctx context.Context, m ent.Mutation) error { - return policy.Mutation.EvalMutation(ctx, m) -} - -// QueryMutationRule is the interface that groups query and mutation rules. -type QueryMutationRule interface { - QueryRule - MutationRule -} - -// AlwaysAllowRule returns a rule that returns an allow decision. -func AlwaysAllowRule() QueryMutationRule { - return fixedDecision{Allow} -} - -// AlwaysDenyRule returns a rule that returns a deny decision. -func AlwaysDenyRule() QueryMutationRule { - return fixedDecision{Deny} -} - -type fixedDecision struct { - decision error -} - -func (f fixedDecision) EvalQuery(context.Context, ent.Query) error { - return f.decision -} - -func (f fixedDecision) EvalMutation(context.Context, ent.Mutation) error { - return f.decision -} - -type contextDecision struct { - eval func(context.Context) error -} - -// ContextQueryMutationRule creates a query/mutation rule from a context eval func. -func ContextQueryMutationRule(eval func(context.Context) error) QueryMutationRule { - return contextDecision{eval} -} - -func (c contextDecision) EvalQuery(ctx context.Context, _ ent.Query) error { - return c.eval(ctx) -} - -func (c contextDecision) EvalMutation(ctx context.Context, _ ent.Mutation) error { - return c.eval(ctx) -} - -// OnMutationOperation evaluates the given rule only on a given mutation operation. -func OnMutationOperation(rule MutationRule, op ent.Op) MutationRule { - return MutationRuleFunc(func(ctx context.Context, m ent.Mutation) error { - if m.Op().Is(op) { - return rule.EvalMutation(ctx, m) - } - return Skip - }) -} - -// DenyMutationOperationRule returns a rule denying specified mutation operation. -func DenyMutationOperationRule(op ent.Op) MutationRule { - rule := MutationRuleFunc(func(_ context.Context, m ent.Mutation) error { - return Denyf("ent/privacy: operation %s is not allowed", m.Op()) - }) - return OnMutationOperation(rule, op) -} - -// The GroupQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type GroupQueryRuleFunc func(context.Context, *ent.GroupQuery) error - -// EvalQuery return f(ctx, q). -func (f GroupQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.GroupQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.GroupQuery", q) -} - -// The GroupMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type GroupMutationRuleFunc func(context.Context, *ent.GroupMutation) error - -// EvalMutation calls f(ctx, m). -func (f GroupMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.GroupMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.GroupMutation", m) -} - -// The UserQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type UserQueryRuleFunc func(context.Context, *ent.UserQuery) error - -// EvalQuery return f(ctx, q). -func (f UserQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.UserQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.UserQuery", q) -} - -// The UserMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type UserMutationRuleFunc func(context.Context, *ent.UserMutation) error - -// EvalMutation calls f(ctx, m). -func (f UserMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.UserMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.UserMutation", m) -} diff --git a/examples/m2mbidi/ent/privacy/privacy.go b/examples/m2mbidi/ent/privacy/privacy.go deleted file mode 100644 index d8a535b72..000000000 --- a/examples/m2mbidi/ent/privacy/privacy.go +++ /dev/null @@ -1,239 +0,0 @@ -// 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. - -// Code generated by entc, DO NOT EDIT. - -package privacy - -import ( - "context" - "errors" - "fmt" - - "github.com/facebook/ent/examples/m2mbidi/ent" -) - -var ( - // Allow may be returned by rules to indicate that the policy - // evaluation should terminate with an allow decision. - Allow = errors.New("ent/privacy: allow rule") - - // Deny may be returned by rules to indicate that the policy - // evaluation should terminate with an deny decision. - Deny = errors.New("ent/privacy: deny rule") - - // Skip may be returned by rules to indicate that the policy - // evaluation should continue to the next rule. - Skip = errors.New("ent/privacy: skip rule") -) - -// Allowf returns an formatted wrapped Allow decision. -func Allowf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Allow)...) -} - -// Denyf returns an formatted wrapped Deny decision. -func Denyf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Deny)...) -} - -// Skipf returns an formatted wrapped Skip decision. -func Skipf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Skip)...) -} - -type decisionCtxKey struct{} - -// DecisionContext creates a decision context. -func DecisionContext(parent context.Context, decision error) context.Context { - if decision == nil || errors.Is(decision, Skip) { - return parent - } - return context.WithValue(parent, decisionCtxKey{}, decision) -} - -func decisionFromContext(ctx context.Context) (error, bool) { - decision, ok := ctx.Value(decisionCtxKey{}).(error) - if ok && errors.Is(decision, Allow) { - decision = nil - } - return decision, ok -} - -type ( - // QueryPolicy combines multiple query rules into a single policy. - QueryPolicy []QueryRule - - // QueryRule defines the interface deciding whether a - // query is allowed and optionally modify it. - QueryRule interface { - EvalQuery(context.Context, ent.Query) error - } -) - -// EvalQuery evaluates a query against a query policy. -func (policy QueryPolicy) EvalQuery(ctx context.Context, q ent.Query) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalQuery(ctx, q); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// QueryRuleFunc type is an adapter to allow the use of -// ordinary functions as query rules. -type QueryRuleFunc func(context.Context, ent.Query) error - -// Eval returns f(ctx, q). -func (f QueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - return f(ctx, q) -} - -type ( - // MutationPolicy combines multiple mutation rules into a single policy. - MutationPolicy []MutationRule - - // MutationRule defines the interface deciding whether a - // mutation is allowed and optionally modify it. - MutationRule interface { - EvalMutation(context.Context, ent.Mutation) error - } -) - -// EvalMutation evaluates a mutation against a mutation policy. -func (policy MutationPolicy) EvalMutation(ctx context.Context, m ent.Mutation) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalMutation(ctx, m); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// MutationRuleFunc type is an adapter to allow the use of -// ordinary functions as mutation rules. -type MutationRuleFunc func(context.Context, ent.Mutation) error - -// EvalMutation returns f(ctx, m). -func (f MutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - return f(ctx, m) -} - -// Policy groups query and mutation policies. -type Policy struct { - Query QueryPolicy - Mutation MutationPolicy -} - -// EvalQuery forwards evaluation to query policy. -func (policy Policy) EvalQuery(ctx context.Context, q ent.Query) error { - return policy.Query.EvalQuery(ctx, q) -} - -// EvalMutation forwards evaluation to mutation policy. -func (policy Policy) EvalMutation(ctx context.Context, m ent.Mutation) error { - return policy.Mutation.EvalMutation(ctx, m) -} - -// QueryMutationRule is the interface that groups query and mutation rules. -type QueryMutationRule interface { - QueryRule - MutationRule -} - -// AlwaysAllowRule returns a rule that returns an allow decision. -func AlwaysAllowRule() QueryMutationRule { - return fixedDecision{Allow} -} - -// AlwaysDenyRule returns a rule that returns a deny decision. -func AlwaysDenyRule() QueryMutationRule { - return fixedDecision{Deny} -} - -type fixedDecision struct { - decision error -} - -func (f fixedDecision) EvalQuery(context.Context, ent.Query) error { - return f.decision -} - -func (f fixedDecision) EvalMutation(context.Context, ent.Mutation) error { - return f.decision -} - -type contextDecision struct { - eval func(context.Context) error -} - -// ContextQueryMutationRule creates a query/mutation rule from a context eval func. -func ContextQueryMutationRule(eval func(context.Context) error) QueryMutationRule { - return contextDecision{eval} -} - -func (c contextDecision) EvalQuery(ctx context.Context, _ ent.Query) error { - return c.eval(ctx) -} - -func (c contextDecision) EvalMutation(ctx context.Context, _ ent.Mutation) error { - return c.eval(ctx) -} - -// OnMutationOperation evaluates the given rule only on a given mutation operation. -func OnMutationOperation(rule MutationRule, op ent.Op) MutationRule { - return MutationRuleFunc(func(ctx context.Context, m ent.Mutation) error { - if m.Op().Is(op) { - return rule.EvalMutation(ctx, m) - } - return Skip - }) -} - -// DenyMutationOperationRule returns a rule denying specified mutation operation. -func DenyMutationOperationRule(op ent.Op) MutationRule { - rule := MutationRuleFunc(func(_ context.Context, m ent.Mutation) error { - return Denyf("ent/privacy: operation %s is not allowed", m.Op()) - }) - return OnMutationOperation(rule, op) -} - -// The UserQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type UserQueryRuleFunc func(context.Context, *ent.UserQuery) error - -// EvalQuery return f(ctx, q). -func (f UserQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.UserQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.UserQuery", q) -} - -// The UserMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type UserMutationRuleFunc func(context.Context, *ent.UserMutation) error - -// EvalMutation calls f(ctx, m). -func (f UserMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.UserMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.UserMutation", m) -} diff --git a/examples/m2mrecur/ent/privacy/privacy.go b/examples/m2mrecur/ent/privacy/privacy.go deleted file mode 100644 index 285753b9b..000000000 --- a/examples/m2mrecur/ent/privacy/privacy.go +++ /dev/null @@ -1,239 +0,0 @@ -// 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. - -// Code generated by entc, DO NOT EDIT. - -package privacy - -import ( - "context" - "errors" - "fmt" - - "github.com/facebook/ent/examples/m2mrecur/ent" -) - -var ( - // Allow may be returned by rules to indicate that the policy - // evaluation should terminate with an allow decision. - Allow = errors.New("ent/privacy: allow rule") - - // Deny may be returned by rules to indicate that the policy - // evaluation should terminate with an deny decision. - Deny = errors.New("ent/privacy: deny rule") - - // Skip may be returned by rules to indicate that the policy - // evaluation should continue to the next rule. - Skip = errors.New("ent/privacy: skip rule") -) - -// Allowf returns an formatted wrapped Allow decision. -func Allowf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Allow)...) -} - -// Denyf returns an formatted wrapped Deny decision. -func Denyf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Deny)...) -} - -// Skipf returns an formatted wrapped Skip decision. -func Skipf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Skip)...) -} - -type decisionCtxKey struct{} - -// DecisionContext creates a decision context. -func DecisionContext(parent context.Context, decision error) context.Context { - if decision == nil || errors.Is(decision, Skip) { - return parent - } - return context.WithValue(parent, decisionCtxKey{}, decision) -} - -func decisionFromContext(ctx context.Context) (error, bool) { - decision, ok := ctx.Value(decisionCtxKey{}).(error) - if ok && errors.Is(decision, Allow) { - decision = nil - } - return decision, ok -} - -type ( - // QueryPolicy combines multiple query rules into a single policy. - QueryPolicy []QueryRule - - // QueryRule defines the interface deciding whether a - // query is allowed and optionally modify it. - QueryRule interface { - EvalQuery(context.Context, ent.Query) error - } -) - -// EvalQuery evaluates a query against a query policy. -func (policy QueryPolicy) EvalQuery(ctx context.Context, q ent.Query) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalQuery(ctx, q); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// QueryRuleFunc type is an adapter to allow the use of -// ordinary functions as query rules. -type QueryRuleFunc func(context.Context, ent.Query) error - -// Eval returns f(ctx, q). -func (f QueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - return f(ctx, q) -} - -type ( - // MutationPolicy combines multiple mutation rules into a single policy. - MutationPolicy []MutationRule - - // MutationRule defines the interface deciding whether a - // mutation is allowed and optionally modify it. - MutationRule interface { - EvalMutation(context.Context, ent.Mutation) error - } -) - -// EvalMutation evaluates a mutation against a mutation policy. -func (policy MutationPolicy) EvalMutation(ctx context.Context, m ent.Mutation) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalMutation(ctx, m); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// MutationRuleFunc type is an adapter to allow the use of -// ordinary functions as mutation rules. -type MutationRuleFunc func(context.Context, ent.Mutation) error - -// EvalMutation returns f(ctx, m). -func (f MutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - return f(ctx, m) -} - -// Policy groups query and mutation policies. -type Policy struct { - Query QueryPolicy - Mutation MutationPolicy -} - -// EvalQuery forwards evaluation to query policy. -func (policy Policy) EvalQuery(ctx context.Context, q ent.Query) error { - return policy.Query.EvalQuery(ctx, q) -} - -// EvalMutation forwards evaluation to mutation policy. -func (policy Policy) EvalMutation(ctx context.Context, m ent.Mutation) error { - return policy.Mutation.EvalMutation(ctx, m) -} - -// QueryMutationRule is the interface that groups query and mutation rules. -type QueryMutationRule interface { - QueryRule - MutationRule -} - -// AlwaysAllowRule returns a rule that returns an allow decision. -func AlwaysAllowRule() QueryMutationRule { - return fixedDecision{Allow} -} - -// AlwaysDenyRule returns a rule that returns a deny decision. -func AlwaysDenyRule() QueryMutationRule { - return fixedDecision{Deny} -} - -type fixedDecision struct { - decision error -} - -func (f fixedDecision) EvalQuery(context.Context, ent.Query) error { - return f.decision -} - -func (f fixedDecision) EvalMutation(context.Context, ent.Mutation) error { - return f.decision -} - -type contextDecision struct { - eval func(context.Context) error -} - -// ContextQueryMutationRule creates a query/mutation rule from a context eval func. -func ContextQueryMutationRule(eval func(context.Context) error) QueryMutationRule { - return contextDecision{eval} -} - -func (c contextDecision) EvalQuery(ctx context.Context, _ ent.Query) error { - return c.eval(ctx) -} - -func (c contextDecision) EvalMutation(ctx context.Context, _ ent.Mutation) error { - return c.eval(ctx) -} - -// OnMutationOperation evaluates the given rule only on a given mutation operation. -func OnMutationOperation(rule MutationRule, op ent.Op) MutationRule { - return MutationRuleFunc(func(ctx context.Context, m ent.Mutation) error { - if m.Op().Is(op) { - return rule.EvalMutation(ctx, m) - } - return Skip - }) -} - -// DenyMutationOperationRule returns a rule denying specified mutation operation. -func DenyMutationOperationRule(op ent.Op) MutationRule { - rule := MutationRuleFunc(func(_ context.Context, m ent.Mutation) error { - return Denyf("ent/privacy: operation %s is not allowed", m.Op()) - }) - return OnMutationOperation(rule, op) -} - -// The UserQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type UserQueryRuleFunc func(context.Context, *ent.UserQuery) error - -// EvalQuery return f(ctx, q). -func (f UserQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.UserQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.UserQuery", q) -} - -// The UserMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type UserMutationRuleFunc func(context.Context, *ent.UserMutation) error - -// EvalMutation calls f(ctx, m). -func (f UserMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.UserMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.UserMutation", m) -} diff --git a/examples/o2m2types/ent/privacy/privacy.go b/examples/o2m2types/ent/privacy/privacy.go deleted file mode 100644 index 2495c0108..000000000 --- a/examples/o2m2types/ent/privacy/privacy.go +++ /dev/null @@ -1,263 +0,0 @@ -// 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. - -// Code generated by entc, DO NOT EDIT. - -package privacy - -import ( - "context" - "errors" - "fmt" - - "github.com/facebook/ent/examples/o2m2types/ent" -) - -var ( - // Allow may be returned by rules to indicate that the policy - // evaluation should terminate with an allow decision. - Allow = errors.New("ent/privacy: allow rule") - - // Deny may be returned by rules to indicate that the policy - // evaluation should terminate with an deny decision. - Deny = errors.New("ent/privacy: deny rule") - - // Skip may be returned by rules to indicate that the policy - // evaluation should continue to the next rule. - Skip = errors.New("ent/privacy: skip rule") -) - -// Allowf returns an formatted wrapped Allow decision. -func Allowf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Allow)...) -} - -// Denyf returns an formatted wrapped Deny decision. -func Denyf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Deny)...) -} - -// Skipf returns an formatted wrapped Skip decision. -func Skipf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Skip)...) -} - -type decisionCtxKey struct{} - -// DecisionContext creates a decision context. -func DecisionContext(parent context.Context, decision error) context.Context { - if decision == nil || errors.Is(decision, Skip) { - return parent - } - return context.WithValue(parent, decisionCtxKey{}, decision) -} - -func decisionFromContext(ctx context.Context) (error, bool) { - decision, ok := ctx.Value(decisionCtxKey{}).(error) - if ok && errors.Is(decision, Allow) { - decision = nil - } - return decision, ok -} - -type ( - // QueryPolicy combines multiple query rules into a single policy. - QueryPolicy []QueryRule - - // QueryRule defines the interface deciding whether a - // query is allowed and optionally modify it. - QueryRule interface { - EvalQuery(context.Context, ent.Query) error - } -) - -// EvalQuery evaluates a query against a query policy. -func (policy QueryPolicy) EvalQuery(ctx context.Context, q ent.Query) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalQuery(ctx, q); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// QueryRuleFunc type is an adapter to allow the use of -// ordinary functions as query rules. -type QueryRuleFunc func(context.Context, ent.Query) error - -// Eval returns f(ctx, q). -func (f QueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - return f(ctx, q) -} - -type ( - // MutationPolicy combines multiple mutation rules into a single policy. - MutationPolicy []MutationRule - - // MutationRule defines the interface deciding whether a - // mutation is allowed and optionally modify it. - MutationRule interface { - EvalMutation(context.Context, ent.Mutation) error - } -) - -// EvalMutation evaluates a mutation against a mutation policy. -func (policy MutationPolicy) EvalMutation(ctx context.Context, m ent.Mutation) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalMutation(ctx, m); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// MutationRuleFunc type is an adapter to allow the use of -// ordinary functions as mutation rules. -type MutationRuleFunc func(context.Context, ent.Mutation) error - -// EvalMutation returns f(ctx, m). -func (f MutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - return f(ctx, m) -} - -// Policy groups query and mutation policies. -type Policy struct { - Query QueryPolicy - Mutation MutationPolicy -} - -// EvalQuery forwards evaluation to query policy. -func (policy Policy) EvalQuery(ctx context.Context, q ent.Query) error { - return policy.Query.EvalQuery(ctx, q) -} - -// EvalMutation forwards evaluation to mutation policy. -func (policy Policy) EvalMutation(ctx context.Context, m ent.Mutation) error { - return policy.Mutation.EvalMutation(ctx, m) -} - -// QueryMutationRule is the interface that groups query and mutation rules. -type QueryMutationRule interface { - QueryRule - MutationRule -} - -// AlwaysAllowRule returns a rule that returns an allow decision. -func AlwaysAllowRule() QueryMutationRule { - return fixedDecision{Allow} -} - -// AlwaysDenyRule returns a rule that returns a deny decision. -func AlwaysDenyRule() QueryMutationRule { - return fixedDecision{Deny} -} - -type fixedDecision struct { - decision error -} - -func (f fixedDecision) EvalQuery(context.Context, ent.Query) error { - return f.decision -} - -func (f fixedDecision) EvalMutation(context.Context, ent.Mutation) error { - return f.decision -} - -type contextDecision struct { - eval func(context.Context) error -} - -// ContextQueryMutationRule creates a query/mutation rule from a context eval func. -func ContextQueryMutationRule(eval func(context.Context) error) QueryMutationRule { - return contextDecision{eval} -} - -func (c contextDecision) EvalQuery(ctx context.Context, _ ent.Query) error { - return c.eval(ctx) -} - -func (c contextDecision) EvalMutation(ctx context.Context, _ ent.Mutation) error { - return c.eval(ctx) -} - -// OnMutationOperation evaluates the given rule only on a given mutation operation. -func OnMutationOperation(rule MutationRule, op ent.Op) MutationRule { - return MutationRuleFunc(func(ctx context.Context, m ent.Mutation) error { - if m.Op().Is(op) { - return rule.EvalMutation(ctx, m) - } - return Skip - }) -} - -// DenyMutationOperationRule returns a rule denying specified mutation operation. -func DenyMutationOperationRule(op ent.Op) MutationRule { - rule := MutationRuleFunc(func(_ context.Context, m ent.Mutation) error { - return Denyf("ent/privacy: operation %s is not allowed", m.Op()) - }) - return OnMutationOperation(rule, op) -} - -// The PetQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type PetQueryRuleFunc func(context.Context, *ent.PetQuery) error - -// EvalQuery return f(ctx, q). -func (f PetQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.PetQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.PetQuery", q) -} - -// The PetMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type PetMutationRuleFunc func(context.Context, *ent.PetMutation) error - -// EvalMutation calls f(ctx, m). -func (f PetMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.PetMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.PetMutation", m) -} - -// The UserQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type UserQueryRuleFunc func(context.Context, *ent.UserQuery) error - -// EvalQuery return f(ctx, q). -func (f UserQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.UserQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.UserQuery", q) -} - -// The UserMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type UserMutationRuleFunc func(context.Context, *ent.UserMutation) error - -// EvalMutation calls f(ctx, m). -func (f UserMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.UserMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.UserMutation", m) -} diff --git a/examples/o2mrecur/ent/privacy/privacy.go b/examples/o2mrecur/ent/privacy/privacy.go deleted file mode 100644 index 1e3d8ba3f..000000000 --- a/examples/o2mrecur/ent/privacy/privacy.go +++ /dev/null @@ -1,239 +0,0 @@ -// 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. - -// Code generated by entc, DO NOT EDIT. - -package privacy - -import ( - "context" - "errors" - "fmt" - - "github.com/facebook/ent/examples/o2mrecur/ent" -) - -var ( - // Allow may be returned by rules to indicate that the policy - // evaluation should terminate with an allow decision. - Allow = errors.New("ent/privacy: allow rule") - - // Deny may be returned by rules to indicate that the policy - // evaluation should terminate with an deny decision. - Deny = errors.New("ent/privacy: deny rule") - - // Skip may be returned by rules to indicate that the policy - // evaluation should continue to the next rule. - Skip = errors.New("ent/privacy: skip rule") -) - -// Allowf returns an formatted wrapped Allow decision. -func Allowf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Allow)...) -} - -// Denyf returns an formatted wrapped Deny decision. -func Denyf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Deny)...) -} - -// Skipf returns an formatted wrapped Skip decision. -func Skipf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Skip)...) -} - -type decisionCtxKey struct{} - -// DecisionContext creates a decision context. -func DecisionContext(parent context.Context, decision error) context.Context { - if decision == nil || errors.Is(decision, Skip) { - return parent - } - return context.WithValue(parent, decisionCtxKey{}, decision) -} - -func decisionFromContext(ctx context.Context) (error, bool) { - decision, ok := ctx.Value(decisionCtxKey{}).(error) - if ok && errors.Is(decision, Allow) { - decision = nil - } - return decision, ok -} - -type ( - // QueryPolicy combines multiple query rules into a single policy. - QueryPolicy []QueryRule - - // QueryRule defines the interface deciding whether a - // query is allowed and optionally modify it. - QueryRule interface { - EvalQuery(context.Context, ent.Query) error - } -) - -// EvalQuery evaluates a query against a query policy. -func (policy QueryPolicy) EvalQuery(ctx context.Context, q ent.Query) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalQuery(ctx, q); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// QueryRuleFunc type is an adapter to allow the use of -// ordinary functions as query rules. -type QueryRuleFunc func(context.Context, ent.Query) error - -// Eval returns f(ctx, q). -func (f QueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - return f(ctx, q) -} - -type ( - // MutationPolicy combines multiple mutation rules into a single policy. - MutationPolicy []MutationRule - - // MutationRule defines the interface deciding whether a - // mutation is allowed and optionally modify it. - MutationRule interface { - EvalMutation(context.Context, ent.Mutation) error - } -) - -// EvalMutation evaluates a mutation against a mutation policy. -func (policy MutationPolicy) EvalMutation(ctx context.Context, m ent.Mutation) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalMutation(ctx, m); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// MutationRuleFunc type is an adapter to allow the use of -// ordinary functions as mutation rules. -type MutationRuleFunc func(context.Context, ent.Mutation) error - -// EvalMutation returns f(ctx, m). -func (f MutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - return f(ctx, m) -} - -// Policy groups query and mutation policies. -type Policy struct { - Query QueryPolicy - Mutation MutationPolicy -} - -// EvalQuery forwards evaluation to query policy. -func (policy Policy) EvalQuery(ctx context.Context, q ent.Query) error { - return policy.Query.EvalQuery(ctx, q) -} - -// EvalMutation forwards evaluation to mutation policy. -func (policy Policy) EvalMutation(ctx context.Context, m ent.Mutation) error { - return policy.Mutation.EvalMutation(ctx, m) -} - -// QueryMutationRule is the interface that groups query and mutation rules. -type QueryMutationRule interface { - QueryRule - MutationRule -} - -// AlwaysAllowRule returns a rule that returns an allow decision. -func AlwaysAllowRule() QueryMutationRule { - return fixedDecision{Allow} -} - -// AlwaysDenyRule returns a rule that returns a deny decision. -func AlwaysDenyRule() QueryMutationRule { - return fixedDecision{Deny} -} - -type fixedDecision struct { - decision error -} - -func (f fixedDecision) EvalQuery(context.Context, ent.Query) error { - return f.decision -} - -func (f fixedDecision) EvalMutation(context.Context, ent.Mutation) error { - return f.decision -} - -type contextDecision struct { - eval func(context.Context) error -} - -// ContextQueryMutationRule creates a query/mutation rule from a context eval func. -func ContextQueryMutationRule(eval func(context.Context) error) QueryMutationRule { - return contextDecision{eval} -} - -func (c contextDecision) EvalQuery(ctx context.Context, _ ent.Query) error { - return c.eval(ctx) -} - -func (c contextDecision) EvalMutation(ctx context.Context, _ ent.Mutation) error { - return c.eval(ctx) -} - -// OnMutationOperation evaluates the given rule only on a given mutation operation. -func OnMutationOperation(rule MutationRule, op ent.Op) MutationRule { - return MutationRuleFunc(func(ctx context.Context, m ent.Mutation) error { - if m.Op().Is(op) { - return rule.EvalMutation(ctx, m) - } - return Skip - }) -} - -// DenyMutationOperationRule returns a rule denying specified mutation operation. -func DenyMutationOperationRule(op ent.Op) MutationRule { - rule := MutationRuleFunc(func(_ context.Context, m ent.Mutation) error { - return Denyf("ent/privacy: operation %s is not allowed", m.Op()) - }) - return OnMutationOperation(rule, op) -} - -// The NodeQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type NodeQueryRuleFunc func(context.Context, *ent.NodeQuery) error - -// EvalQuery return f(ctx, q). -func (f NodeQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.NodeQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.NodeQuery", q) -} - -// The NodeMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type NodeMutationRuleFunc func(context.Context, *ent.NodeMutation) error - -// EvalMutation calls f(ctx, m). -func (f NodeMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.NodeMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.NodeMutation", m) -} diff --git a/examples/o2o2types/ent/privacy/privacy.go b/examples/o2o2types/ent/privacy/privacy.go deleted file mode 100644 index 60343f5ab..000000000 --- a/examples/o2o2types/ent/privacy/privacy.go +++ /dev/null @@ -1,263 +0,0 @@ -// 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. - -// Code generated by entc, DO NOT EDIT. - -package privacy - -import ( - "context" - "errors" - "fmt" - - "github.com/facebook/ent/examples/o2o2types/ent" -) - -var ( - // Allow may be returned by rules to indicate that the policy - // evaluation should terminate with an allow decision. - Allow = errors.New("ent/privacy: allow rule") - - // Deny may be returned by rules to indicate that the policy - // evaluation should terminate with an deny decision. - Deny = errors.New("ent/privacy: deny rule") - - // Skip may be returned by rules to indicate that the policy - // evaluation should continue to the next rule. - Skip = errors.New("ent/privacy: skip rule") -) - -// Allowf returns an formatted wrapped Allow decision. -func Allowf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Allow)...) -} - -// Denyf returns an formatted wrapped Deny decision. -func Denyf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Deny)...) -} - -// Skipf returns an formatted wrapped Skip decision. -func Skipf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Skip)...) -} - -type decisionCtxKey struct{} - -// DecisionContext creates a decision context. -func DecisionContext(parent context.Context, decision error) context.Context { - if decision == nil || errors.Is(decision, Skip) { - return parent - } - return context.WithValue(parent, decisionCtxKey{}, decision) -} - -func decisionFromContext(ctx context.Context) (error, bool) { - decision, ok := ctx.Value(decisionCtxKey{}).(error) - if ok && errors.Is(decision, Allow) { - decision = nil - } - return decision, ok -} - -type ( - // QueryPolicy combines multiple query rules into a single policy. - QueryPolicy []QueryRule - - // QueryRule defines the interface deciding whether a - // query is allowed and optionally modify it. - QueryRule interface { - EvalQuery(context.Context, ent.Query) error - } -) - -// EvalQuery evaluates a query against a query policy. -func (policy QueryPolicy) EvalQuery(ctx context.Context, q ent.Query) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalQuery(ctx, q); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// QueryRuleFunc type is an adapter to allow the use of -// ordinary functions as query rules. -type QueryRuleFunc func(context.Context, ent.Query) error - -// Eval returns f(ctx, q). -func (f QueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - return f(ctx, q) -} - -type ( - // MutationPolicy combines multiple mutation rules into a single policy. - MutationPolicy []MutationRule - - // MutationRule defines the interface deciding whether a - // mutation is allowed and optionally modify it. - MutationRule interface { - EvalMutation(context.Context, ent.Mutation) error - } -) - -// EvalMutation evaluates a mutation against a mutation policy. -func (policy MutationPolicy) EvalMutation(ctx context.Context, m ent.Mutation) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalMutation(ctx, m); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// MutationRuleFunc type is an adapter to allow the use of -// ordinary functions as mutation rules. -type MutationRuleFunc func(context.Context, ent.Mutation) error - -// EvalMutation returns f(ctx, m). -func (f MutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - return f(ctx, m) -} - -// Policy groups query and mutation policies. -type Policy struct { - Query QueryPolicy - Mutation MutationPolicy -} - -// EvalQuery forwards evaluation to query policy. -func (policy Policy) EvalQuery(ctx context.Context, q ent.Query) error { - return policy.Query.EvalQuery(ctx, q) -} - -// EvalMutation forwards evaluation to mutation policy. -func (policy Policy) EvalMutation(ctx context.Context, m ent.Mutation) error { - return policy.Mutation.EvalMutation(ctx, m) -} - -// QueryMutationRule is the interface that groups query and mutation rules. -type QueryMutationRule interface { - QueryRule - MutationRule -} - -// AlwaysAllowRule returns a rule that returns an allow decision. -func AlwaysAllowRule() QueryMutationRule { - return fixedDecision{Allow} -} - -// AlwaysDenyRule returns a rule that returns a deny decision. -func AlwaysDenyRule() QueryMutationRule { - return fixedDecision{Deny} -} - -type fixedDecision struct { - decision error -} - -func (f fixedDecision) EvalQuery(context.Context, ent.Query) error { - return f.decision -} - -func (f fixedDecision) EvalMutation(context.Context, ent.Mutation) error { - return f.decision -} - -type contextDecision struct { - eval func(context.Context) error -} - -// ContextQueryMutationRule creates a query/mutation rule from a context eval func. -func ContextQueryMutationRule(eval func(context.Context) error) QueryMutationRule { - return contextDecision{eval} -} - -func (c contextDecision) EvalQuery(ctx context.Context, _ ent.Query) error { - return c.eval(ctx) -} - -func (c contextDecision) EvalMutation(ctx context.Context, _ ent.Mutation) error { - return c.eval(ctx) -} - -// OnMutationOperation evaluates the given rule only on a given mutation operation. -func OnMutationOperation(rule MutationRule, op ent.Op) MutationRule { - return MutationRuleFunc(func(ctx context.Context, m ent.Mutation) error { - if m.Op().Is(op) { - return rule.EvalMutation(ctx, m) - } - return Skip - }) -} - -// DenyMutationOperationRule returns a rule denying specified mutation operation. -func DenyMutationOperationRule(op ent.Op) MutationRule { - rule := MutationRuleFunc(func(_ context.Context, m ent.Mutation) error { - return Denyf("ent/privacy: operation %s is not allowed", m.Op()) - }) - return OnMutationOperation(rule, op) -} - -// The CardQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type CardQueryRuleFunc func(context.Context, *ent.CardQuery) error - -// EvalQuery return f(ctx, q). -func (f CardQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.CardQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.CardQuery", q) -} - -// The CardMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type CardMutationRuleFunc func(context.Context, *ent.CardMutation) error - -// EvalMutation calls f(ctx, m). -func (f CardMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.CardMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.CardMutation", m) -} - -// The UserQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type UserQueryRuleFunc func(context.Context, *ent.UserQuery) error - -// EvalQuery return f(ctx, q). -func (f UserQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.UserQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.UserQuery", q) -} - -// The UserMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type UserMutationRuleFunc func(context.Context, *ent.UserMutation) error - -// EvalMutation calls f(ctx, m). -func (f UserMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.UserMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.UserMutation", m) -} diff --git a/examples/o2obidi/ent/privacy/privacy.go b/examples/o2obidi/ent/privacy/privacy.go deleted file mode 100644 index 7c569b028..000000000 --- a/examples/o2obidi/ent/privacy/privacy.go +++ /dev/null @@ -1,239 +0,0 @@ -// 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. - -// Code generated by entc, DO NOT EDIT. - -package privacy - -import ( - "context" - "errors" - "fmt" - - "github.com/facebook/ent/examples/o2obidi/ent" -) - -var ( - // Allow may be returned by rules to indicate that the policy - // evaluation should terminate with an allow decision. - Allow = errors.New("ent/privacy: allow rule") - - // Deny may be returned by rules to indicate that the policy - // evaluation should terminate with an deny decision. - Deny = errors.New("ent/privacy: deny rule") - - // Skip may be returned by rules to indicate that the policy - // evaluation should continue to the next rule. - Skip = errors.New("ent/privacy: skip rule") -) - -// Allowf returns an formatted wrapped Allow decision. -func Allowf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Allow)...) -} - -// Denyf returns an formatted wrapped Deny decision. -func Denyf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Deny)...) -} - -// Skipf returns an formatted wrapped Skip decision. -func Skipf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Skip)...) -} - -type decisionCtxKey struct{} - -// DecisionContext creates a decision context. -func DecisionContext(parent context.Context, decision error) context.Context { - if decision == nil || errors.Is(decision, Skip) { - return parent - } - return context.WithValue(parent, decisionCtxKey{}, decision) -} - -func decisionFromContext(ctx context.Context) (error, bool) { - decision, ok := ctx.Value(decisionCtxKey{}).(error) - if ok && errors.Is(decision, Allow) { - decision = nil - } - return decision, ok -} - -type ( - // QueryPolicy combines multiple query rules into a single policy. - QueryPolicy []QueryRule - - // QueryRule defines the interface deciding whether a - // query is allowed and optionally modify it. - QueryRule interface { - EvalQuery(context.Context, ent.Query) error - } -) - -// EvalQuery evaluates a query against a query policy. -func (policy QueryPolicy) EvalQuery(ctx context.Context, q ent.Query) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalQuery(ctx, q); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// QueryRuleFunc type is an adapter to allow the use of -// ordinary functions as query rules. -type QueryRuleFunc func(context.Context, ent.Query) error - -// Eval returns f(ctx, q). -func (f QueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - return f(ctx, q) -} - -type ( - // MutationPolicy combines multiple mutation rules into a single policy. - MutationPolicy []MutationRule - - // MutationRule defines the interface deciding whether a - // mutation is allowed and optionally modify it. - MutationRule interface { - EvalMutation(context.Context, ent.Mutation) error - } -) - -// EvalMutation evaluates a mutation against a mutation policy. -func (policy MutationPolicy) EvalMutation(ctx context.Context, m ent.Mutation) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalMutation(ctx, m); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// MutationRuleFunc type is an adapter to allow the use of -// ordinary functions as mutation rules. -type MutationRuleFunc func(context.Context, ent.Mutation) error - -// EvalMutation returns f(ctx, m). -func (f MutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - return f(ctx, m) -} - -// Policy groups query and mutation policies. -type Policy struct { - Query QueryPolicy - Mutation MutationPolicy -} - -// EvalQuery forwards evaluation to query policy. -func (policy Policy) EvalQuery(ctx context.Context, q ent.Query) error { - return policy.Query.EvalQuery(ctx, q) -} - -// EvalMutation forwards evaluation to mutation policy. -func (policy Policy) EvalMutation(ctx context.Context, m ent.Mutation) error { - return policy.Mutation.EvalMutation(ctx, m) -} - -// QueryMutationRule is the interface that groups query and mutation rules. -type QueryMutationRule interface { - QueryRule - MutationRule -} - -// AlwaysAllowRule returns a rule that returns an allow decision. -func AlwaysAllowRule() QueryMutationRule { - return fixedDecision{Allow} -} - -// AlwaysDenyRule returns a rule that returns a deny decision. -func AlwaysDenyRule() QueryMutationRule { - return fixedDecision{Deny} -} - -type fixedDecision struct { - decision error -} - -func (f fixedDecision) EvalQuery(context.Context, ent.Query) error { - return f.decision -} - -func (f fixedDecision) EvalMutation(context.Context, ent.Mutation) error { - return f.decision -} - -type contextDecision struct { - eval func(context.Context) error -} - -// ContextQueryMutationRule creates a query/mutation rule from a context eval func. -func ContextQueryMutationRule(eval func(context.Context) error) QueryMutationRule { - return contextDecision{eval} -} - -func (c contextDecision) EvalQuery(ctx context.Context, _ ent.Query) error { - return c.eval(ctx) -} - -func (c contextDecision) EvalMutation(ctx context.Context, _ ent.Mutation) error { - return c.eval(ctx) -} - -// OnMutationOperation evaluates the given rule only on a given mutation operation. -func OnMutationOperation(rule MutationRule, op ent.Op) MutationRule { - return MutationRuleFunc(func(ctx context.Context, m ent.Mutation) error { - if m.Op().Is(op) { - return rule.EvalMutation(ctx, m) - } - return Skip - }) -} - -// DenyMutationOperationRule returns a rule denying specified mutation operation. -func DenyMutationOperationRule(op ent.Op) MutationRule { - rule := MutationRuleFunc(func(_ context.Context, m ent.Mutation) error { - return Denyf("ent/privacy: operation %s is not allowed", m.Op()) - }) - return OnMutationOperation(rule, op) -} - -// The UserQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type UserQueryRuleFunc func(context.Context, *ent.UserQuery) error - -// EvalQuery return f(ctx, q). -func (f UserQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.UserQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.UserQuery", q) -} - -// The UserMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type UserMutationRuleFunc func(context.Context, *ent.UserMutation) error - -// EvalMutation calls f(ctx, m). -func (f UserMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.UserMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.UserMutation", m) -} diff --git a/examples/o2orecur/ent/privacy/privacy.go b/examples/o2orecur/ent/privacy/privacy.go deleted file mode 100644 index 55e9e4c6e..000000000 --- a/examples/o2orecur/ent/privacy/privacy.go +++ /dev/null @@ -1,239 +0,0 @@ -// 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. - -// Code generated by entc, DO NOT EDIT. - -package privacy - -import ( - "context" - "errors" - "fmt" - - "github.com/facebook/ent/examples/o2orecur/ent" -) - -var ( - // Allow may be returned by rules to indicate that the policy - // evaluation should terminate with an allow decision. - Allow = errors.New("ent/privacy: allow rule") - - // Deny may be returned by rules to indicate that the policy - // evaluation should terminate with an deny decision. - Deny = errors.New("ent/privacy: deny rule") - - // Skip may be returned by rules to indicate that the policy - // evaluation should continue to the next rule. - Skip = errors.New("ent/privacy: skip rule") -) - -// Allowf returns an formatted wrapped Allow decision. -func Allowf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Allow)...) -} - -// Denyf returns an formatted wrapped Deny decision. -func Denyf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Deny)...) -} - -// Skipf returns an formatted wrapped Skip decision. -func Skipf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Skip)...) -} - -type decisionCtxKey struct{} - -// DecisionContext creates a decision context. -func DecisionContext(parent context.Context, decision error) context.Context { - if decision == nil || errors.Is(decision, Skip) { - return parent - } - return context.WithValue(parent, decisionCtxKey{}, decision) -} - -func decisionFromContext(ctx context.Context) (error, bool) { - decision, ok := ctx.Value(decisionCtxKey{}).(error) - if ok && errors.Is(decision, Allow) { - decision = nil - } - return decision, ok -} - -type ( - // QueryPolicy combines multiple query rules into a single policy. - QueryPolicy []QueryRule - - // QueryRule defines the interface deciding whether a - // query is allowed and optionally modify it. - QueryRule interface { - EvalQuery(context.Context, ent.Query) error - } -) - -// EvalQuery evaluates a query against a query policy. -func (policy QueryPolicy) EvalQuery(ctx context.Context, q ent.Query) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalQuery(ctx, q); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// QueryRuleFunc type is an adapter to allow the use of -// ordinary functions as query rules. -type QueryRuleFunc func(context.Context, ent.Query) error - -// Eval returns f(ctx, q). -func (f QueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - return f(ctx, q) -} - -type ( - // MutationPolicy combines multiple mutation rules into a single policy. - MutationPolicy []MutationRule - - // MutationRule defines the interface deciding whether a - // mutation is allowed and optionally modify it. - MutationRule interface { - EvalMutation(context.Context, ent.Mutation) error - } -) - -// EvalMutation evaluates a mutation against a mutation policy. -func (policy MutationPolicy) EvalMutation(ctx context.Context, m ent.Mutation) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalMutation(ctx, m); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// MutationRuleFunc type is an adapter to allow the use of -// ordinary functions as mutation rules. -type MutationRuleFunc func(context.Context, ent.Mutation) error - -// EvalMutation returns f(ctx, m). -func (f MutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - return f(ctx, m) -} - -// Policy groups query and mutation policies. -type Policy struct { - Query QueryPolicy - Mutation MutationPolicy -} - -// EvalQuery forwards evaluation to query policy. -func (policy Policy) EvalQuery(ctx context.Context, q ent.Query) error { - return policy.Query.EvalQuery(ctx, q) -} - -// EvalMutation forwards evaluation to mutation policy. -func (policy Policy) EvalMutation(ctx context.Context, m ent.Mutation) error { - return policy.Mutation.EvalMutation(ctx, m) -} - -// QueryMutationRule is the interface that groups query and mutation rules. -type QueryMutationRule interface { - QueryRule - MutationRule -} - -// AlwaysAllowRule returns a rule that returns an allow decision. -func AlwaysAllowRule() QueryMutationRule { - return fixedDecision{Allow} -} - -// AlwaysDenyRule returns a rule that returns a deny decision. -func AlwaysDenyRule() QueryMutationRule { - return fixedDecision{Deny} -} - -type fixedDecision struct { - decision error -} - -func (f fixedDecision) EvalQuery(context.Context, ent.Query) error { - return f.decision -} - -func (f fixedDecision) EvalMutation(context.Context, ent.Mutation) error { - return f.decision -} - -type contextDecision struct { - eval func(context.Context) error -} - -// ContextQueryMutationRule creates a query/mutation rule from a context eval func. -func ContextQueryMutationRule(eval func(context.Context) error) QueryMutationRule { - return contextDecision{eval} -} - -func (c contextDecision) EvalQuery(ctx context.Context, _ ent.Query) error { - return c.eval(ctx) -} - -func (c contextDecision) EvalMutation(ctx context.Context, _ ent.Mutation) error { - return c.eval(ctx) -} - -// OnMutationOperation evaluates the given rule only on a given mutation operation. -func OnMutationOperation(rule MutationRule, op ent.Op) MutationRule { - return MutationRuleFunc(func(ctx context.Context, m ent.Mutation) error { - if m.Op().Is(op) { - return rule.EvalMutation(ctx, m) - } - return Skip - }) -} - -// DenyMutationOperationRule returns a rule denying specified mutation operation. -func DenyMutationOperationRule(op ent.Op) MutationRule { - rule := MutationRuleFunc(func(_ context.Context, m ent.Mutation) error { - return Denyf("ent/privacy: operation %s is not allowed", m.Op()) - }) - return OnMutationOperation(rule, op) -} - -// The NodeQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type NodeQueryRuleFunc func(context.Context, *ent.NodeQuery) error - -// EvalQuery return f(ctx, q). -func (f NodeQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.NodeQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.NodeQuery", q) -} - -// The NodeMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type NodeMutationRuleFunc func(context.Context, *ent.NodeMutation) error - -// EvalMutation calls f(ctx, m). -func (f NodeMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.NodeMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.NodeMutation", m) -} diff --git a/examples/start/ent/privacy/privacy.go b/examples/start/ent/privacy/privacy.go deleted file mode 100644 index 8d5de0ec3..000000000 --- a/examples/start/ent/privacy/privacy.go +++ /dev/null @@ -1,287 +0,0 @@ -// 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. - -// Code generated by entc, DO NOT EDIT. - -package privacy - -import ( - "context" - "errors" - "fmt" - - "github.com/facebook/ent/examples/start/ent" -) - -var ( - // Allow may be returned by rules to indicate that the policy - // evaluation should terminate with an allow decision. - Allow = errors.New("ent/privacy: allow rule") - - // Deny may be returned by rules to indicate that the policy - // evaluation should terminate with an deny decision. - Deny = errors.New("ent/privacy: deny rule") - - // Skip may be returned by rules to indicate that the policy - // evaluation should continue to the next rule. - Skip = errors.New("ent/privacy: skip rule") -) - -// Allowf returns an formatted wrapped Allow decision. -func Allowf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Allow)...) -} - -// Denyf returns an formatted wrapped Deny decision. -func Denyf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Deny)...) -} - -// Skipf returns an formatted wrapped Skip decision. -func Skipf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Skip)...) -} - -type decisionCtxKey struct{} - -// DecisionContext creates a decision context. -func DecisionContext(parent context.Context, decision error) context.Context { - if decision == nil || errors.Is(decision, Skip) { - return parent - } - return context.WithValue(parent, decisionCtxKey{}, decision) -} - -func decisionFromContext(ctx context.Context) (error, bool) { - decision, ok := ctx.Value(decisionCtxKey{}).(error) - if ok && errors.Is(decision, Allow) { - decision = nil - } - return decision, ok -} - -type ( - // QueryPolicy combines multiple query rules into a single policy. - QueryPolicy []QueryRule - - // QueryRule defines the interface deciding whether a - // query is allowed and optionally modify it. - QueryRule interface { - EvalQuery(context.Context, ent.Query) error - } -) - -// EvalQuery evaluates a query against a query policy. -func (policy QueryPolicy) EvalQuery(ctx context.Context, q ent.Query) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalQuery(ctx, q); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// QueryRuleFunc type is an adapter to allow the use of -// ordinary functions as query rules. -type QueryRuleFunc func(context.Context, ent.Query) error - -// Eval returns f(ctx, q). -func (f QueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - return f(ctx, q) -} - -type ( - // MutationPolicy combines multiple mutation rules into a single policy. - MutationPolicy []MutationRule - - // MutationRule defines the interface deciding whether a - // mutation is allowed and optionally modify it. - MutationRule interface { - EvalMutation(context.Context, ent.Mutation) error - } -) - -// EvalMutation evaluates a mutation against a mutation policy. -func (policy MutationPolicy) EvalMutation(ctx context.Context, m ent.Mutation) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalMutation(ctx, m); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// MutationRuleFunc type is an adapter to allow the use of -// ordinary functions as mutation rules. -type MutationRuleFunc func(context.Context, ent.Mutation) error - -// EvalMutation returns f(ctx, m). -func (f MutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - return f(ctx, m) -} - -// Policy groups query and mutation policies. -type Policy struct { - Query QueryPolicy - Mutation MutationPolicy -} - -// EvalQuery forwards evaluation to query policy. -func (policy Policy) EvalQuery(ctx context.Context, q ent.Query) error { - return policy.Query.EvalQuery(ctx, q) -} - -// EvalMutation forwards evaluation to mutation policy. -func (policy Policy) EvalMutation(ctx context.Context, m ent.Mutation) error { - return policy.Mutation.EvalMutation(ctx, m) -} - -// QueryMutationRule is the interface that groups query and mutation rules. -type QueryMutationRule interface { - QueryRule - MutationRule -} - -// AlwaysAllowRule returns a rule that returns an allow decision. -func AlwaysAllowRule() QueryMutationRule { - return fixedDecision{Allow} -} - -// AlwaysDenyRule returns a rule that returns a deny decision. -func AlwaysDenyRule() QueryMutationRule { - return fixedDecision{Deny} -} - -type fixedDecision struct { - decision error -} - -func (f fixedDecision) EvalQuery(context.Context, ent.Query) error { - return f.decision -} - -func (f fixedDecision) EvalMutation(context.Context, ent.Mutation) error { - return f.decision -} - -type contextDecision struct { - eval func(context.Context) error -} - -// ContextQueryMutationRule creates a query/mutation rule from a context eval func. -func ContextQueryMutationRule(eval func(context.Context) error) QueryMutationRule { - return contextDecision{eval} -} - -func (c contextDecision) EvalQuery(ctx context.Context, _ ent.Query) error { - return c.eval(ctx) -} - -func (c contextDecision) EvalMutation(ctx context.Context, _ ent.Mutation) error { - return c.eval(ctx) -} - -// OnMutationOperation evaluates the given rule only on a given mutation operation. -func OnMutationOperation(rule MutationRule, op ent.Op) MutationRule { - return MutationRuleFunc(func(ctx context.Context, m ent.Mutation) error { - if m.Op().Is(op) { - return rule.EvalMutation(ctx, m) - } - return Skip - }) -} - -// DenyMutationOperationRule returns a rule denying specified mutation operation. -func DenyMutationOperationRule(op ent.Op) MutationRule { - rule := MutationRuleFunc(func(_ context.Context, m ent.Mutation) error { - return Denyf("ent/privacy: operation %s is not allowed", m.Op()) - }) - return OnMutationOperation(rule, op) -} - -// The CarQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type CarQueryRuleFunc func(context.Context, *ent.CarQuery) error - -// EvalQuery return f(ctx, q). -func (f CarQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.CarQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.CarQuery", q) -} - -// The CarMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type CarMutationRuleFunc func(context.Context, *ent.CarMutation) error - -// EvalMutation calls f(ctx, m). -func (f CarMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.CarMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.CarMutation", m) -} - -// The GroupQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type GroupQueryRuleFunc func(context.Context, *ent.GroupQuery) error - -// EvalQuery return f(ctx, q). -func (f GroupQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.GroupQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.GroupQuery", q) -} - -// The GroupMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type GroupMutationRuleFunc func(context.Context, *ent.GroupMutation) error - -// EvalMutation calls f(ctx, m). -func (f GroupMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.GroupMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.GroupMutation", m) -} - -// The UserQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type UserQueryRuleFunc func(context.Context, *ent.UserQuery) error - -// EvalQuery return f(ctx, q). -func (f UserQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.UserQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.UserQuery", q) -} - -// The UserMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type UserMutationRuleFunc func(context.Context, *ent.UserMutation) error - -// EvalMutation calls f(ctx, m). -func (f UserMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.UserMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.UserMutation", m) -} diff --git a/examples/traversal/ent/privacy/privacy.go b/examples/traversal/ent/privacy/privacy.go deleted file mode 100644 index d6c95572a..000000000 --- a/examples/traversal/ent/privacy/privacy.go +++ /dev/null @@ -1,287 +0,0 @@ -// 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. - -// Code generated by entc, DO NOT EDIT. - -package privacy - -import ( - "context" - "errors" - "fmt" - - "github.com/facebook/ent/examples/traversal/ent" -) - -var ( - // Allow may be returned by rules to indicate that the policy - // evaluation should terminate with an allow decision. - Allow = errors.New("ent/privacy: allow rule") - - // Deny may be returned by rules to indicate that the policy - // evaluation should terminate with an deny decision. - Deny = errors.New("ent/privacy: deny rule") - - // Skip may be returned by rules to indicate that the policy - // evaluation should continue to the next rule. - Skip = errors.New("ent/privacy: skip rule") -) - -// Allowf returns an formatted wrapped Allow decision. -func Allowf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Allow)...) -} - -// Denyf returns an formatted wrapped Deny decision. -func Denyf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Deny)...) -} - -// Skipf returns an formatted wrapped Skip decision. -func Skipf(format string, a ...interface{}) error { - return fmt.Errorf(format+": %w", append(a, Skip)...) -} - -type decisionCtxKey struct{} - -// DecisionContext creates a decision context. -func DecisionContext(parent context.Context, decision error) context.Context { - if decision == nil || errors.Is(decision, Skip) { - return parent - } - return context.WithValue(parent, decisionCtxKey{}, decision) -} - -func decisionFromContext(ctx context.Context) (error, bool) { - decision, ok := ctx.Value(decisionCtxKey{}).(error) - if ok && errors.Is(decision, Allow) { - decision = nil - } - return decision, ok -} - -type ( - // QueryPolicy combines multiple query rules into a single policy. - QueryPolicy []QueryRule - - // QueryRule defines the interface deciding whether a - // query is allowed and optionally modify it. - QueryRule interface { - EvalQuery(context.Context, ent.Query) error - } -) - -// EvalQuery evaluates a query against a query policy. -func (policy QueryPolicy) EvalQuery(ctx context.Context, q ent.Query) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalQuery(ctx, q); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// QueryRuleFunc type is an adapter to allow the use of -// ordinary functions as query rules. -type QueryRuleFunc func(context.Context, ent.Query) error - -// Eval returns f(ctx, q). -func (f QueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - return f(ctx, q) -} - -type ( - // MutationPolicy combines multiple mutation rules into a single policy. - MutationPolicy []MutationRule - - // MutationRule defines the interface deciding whether a - // mutation is allowed and optionally modify it. - MutationRule interface { - EvalMutation(context.Context, ent.Mutation) error - } -) - -// EvalMutation evaluates a mutation against a mutation policy. -func (policy MutationPolicy) EvalMutation(ctx context.Context, m ent.Mutation) error { - if decision, ok := decisionFromContext(ctx); ok { - return decision - } - for _, rule := range policy { - switch decision := rule.EvalMutation(ctx, m); { - case decision == nil || errors.Is(decision, Skip): - case errors.Is(decision, Allow): - return nil - default: - return decision - } - } - return nil -} - -// MutationRuleFunc type is an adapter to allow the use of -// ordinary functions as mutation rules. -type MutationRuleFunc func(context.Context, ent.Mutation) error - -// EvalMutation returns f(ctx, m). -func (f MutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - return f(ctx, m) -} - -// Policy groups query and mutation policies. -type Policy struct { - Query QueryPolicy - Mutation MutationPolicy -} - -// EvalQuery forwards evaluation to query policy. -func (policy Policy) EvalQuery(ctx context.Context, q ent.Query) error { - return policy.Query.EvalQuery(ctx, q) -} - -// EvalMutation forwards evaluation to mutation policy. -func (policy Policy) EvalMutation(ctx context.Context, m ent.Mutation) error { - return policy.Mutation.EvalMutation(ctx, m) -} - -// QueryMutationRule is the interface that groups query and mutation rules. -type QueryMutationRule interface { - QueryRule - MutationRule -} - -// AlwaysAllowRule returns a rule that returns an allow decision. -func AlwaysAllowRule() QueryMutationRule { - return fixedDecision{Allow} -} - -// AlwaysDenyRule returns a rule that returns a deny decision. -func AlwaysDenyRule() QueryMutationRule { - return fixedDecision{Deny} -} - -type fixedDecision struct { - decision error -} - -func (f fixedDecision) EvalQuery(context.Context, ent.Query) error { - return f.decision -} - -func (f fixedDecision) EvalMutation(context.Context, ent.Mutation) error { - return f.decision -} - -type contextDecision struct { - eval func(context.Context) error -} - -// ContextQueryMutationRule creates a query/mutation rule from a context eval func. -func ContextQueryMutationRule(eval func(context.Context) error) QueryMutationRule { - return contextDecision{eval} -} - -func (c contextDecision) EvalQuery(ctx context.Context, _ ent.Query) error { - return c.eval(ctx) -} - -func (c contextDecision) EvalMutation(ctx context.Context, _ ent.Mutation) error { - return c.eval(ctx) -} - -// OnMutationOperation evaluates the given rule only on a given mutation operation. -func OnMutationOperation(rule MutationRule, op ent.Op) MutationRule { - return MutationRuleFunc(func(ctx context.Context, m ent.Mutation) error { - if m.Op().Is(op) { - return rule.EvalMutation(ctx, m) - } - return Skip - }) -} - -// DenyMutationOperationRule returns a rule denying specified mutation operation. -func DenyMutationOperationRule(op ent.Op) MutationRule { - rule := MutationRuleFunc(func(_ context.Context, m ent.Mutation) error { - return Denyf("ent/privacy: operation %s is not allowed", m.Op()) - }) - return OnMutationOperation(rule, op) -} - -// The GroupQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type GroupQueryRuleFunc func(context.Context, *ent.GroupQuery) error - -// EvalQuery return f(ctx, q). -func (f GroupQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.GroupQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.GroupQuery", q) -} - -// The GroupMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type GroupMutationRuleFunc func(context.Context, *ent.GroupMutation) error - -// EvalMutation calls f(ctx, m). -func (f GroupMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.GroupMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.GroupMutation", m) -} - -// The PetQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type PetQueryRuleFunc func(context.Context, *ent.PetQuery) error - -// EvalQuery return f(ctx, q). -func (f PetQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.PetQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.PetQuery", q) -} - -// The PetMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type PetMutationRuleFunc func(context.Context, *ent.PetMutation) error - -// EvalMutation calls f(ctx, m). -func (f PetMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.PetMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.PetMutation", m) -} - -// The UserQueryRuleFunc type is an adapter to allow the use of ordinary -// functions as a query rule. -type UserQueryRuleFunc func(context.Context, *ent.UserQuery) error - -// EvalQuery return f(ctx, q). -func (f UserQueryRuleFunc) EvalQuery(ctx context.Context, q ent.Query) error { - if q, ok := q.(*ent.UserQuery); ok { - return f(ctx, q) - } - return Denyf("ent/privacy: unexpected query type %T, expect *ent.UserQuery", q) -} - -// The UserMutationRuleFunc type is an adapter to allow the use of ordinary -// functions as a mutation rule. -type UserMutationRuleFunc func(context.Context, *ent.UserMutation) error - -// EvalMutation calls f(ctx, m). -func (f UserMutationRuleFunc) EvalMutation(ctx context.Context, m ent.Mutation) error { - if m, ok := m.(*ent.UserMutation); ok { - return f(ctx, m) - } - return Denyf("ent/privacy: unexpected mutation type %T, expect *ent.UserMutation", m) -}