examples/entcpkg: add graph function to example (#1326)

This commit is contained in:
Ariel Mashraki
2021-03-12 16:41:12 +02:00
committed by GitHub
parent 5645e39a23
commit c33512bb94
6 changed files with 63 additions and 5 deletions

View File

@@ -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} cfg := config{driver: dialect.Debug(c.driver, c.log), log: c.log, debug: true, hooks: c.hooks}
return &UserClient{config: cfg} 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}
}

View File

@@ -7,6 +7,7 @@
package main package main
import ( import (
"fmt"
"log" "log"
"strings" "strings"
"text/template" "text/template"
@@ -18,9 +19,9 @@ import (
func main() { func main() {
// A usage for custom templates with external functions. // A usage for custom templates with external functions.
// One template is defined in the option below, and the // 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{ opts := []entc.Option{
entc.TemplateFiles("template/debug.tmpl"), entc.TemplateFiles("template/stringer.tmpl"),
} }
err := entc.Generate("./schema", &gen.Config{ err := entc.Generate("./schema", &gen.Config{
Header: ` Header: `
@@ -34,9 +35,22 @@ func main() {
gen.MustParse(gen.NewTemplate("static"). gen.MustParse(gen.NewTemplate("static").
Funcs(template.FuncMap{"title": strings.ToTitle}). Funcs(template.FuncMap{"title": strings.ToTitle}).
ParseFiles("template/static.tmpl")), ParseFiles("template/static.tmpl")),
gen.MustParse(gen.NewTemplate("debug").
Funcs(template.FuncMap{"byName": byName}).
ParseFiles("template/debug.tmpl")),
}, },
}, opts...) }, opts...)
if err != nil { if err != nil {
log.Fatalf("running ent codegen: %v", err) 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)
}

View File

@@ -1,13 +1,13 @@
{{ define "debug" }} {{ define "debug" }}
{{/* A template that will be generated to a file named "debug.go" and {{/* A template that will be generated to a file named "debug.go" and
add the functionality for running each client <T> in debug mode */}} add the functionality for running each client <T> in debug mode. */}}
{{/* Add the base header for the generated file */}} {{/* Add the base header for the generated file. */}}
{{ $pkg := base $.Config.Package }} {{ $pkg := base $.Config.Package }}
{{ template "header" $ }} {{ 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 }} {{ range $n := $.Nodes }}
{{ $client := print $n.Name "Client" }} {{ $client := print $n.Name "Client" }}
func (c *{{ $client }}) Debug() *{{ $client }} { func (c *{{ $client }}) Debug() *{{ $client }} {
@@ -19,4 +19,16 @@
} }
{{ end }} {{ 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 }} {{ end }}

View File

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

View File

@@ -7,6 +7,7 @@
package ent package ent
import ( import (
"encoding/json"
"fmt" "fmt"
"strings" "strings"
@@ -84,6 +85,15 @@ func (u *User) String() string {
return builder.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. // Users is a parsable slice of User.
type Users []*User type Users []*User

View File

@@ -27,5 +27,6 @@ func Example_EntcPkg() {
} }
usr := client.User.Create().SaveX(ctx) usr := client.User.Create().SaveX(ctx)
fmt.Println("boring user:", usr) fmt.Println("boring user:", usr)
// Output: boring user: User(id=1) // Output: boring user: User(id=1)
} }