entc/gen: add support for passing functions to external templates (#732)

Fixed #671
This commit is contained in:
Ariel Mashraki
2020-09-07 12:02:22 +03:00
committed by GitHub
parent 8e824c14eb
commit 5450481513
7 changed files with 43 additions and 12 deletions

View File

@@ -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)

View 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 }}

View File

@@ -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.