-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathexport.go
137 lines (115 loc) · 2.97 KB
/
export.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package datatable
// ExportOptions to add options for exporting (like showing hidden columns)
type ExportOptions struct {
WithHiddenCols bool
}
type ExportOption func(*ExportOptions)
// ExportHidden to show a column when exporting (default false)
func ExportHidden(v bool) ExportOption {
return func(opts *ExportOptions) {
opts.WithHiddenCols = v
}
}
// newExportOptions to build the ExportOptions in order to acces the parameters
func newExportOptions(opt ...ExportOption) ExportOptions {
var opts ExportOptions
for _, o := range opt {
o(&opts)
}
return opts
}
// ToMap to export the datatable to a json-like struct
func (t *DataTable) ToMap(opt ...ExportOption) []map[string]interface{} {
if t == nil {
return nil
}
opts := newExportOptions(opt...)
if err := t.evaluateExpressions(); err != nil {
panic(err)
}
// visible columns
cols := make(map[string]int)
for i, col := range t.cols {
if opts.WithHiddenCols || col.IsVisible() {
cols[col.Name()] = i
}
}
rows := make([]map[string]interface{}, 0, t.nrows)
for i := 0; i < t.nrows; i++ {
r := make(map[string]interface{}, len(cols))
for name, pos := range cols {
r[name] = t.cols[pos].serie.Get(i)
}
rows = append(rows, r)
}
return rows
}
// ToTable to export the datatable to a csv-like struct
func (t *DataTable) ToTable(opt ...ExportOption) [][]interface{} {
if t == nil {
return nil
}
opts := newExportOptions(opt...)
if err := t.evaluateExpressions(); err != nil {
panic(err)
}
rows := make([][]interface{}, 0, t.nrows+1)
// visible columns
var headers []interface{}
var cols []int
for i, col := range t.cols {
if opts.WithHiddenCols || col.IsVisible() {
cols = append(cols, i)
headers = append(headers, col.Name())
}
}
rows = append(rows, headers)
for i := 0; i < t.nrows; i++ {
r := make([]interface{}, 0, len(cols))
for _, pos := range cols {
r = append(r, t.cols[pos].serie.Get(i))
}
rows = append(rows, r)
}
return rows
}
// Schema describes a datatable
type Schema struct {
Name string `json:"name"`
Columns []SchemaColumn `json:"cols"`
Rows [][]interface{} `json:"rows"`
}
type SchemaColumn struct {
Name string `json:"name"`
Type string `json:"type"`
}
// ToSchema to export the datatable to a schema struct
func (t *DataTable) ToSchema(opt ...ExportOption) *Schema {
if t == nil {
return nil
}
opts := newExportOptions(opt...)
if err := t.evaluateExpressions(); err != nil {
panic(err)
}
schema := &Schema{
Name: t.name,
Rows: make([][]interface{}, 0, t.nrows),
}
// visible columns
var cols []int
for i, col := range t.cols {
if opts.WithHiddenCols || col.IsVisible() {
cols = append(cols, i)
schema.Columns = append(schema.Columns, SchemaColumn{Type: col.UnderlyingType().Name(), Name: col.Name()})
}
}
for i := 0; i < t.nrows; i++ {
r := make([]interface{}, 0, len(cols))
for _, pos := range cols {
r = append(r, t.cols[pos].serie.Get(i))
}
schema.Rows = append(schema.Rows, r)
}
return schema
}