-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstruct.go
More file actions
110 lines (83 loc) · 2.6 KB
/
Copy pathstruct.go
File metadata and controls
110 lines (83 loc) · 2.6 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
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
package htmldump
import (
"fmt"
"reflect"
)
func structToHTML(doc *htmlDocument, input interface{}) error {
structType := reflect.TypeOf(input)
if !isStructOrPointerToStruct(structType) {
return fmt.Errorf(`[structToHTML] only accepts struct or pointer to struct, got %s`, structType.Kind())
}
var caption string
if structType.Kind() == reflect.Pointer {
caption = `*struct ` + structType.Elem().Name()
} else {
caption = `struct ` + structType.Name()
}
table := new(tableT).
Caption(caption).
structHeader().
structBody(input, 0)
table.toHTML(doc)
return nil
}
func (table *tableT) structHeader() *tableT {
row := new(rowT)
row.cells = append(row.cells, cellT{value: `Field`})
row.cells = append(row.cells, cellT{value: `Type`})
row.cells = append(row.cells, cellT{value: `Value`})
table.addHeaderRow(*row)
return table
}
func (table *tableT) structBody(input interface{}, level int) *tableT {
const spacer = 12
pointer := convertToPointer(input)
structType := reflect.TypeOf(pointer).Elem()
structValue := reflect.ValueOf(pointer).Elem()
for idx := 0; idx < structType.NumField(); idx++ {
row := new(rowT)
structField := structType.Field(idx)
fieldName := structField.Name
fieldTypeName := getFieldTypeName(structField.Type)
fieldValue := getUnexportedField(structValue.Field(idx))
nameStyle := styleT{paddingLeft: spacer * level}
style := styleT{}
if isStructOrPointerToStruct(fieldValue.Type()) && !isSkippedType(fieldValue.Type()) {
style.background = getBackground(level)
nameStyle.background = style.background
row.addCellStr(fieldName, nameStyle).
addCellStr(fieldTypeName, style).
addCellStr(structFieldValue(structField, fieldValue), style)
table.addBodyRow(*row)
if fieldValue.IsValid() && !fieldValue.IsZero() {
table.structBody(fieldValue.Interface(), level+1)
}
continue
}
style.background = getBackground(level)
nameStyle.background = style.background
row.addCellStr(fieldName, nameStyle).
addCellStr(fieldTypeName, style).
addCellStr(formatValue(fieldValue), style)
table.addBodyRow(*row)
}
return table
}
func structFieldValue(structField reflect.StructField, fieldValue reflect.Value) string {
switch {
case structField.Type.Kind() == reflect.Pointer && fieldValue.IsNil():
return NULL
case structField.Type.Kind() != reflect.Pointer && fieldValue.IsZero():
return `{}`
default:
return ``
}
}
func getBackground(level int) string {
gradient := []string{``, `#AFFFFF`, `#6DEFFF`, `#47D3FF`, `#00B7EB`}
index := level
if index > len(gradient)-1 {
index = len(gradient) - 1
}
return gradient[index]
}