Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions cmd/internal/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"entgo.io/ent/entc/gen"

"github.com/olekukonko/tablewriter"
"github.com/olekukonko/tablewriter/tw"
)

// A Config controls the output of Fprint.
Expand Down Expand Up @@ -43,12 +44,10 @@ 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"}
table = tablewriter.NewTable(&b, tablewriter.WithHeaderAutoFormat(tw.Off), tablewriter.WithHeader(header))
)
b.WriteString(t.Name + ":\n")
table.SetAutoFormatHeaders(false)
table.SetHeader(header)
if t.ID != nil {
id = append(id, t.ID)
}
Expand All @@ -63,14 +62,13 @@ func (p Config) node(t *gen.Type) {
row[i] = fmt.Sprint(field.Interface())
}
row[len(row)-1] = f.Comment()
table.Append(row)
_ = table.Append(row)
}
table.Render()
table = tablewriter.NewWriter(&b)
table.SetAutoFormatHeaders(false)
table.SetHeader([]string{"Edge", "Type", "Inverse", "BackRef", "Relation", "Unique", "Optional", "Comment"})
_ = table.Render()
table = tablewriter.NewTable(&b, tablewriter.WithHeaderAutoFormat(tw.Off),
tablewriter.WithHeader([]string{"Edge", "Type", "Inverse", "BackRef", "Relation", "Unique", "Optional", "Comment"}))
for _, e := range t.Edges {
table.Append([]string{
_ = table.Append([]string{
e.Name,
e.Type.Name,
strconv.FormatBool(e.IsInverse()),
Expand All @@ -81,8 +79,9 @@ func (p Config) node(t *gen.Type) {
e.Comment(),
})
}
if table.NumLines() > 0 {
table.Render()

if len(t.Edges) > 0 {
_ = table.Render()
}
io.WriteString(p, strings.ReplaceAll(b.String(), "\n", "\n\t")+"\n")
}
112 changes: 56 additions & 56 deletions cmd/internal/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ func TestPrinter_Print(t *testing.T) {
},
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 | |
+------------+-----------+--------+----------+----------+---------+---------------+-----------+-----------+------------+---------+
┌────────────┬───────────┬────────┬──────────┬──────────┬─────────┬───────────────┬───────────┬───────────┬────────────┬─────────┐
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
└────────────┴───────────┴────────┴──────────┴──────────┴─────────┴───────────────┴───────────┴───────────┴────────────┴─────────┘

`,
},
Expand All @@ -61,17 +61,17 @@ User:
},
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 | |
+--------+-------+---------+---------+----------+--------+----------+---------+
┌───────┬──────┬────────┬──────────┬──────────┬─────────┬───────────────┬───────────┬───────────┬────────────┬─────────┐
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
└────────┴───────┴─────────┴─────────┴──────────┴────────┴──────────┴─────────┘

`,
},
Expand All @@ -94,19 +94,19 @@ User:
},
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 | |
+--------+-------+---------+---------+----------+--------+----------+---------+
┌───────┬────────┬────────┬──────────┬──────────┬─────────┬───────────────┬───────────┬───────────┬────────────┬─────────┐
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
└────────┴───────┴─────────┴─────────┴──────────┴────────┴──────────┴─────────┘

`,
},
Expand Down Expand Up @@ -139,32 +139,32 @@ User:
},
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 | |
+--------+-------+---------+---------+----------+--------+----------+---------+
┌───────┬────────┬────────┬──────────┬──────────┬─────────┬───────────────┬───────────┬───────────┬────────────┬─────────┐
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 | |
+-------+------+---------+---------+----------+--------+----------+---------+
┌───────┬────────┬────────┬──────────┬──────────┬─────────┬───────────────┬───────────┬───────────┬────────────┬─────────┐
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
└───────┴──────┴─────────┴─────────┴──────────┴────────┴──────────┴─────────┘

`,
},
Expand Down
Loading