/
table.go
174 lines (148 loc) · 3.94 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
/*----------------------------------------------------------------
* Copyright (c) ThoughtWorks, Inc.
* Licensed under the Apache License, Version 2.0
* See LICENSE in the project root for license information.
*----------------------------------------------------------------*/
package gauge
import "fmt"
type Table struct {
headerIndexMap map[string]int
Columns [][]TableCell
Headers []string
LineNo int
}
type DataTable struct {
Table *Table
Value string
LineNo int
IsExternal bool
}
type TableCell struct {
Value string
CellType ArgType
}
func NewTable(headers []string, cols [][]TableCell, lineNo int) *Table {
headerIndx := make(map[string]int)
for i, h := range headers {
headerIndx[h] = i
}
return &Table{
headerIndexMap: headerIndx,
Columns: cols,
Headers: headers,
LineNo: lineNo,
}
}
func (table *Table) IsInitialized() bool {
return table != nil && table.headerIndexMap != nil
}
func (cell *TableCell) GetValue() string {
value := cell.Value
if cell.CellType == Dynamic || cell.CellType == SpecialString {
value = fmt.Sprintf("<%s>", value)
}
return value
}
func (dataTable *DataTable) IsInitialized() bool {
return dataTable.Table != nil && dataTable.Table.headerIndexMap != nil
}
func (table *Table) String() string {
return fmt.Sprintf("%v\n%v", table.Headers, table.Columns)
}
func (table *Table) GetDynamicArgs() []string {
args := make([]string, 0)
for _, row := range table.Columns {
for _, column := range row {
if column.CellType == Dynamic {
args = append(args, column.Value)
}
}
}
return args
}
func (table *Table) Get(header string) ([]TableCell, error) {
if !table.headerExists(header) {
return nil, fmt.Errorf("Table column %s not found", header)
}
return table.Columns[table.headerIndexMap[header]], nil
}
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 {
table.Headers[i] = column
table.headerIndexMap[column] = i
table.Columns[i] = make([]TableCell, 0)
}
}
func (table *Table) AddRowValues(tableCells []TableCell) {
table.addRows(tableCells)
}
func (table *Table) CreateTableCells(rowValues []string) []TableCell {
tableCells := make([]TableCell, 0)
for _, value := range rowValues {
tableCells = append(tableCells, GetTableCell(value))
}
return tableCells
}
func (table *Table) toHeaderSizeRow(rows []TableCell) []TableCell {
finalCells := make([]TableCell, 0)
for i := range table.Headers {
var cell TableCell
if len(rows)-1 >= i {
cell = rows[i]
} else {
cell = GetDefaultTableCell()
}
finalCells = append(finalCells, cell)
}
return finalCells
}
func (table *Table) addRows(rows []TableCell) {
for i, value := range table.toHeaderSizeRow(rows) {
table.Columns[i] = append(table.Columns[i], value)
}
}
func (table *Table) Rows() [][]string {
if !table.IsInitialized() {
return nil
}
tableRows := make([][]string, 0)
if len(table.Columns) == 0 {
return tableRows
}
for i := 0; i < len(table.Columns[0]); i++ {
row := make([]string, 0)
for _, header := range table.Headers {
tableCells, _ := table.Get(header)
tableCell := tableCells[i]
value := tableCell.GetValue()
row = append(row, value)
}
tableRows = append(tableRows, row)
}
return tableRows
}
func (table *Table) GetRowCount() int {
if table.IsInitialized() {
return len(table.Columns[0])
}
return 0
}
func (table *Table) Kind() TokenKind {
return TableKind
}
func (externalTable *DataTable) Kind() TokenKind {
return DataTableKind
}
func GetTableCell(value string) TableCell {
return TableCell{Value: value, CellType: Static}
}
func GetDefaultTableCell() TableCell {
return TableCell{Value: "", CellType: Static}
}