entc/gen: allow users to alter Query initialization using templates (#1453)

* Templating: Allow user to alter Query struct init

* Update entc/gen/template/dialect/sql/query.tmpl

Co-authored-by: Ariel Mashraki <7413593+a8m@users.noreply.github.com>

* pr feedback

* renamed template

* changed sql to *

* to tabs

* removed old template

* update bin-data

* Update entc/integration/template/ent/template/query.tmpl

Co-authored-by: Ariel Mashraki <7413593+a8m@users.noreply.github.com>

* restore newline

* update bindata

Co-authored-by: Ariel Mashraki <7413593+a8m@users.noreply.github.com>
This commit is contained in:
Ciaran Liedeman
2021-04-14 13:04:29 +02:00
committed by GitHub
parent f59e25d228
commit eff6552989
36 changed files with 330 additions and 101 deletions

File diff suppressed because one or more lines are too long

View File

@@ -55,8 +55,9 @@ var (
Format: pkgf("%s_delete.go"),
},
{
Name: "query",
Format: pkgf("%s_query.go"),
Name: "query",
Format: pkgf("%s_query.go"),
ExtendPatterns: []string{"dialect/*/query/fields/additional/*"},
},
{
Name: "model",
@@ -90,6 +91,7 @@ var (
Format: "client.go",
ExtendPatterns: []string{
"client/fields/additional/*",
"dialect/*/query/fields/init/*",
},
},
{

View File

@@ -204,7 +204,14 @@ func (c *{{ $client }}) DeleteOneID(id {{ $n.ID.Type }}) *{{ $n.DeleteOneName }}
// Query returns a query builder for {{ $n.Name }}.
func (c *{{ $client }}) Query() *{{ $n.QueryName }} {
return &{{ $n.QueryName }}{config: c.config}
return &{{ $n.QueryName }}{
config: c.config,
{{- with $tmpls := matchTemplate (printf "dialect/%s/query/fields/init/*" $.Storage) }}
{{- range $tmpl := $tmpls }}
{{- xtemplate $tmpl $n }}
{{- end }}
{{- end }}
}
}
// Get returns a {{ $n.Name }} entity by its id.
@@ -251,6 +258,5 @@ func (c *{{ $client }}) Hooks() []Hook {
{{ end }}
{{ end }}
{{/* A template that can be overrided in order to add additional fields to the client.*/}}
{{/* A template that can be overridden in order to add additional fields to the client.*/}}
{{ define "client/fields/additional" }}{{ end }}

View File

@@ -11,6 +11,11 @@ in the LICENSE file in the root directory of this source tree.
{{- with $.UnexportedForeignKeys }}
withFKs bool
{{- end }}
{{- with $tmpls := matchTemplate "dialect/sql/query/fields/additional/*" }}
{{- range $tmpl := $tmpls }}
{{ xtemplate $tmpl $ }}
{{- end }}
{{- end }}
{{- end }}
{{ define "dialect/sql/query" }}