mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
entc/gen: introduce the entc.Dependency option
This commit is contained in:
committed by
Ariel Mashraki
parent
5bbd973ffa
commit
82eeeb5a1c
@@ -7,6 +7,9 @@
|
||||
package ent
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/dialect"
|
||||
)
|
||||
@@ -23,7 +26,9 @@ 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
|
||||
Writer io.Writer
|
||||
}
|
||||
|
||||
// hooks per client, for fast access.
|
||||
@@ -61,3 +66,17 @@ 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
|
||||
}
|
||||
}
|
||||
|
||||
// Writer configures the Writer.
|
||||
func Writer(v io.Writer) Option {
|
||||
return func(c *config) {
|
||||
c.Writer = v
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,12 +10,14 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"entgo.io/ent/entc"
|
||||
"entgo.io/ent/entc/gen"
|
||||
"entgo.io/ent/schema"
|
||||
"entgo.io/ent/schema/field"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -23,9 +25,20 @@ func main() {
|
||||
if err != nil {
|
||||
log.Fatalf("creating extension: %v", err)
|
||||
}
|
||||
// A usage for custom options to configure the
|
||||
// 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.Extensions(ex),
|
||||
entc.Dependency(
|
||||
entc.DependencyType(&http.Client{}),
|
||||
),
|
||||
entc.Dependency(
|
||||
entc.DependencyName("Writer"),
|
||||
entc.DependencyTypeInfo(&field.TypeInfo{
|
||||
Ident: "io.Writer",
|
||||
PkgPath: "io",
|
||||
}),
|
||||
),
|
||||
}
|
||||
err = entc.Generate("./schema", &gen.Config{
|
||||
Header: `
|
||||
|
||||
@@ -8,20 +8,36 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"entgo.io/ent/examples/entcpkg/ent"
|
||||
"entgo.io/ent/examples/entcpkg/ent/hook"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
func Example_EntcPkg() {
|
||||
client, err := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
|
||||
client, err := ent.Open(
|
||||
"sqlite3",
|
||||
"file:ent?mode=memory&cache=shared&_fk=1",
|
||||
ent.Writer(os.Stdout),
|
||||
ent.HTTPClient(http.DefaultClient),
|
||||
)
|
||||
if err != nil {
|
||||
log.Fatalf("failed opening connection to sqlite: %v", err)
|
||||
}
|
||||
defer client.Close()
|
||||
// An example for using the injected dependencies in the generated builders.
|
||||
client.User.Use(func(next ent.Mutator) ent.Mutator {
|
||||
return hook.UserFunc(func(ctx context.Context, m *ent.UserMutation) (ent.Value, error) {
|
||||
_ = m.HTTPClient
|
||||
_ = m.Writer
|
||||
return next.Mutate(ctx, m)
|
||||
})
|
||||
})
|
||||
ctx := context.Background()
|
||||
// run the auto migration tool.
|
||||
// Run the auto migration tool.
|
||||
if err := client.Schema.Create(ctx); err != nil {
|
||||
log.Fatalf("failed creating schema resources: %v", err)
|
||||
}
|
||||
|
||||
@@ -10,4 +10,4 @@
|
||||
// Package internal holds a loadable version of the latest schema.
|
||||
package internal
|
||||
|
||||
const Schema = `{"Schema":"entgo.io/ent/examples/privacyadmin/ent/schema","Package":"entgo.io/ent/examples/privacyadmin/ent","Schemas":[{"name":"User","config":{"Table":""},"fields":[{"name":"name","type":{"Type":7,"Ident":"","PkgPath":"","Nillable":false,"RType":null},"default":true,"default_value":"Unknown","default_kind":24,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}}],"policy":[{"Index":0,"MixedIn":false,"MixinIndex":0}]}],"Features":["schema/snapshot","privacy"]}`
|
||||
const Schema = `{"Schema":"entgo.io/ent/examples/privacyadmin/ent/schema","Package":"entgo.io/ent/examples/privacyadmin/ent","Schemas":[{"name":"User","config":{"Table":""},"fields":[{"name":"name","type":{"Type":7,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"default":true,"default_value":"Unknown","default_kind":24,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}}],"policy":[{"Index":0,"MixedIn":false,"MixinIndex":0}]}],"Features":["schema/snapshot","privacy"]}`
|
||||
|
||||
@@ -10,4 +10,4 @@
|
||||
// Package internal holds a loadable version of the latest schema.
|
||||
package internal
|
||||
|
||||
const Schema = `{"Schema":"entgo.io/ent/examples/privacytenant/ent/schema","Package":"entgo.io/ent/examples/privacytenant/ent","Schemas":[{"name":"Group","config":{"Table":""},"edges":[{"name":"tenant","type":"Tenant","unique":true,"required":true},{"name":"users","type":"User","ref_name":"groups","inverse":true}],"fields":[{"name":"name","type":{"Type":7,"Ident":"","PkgPath":"","Nillable":false,"RType":null},"default":true,"default_value":"Unknown","default_kind":24,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}}],"policy":[{"Index":0,"MixedIn":true,"MixinIndex":0},{"Index":0,"MixedIn":true,"MixinIndex":1},{"Index":0,"MixedIn":false,"MixinIndex":0}]},{"name":"Tenant","config":{"Table":""},"fields":[{"name":"name","type":{"Type":7,"Ident":"","PkgPath":"","Nillable":false,"RType":null},"validators":1,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}}],"policy":[{"Index":0,"MixedIn":true,"MixinIndex":0},{"Index":0,"MixedIn":false,"MixinIndex":0}]},{"name":"User","config":{"Table":""},"edges":[{"name":"tenant","type":"Tenant","unique":true,"required":true},{"name":"groups","type":"Group"}],"fields":[{"name":"name","type":{"Type":7,"Ident":"","PkgPath":"","Nillable":false,"RType":null},"default":true,"default_value":"Unknown","default_kind":24,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}},{"name":"foods","type":{"Type":3,"Ident":"[]string","PkgPath":"","Nillable":true,"RType":{"Name":"","Ident":"[]string","Kind":23,"PkgPath":"","Methods":{}}},"optional":true,"position":{"Index":1,"MixedIn":false,"MixinIndex":0}}],"policy":[{"Index":0,"MixedIn":true,"MixinIndex":0},{"Index":0,"MixedIn":true,"MixinIndex":1}]}],"Features":["privacy","entql","schema/snapshot"]}`
|
||||
const Schema = `{"Schema":"entgo.io/ent/examples/privacytenant/ent/schema","Package":"entgo.io/ent/examples/privacytenant/ent","Schemas":[{"name":"Group","config":{"Table":""},"edges":[{"name":"tenant","type":"Tenant","unique":true,"required":true},{"name":"users","type":"User","ref_name":"groups","inverse":true}],"fields":[{"name":"name","type":{"Type":7,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"default":true,"default_value":"Unknown","default_kind":24,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}}],"policy":[{"Index":0,"MixedIn":true,"MixinIndex":0},{"Index":0,"MixedIn":true,"MixinIndex":1},{"Index":0,"MixedIn":false,"MixinIndex":0}]},{"name":"Tenant","config":{"Table":""},"fields":[{"name":"name","type":{"Type":7,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"validators":1,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}}],"policy":[{"Index":0,"MixedIn":true,"MixinIndex":0},{"Index":0,"MixedIn":false,"MixinIndex":0}]},{"name":"User","config":{"Table":""},"edges":[{"name":"tenant","type":"Tenant","unique":true,"required":true},{"name":"groups","type":"Group"}],"fields":[{"name":"name","type":{"Type":7,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"default":true,"default_value":"Unknown","default_kind":24,"position":{"Index":0,"MixedIn":false,"MixinIndex":0}},{"name":"foods","type":{"Type":3,"Ident":"[]string","PkgPath":"","PkgName":"","Nillable":true,"RType":{"Name":"","Ident":"[]string","Kind":23,"PkgPath":"","Methods":{}}},"optional":true,"position":{"Index":1,"MixedIn":false,"MixinIndex":0}}],"policy":[{"Index":0,"MixedIn":true,"MixinIndex":0},{"Index":0,"MixedIn":true,"MixinIndex":1}]}],"Features":["privacy","entql","schema/snapshot"]}`
|
||||
|
||||
@@ -10,4 +10,4 @@
|
||||
// Package internal holds a loadable version of the latest schema.
|
||||
package internal
|
||||
|
||||
const Schema = `{"Schema":"entgo.io/ent/examples/version/ent/schema","Package":"entgo.io/ent/examples/version/ent","Schemas":[{"name":"User","config":{"Table":""},"fields":[{"name":"version","type":{"Type":13,"Ident":"","PkgPath":"","Nillable":false,"RType":null},"default":true,"default_kind":19,"position":{"Index":0,"MixedIn":true,"MixinIndex":0},"comment":"Unix time of when the latest update occurred"},{"name":"status","type":{"Type":6,"Ident":"user.Status","PkgPath":"","Nillable":false,"RType":null},"enums":[{"N":"online","V":"online"},{"N":"offline","V":"offline"}],"position":{"Index":0,"MixedIn":false,"MixinIndex":0}}]}],"Features":["schema/snapshot"]}`
|
||||
const Schema = `{"Schema":"entgo.io/ent/examples/version/ent/schema","Package":"entgo.io/ent/examples/version/ent","Schemas":[{"name":"User","config":{"Table":""},"fields":[{"name":"version","type":{"Type":13,"Ident":"","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"default":true,"default_kind":19,"position":{"Index":0,"MixedIn":true,"MixinIndex":0},"comment":"Unix time of when the latest update occurred"},{"name":"status","type":{"Type":6,"Ident":"user.Status","PkgPath":"","PkgName":"","Nillable":false,"RType":null},"enums":[{"N":"online","V":"online"},{"N":"offline","V":"offline"}],"position":{"Index":0,"MixedIn":false,"MixinIndex":0}}]}],"Features":["schema/snapshot"]}`
|
||||
|
||||
Reference in New Issue
Block a user