-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap_table.go
232 lines (207 loc) · 6.02 KB
/
map_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package statuspage
import (
"fmt"
"reflect"
"strconv"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
// column header names for maps
const mapKeyHeader = "key"
const mapValueHeader = "value"
// max len of elements shown from a slice if the map value is slice
const maxSliceLen = 5
// isSet checks if t is a map where the value type is struct{}
func isSet(t reflect.Type) bool {
return t.Kind() == reflect.Map && t.Elem().Kind() == reflect.Struct && t.Elem().NumField() == 0
}
// only works for v where the type is simple (doesn't need a table)
func (s *Status[T]) simpleTableCell(v reflect.Value) (*html.Node, error) {
cell := createElemAtom(atom.Td)
ns, genErr := s.genValSection(v)
if genErr != nil {
return nil, genErr
}
for _, n := range ns {
cell.AppendChild(n)
}
return cell, nil
}
func (s *Status[T]) genMapTable(v reflect.Value) ([]*html.Node, error) {
if v.Kind() != reflect.Map {
panic(fmt.Errorf("non-map kind: %s type %s", v.Kind(), v.Type()))
}
baseTable := createElemAtom(atom.Table)
headerRow := createElemAtom(atom.Tr)
baseTable.AppendChild(headerRow)
keyHeader := createElemAtom(atom.Th)
headerRow.AppendChild(keyHeader)
keyHeader.AppendChild(textNode(mapKeyHeader))
valHeader := createElemAtom(atom.Th)
valHeader.Attr = append(valHeader.Attr, html.Attribute{Key: "colspan", Val: "100%"})
headerRow.AppendChild(valHeader)
valHeader.AppendChild(textNode(mapValueHeader))
valType := v.Type().Elem()
valSet := isSet(valType)
if valSet {
// if valType is a map[someType]struct{}, values are a slice of someType (key type)
valType = reflect.SliceOf(valType.Key())
}
// header row for the map key if applicable
var hRowKey *html.Node
iter := v.MapRange()
for iter.Next() {
ikey := iter.Key()
ival := iter.Value()
if valSet {
// ival is actually a slice of valType where the values are ival's map keys, make it so!
// ex. map[string]struct{}{"dog":struct{}, "cat":struct{}} => ival should be []string{"dog", "cat"}
keys := ival.MapKeys()
ival = reflect.MakeSlice(valType, ival.Len(), ival.Len())
for i, key := range keys {
ival.Index(i).Set(key)
}
}
row := createElemAtom(atom.Tr)
// add cells in this row from each key and value
// indexed by their "level"
headerRows := []*html.Node{}
// cells for keys
// TODO: pull this out into helper
if !needsTable(ikey.Type()) {
cell, cellErr := s.simpleTableCell(ikey)
if cellErr != nil {
return nil, cellErr
}
row.AppendChild(cell)
} else if ikey.Kind() == reflect.Struct {
fields := reflect.VisibleFields(ikey.Type())
hRowKey = createElemAtom(atom.Tr)
keyHeader.Attr = append(keyHeader.Attr, html.Attribute{Key: atom.Colspan.String(), Val: strconv.Itoa(len(fields))})
for _, field := range fields {
if !field.IsExported() {
// skip the unexported fields for now
continue
}
// TODO: pull out into a helper
if v, ok := field.Tag.Lookup("statuspage"); ok && v == "-" {
// The caller asked us to skip this
continue
}
if needsTable(field.Type) {
// TODO: add in recursion with depth for header row levels, but for now, just stick in a table within this table
ns, genErr := s.genValSection(ikey.FieldByIndex(field.Index))
if genErr != nil {
return nil, genErr
}
for _, n := range ns {
cell := createElemAtom(atom.Td)
row.AppendChild(cell)
if genErr != nil {
return nil, genErr
}
cell.AppendChild(n)
}
}
th := createElemAtom(atom.Th)
hRowKey.AppendChild(th)
th.AppendChild(textNode(field.Name))
// add the field values from this struct to the values row
if needsTable(field.Type) {
continue
}
fieldValCell, cellErr := s.simpleTableCell(ikey.FieldByName(field.Name))
if cellErr != nil {
return nil, cellErr
}
row.AppendChild(fieldValCell)
}
}
if hRowKey != nil {
headerRows = append(headerRows, hRowKey)
}
// cells for values
if !needsTable(ival.Type()) {
cell, cellErr := s.simpleTableCell(ival)
if cellErr != nil {
return nil, cellErr
}
row.AppendChild(cell)
} else if ival.Kind() == reflect.Struct {
fields := reflect.VisibleFields(ival.Type())
var hRowVal *html.Node
if len(headerRows) > 0 {
hRowVal = headerRows[0]
} else {
hRowVal = createElemAtom(atom.Tr)
}
for _, field := range fields {
if !field.IsExported() {
// skip the unexported fields for now
continue
}
// TODO: pull out into a helper
if v, ok := field.Tag.Lookup("statuspage"); ok && v == "-" {
// The caller asked us to skip this
continue
}
if needsTable(field.Type) {
ns, genErr := s.genValSection(ival.FieldByIndex(field.Index))
if genErr != nil {
return nil, genErr
}
for _, n := range ns {
cell := createElemAtom(atom.Td)
row.AppendChild(cell)
if genErr != nil {
return nil, genErr
}
cell.AppendChild(n)
}
}
th := createElemAtom(atom.Th)
hRowVal.AppendChild(th)
th.AppendChild(textNode(field.Name))
if needsTable(field.Type) {
continue
}
// add the field values from this struct to the values row
fieldValCell, cellErr := s.simpleTableCell(ival.FieldByName(field.Name))
if cellErr != nil {
return nil, cellErr
}
row.AppendChild(fieldValCell)
}
} else if ival.Kind() == reflect.Slice || ival.Kind() == reflect.Array {
if ival.Kind() != reflect.Array && ival.IsNil() {
nilCell, cErr := s.simpleTableCell(ival)
if cErr != nil {
return nil, cErr
}
row.AppendChild(nilCell)
continue
}
size := ival.Len()
if size > maxSliceLen {
size = maxSliceLen
}
for i := 0; i < size; i++ {
sliceVal := ival.Index(i)
if needsTable(sliceVal.Type()) {
// TODO
continue
}
c, cellErr := s.simpleTableCell(sliceVal)
if cellErr != nil {
return nil, cellErr
}
row.AppendChild(c)
}
}
for _, hRow := range headerRows {
baseTable.AppendChild(hRow)
}
baseTable.AppendChild(row)
}
return []*html.Node{baseTable}, nil
}