8000 feat(entc): add TemplateOption to `entc.TemplateDir`, `entc.TemplateGlob` by eiixy · Pull Request #4118 · ent/ent · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat(entc): add TemplateOption to entc.TemplateDir, entc.TemplateGlob #4118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
< 8000 svg style="box-sizing: content-box; color: var(--color-icon-primary);" width="32" height="32" viewBox="0 0 16 16" fill="none" aria-hidden="true" data-view-component="true" class="flex-1 anim-rotate"> Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions entc/entc.go
type TemplateOption func(*gen.Template) *gen.Template
Original file line number Diff line number Diff line change
8000 Expand Up @@ -14,6 +14,7 @@ import (
"path/filepath"
"reflect"
"strings"
"text/template"

"entgo.io/ent/entc/gen"
"entgo.io/ent/entc/internal"
Expand Down Expand Up @@ -51,7 +52,6 @@ func LoadGraph(schemaPath string, cfg *gen.Config) (*gen.Graph, error) {
// Header: "// Custom header",
// IDType: &field.TypeInfo{Type: field.TypeInt},
// })
//
func Generate(schemaPath string, cfg *gen.Config, options ...Option) error {
if cfg.Target == "" {
abs, err := filepath.Abs(schemaPath)
Expand Down Expand Up @@ -178,20 +178,42 @@ func TemplateFiles(filenames ...string) Option {

// TemplateGlob parses the template definitions from the files identified
// by the pattern and associates the resulting templates with codegen templates.
func TemplateGlob(pattern string) Option {
func TemplateGlob(pattern string, opts ...TemplateOption) Option {
return templateOption(func(t *gen.Template) (*gen.Template, error) {
for _, opt := range opts {
opt(t)
}
return t.ParseGlob(pattern)
})
}

// TemplateDir parses the template definitions from the files in the directory
// and associates the resulting templates with codegen templates.
func TemplateDir(path string) Option {
func TemplateDir(path string, opts ...TemplateOption) Option {
return templateOption(func(t *gen.Template) (*gen.Template, error) {
for _, opt := range opts {
opt(t)
}
return t.ParseDir(path)
})
}


// TemplateFuncs merges the given funcMap with the template functions.
func TemplateFuncs(funcMap template.FuncMap) TemplateOption {
return func(t *gen.Template) *gen.Template {
return t.Funcs(funcMap)
}
}

// TemplateSkipIf allows registering a function to determine if the template needs to be skipped or not.
func TemplateSkipIf(cond func(*gen.Graph) bool) TemplateOption {
return func(t *gen.Template) *gen.Template {
return t.SkipIf(cond)
}
}

// Extension describes an Ent code generation extension that
// allows customizing the code generation and integrate with
// other tools and libraries (e.g. GraphQL, gRPC, OpenAPI) by
Expand All @@ -211,7 +233,6 @@ func TemplateDir(path string) Option {
// if err != nil {
// log.Fatalf("running ent codegen: %v", err)
// }
//
type Extension interface {
// Hooks holds an optional list of Hooks to apply
// on the graph before/after the code-generation.
Expand Down Expand Up @@ -263,7 +284,6 @@ func Extensions(extensions ...Extension) Option {
// type Extension struct {
// entc.DefaultExtension
// }
//
type DefaultExtension struct{}

// Hooks of the extensions.
Expand Down Expand Up @@ -343,7 +363,6 @@ func DependencyName(name string) DependencyOption {
// if err := entc.Generate("./ent/path", &gen.Config{}, opts...); err != nil {
// log.Fatalf("running ent codegen: %v", err)
// }
//
func Dependency(opts ...DependencyOption) Option {
return func(cfg *gen.Config) error {
d := &gen.Dependency{}
Expand Down
0