diff --git a/examples/entcpkg/ent/debug.go b/examples/entcpkg/ent/debug.go index c6a7ae20c..7e807bbcc 100644 --- a/examples/entcpkg/ent/debug.go +++ b/examples/entcpkg/ent/debug.go @@ -15,3 +15,11 @@ func (c *UserClient) Debug() *UserClient { cfg := config{driver: dialect.Debug(c.driver, c.log), log: c.log, debug: true, hooks: c.hooks} return &UserClient{config: cfg} } + +func (c *UserClient) DebugLog(fn func(...interface{})) *UserClient { + if c.debug { + return c + } + cfg := config{driver: dialect.Debug(c.driver, c.log), log: fn, debug: true, hooks: c.hooks} + return &UserClient{config: cfg} +} diff --git a/examples/entcpkg/ent/entc.go b/examples/entcpkg/ent/entc.go index 6b4d2bdb9..0ef2f1183 100644 --- a/examples/entcpkg/ent/entc.go +++ b/examples/entcpkg/ent/entc.go @@ -7,6 +7,7 @@ package main import ( + "fmt" "log" "strings" "text/template" @@ -18,9 +19,9 @@ import ( func main() { // A usage for custom templates with external functions. // One template is defined in the option below, and the - // second template is provided with the `Templates` option. + // rest are provided with the `Templates` option. opts := []entc.Option{ - entc.TemplateFiles("template/debug.tmpl"), + entc.TemplateFiles("template/stringer.tmpl"), } err := entc.Generate("./schema", &gen.Config{ Header: ` @@ -34,9 +35,22 @@ func main() { gen.MustParse(gen.NewTemplate("static"). Funcs(template.FuncMap{"title": strings.ToTitle}). ParseFiles("template/static.tmpl")), + gen.MustParse(gen.NewTemplate("debug"). + Funcs(template.FuncMap{"byName": byName}). + ParseFiles("template/debug.tmpl")), }, }, opts...) if err != nil { log.Fatalf("running ent codegen: %v", err) } } + +// byName returns a node in the graph by its label/name. +func byName(g *gen.Graph, name string) (*gen.Type, error) { + for _, n := range g.Nodes { + if n.Name == name { + return n, nil + } + } + return nil, fmt.Errorf("node %q was not found in the graph", name) +} diff --git a/examples/entcpkg/ent/template/debug.tmpl b/examples/entcpkg/ent/template/debug.tmpl index 40e3fdab6..e93782c7d 100644 --- a/examples/entcpkg/ent/template/debug.tmpl +++ b/examples/entcpkg/ent/template/debug.tmpl @@ -1,13 +1,13 @@ {{ define "debug" }} {{/* A template that will be generated to a file named "debug.go" and - add the functionality for running each client in debug mode */}} + add the functionality for running each client in debug mode. */}} -{{/* Add the base header for the generated file */}} +{{/* Add the base header for the generated file. */}} {{ $pkg := base $.Config.Package }} {{ template "header" $ }} -{{/* Loop over all nodes and add option the "Debug()" method */}} +{{/* Loop over all nodes and add option the "Debug()" method. */}} {{ range $n := $.Nodes }} {{ $client := print $n.Name "Client" }} func (c *{{ $client }}) Debug() *{{ $client }} { @@ -19,4 +19,16 @@ } {{ end }} +{{/* Add the "DebugLog()" only to the "User" type. */}} +{{ with $n := byName $ "User" }} + {{ $client := print $n.Name "Client" }} + func (c *{{ $client }}) DebugLog(fn func(...interface{})) *{{ $client }} { + if c.debug { + return c + } + cfg := config{driver: dialect.Debug(c.driver, c.log), log: fn, debug: true, hooks: c.hooks} + return &{{ $client }}{config: cfg} + } +{{ end }} + {{ end }} diff --git a/examples/entcpkg/ent/template/stringer.tmpl b/examples/entcpkg/ent/template/stringer.tmpl new file mode 100644 index 000000000..a25a353a1 --- /dev/null +++ b/examples/entcpkg/ent/template/stringer.tmpl @@ -0,0 +1,13 @@ +{{ define "model/additional/stringer" }} + +{{/* A template that adds the "GoString" method to all generated models on the same file they are defined. */}} + +// GoString implements the fmt.GoStringer interface. +func ({{ $.Receiver }} *{{ $.Name }}) GoString() string { + json, err := json.MarshalIndent({{ $.Receiver }}, "", "\t") + if err != nil { + return err.Error() + } + return string(json) +} +{{ end }} diff --git a/examples/entcpkg/ent/user.go b/examples/entcpkg/ent/user.go index b97e9e6e1..d55cfe5bd 100644 --- a/examples/entcpkg/ent/user.go +++ b/examples/entcpkg/ent/user.go @@ -7,6 +7,7 @@ package ent import ( + "encoding/json" "fmt" "strings" @@ -84,6 +85,15 @@ func (u *User) String() string { return builder.String() } +// GoString implements the fmt.GoStringer interface. +func (u *User) GoString() string { + json, err := json.MarshalIndent(u, "", "\t") + if err != nil { + return err.Error() + } + return string(json) +} + // Users is a parsable slice of User. type Users []*User diff --git a/examples/entcpkg/example_test.go b/examples/entcpkg/example_test.go index 44bccf498..44371241e 100644 --- a/examples/entcpkg/example_test.go +++ b/examples/entcpkg/example_test.go @@ -27,5 +27,6 @@ func Example_EntcPkg() { } usr := client.User.Create().SaveX(ctx) fmt.Println("boring user:", usr) + // Output: boring user: User(id=1) }