-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcolumn.go
221 lines (196 loc) · 5.07 KB
/
column.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package datatable
import (
"reflect"
"strings"
"github.com/datasweet/datatable/serie"
"github.com/datasweet/expr"
"github.com/pkg/errors"
)
// ColumnType defines the valid column type in datatable
type ColumnType string
const (
Bool ColumnType = "bool"
String ColumnType = "string"
Int ColumnType = "int"
// Int8 ColumnType = "int8"
// Int16 ColumnType = "int16"
Int32 ColumnType = "int32"
Int64 ColumnType = "int64"
// Uint ColumnType = "uint"
// Uint8 ColumnType = "uint8"
// Uint16 ColumnType = "uint16"
// Uint32 ColumnType = "uint32"
// Uint64 ColumnType = "uint64"
Float32 ColumnType = "float32"
Float64 ColumnType = "float64"
Time ColumnType = "time"
Raw ColumnType = "raw"
)
// ColumnOptions describes options to be apply on a column
type ColumnOptions struct {
Hidden bool
Expr string
Values []interface{}
TimeFormats []string
}
// ColumnOption sets column options
type ColumnOption func(opts *ColumnOptions)
// ColumnHidden sets the visibility
func ColumnHidden(v bool) ColumnOption {
return func(opts *ColumnOptions) {
opts.Hidden = v
}
}
// Expr sets the expr for the column
// <!> Incompatible with ColumnValues
func Expr(v string) ColumnOption {
return func(opts *ColumnOptions) {
opts.Expr = v
}
}
// Values fills the column with the values
// <!> Incompatible with ColumnExpr
func Values(v ...interface{}) ColumnOption {
return func(opts *ColumnOptions) {
opts.Values = v
}
}
// TimeFormats sets the valid time formats.
// <!> Only for Time Column
func TimeFormats(v ...string) ColumnOption {
return func(opts *ColumnOptions) {
opts.TimeFormats = append(opts.TimeFormats, v...)
}
}
// ColumnSerier to create a serie from column options
type ColumnSerier func(ColumnOptions) serie.Serie
// ctypes is our column type registry
var ctypes map[ColumnType]ColumnSerier
func init() {
ctypes = make(map[ColumnType]ColumnSerier)
RegisterColumnType(Bool, func(opts ColumnOptions) serie.Serie {
return serie.BoolN(opts.Values...)
})
RegisterColumnType(String, func(opts ColumnOptions) serie.Serie {
return serie.StringN(opts.Values...)
})
RegisterColumnType(Int, func(opts ColumnOptions) serie.Serie {
return serie.IntN(opts.Values...)
})
RegisterColumnType(Int32, func(opts ColumnOptions) serie.Serie {
return serie.Int32N(opts.Values...)
})
RegisterColumnType(Int64, func(opts ColumnOptions) serie.Serie {
return serie.Int64N(opts.Values...)
})
RegisterColumnType(Float32, func(opts ColumnOptions) serie.Serie {
return serie.Float32N(opts.Values...)
})
RegisterColumnType(Float64, func(opts ColumnOptions) serie.Serie {
return serie.Float64N(opts.Values...)
})
RegisterColumnType(Time, func(opts ColumnOptions) serie.Serie {
sr := serie.TimeN(opts.TimeFormats...)
if len(opts.Values) > 0 {
sr.Append(opts.Values...)
}
return sr
})
RegisterColumnType(Raw, func(opts ColumnOptions) serie.Serie {
return serie.Raw(opts.Values...)
})
}
// RegisterColumnType to extends the known type
func RegisterColumnType(name ColumnType, serier ColumnSerier) error {
name = ColumnType(strings.TrimSpace(string(name)))
if len(name) == 0 {
return ErrEmptyName
}
if serier == nil {
return ErrNilFactory
}
if _, ok := ctypes[name]; ok {
err := errors.Errorf("type '%s' already exists", name)
return errors.Wrap(err, ErrTypeAlreadyExists.Error())
}
ctypes[name] = serier
return nil
}
// ColumnTypes to list all column type
func ColumnTypes() []ColumnType {
ctyp := make([]ColumnType, 0, len(ctypes))
for k := range ctypes {
ctyp = append(ctyp, k)
}
return ctyp
}
// newColumnSerie to create a serie from a known type
func newColumnSerie(ctyp ColumnType, options ColumnOptions) (serie.Serie, error) {
if s, ok := ctypes[ctyp]; ok {
return s(options), nil
}
err := errors.Errorf("unknown column type '%s'", ctyp)
return nil, errors.Wrap(err, ErrUnknownColumnType.Error())
}
// Column describes a column in our datatable
type Column interface {
Name() string
Type() ColumnType
UnderlyingType() reflect.Type
IsVisible() bool
IsComputed() bool
//Clone(includeValues bool) Column
}
type column struct {
name string
typ ColumnType
hidden bool
formulae string
expr expr.Node
serie serie.Serie
}
func (c *column) Name() string {
return c.name
}
func (c *column) Type() ColumnType {
return c.typ
}
func (c *column) UnderlyingType() reflect.Type {
return c.serie.Type()
}
func (c *column) IsVisible() bool {
return !c.hidden
}
func (c *column) IsComputed() bool {
return len(c.formulae) > 0
}
func (c *column) emptyCopy() *column {
cpy := &column{
name: c.name,
typ: c.typ,
hidden: c.hidden,
formulae: c.formulae,
serie: c.serie.EmptyCopy(),
}
if len(cpy.formulae) > 0 {
if parsed, err := expr.Parse(cpy.formulae); err == nil {
cpy.expr = parsed
}
}
return cpy
}
func (c *column) copy() *column {
cpy := &column{
name: c.name,
typ: c.typ,
hidden: c.hidden,
formulae: c.formulae,
serie: c.serie.Copy(),
}
if len(cpy.formulae) > 0 {
if parsed, err := expr.Parse(cpy.formulae); err == nil {
cpy.expr = parsed
}
}
return cpy
}