snapshot of https://github.com/ent/ent.git (master, 2026-09-15; blobs > 131072B omitted)
Some checks failed
Atlas CI Public / lint (push) Failing after 13s
Atlas CI Public / sync (push) Has been skipped

This commit is contained in:
upstream-mirror
2026-09-15 22:12:36 +05:00
commit 4b3ad0b611
2680 changed files with 719472 additions and 0 deletions

View File

@@ -0,0 +1,141 @@
// Copyright 2019-present Facebook Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
package printer
import (
"fmt"
"io"
"reflect"
"strconv"
"strings"
"entgo.io/ent/entc/gen"
"github.com/olekukonko/tablewriter"
"github.com/olekukonko/tablewriter/tw"
)
// A Config controls the output of Fprint.
type Config struct {
io.Writer
}
// Print prints a table description of the graph to the given writer.
func (p Config) Print(g *gen.Graph) {
for _, n := range g.Nodes {
p.node(n)
}
}
// Fprint executes "pretty-printer" on the given writer.
func Fprint(w io.Writer, g *gen.Graph) {
Config{Writer: w}.Print(g)
}
// node returns description of a type. The format of the description is:
//
// Type:
// <Fields Table>
//
// <Edges Table>
func (p Config) node(t *gen.Type) {
var (
b strings.Builder
id []*gen.Field
table = tablewriter.NewWriter(&b)
header = []string{"Field", "Type", "Unique", "Optional", "Nillable", "Default", "UpdateDefault", "Immutable", "StructTag", "Validators", "Comment"}
)
b.WriteString(t.Name + ":\n")
table.Options(
tablewriter.WithHeaderConfig(tw.CellConfig{
Padding: tw.CellPadding{
Global: tw.Padding{
Left: tw.Space,
Right: tw.Space,
},
},
Formatting: tw.CellFormatting{
AutoFormat: tw.Off,
},
}),
tablewriter.WithRendition(tw.Rendition{
Symbols: tw.NewSymbols(tw.StyleASCII),
}),
)
table.Header(header)
var alignment = make([]tw.Align, 0)
if t.ID != nil {
id = append(id, t.ID)
}
for _, f := range append(id, t.Fields...) {
v := reflect.ValueOf(*f)
row := make([]string, len(header))
for i := 0; i < len(row)-1; i++ {
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())
_, err := strconv.Atoi(row[i])
if err == nil {
alignment = append(alignment, tw.AlignRight)
} else {
alignment = append(alignment, tw.AlignLeft)
}
}
row[len(row)-1] = f.Comment()
_ = table.Append(row)
table.Options(
tablewriter.WithRowAlignmentConfig(
tw.CellAlignment{PerColumn: alignment},
),
)
}
err := table.Render()
if err != nil {
return
}
// Create new table for edges
table = tablewriter.NewWriter(&b)
table.Options(
tablewriter.WithHeaderConfig(tw.CellConfig{
Formatting: tw.CellFormatting{AutoFormat: tw.Off},
Padding: tw.CellPadding{
Global: tw.Padding{
Left: tw.Space,
Right: tw.Space,
},
},
}),
tablewriter.WithRendition(tw.Rendition{
Symbols: tw.NewSymbols(tw.StyleASCII),
}),
)
table.Header([]string{"Edge", "Type", "Inverse", "BackRef", "Relation", "Unique", "Optional", "Comment"})
hasEdges := false
for _, e := range t.Edges {
hasEdges = true
err := 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),
e.Comment(),
})
if err != nil {
return
}
}
if hasEdges {
err := table.Render()
if err != nil {
return
}
}
io.WriteString(p, strings.ReplaceAll(b.String(), "\n", "\n\t")+"\n")
}

View File

@@ -0,0 +1,177 @@
// Copyright 2019-present Facebook Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
package printer
import (
"strings"
"testing"
"entgo.io/ent/entc/gen"
"entgo.io/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 | Comment |
+------------+-----------+--------+----------+----------+---------+---------------+-----------+-----------+------------+---------+
| 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 | Comment |
+-------+------+--------+----------+----------+---------+---------------+-----------+-----------+------------+---------+
| id | int | false | false | false | false | false | false | | 0 | |
+-------+------+--------+----------+----------+---------+---------------+-----------+-----------+------------+---------+
+--------+-------+---------+---------+----------+--------+----------+---------+
| Edge | Type | Inverse | BackRef | Relation | Unique | Optional | Comment |
+--------+-------+---------+---------+----------+--------+----------+---------+
| 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 | Comment |
+-------+--------+--------+----------+----------+---------+---------------+-----------+-----------+------------+---------+
| 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 | Comment |
+--------+-------+---------+---------+----------+--------+----------+---------+
| 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 | Comment |
+-------+--------+--------+----------+----------+---------+---------------+-----------+-----------+------------+---------+
| 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 | Comment |
+--------+-------+---------+---------+----------+--------+----------+---------+
| groups | Group | false | | M2M | false | true | |
| spouse | User | false | | O2O | true | false | |
+--------+-------+---------+---------+----------+--------+----------+---------+
Group:
+-------+--------+--------+----------+----------+---------+---------------+-----------+-----------+------------+---------+
| Field | Type | Unique | Optional | Nillable | Default | UpdateDefault | Immutable | StructTag | Validators | Comment |
+-------+--------+--------+----------+----------+---------+---------------+-----------+-----------+------------+---------+
| 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 | Comment |
+-------+------+---------+---------+----------+--------+----------+---------+
| users | User | false | | M2M | false | true | |
+-------+------+---------+---------+----------+--------+----------+---------+
`,
},
}
for _, tt := range tests {
b := &strings.Builder{}
Fprint(b, tt.input)
assert.Equal(t, tt.out, "\n"+b.String())
}
}