mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
entc/gen: add support for passing functions to external templates (#732)
Fixed #671
This commit is contained in:
@@ -8,10 +8,11 @@ package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/facebook/ent/entc"
|
||||
"github.com/facebook/ent/entc/gen"
|
||||
"github.com/facebook/ent/schema/field"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -23,7 +24,10 @@ func main() {
|
||||
|
||||
// Code generated by entc, DO NOT EDIT.
|
||||
`,
|
||||
IDType: &field.TypeInfo{Type: field.TypeInt},
|
||||
// A usage for custom templates with external functions.
|
||||
Template: template.Must(template.New("template").
|
||||
Funcs(template.FuncMap{"title": strings.ToTitle}).
|
||||
ParseFiles("template/static.tmpl")),
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal("running ent codegen:", err)
|
||||
|
||||
9
examples/entcpkg/ent/template/static.tmpl
Normal file
9
examples/entcpkg/ent/template/static.tmpl
Normal file
@@ -0,0 +1,9 @@
|
||||
{{/* A template for adding additional static fields to specific types. */}}
|
||||
{{ define "model/fields/additional" }}
|
||||
{{- /* Add static fields to the "Card" entity. */}}
|
||||
{{- if eq $.Name "User" }}
|
||||
{{- /* A usage of an external function. */ -}}
|
||||
// StaticField defined by templates (titled {{ title "staticField" }}).
|
||||
StaticField string `json:"static_field,omitempty"`
|
||||
{{- end }}
|
||||
{{ end }}
|
||||
@@ -19,6 +19,9 @@ type User struct {
|
||||
config
|
||||
// ID of the ent.
|
||||
ID int `json:"id,omitempty"`
|
||||
|
||||
// StaticField defined by templates (titled STATICFIELD).
|
||||
StaticField string `json:"static_field,omitempty"`
|
||||
}
|
||||
|
||||
// scanValues returns the types for scanning values from sql.Rows.
|
||||
|
||||
Reference in New Issue
Block a user