cmd/entc: remove graph printing from generator

Summary: Pull Request resolved: https://github.com/facebookincubator/ent/pull/163

Reviewed By: alexsn

Differential Revision: D18503829

fbshipit-source-id: 1269d4c8c30f848c5096258f2328230f5f378f69
This commit is contained in:
Ariel Mashraki
2019-11-14 08:19:54 -08:00
committed by Facebook Github Bot
parent 54b1a2ead9
commit e161ecc29c
6 changed files with 250 additions and 149 deletions

View File

@@ -72,7 +72,8 @@ func main() {
Run: func(cmd *cobra.Command, path []string) {
graph, err := entc.LoadGraph(path[0], &gen.Config{})
failOnErr(err)
graph.Describe(os.Stdout)
p := printer{os.Stdout}
p.Print(graph)
},
},
func() *cobra.Command {

74
cmd/entc/print.go Normal file
View File

@@ -0,0 +1,74 @@
package main
import (
"fmt"
"io"
"reflect"
"strconv"
"strings"
"github.com/facebookincubator/ent/entc/gen"
"github.com/olekukonko/tablewriter"
)
// printer is a table printer for ent graphs.
type printer struct {
io.Writer
}
// Print prints a table description of the graph to the given writer.
func (p *printer) Print(g *gen.Graph) {
for _, n := range g.Nodes {
p.node(n)
}
}
// node returns description of a type. The format of the description is:
//
// Type:
// <Fields Table>
//
// <Edges Table>
//
func (p *printer) node(t *gen.Type) {
var (
b strings.Builder
table = tablewriter.NewWriter(&b)
header = []string{"Field", "Type", "Unique", "Optional", "Nillable", "Default", "UpdateDefault", "Immutable", "StructTag", "Validators"}
)
b.WriteString(t.Name + ":\n")
table.SetAutoFormatHeaders(false)
table.SetHeader(header)
for _, f := range append([]*gen.Field{t.ID}, t.Fields...) {
v := reflect.ValueOf(*f)
row := make([]string, len(header))
for i := range row {
field := v.FieldByNameFunc(func(name string) bool {
// The first field is mapped from "Name" to "Field".
return name == "Name" && i == 0 || name == header[i]
})
row[i] = fmt.Sprint(field.Interface())
}
table.Append(row)
}
table.Render()
table = tablewriter.NewWriter(&b)
table.SetAutoFormatHeaders(false)
table.SetHeader([]string{"Edge", "Type", "Inverse", "BackRef", "Relation", "Unique", "Optional"})
for _, e := range t.Edges {
table.Append([]string{
e.Name,
e.Type.Name,
strconv.FormatBool(e.IsInverse()),
e.Inverse,
e.Rel.Type.String(),
strconv.FormatBool(e.Unique),
strconv.FormatBool(e.Optional),
})
}
if table.NumLines() > 0 {
table.Render()
}
io.WriteString(p, strings.ReplaceAll(b.String(), "\n", "\n\t")+"\n")
}

174
cmd/entc/print_test.go Normal file
View File

@@ -0,0 +1,174 @@
package main
import (
"strings"
"testing"
"github.com/facebookincubator/ent/entc/gen"
"github.com/facebookincubator/ent/schema/field"
"github.com/stretchr/testify/assert"
)
func TestPrinter_Print(t *testing.T) {
tests := []struct {
input *gen.Graph
out string
}{
{
input: &gen.Graph{
Nodes: []*gen.Type{
{
Name: "User",
ID: &gen.Field{Name: "id", Type: &field.TypeInfo{Type: field.TypeInt}},
Fields: []*gen.Field{
{Name: "name", Type: &field.TypeInfo{Type: field.TypeString}, Validators: 1},
{Name: "age", Type: &field.TypeInfo{Type: field.TypeInt}, Nillable: true},
{Name: "created_at", Type: &field.TypeInfo{Type: field.TypeTime}, Nillable: true, Immutable: true},
},
},
},
},
out: `
User:
+------------+-----------+--------+----------+----------+---------+---------------+-----------+-----------+------------+
| Field | Type | Unique | Optional | Nillable | Default | UpdateDefault | Immutable | StructTag | Validators |
+------------+-----------+--------+----------+----------+---------+---------------+-----------+-----------+------------+
| id | int | false | false | false | false | false | false | | 0 |
| name | string | false | false | false | false | false | false | | 1 |
| age | int | false | false | true | false | false | false | | 0 |
| created_at | time.Time | false | false | true | false | false | true | | 0 |
+------------+-----------+--------+----------+----------+---------+---------------+-----------+-----------+------------+
`,
},
{
input: &gen.Graph{
Nodes: []*gen.Type{
{
Name: "User",
ID: &gen.Field{Name: "id", Type: &field.TypeInfo{Type: field.TypeInt}},
Edges: []*gen.Edge{
{Name: "groups", Type: &gen.Type{Name: "Group"}, Rel: gen.Relation{Type: gen.M2M}, Optional: true},
{Name: "spouse", Type: &gen.Type{Name: "User"}, Unique: true, Rel: gen.Relation{Type: gen.O2O}},
},
},
},
},
out: `
User:
+-------+------+--------+----------+----------+---------+---------------+-----------+-----------+------------+
| Field | Type | Unique | Optional | Nillable | Default | UpdateDefault | Immutable | StructTag | Validators |
+-------+------+--------+----------+----------+---------+---------------+-----------+-----------+------------+
| id | int | false | false | false | false | false | false | | 0 |
+-------+------+--------+----------+----------+---------+---------------+-----------+-----------+------------+
+--------+-------+---------+---------+----------+--------+----------+
| Edge | Type | Inverse | BackRef | Relation | Unique | Optional |
+--------+-------+---------+---------+----------+--------+----------+
| groups | Group | false | | M2M | false | true |
| spouse | User | false | | O2O | true | false |
+--------+-------+---------+---------+----------+--------+----------+
`,
},
{
input: &gen.Graph{
Nodes: []*gen.Type{
{
Name: "User",
ID: &gen.Field{Name: "id", Type: &field.TypeInfo{Type: field.TypeInt}},
Fields: []*gen.Field{
{Name: "name", Type: &field.TypeInfo{Type: field.TypeString}, Validators: 1},
{Name: "age", Type: &field.TypeInfo{Type: field.TypeInt}, Nillable: true},
},
Edges: []*gen.Edge{
{Name: "groups", Type: &gen.Type{Name: "Group"}, Rel: gen.Relation{Type: gen.M2M}, Optional: true},
{Name: "spouse", Type: &gen.Type{Name: "User"}, Unique: true, Rel: gen.Relation{Type: gen.O2O}},
},
},
},
},
out: `
User:
+-------+--------+--------+----------+----------+---------+---------------+-----------+-----------+------------+
| Field | Type | Unique | Optional | Nillable | Default | UpdateDefault | Immutable | StructTag | Validators |
+-------+--------+--------+----------+----------+---------+---------------+-----------+-----------+------------+
| id | int | false | false | false | false | false | false | | 0 |
| name | string | false | false | false | false | false | false | | 1 |
| age | int | false | false | true | false | false | false | | 0 |
+-------+--------+--------+----------+----------+---------+---------------+-----------+-----------+------------+
+--------+-------+---------+---------+----------+--------+----------+
| Edge | Type | Inverse | BackRef | Relation | Unique | Optional |
+--------+-------+---------+---------+----------+--------+----------+
| groups | Group | false | | M2M | false | true |
| spouse | User | false | | O2O | true | false |
+--------+-------+---------+---------+----------+--------+----------+
`,
},
{
input: &gen.Graph{
Nodes: []*gen.Type{
{
Name: "User",
ID: &gen.Field{Name: "id", Type: &field.TypeInfo{Type: field.TypeInt}},
Fields: []*gen.Field{
{Name: "name", Type: &field.TypeInfo{Type: field.TypeString}, Validators: 1},
{Name: "age", Type: &field.TypeInfo{Type: field.TypeInt}, Nillable: true},
},
Edges: []*gen.Edge{
{Name: "groups", Type: &gen.Type{Name: "Group"}, Rel: gen.Relation{Type: gen.M2M}, Optional: true},
{Name: "spouse", Type: &gen.Type{Name: "User"}, Unique: true, Rel: gen.Relation{Type: gen.O2O}},
},
},
{
Name: "Group",
ID: &gen.Field{Name: "id", Type: &field.TypeInfo{Type: field.TypeInt}},
Fields: []*gen.Field{
{Name: "name", Type: &field.TypeInfo{Type: field.TypeString}},
},
Edges: []*gen.Edge{
{Name: "users", Type: &gen.Type{Name: "User"}, Rel: gen.Relation{Type: gen.M2M}, Optional: true},
},
},
},
},
out: `
User:
+-------+--------+--------+----------+----------+---------+---------------+-----------+-----------+------------+
| Field | Type | Unique | Optional | Nillable | Default | UpdateDefault | Immutable | StructTag | Validators |
+-------+--------+--------+----------+----------+---------+---------------+-----------+-----------+------------+
| id | int | false | false | false | false | false | false | | 0 |
| name | string | false | false | false | false | false | false | | 1 |
| age | int | false | false | true | false | false | false | | 0 |
+-------+--------+--------+----------+----------+---------+---------------+-----------+-----------+------------+
+--------+-------+---------+---------+----------+--------+----------+
| Edge | Type | Inverse | BackRef | Relation | Unique | Optional |
+--------+-------+---------+---------+----------+--------+----------+
| groups | Group | false | | M2M | false | true |
| spouse | User | false | | O2O | true | false |
+--------+-------+---------+---------+----------+--------+----------+
Group:
+-------+--------+--------+----------+----------+---------+---------------+-----------+-----------+------------+
| Field | Type | Unique | Optional | Nillable | Default | UpdateDefault | Immutable | StructTag | Validators |
+-------+--------+--------+----------+----------+---------+---------------+-----------+-----------+------------+
| id | int | false | false | false | false | false | false | | 0 |
| name | string | false | false | false | false | false | false | | 0 |
+-------+--------+--------+----------+----------+---------+---------------+-----------+-----------+------------+
+-------+------+---------+---------+----------+--------+----------+
| Edge | Type | Inverse | BackRef | Relation | Unique | Optional |
+-------+------+---------+---------+----------+--------+----------+
| users | User | false | | M2M | false | true |
+-------+------+---------+---------+----------+--------+----------+
`,
},
}
for _, tt := range tests {
b := &strings.Builder{}
p := printer{b}
p.Print(tt.input)
assert.Equal(t, tt.out, "\n"+b.String())
}
}