github.com/getgauge/gauge@v1.6.9/gauge/table.go (about) 1 /*---------------------------------------------------------------- 2 * Copyright (c) ThoughtWorks, Inc. 3 * Licensed under the Apache License, Version 2.0 4 * See LICENSE in the project root for license information. 5 *----------------------------------------------------------------*/ 6 7 package gauge 8 9 import "fmt" 10 11 type Table struct { 12 headerIndexMap map[string]int 13 Columns [][]TableCell 14 Headers []string 15 LineNo int 16 } 17 18 type DataTable struct { 19 Table *Table 20 Value string 21 LineNo int 22 IsExternal bool 23 } 24 25 type TableCell struct { 26 Value string 27 CellType ArgType 28 } 29 30 func NewTable(headers []string, cols [][]TableCell, lineNo int) *Table { 31 headerIndx := make(map[string]int) 32 for i, h := range headers { 33 headerIndx[h] = i 34 } 35 36 return &Table{ 37 headerIndexMap: headerIndx, 38 Columns: cols, 39 Headers: headers, 40 LineNo: lineNo, 41 } 42 } 43 44 func (table *Table) IsInitialized() bool { 45 return table != nil && table.headerIndexMap != nil 46 } 47 48 func (cell *TableCell) GetValue() string { 49 value := cell.Value 50 if cell.CellType == Dynamic || cell.CellType == SpecialString { 51 value = fmt.Sprintf("<%s>", value) 52 } 53 return value 54 } 55 56 func (dataTable *DataTable) IsInitialized() bool { 57 return dataTable.Table != nil && dataTable.Table.headerIndexMap != nil 58 } 59 60 func (table *Table) String() string { 61 return fmt.Sprintf("%v\n%v", table.Headers, table.Columns) 62 } 63 64 func (table *Table) GetDynamicArgs() []string { 65 args := make([]string, 0) 66 for _, row := range table.Columns { 67 for _, column := range row { 68 if column.CellType == Dynamic { 69 args = append(args, column.Value) 70 } 71 } 72 } 73 return args 74 } 75 76 func (table *Table) Get(header string) ([]TableCell, error) { 77 if !table.headerExists(header) { 78 return nil, fmt.Errorf("Table column %s not found", header) 79 } 80 return table.Columns[table.headerIndexMap[header]], nil 81 } 82 83 func (table *Table) headerExists(header string) bool { 84 _, ok := table.headerIndexMap[header] 85 return ok 86 } 87 88 func (table *Table) AddHeaders(columnNames []string) { 89 table.headerIndexMap = make(map[string]int) 90 table.Headers = make([]string, len(columnNames)) 91 table.Columns = make([][]TableCell, len(columnNames)) 92 for i, column := range columnNames { 93 table.Headers[i] = column 94 table.headerIndexMap[column] = i 95 table.Columns[i] = make([]TableCell, 0) 96 } 97 } 98 99 func (table *Table) AddRowValues(tableCells []TableCell) { 100 table.addRows(tableCells) 101 } 102 103 func (table *Table) CreateTableCells(rowValues []string) []TableCell { 104 tableCells := make([]TableCell, 0) 105 for _, value := range rowValues { 106 tableCells = append(tableCells, GetTableCell(value)) 107 } 108 return tableCells 109 } 110 111 func (table *Table) toHeaderSizeRow(rows []TableCell) []TableCell { 112 finalCells := make([]TableCell, 0) 113 for i := range table.Headers { 114 var cell TableCell 115 if len(rows)-1 >= i { 116 cell = rows[i] 117 } else { 118 cell = GetDefaultTableCell() 119 } 120 finalCells = append(finalCells, cell) 121 } 122 return finalCells 123 } 124 125 func (table *Table) addRows(rows []TableCell) { 126 for i, value := range table.toHeaderSizeRow(rows) { 127 table.Columns[i] = append(table.Columns[i], value) 128 } 129 } 130 131 func (table *Table) Rows() [][]string { 132 if !table.IsInitialized() { 133 return nil 134 } 135 136 tableRows := make([][]string, 0) 137 if len(table.Columns) == 0 { 138 return tableRows 139 } 140 for i := 0; i < len(table.Columns[0]); i++ { 141 row := make([]string, 0) 142 for _, header := range table.Headers { 143 tableCells, _ := table.Get(header) 144 tableCell := tableCells[i] 145 value := tableCell.GetValue() 146 row = append(row, value) 147 } 148 tableRows = append(tableRows, row) 149 } 150 return tableRows 151 } 152 153 func (table *Table) GetRowCount() int { 154 if table.IsInitialized() { 155 return len(table.Columns[0]) 156 } 157 return 0 158 } 159 160 func (table *Table) Kind() TokenKind { 161 return TableKind 162 } 163 164 func (externalTable *DataTable) Kind() TokenKind { 165 return DataTableKind 166 } 167 168 func GetTableCell(value string) TableCell { 169 return TableCell{Value: value, CellType: Static} 170 } 171 172 func GetDefaultTableCell() TableCell { 173 return TableCell{Value: "", CellType: Static} 174 }