/
table.go
230 lines (201 loc) · 5.74 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
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
package formatter
import (
"fmt"
"reflect"
"strings"
"text/tabwriter"
)
type Table struct {
writer tabwriter.Writer
}
const (
noPrefix = ""
emptyVal = "-"
prefixNameDivider = "-"
tableDelimiter = "\t"
)
// ListResult only applies for table formatter
type ListResult interface {
BriefItems() reflect.Value
}
var listResponseType = reflect.TypeOf(new(ListResult)).Elem()
func (f *Table) Write(o interface{}) {
oType := reflect.TypeOf(o)
if valid := f.validate(oType); valid {
val := reflect.ValueOf(o)
if !f.isSlice(oType) {
if val.Type().Implements(listResponseType) {
val = o.(ListResult).BriefItems()
}
}
f.writeHeader(val)
f.writeValue(val)
f.writer.Flush()
}
}
func (f *Table) validate(val reflect.Type) bool {
if f.isSlice(val) {
return f.validateStruct(val.Elem())
} else if f.isStruct(val) {
return f.validateStruct(val)
} else {
return true
}
}
func (f *Table) validateStruct(val reflect.Type) bool {
for i := 0; i < val.NumField(); i++ {
structFieldType := val.Field(i).Type
if f.isStruct(structFieldType) {
return false
} else if f.isSlice(structFieldType) && !f.validateSlice(structFieldType.Elem()) {
return false
}
}
return true
}
func (f *Table) validateSlice(val reflect.Type) bool {
if f.isSimpleType(val) {
return true
} else if f.isStruct(val) {
for i := 0; i < val.NumField(); i++ {
structFieldType := val.Field(i).Type
if !f.isSimpleType(structFieldType) {
return false
}
}
}
return true
}
func (f *Table) writeHeader(value reflect.Value) {
f.write(strings.Join(f.getHeader(noPrefix, value.Type()), tableDelimiter))
f.newLine()
}
func (f *Table) getHeader(prefix string, value reflect.Type) []string {
if f.isStruct(value) {
headers := f.getStructHeaders(prefix, value)
return headers
} else if f.isSlice(value) {
headers := f.getHeader(prefix, value.Elem())
return headers
} else {
return []string{prefix + value.Name()}
}
}
func (f *Table) getStructHeaders(prefix string, val reflect.Type) []string {
var headers []string
for i := 0; i < val.NumField(); i++ {
structFieldType := val.Field(i).Type
if f.isSlice(structFieldType) {
headers = append(headers, f.getHeader(prefix+val.Field(i).Name+prefixNameDivider, val.Field(i).Type.Elem())...)
} else {
headers = append(headers, prefix+val.Field(i).Name)
}
}
return headers
}
func (f *Table) writeValue(value reflect.Value) {
val := reflect.Indirect(value)
if f.isSimpleType(value.Type()) {
f.write(fmt.Sprint(value))
} else if f.isStruct(value.Type()) {
maxCollectionSize := f.getMaxSliceSize(value)
if maxCollectionSize == 0 {
f.write(f.getSimpleStructOutput(val))
} else {
f.write(f.getSliceOutput(maxCollectionSize, value))
}
} else {
f.writeCollection(value)
}
}
func (f *Table) writeCollection(value reflect.Value) {
for i := 0; i < value.Len(); i++ {
maxCollectionSize := f.getMaxSliceSize(value.Index(i))
if maxCollectionSize == 0 {
f.write(f.getSimpleStructOutput(value.Index(i)))
} else {
f.write(f.getSliceOutput(maxCollectionSize, value.Index(i)))
}
f.newLine()
}
}
func (f *Table) getMaxSliceSize(value reflect.Value) int {
maxSize := 0
for i := 0; i < value.Type().NumField(); i++ {
field := value.Field(i)
if f.isSlice(field.Type()) && field.Len() > maxSize {
maxSize = field.Len()
}
}
return maxSize
}
func (f *Table) getSliceOutput(maxCollectionSize int, value reflect.Value) string {
val := reflect.Indirect(value)
var outputRowList []string
output := ""
for rowIndex := 0; rowIndex < maxCollectionSize; rowIndex++ {
for structIndex := 0; structIndex < val.Type().NumField(); structIndex++ {
structField := val.Field(structIndex)
if f.isSimpleType(structField.Type()) {
outputRowList = f.appendSimpleValue(structField, outputRowList, rowIndex)
} else if f.isStruct(structField.Type().Elem()) {
outputRowList = f.appendSliceStructRow(structField, outputRowList, rowIndex)
} else if f.isSlice(structField.Type()) {
outputRowList = f.appendSimpleSlice(structField, outputRowList, rowIndex)
} else {
outputRowList = append(outputRowList, fmt.Sprint(structField))
}
}
output += strings.Join(outputRowList, tableDelimiter) + "\n"
outputRowList = outputRowList[:0]
}
return output
}
func (f *Table) appendSliceStructRow(field reflect.Value, outputList []string, row int) []string {
for k := 0; k < field.Type().Elem().NumField(); k++ {
if row >= field.Len() {
outputList = append(outputList, emptyVal)
} else {
outputList = append(outputList, fmt.Sprint(field.Index(row).Field(k)))
}
}
return outputList
}
func (f *Table) appendSimpleSlice(field reflect.Value, outputList []string, row int) []string {
if row >= field.Len() {
outputList = append(outputList, emptyVal)
} else {
outputList = append(outputList, fmt.Sprint(field.Index(row)))
}
return outputList
}
func (f *Table) appendSimpleValue(field reflect.Value, outputList []string, row int) []string {
if row > 0 {
outputList = append(outputList, emptyVal)
} else {
outputList = append(outputList, fmt.Sprint(field))
}
return outputList
}
func (f *Table) getSimpleStructOutput(val reflect.Value) string {
var outputList []string
for i := 0; i < val.Type().NumField(); i++ {
outputList = append(outputList, fmt.Sprint(val.Field(i)))
}
return strings.Join(outputList, tableDelimiter)
}
func (f *Table) isSimpleType(value reflect.Type) bool {
return !(value.Kind() == reflect.Slice || value.Kind() == reflect.Struct)
}
func (f *Table) isSlice(value reflect.Type) bool {
return value.Kind() == reflect.Slice
}
func (f *Table) isStruct(value reflect.Type) bool {
return value.Kind() == reflect.Struct
}
func (f *Table) write(v interface{}) {
_, _ = fmt.Fprint(&f.writer, v)
}
func (f *Table) newLine() {
_, _ = fmt.Fprintln(&f.writer)
}