github.com/mattdotmatt/gauge@v0.3.2-0.20160421115137-425a4cdccb62/gauge/table.go (about)

     1  // Copyright 2015 ThoughtWorks, Inc.
     2  
     3  // This file is part of Gauge.
     4  
     5  // Gauge is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  
    10  // Gauge is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU General Public License for more details.
    14  
    15  // You should have received a copy of the GNU General Public License
    16  // along with Gauge.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package gauge
    19  
    20  import (
    21  	"fmt"
    22  	"strings"
    23  )
    24  
    25  type Table struct {
    26  	headerIndexMap map[string]int
    27  	Columns        [][]TableCell
    28  	Headers        []string
    29  	LineNo         int
    30  }
    31  
    32  type DataTable struct {
    33  	Table      Table
    34  	Value      string
    35  	LineNo     int
    36  	IsExternal bool
    37  }
    38  
    39  type TableCell struct {
    40  	Value    string
    41  	CellType ArgType
    42  }
    43  
    44  func (table *Table) IsInitialized() bool {
    45  	return table.headerIndexMap != nil
    46  }
    47  
    48  func (cell *TableCell) GetValue() string {
    49  	value := cell.Value
    50  	if cell.CellType == Dynamic {
    51  		value = fmt.Sprintf("<%s>", value)
    52  	}
    53  	return value
    54  }
    55  
    56  func (dataTable *DataTable) IsInitialized() bool {
    57  	return 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 {
    77  	if !table.headerExists(header) {
    78  		panic(fmt.Sprintf("Table column %s not found", header))
    79  	}
    80  	return table.Columns[table.headerIndexMap[header]]
    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  		trimmedHeader := strings.TrimSpace(column)
    94  		table.Headers[i] = trimmedHeader
    95  		table.headerIndexMap[trimmedHeader] = i
    96  		table.Columns[i] = make([]TableCell, 0)
    97  	}
    98  }
    99  
   100  func (table *Table) AddRowValues(rowValues []string) {
   101  	tableCells := table.createTableCells(rowValues)
   102  	table.addRows(tableCells)
   103  }
   104  
   105  func (table *Table) createTableCells(rowValues []string) []TableCell {
   106  	tableCells := make([]TableCell, 0)
   107  	for _, value := range rowValues {
   108  		tableCells = append(tableCells, GetTableCell(strings.TrimSpace(value)))
   109  	}
   110  	return tableCells
   111  }
   112  
   113  func (table *Table) toHeaderSizeRow(rows []TableCell) []TableCell {
   114  	finalCells := make([]TableCell, 0)
   115  	for i, _ := range table.Headers {
   116  		var cell TableCell
   117  		if len(rows)-1 >= i {
   118  			cell = rows[i]
   119  		} else {
   120  			cell = GetDefaultTableCell()
   121  		}
   122  		finalCells = append(finalCells, cell)
   123  	}
   124  	return finalCells
   125  }
   126  
   127  func (table *Table) addRows(rows []TableCell) {
   128  	for i, value := range table.toHeaderSizeRow(rows) {
   129  		table.Columns[i] = append(table.Columns[i], value)
   130  	}
   131  }
   132  
   133  func (table *Table) Rows() [][]string {
   134  	if !table.IsInitialized() {
   135  		return nil
   136  	}
   137  
   138  	tableRows := make([][]string, 0)
   139  	if len(table.Columns) == 0 {
   140  		return tableRows
   141  	}
   142  	for i := 0; i < len(table.Columns[0]); i++ {
   143  		row := make([]string, 0)
   144  		for _, header := range table.Headers {
   145  			tableCell := table.Get(header)[i]
   146  			value := tableCell.GetValue()
   147  			row = append(row, value)
   148  		}
   149  		tableRows = append(tableRows, row)
   150  	}
   151  	return tableRows
   152  }
   153  
   154  func (table *Table) GetRowCount() int {
   155  	if table.IsInitialized() {
   156  		return len(table.Columns[0])
   157  	} else {
   158  		return 0
   159  	}
   160  }
   161  
   162  func (table *Table) Kind() TokenKind {
   163  	return TableKind
   164  }
   165  
   166  func (externalTable *DataTable) Kind() TokenKind {
   167  	return DataTableKind
   168  }
   169  
   170  func GetTableCell(value string) TableCell {
   171  	return TableCell{Value: value, CellType: Static}
   172  }
   173  
   174  func GetDefaultTableCell() TableCell {
   175  	return TableCell{Value: "", CellType: Static}
   176  }