MCPcopy
hub / github.com/uber/aresdb / WriteTable

Function WriteTable

utils/table_writer.go:64–119  ·  view source on GitHub ↗

WriteTable renders a tabular representation from underlying data source. If there is no column for this data source, it will return an empty string. All elements of the table will be right justify (left padding). Column splitter is "|" for now.

(dataSource TableDataSource)

Source from the content-addressed store, hash-verified

62// All elements of the table will be right justify (left padding). Column
63// splitter is "|" for now.
64func WriteTable(dataSource TableDataSource) string {
65 columnHeaders := dataSource.ColumnHeaders()
66 numCols := len(columnHeaders)
67
68 // Return empty string if no columns.
69 if numCols == 0 {
70 return ""
71 }
72 // Compute column widths.
73 columnWidths := make([]int, numCols)
74
75 // Then compare with the length of each value.
76 numRows := dataSource.NumRows()
77 for c := 0; c < numCols; c++ {
78 header := columnHeaders[c]
79 columnWidths[c] = len(header)
80 for r := 0; r < numRows; r++ {
81 value := dataSource.GetValue(r, c)
82 expandColumnWidth(columnWidths, value, c)
83 }
84 }
85
86 // string buffer for final result.
87 var buffer bytes.Buffer
88 // Prepare format for header.
89 headerFormat := "|"
90 for _, columnWidth := range columnWidths {
91 headerFormat += fmt.Sprintf("%%%ds|", columnWidth)
92 }
93 headerFormat += "\n"
94
95 // Write column header.
96 buffer.WriteString(sprintfStrings(headerFormat, columnHeaders))
97
98 if numRows > 0 {
99 // Prepare format for rows.
100 rowFormat := "|"
101 for c := 0; c < numCols; c++ {
102 // get formatter of first row.
103 value := dataSource.GetValue(0, c)
104 modifier := getFormatModifier(value)
105 rowFormat += fmt.Sprintf("%%%d%s|", columnWidths[c], modifier)
106 }
107 rowFormat += "\n"
108
109 // Write rows.
110 for r := 0; r < numRows; r++ {
111 row := make([]interface{}, numCols)
112 for c := 0; c < numCols; c++ {
113 row[c] = dataSource.GetValue(r, c)
114 }
115 buffer.WriteString(fmt.Sprintf(rowFormat, row...))
116 }
117 }
118 return buffer.String()
119}

Callers 2

writeToLogMethod · 0.92

Calls 7

expandColumnWidthFunction · 0.85
sprintfStringsFunction · 0.85
getFormatModifierFunction · 0.85
ColumnHeadersMethod · 0.65
NumRowsMethod · 0.65
GetValueMethod · 0.65
StringMethod · 0.65

Tested by

no test coverage detected