-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.go
137 lines (114 loc) · 2.8 KB
/
table.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package emailt
import (
"fmt"
"io"
"reflect"
"sort"
"text/template"
"github.com/gochore/emailt/internal/rend"
"github.com/gochore/emailt/style"
)
type Column struct {
Name string
Template string
}
type Columns []Column
type Table struct {
dataset interface{}
columns Columns
funcs template.FuncMap
}
func NewTable() *Table {
return &Table{}
}
func (t *Table) SetDataset(dataset interface{}) {
t.dataset = dataset
}
func (t *Table) SetColumns(columns Columns) {
t.columns = columns
}
func (t *Table) SetFuncs(funcs template.FuncMap) {
t.funcs = funcs
}
func (t *Table) Render(writer io.Writer, themes ...style.Theme) error {
errPrefix := "Table.Render: "
theme := rend.MergeThemes(themes)
dataset := reflect.ValueOf(t.dataset)
if dataset.Kind() != reflect.Slice {
return fmt.Errorf(errPrefix+"%v is not a slice", dataset.Type())
}
if dataset.Len() == 0 {
return fmt.Errorf(errPrefix + "empty data")
}
mapItem := false
typ := dataset.Index(0).Type()
switch typ.Kind() {
case reflect.Map:
mapItem = true
case reflect.Struct:
// do nothing
default:
return fmt.Errorf(errPrefix+"unsupported slice item type: %v", typ)
}
if typ.Kind() != reflect.Struct && typ.Kind() != reflect.Map {
return fmt.Errorf(errPrefix+"%v is not a struct", typ)
}
for i := 0; i < dataset.Len(); i++ {
if t := dataset.Index(i).Type(); t != typ {
return fmt.Errorf(errPrefix+"item %v is %v, not %v", i, t, typ)
}
}
columns := t.columns
if len(columns) == 0 {
if mapItem {
var keys []string
for _, v := range dataset.Index(0).MapKeys() {
keys = append(keys, fmt.Sprint(v.Interface()))
}
sort.Strings(keys)
for _, key := range keys {
columns = append(columns, Column{
Name: key,
Template: fmt.Sprintf("{{.%s}}", key),
})
}
} else {
numField := typ.NumField()
for i := 0; i < numField; i++ {
field := typ.Field(i)
columns = append(columns, Column{
Name: field.Name,
Template: fmt.Sprintf("{{.%s}}", field.Name),
})
}
}
}
render := rend.NewFmtWriter(writer)
render.Printlnf("<table %s>", theme.Attributes("table"))
render.Println("<tr>")
for _, column := range columns {
render.Printlnf("<th %s>%s</th>", theme.Attributes("th"), column.Name)
}
render.Println("</tr>")
for i := 0; i < dataset.Len(); i++ {
render.Println("<tr>")
for _, column := range columns {
render.Printlnf("<td %s>", theme.Attributes("td"))
e := Template{
Data: dataset.Index(i),
Template: column.Template,
Funcs: t.funcs,
}
if err := e.Render(writer, theme); err != nil {
return fmt.Errorf(errPrefix+"render td: %w", err)
}
render.Println("\n</td>")
}
render.Println("</tr>")
}
render.Println("</table>")
if err := render.Err(); err != nil {
return fmt.Errorf(errPrefix+"%w", err)
}
return nil
}