mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
gen/template: allow overriding of client struct and initialization (#2503)
This commit is contained in:
@@ -6,25 +6,7 @@ in the LICENSE file in the root directory of this source tree.
|
|||||||
|
|
||||||
{{/* gotype: entgo.io/ent/entc/gen.Graph */}}
|
{{/* gotype: entgo.io/ent/entc/gen.Graph */}}
|
||||||
|
|
||||||
{{ define "client" }}
|
{{ define "client/init" }}
|
||||||
|
|
||||||
{{ $pkg := base $.Config.Package }}
|
|
||||||
{{ template "header" $ }}
|
|
||||||
|
|
||||||
import (
|
|
||||||
"log"
|
|
||||||
|
|
||||||
"{{ $.Config.Package }}/migrate"
|
|
||||||
{{ range $n := $.Nodes }}
|
|
||||||
{{ $n.PackageAlias }} "{{ $n.Config.Package }}/{{ $n.PackageDir }}"
|
|
||||||
{{- end }}
|
|
||||||
|
|
||||||
"entgo.io/ent/dialect"
|
|
||||||
{{ range $import := $.Storage.Imports -}}
|
|
||||||
"{{ $import }}"
|
|
||||||
{{ end -}}
|
|
||||||
)
|
|
||||||
|
|
||||||
// Client is the client that holds all ent builders.
|
// Client is the client that holds all ent builders.
|
||||||
type Client struct {
|
type Client struct {
|
||||||
config
|
config
|
||||||
@@ -58,9 +40,31 @@ func (c *Client) init() {
|
|||||||
c.Schema = migrate.NewSchema(c.driver)
|
c.Schema = migrate.NewSchema(c.driver)
|
||||||
{{- end }}
|
{{- end }}
|
||||||
{{- range $n := $.Nodes }}
|
{{- range $n := $.Nodes }}
|
||||||
c.{{ $n.Name }} = New{{ $n.Name }}Client(c.config)
|
c.{{ $n.Name }} = New{{ $n.Name }}Client(c.config)
|
||||||
{{- end }}
|
{{- end }}
|
||||||
}
|
}
|
||||||
|
{{ end }}
|
||||||
|
|
||||||
|
{{ define "client" }}
|
||||||
|
|
||||||
|
{{ $pkg := base $.Config.Package }}
|
||||||
|
{{ template "header" $ }}
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"{{ $.Config.Package }}/migrate"
|
||||||
|
{{ range $n := $.Nodes }}
|
||||||
|
{{ $n.PackageAlias }} "{{ $n.Config.Package }}/{{ $n.PackageDir }}"
|
||||||
|
{{- end }}
|
||||||
|
|
||||||
|
"entgo.io/ent/dialect"
|
||||||
|
{{ range $import := $.Storage.Imports -}}
|
||||||
|
"{{ $import }}"
|
||||||
|
{{ end -}}
|
||||||
|
)
|
||||||
|
|
||||||
|
{{ template "client/init" $ }}
|
||||||
|
|
||||||
// Open opens a database/sql.DB specified by the driver name and
|
// Open opens a database/sql.DB specified by the driver name and
|
||||||
// the data source name, and returns a new client attached to it.
|
// the data source name, and returns a new client attached to it.
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ import (
|
|||||||
// Client is the client that holds all ent builders.
|
// Client is the client that holds all ent builders.
|
||||||
type Client struct {
|
type Client struct {
|
||||||
config
|
config
|
||||||
|
extraHiddenField int
|
||||||
// Schema is the client for creating, migrating and dropping schema.
|
// Schema is the client for creating, migrating and dropping schema.
|
||||||
Schema *migrate.Schema
|
Schema *migrate.Schema
|
||||||
// Group is the client for interacting with the Group builders.
|
// Group is the client for interacting with the Group builders.
|
||||||
@@ -47,12 +48,18 @@ func NewClient(opts ...Option) *Client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) init() {
|
func (c *Client) init() {
|
||||||
|
c.extraHiddenField = 20
|
||||||
c.Schema = migrate.NewSchema(c.driver)
|
c.Schema = migrate.NewSchema(c.driver)
|
||||||
c.Group = NewGroupClient(c.config)
|
c.Group = NewGroupClient(c.config)
|
||||||
c.Pet = NewPetClient(c.config)
|
c.Pet = NewPetClient(c.config)
|
||||||
c.User = NewUserClient(c.config)
|
c.User = NewUserClient(c.config)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HiddenData returns the extraHiddenField value.
|
||||||
|
func (c *Client) HiddenData() int {
|
||||||
|
return c.extraHiddenField
|
||||||
|
}
|
||||||
|
|
||||||
// Open opens a database/sql.DB specified by the driver name and
|
// Open opens a database/sql.DB specified by the driver name and
|
||||||
// the data source name, and returns a new client attached to it.
|
// the data source name, and returns a new client attached to it.
|
||||||
// Optional parameters can be added for configuring the client.
|
// Optional parameters can be added for configuring the client.
|
||||||
|
|||||||
@@ -7,6 +7,52 @@ in the LICENSE file in the root directory of this source tree.
|
|||||||
{{/* The line below tells Intellij/GoLand to enable the autocompletion based *gen.Graph type. */}}
|
{{/* The line below tells Intellij/GoLand to enable the autocompletion based *gen.Graph type. */}}
|
||||||
{{/* gotype: entgo.io/ent/entc/gen.Graph */}}
|
{{/* gotype: entgo.io/ent/entc/gen.Graph */}}
|
||||||
|
|
||||||
|
{{ define "client/init" }}
|
||||||
|
// Client is the client that holds all ent builders.
|
||||||
|
type Client struct {
|
||||||
|
config
|
||||||
|
extraHiddenField int
|
||||||
|
{{- if $.SupportMigrate }}
|
||||||
|
// Schema is the client for creating, migrating and dropping schema.
|
||||||
|
Schema *migrate.Schema
|
||||||
|
{{- end }}
|
||||||
|
{{- range $n := $.Nodes }}
|
||||||
|
// {{ $n.Name }} is the client for interacting with the {{ $n.Name }} builders.
|
||||||
|
{{ $n.Name }} *{{ $n.Name }}Client
|
||||||
|
{{- end }}
|
||||||
|
{{- template "client/fields/additional" $ }}
|
||||||
|
{{- with $tmpls := matchTemplate "client/fields/additional/*" }}
|
||||||
|
{{- range $tmpl := $tmpls }}
|
||||||
|
{{- xtemplate $tmpl $ }}
|
||||||
|
{{- end }}
|
||||||
|
{{- end }}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewClient creates a new client configured with the given options.
|
||||||
|
func NewClient(opts ...Option) *Client {
|
||||||
|
cfg := config{log: log.Println, hooks: &hooks{}}
|
||||||
|
cfg.options(opts...)
|
||||||
|
client := &Client{config: cfg}
|
||||||
|
client.init()
|
||||||
|
return client
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) init() {
|
||||||
|
c.extraHiddenField = 20
|
||||||
|
{{- if $.SupportMigrate }}
|
||||||
|
c.Schema = migrate.NewSchema(c.driver)
|
||||||
|
{{- end }}
|
||||||
|
{{- range $n := $.Nodes }}
|
||||||
|
c.{{ $n.Name }} = New{{ $n.Name }}Client(c.config)
|
||||||
|
{{- end }}
|
||||||
|
}
|
||||||
|
|
||||||
|
// HiddenData returns the extraHiddenField value.
|
||||||
|
func (c *Client) HiddenData() int {
|
||||||
|
return c.extraHiddenField
|
||||||
|
}
|
||||||
|
{{ end }}
|
||||||
|
|
||||||
{{ define "client/fields/additional" }}
|
{{ define "client/fields/additional" }}
|
||||||
// additional fields.
|
// additional fields.
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
|
|||||||
@@ -91,4 +91,6 @@ func TestCustomTemplate(t *testing.T) {
|
|||||||
Select().
|
Select().
|
||||||
IntX(ctx)
|
IntX(ctx)
|
||||||
require.Equal(t, sum, got)
|
require.Equal(t, sum, got)
|
||||||
|
|
||||||
|
require.Equal(t, 20, client.HiddenData())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user