@@ -12,7 +12,7 @@ import (
12
12
13
13
// Pivot takes a "straight table" where the columnn names
14
14
// and values are in a single column and lays it out as a standard tabular data.
15
- func (tbl * Table ) Pivot (colCount uint ) (Table , error ) {
15
+ func (tbl * Table ) Pivot (colCount uint , haveColumns bool ) (Table , error ) {
16
16
newTbl := NewTable (tbl .Name )
17
17
if len (tbl .Columns ) != 0 {
18
18
return newTbl , fmt .Errorf ("has defined columns count [%d]" , len (tbl .Columns ))
@@ -28,19 +28,30 @@ func (tbl *Table) Pivot(colCount uint) (Table, error) {
28
28
if remainder != 0 {
29
29
return newTbl , fmt .Errorf ("row count [%d] is not a multiple of col count [%d]" , rowCount , colCount )
30
30
}
31
+ addedColumns := false
31
32
newRow := []string {}
32
33
for i , row := range tbl .Rows {
33
34
_ , remainder := mathutil .DivideInt64 (int64 (i ), int64 (colCount ))
34
35
if remainder == 0 {
35
36
if len (newRow ) > 0 {
36
- newTbl .Rows = append (newTbl .Rows , newRow )
37
+ if haveColumns && ! addedColumns {
38
+ newTbl .Columns = newRow
39
+ addedColumns = true
40
+ } else {
41
+ newTbl .Rows = append (newTbl .Rows , newRow )
42
+ }
37
43
newRow = []string {}
38
44
}
39
45
}
40
46
newRow = append (newRow , row [0 ])
41
47
}
42
48
if len (newRow ) > 0 {
43
- newTbl .Rows = append (newTbl .Rows , newRow )
49
+ if haveColumns && ! addedColumns {
50
+ newTbl .Columns = newRow
51
+ addedColumns = true
52
+ } else {
53
+ newTbl .Rows = append (newTbl .Rows , newRow )
54
+ }
44
55
}
45
56
return newTbl , nil
46
57
}
0 commit comments