-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.go
More file actions
82 lines (63 loc) · 1.84 KB
/
Copy pathmap.go
File metadata and controls
82 lines (63 loc) · 1.84 KB
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
package htmldump
import (
"fmt"
"reflect"
)
// mapToHTML dumps a map to HTML table.
func mapToHTML(doc *htmlDocument, reflectedMap reflect.Value) error {
if !isMapOrPointerToMap(reflectedMap) {
return fmt.Errorf(`[mapToHTML] only accepts map or pointers to map, got %s`, reflectedMap.Kind())
}
caption, err := mapCaption(reflectedMap)
if err != nil {
return err
}
table := new(tableT)
table.Caption(caption).
mapHeader(reflectedMap).
mapBody(reflectedMap).
toHTML(doc)
return nil
}
// Returns the caption of HTML table for a map key and value types.
func mapCaption(reflectedMap reflect.Value) (string, error) {
typeName, err := MapTypeName(reflectedMap.Type())
if err != nil {
return ``, err
}
return fmt.Sprintf(`%s (length: %d)`, typeName, reflectedMap.Len()), nil
}
func (table *tableT) mapHeader(reflectedMap reflect.Value) *tableT {
var captions, types rowT
captions.addCell(cellT{value: `map key`, key: true})
types.addCell(cellT{value: reflectedMap.Type().Key().Name(), key: true})
table.headerRow(reflectedMap.Type().Elem(), &captions, &types)
return table
}
// Generate table body for a map.
func (table *tableT) mapBody(reflectedMap reflect.Value) *tableT {
for _, key := range reflectedMap.MapKeys() {
var row rowT
row.addCell(cellT{value: formatValue(key), key: true})
item := reflectedMap.MapIndex(key)
if item.Kind() == reflect.Pointer {
item = item.Elem()
}
switch {
case !item.IsValid():
row.addCell(cellT{value: NULL, colspan: table.columns - 1})
case item.Kind() == reflect.Struct:
structRow(&row, item)
default:
row.addCellStr(formatValue(item))
}
table.addBodyRow(row)
}
return table
}
func isMapOrPointerToMap(reflectedMap reflect.Value) bool {
if reflectedMap.Kind() == reflect.Pointer {
reflectedMap = reflectedMap.Elem()
}
return reflectedMap.Kind() == reflect.Map
}