mirror of
https://github.com/ent/ent.git
synced 2026-05-28 09:49:08 +03:00
entc/gen: expose config on generated filters (#2473)
This commit is contained in:
@@ -7,6 +7,8 @@
|
||||
package ent
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/dialect"
|
||||
)
|
||||
@@ -23,7 +25,8 @@ type config struct {
|
||||
// log used for logging on debug mode.
|
||||
log func(...interface{})
|
||||
// hooks to execute on mutations.
|
||||
hooks *hooks
|
||||
hooks *hooks
|
||||
HTTPClient *http.Client
|
||||
}
|
||||
|
||||
// hooks per client, for fast access.
|
||||
@@ -63,3 +66,10 @@ func Driver(driver dialect.Driver) Option {
|
||||
c.driver = driver
|
||||
}
|
||||
}
|
||||
|
||||
// HTTPClient configures the HTTPClient.
|
||||
func HTTPClient(v *http.Client) Option {
|
||||
return func(c *config) {
|
||||
c.HTTPClient = v
|
||||
}
|
||||
}
|
||||
|
||||
43
entc/integration/privacy/ent/entc.go
Normal file
43
entc/integration/privacy/ent/entc.go
Normal file
@@ -0,0 +1,43 @@
|
||||
// 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.
|
||||
|
||||
//go:build ignore
|
||||
// +build ignore
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"entgo.io/ent/entc"
|
||||
"entgo.io/ent/entc/gen"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// A usage for custom options to configure the code generator to use
|
||||
// an extension and inject external dependencies in the generated API.
|
||||
opts := []entc.Option{
|
||||
entc.Dependency(
|
||||
entc.DependencyType(&http.Client{}),
|
||||
),
|
||||
entc.FeatureNames(
|
||||
"privacy",
|
||||
"entql",
|
||||
"schema/snapshot",
|
||||
),
|
||||
}
|
||||
err := entc.Generate("./schema", &gen.Config{
|
||||
Header: `
|
||||
// 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.
|
||||
`,
|
||||
}, opts...)
|
||||
if err != nil {
|
||||
log.Fatalf("running ent codegen: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -155,7 +155,7 @@ func (tq *TaskQuery) addPredicate(pred func(s *sql.Selector)) {
|
||||
|
||||
// Filter returns a Filter implementation to apply filters on the TaskQuery builder.
|
||||
func (tq *TaskQuery) Filter() *TaskFilter {
|
||||
return &TaskFilter{tq}
|
||||
return &TaskFilter{tq.config, tq}
|
||||
}
|
||||
|
||||
// addPredicate implements the predicateAdder interface.
|
||||
@@ -165,11 +165,12 @@ func (m *TaskMutation) addPredicate(pred func(s *sql.Selector)) {
|
||||
|
||||
// Filter returns an entql.Where implementation to apply filters on the TaskMutation builder.
|
||||
func (m *TaskMutation) Filter() *TaskFilter {
|
||||
return &TaskFilter{m}
|
||||
return &TaskFilter{m.config, m}
|
||||
}
|
||||
|
||||
// TaskFilter provides a generic filtering capability at runtime for TaskQuery.
|
||||
type TaskFilter struct {
|
||||
config
|
||||
predicateAdder
|
||||
}
|
||||
|
||||
@@ -242,7 +243,7 @@ func (tq *TeamQuery) addPredicate(pred func(s *sql.Selector)) {
|
||||
|
||||
// Filter returns a Filter implementation to apply filters on the TeamQuery builder.
|
||||
func (tq *TeamQuery) Filter() *TeamFilter {
|
||||
return &TeamFilter{tq}
|
||||
return &TeamFilter{tq.config, tq}
|
||||
}
|
||||
|
||||
// addPredicate implements the predicateAdder interface.
|
||||
@@ -252,11 +253,12 @@ func (m *TeamMutation) addPredicate(pred func(s *sql.Selector)) {
|
||||
|
||||
// Filter returns an entql.Where implementation to apply filters on the TeamMutation builder.
|
||||
func (m *TeamMutation) Filter() *TeamFilter {
|
||||
return &TeamFilter{m}
|
||||
return &TeamFilter{m.config, m}
|
||||
}
|
||||
|
||||
// TeamFilter provides a generic filtering capability at runtime for TeamQuery.
|
||||
type TeamFilter struct {
|
||||
config
|
||||
predicateAdder
|
||||
}
|
||||
|
||||
@@ -314,7 +316,7 @@ func (uq *UserQuery) addPredicate(pred func(s *sql.Selector)) {
|
||||
|
||||
// Filter returns a Filter implementation to apply filters on the UserQuery builder.
|
||||
func (uq *UserQuery) Filter() *UserFilter {
|
||||
return &UserFilter{uq}
|
||||
return &UserFilter{uq.config, uq}
|
||||
}
|
||||
|
||||
// addPredicate implements the predicateAdder interface.
|
||||
@@ -324,11 +326,12 @@ func (m *UserMutation) addPredicate(pred func(s *sql.Selector)) {
|
||||
|
||||
// Filter returns an entql.Where implementation to apply filters on the UserMutation builder.
|
||||
func (m *UserMutation) Filter() *UserFilter {
|
||||
return &UserFilter{m}
|
||||
return &UserFilter{m.config, m}
|
||||
}
|
||||
|
||||
// UserFilter provides a generic filtering capability at runtime for UserQuery.
|
||||
type UserFilter struct {
|
||||
config
|
||||
predicateAdder
|
||||
}
|
||||
|
||||
|
||||
@@ -4,4 +4,4 @@
|
||||
|
||||
package ent
|
||||
|
||||
//go:generate go run -mod=mod entgo.io/ent/cmd/ent generate --feature privacy,entql,schema/snapshot --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 -mod=mod entc.go
|
||||
|
||||
@@ -107,6 +107,19 @@ func FilterTeamRule() privacy.QueryRule {
|
||||
})
|
||||
}
|
||||
|
||||
// FilterUsesDep is a filter query rule that uses its injected dependency using type-assertion.
|
||||
func FilterUsesDep() privacy.QueryRule {
|
||||
return privacy.FilterFunc(func(ctx context.Context, f privacy.Filter) error {
|
||||
u, ok := f.(*ent.UserFilter)
|
||||
if !ok {
|
||||
return privacy.Denyf("unexpected filter type %T", f)
|
||||
}
|
||||
// Access the dependency after the type is resolved.
|
||||
_ = u.HTTPClient
|
||||
return privacy.Skip
|
||||
})
|
||||
}
|
||||
|
||||
// DenyIfStatusChangedByOther is a mutation rule that returns a deny decision if the
|
||||
// task status was changed by someone that is not the owner of the task, or an admin.
|
||||
func DenyIfStatusChangedByOther() privacy.MutationRule {
|
||||
@@ -184,7 +197,7 @@ func SetMutationLogFunc(f func(string, ...interface{})) func(string, ...interfac
|
||||
return logf
|
||||
}
|
||||
|
||||
// LogPlanetMutationHook returns a hook logging planet mutations.
|
||||
// LogTaskMutationHook returns a hook logging planet mutations.
|
||||
func LogTaskMutationHook() ent.Hook {
|
||||
return func(next ent.Mutator) ent.Mutator {
|
||||
return hook.TaskFunc(func(ctx context.Context, m *ent.TaskMutation) (ent.Value, error) {
|
||||
|
||||
Reference in New Issue
Block a user