-
Notifications
You must be signed in to change notification settings - Fork 12
/
mutate_row.go
93 lines (81 loc) · 1.72 KB
/
mutate_row.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
package datatable
import (
"github.com/pkg/errors"
)
// NewRow create a new row
func (t *DataTable) NewRow() Row {
r := make(Row)
return r
}
// Append rows to the table
func (t *DataTable) Append(row ...Row) {
for _, r := range row {
if r == nil {
continue
}
for _, col := range t.cols {
if !col.IsComputed() {
if cell, ok := r[col.Name()]; ok {
col.serie.Append(cell)
continue
}
}
col.serie.Grow(1)
}
t.nrows++
}
t.dirty = true
}
// AppendRow creates a new row and append cells to this row
func (t *DataTable) AppendRow(v ...interface{}) error {
if len(v) != len(t.cols) {
err := errors.Errorf("length mismatch: expected %d elements, values have %d elements", len(t.cols), len(v))
return errors.Wrap(err, ErrLengthMismatch.Error())
}
for i, col := range t.cols {
if col.IsComputed() {
col.serie.Grow(1)
} else {
col.serie.Append(v[i])
}
}
t.nrows++
t.dirty = true
return nil
}
// SwapRow in table
func (t *DataTable) SwapRow(i, j int) {
for _, col := range t.cols {
col.serie.Swap(i, j)
}
}
// Grow the table by size
func (t *DataTable) Grow(size int) {
for _, col := range t.cols {
col.serie.Grow(size)
}
}
// Update the row at index
func (t *DataTable) Update(at int, row Row) error {
if row == nil {
row = make(Row, 0)
}
for _, col := range t.cols {
if col.IsComputed() {
continue
}
cell, ok := row[col.name]
if ok {
if err := col.serie.Set(at, cell); err != nil {
err := errors.Wrapf(err, "col %s", col.name)
return errors.Wrap(err, ErrUpdateRow.Error())
}
continue
}
if err := col.serie.Set(at, nil); err != nil {
err := errors.Wrapf(err, "col %s", col.name)
return errors.Wrap(err, ErrUpdateRow.Error())
}
}
return nil
}