forked from getgauge/gauge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.go
115 lines (99 loc) · 2.63 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
package main
import (
"fmt"
"strings"
)
type table struct {
headerIndexMap map[string]int
columns [][]tableCell
headers []string
lineNo int
}
type tableCell struct {
value string
cellType argType
}
func (table *table) isInitialized() bool {
return table.headerIndexMap != nil
}
func (table *table) get(header string) []tableCell {
if !table.headerExists(header) {
panic(fmt.Sprintf("Table column %s not found", header))
}
return table.columns[table.headerIndexMap[header]]
}
func (table *table) headerExists(header string) bool {
_, ok := table.headerIndexMap[header]
return ok
}
func (table *table) addHeaders(columnNames []string) {
table.headerIndexMap = make(map[string]int)
table.headers = make([]string, len(columnNames))
table.columns = make([][]tableCell, len(columnNames))
for i, column := range columnNames {
trimmedHeader := strings.TrimSpace(column)
table.headers[i] = trimmedHeader
table.headerIndexMap[trimmedHeader] = i
table.columns[i] = make([]tableCell, 0)
}
}
func (table *table) addRowValues(rowValues []string) {
for i, value := range rowValues {
table.columns[i] = append(table.columns[i], getTableCell(strings.TrimSpace(value)))
}
if len(table.headers) > len(rowValues) {
for i := len(rowValues); i < len(table.headers); i++ {
table.columns[i] = append(table.columns[i], getDefaultTableCell())
}
}
}
func (table *table) addRows(rows []tableCell) {
for i, value := range rows {
table.columns[i] = append(table.columns[i], value)
}
if len(table.headers) > len(rows) {
for i := len(rows); i < len(table.headers); i++ {
table.columns[i] = append(table.columns[i], getDefaultTableCell())
}
}
}
func (table *table) getRows() [][]string {
if !table.isInitialized() {
return nil
}
tableRows := make([][]string, 0)
for i := 0; i < len(table.columns[0]); i++ {
row := make([]string, 0)
for _, header := range table.headers {
tableCell := table.get(header)[i]
value := tableCell.value
row = append(row, value)
}
tableRows = append(tableRows, row)
}
return tableRows
}
func (table *table) getRowCount() int {
if table.isInitialized() {
return len(table.columns[0])
} else {
return 0
}
}
func (table *table) kind() tokenKind {
return tableKind
}
func getTableCell(value string) tableCell {
return tableCell{value: value, cellType: static}
}
func getDefaultTableCell() tableCell {
return tableCell{value: "", cellType: static}
}
func tableFrom(protoTable *ProtoTable) *table {
table := &table{}
table.addHeaders(protoTable.GetHeaders().GetCells())
for _, row := range protoTable.GetRows() {
table.addRowValues(row.GetCells())
}
return table
}