-
Notifications
You must be signed in to change notification settings - Fork 4
/
datarows.go
64 lines (61 loc) · 1.61 KB
/
datarows.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
package datatable
import (
"reflect"
)
type dataRows struct {
data []interface{}
}
func (r *dataRows) Merge(src *dataRows) {
for i, _ := range r.data {
r.data[i] = reflect.AppendSlice(reflect.ValueOf(r.data[i]), reflect.ValueOf(src.data[i])).Interface()
}
}
func (r *dataRows) Remove(rowIndex int) {
for i, _ := range r.data {
lastIdx := reflect.ValueOf(r.data[i]).Len() - 1
reflect.ValueOf(r.data[i]).Index(rowIndex).Set(reflect.ValueOf(r.data[i]).Index(lastIdx))
r.data[i] = reflect.ValueOf(r.data[i]).Slice(0, lastIdx).Interface()
}
return
}
func (r *dataRows) Count() int {
if len(r.data) == 0 {
return 0
}
return reflect.ValueOf(r.data[0]).Len()
}
func (r *dataRows) Get(col, row int) interface{} {
return reflect.ValueOf(r.data[col]).Index(row).Interface()
}
func (r *dataRows) AddColumn(t reflect.Type) {
r.data = append(r.data, reflect.MakeSlice(reflect.SliceOf(t), r.Count(), r.Count()).Interface())
return
}
func ValueOf(v interface{}) reflect.Value {
if v == nil {
return NilValue
}
return reflect.ValueOf(v)
}
func (r *dataRows) AddRow(values []interface{}) {
for i, _ := range r.data {
r.data[i] = reflect.Append(reflect.ValueOf(r.data[i]), ValueOf(values[i])).Interface()
}
return
}
func (r *dataRows) GetRow(row int) []interface{} {
var result []interface{}
for i, _ := range r.data {
result = append(result, r.Get(i, row))
}
return result
}
func (r *dataRows) Set(col, row int, value interface{}) {
reflect.ValueOf(r.data[col]).Index(row).Set(ValueOf(value))
}
func (r *dataRows) SetRow(row int, values []interface{}) {
for i, _ := range r.data {
r.Set(i, row, values[i])
}
return
}