mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
examples/entcpkg: add graph function to example (#1326)
This commit is contained in:
@@ -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}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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 <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 }}
|
||||
{{ 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 }}
|
||||
|
||||
13
examples/entcpkg/ent/template/stringer.tmpl
Normal file
13
examples/entcpkg/ent/template/stringer.tmpl
Normal 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 }}
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -27,5 +27,6 @@ func Example_EntcPkg() {
|
||||
}
|
||||
usr := client.User.Create().SaveX(ctx)
|
||||
fmt.Println("boring user:", usr)
|
||||
|
||||
// Output: boring user: User(id=1)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user