-
Notifications
You must be signed in to change notification settings - Fork 14
/
table.go
283 lines (244 loc) · 6.05 KB
/
table.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package goql
import (
"fmt"
"strings"
"sync"
"github.com/fzerorubigd/goql/astdata"
)
// ValueType is an enum contain supported value type in system
type ValueType int
// String is the string type, like function name, file name and ...
type String struct {
String string
Null bool
}
// Get return the actual value (and nil)
func (s String) Get() interface{} {
if s.Null {
return nil
}
return s.String
}
// Number is the number, only float64 is supported
type Number struct {
Number float64
Null bool
}
// Get return the actual value (and nil)
func (n Number) Get() interface{} {
if n.Null {
return nil
}
return n.Number
}
// Bool is the boolean type
type Bool struct {
Bool bool
Null bool
}
// Get return the actual value (and nil)
func (b Bool) Get() interface{} {
if b.Null {
return nil
}
return b.Bool
}
// Definition is the type to handle type definition
type Definition struct {
Definition astdata.Definition
}
// Get return the actual definition
func (d Definition) Get() interface{} {
// since its an interface so it could be nil
return d.Definition
}
const (
// ValueTypeString is the string type
ValueTypeString ValueType = iota
// ValueTypeNumber is the number type
ValueTypeNumber
// ValueTypeBool is the bool type
ValueTypeBool
// ValueTypeDefinition is a definition special type
ValueTypeDefinition
)
var (
tables = make(map[string]*table)
lock = &sync.Mutex{}
)
// Getter is replacement to prevent using the interface{} it is used when the value is required
type Getter interface {
Get() interface{}
}
// StringValuer is provider for a value for a table
type StringValuer interface {
Value(interface{}) String
}
// NumberValuer is the number valuer (float64 is supported only )
type NumberValuer interface {
Value(interface{}) Number
}
// BoolValuer is the Boolean valuer
type BoolValuer interface {
Value(interface{}) Bool
}
// DefinitionValuer is used to handle definition column
type DefinitionValuer interface {
Value(interface{}) Definition
}
// columnDef is the helper for column definition
type columnDef struct {
name string
typ interface{}
order int
}
// Order return order of registration
func (c columnDef) Order() int {
return c.order
}
// Type return the type of value of column
func (c columnDef) Type() ValueType {
switch c.typ.(type) {
case StringValuer:
return ValueTypeString
case NumberValuer:
return ValueTypeNumber
case BoolValuer:
return ValueTypeBool
case DefinitionValuer:
return ValueTypeDefinition
default:
panic("invalid valuer!")
}
}
// table is the single table in system
type table struct {
name string
fields map[string]columnDef // interface is one of the Valuer interface and not anything else
data Table
lock *sync.Mutex
}
// Table is a interface for registration of a data
type Table interface {
// the function argument is the object used as database. in our case it is the astdata.Package and the result
// must be an array of items. items are depends on the table. for example on funcs table, the result
// is a slice of astdata.Functions
Provide(interface{}) []interface{}
}
// RegisterTable is the function to handle registration of a table, the name must be unique, and duplicate registration
// panics
func RegisterTable(name string, data Table) {
lock.Lock()
defer lock.Unlock()
if _, ok := tables[name]; ok {
panic(fmt.Sprintf("table with name %s is already registered", name))
}
tables[name] = &table{
name: name,
data: data,
fields: make(map[string]columnDef),
lock: &sync.Mutex{},
}
}
// getTable return the table definition
func getTable(t string) (map[string]columnDef, error) {
tbl, ok := tables[t]
if !ok {
return nil, fmt.Errorf("table %s is not available", t)
}
return tbl.fields, nil
}
// RegisterField is the field registration for a table, table must registered before and the name must be unique in that table
// the value is one of the String/Bool/NumberValuer in any other case, it panics
func RegisterField(t string, name string, valuer interface{}) {
lock.Lock()
defer lock.Unlock()
tbl, ok := tables[t]
if !ok {
panic(fmt.Sprintf("table %s is not available", t))
}
max := -1
for i := range tbl.fields {
if tbl.fields[i].order > max {
max = tbl.fields[i].order
}
}
max++
if _, ok := tbl.fields[name]; ok {
panic(fmt.Sprintf("table %s is already have field %s", t, name))
}
switch valuer.(type) {
case BoolValuer:
case NumberValuer:
case StringValuer:
case DefinitionValuer:
default:
panic(fmt.Sprintf("valuer is not a valid valuer, its is %T", valuer))
}
tbl.fields[name] = columnDef{
typ: valuer,
name: name,
order: max,
}
}
func checkTableFields(tbl *table, fields ...string) error {
var invalid []string
for i := range fields {
if fields[i] == "" {
continue
}
if _, ok := tbl.fields[fields[i]]; !ok {
invalid = append(invalid, fields[i])
}
}
if len(invalid) > 0 {
return fmt.Errorf("invalid field(s) : %s", strings.Join(invalid, ", "))
}
return nil
}
// getTableFields is the get field for a table, empty field name is ignored, so the caller could fill
// calculated item
func getTableFields(p interface{}, t string, res chan<- []Getter, quit chan struct{}, fields ...string) error {
lock.Lock()
defer lock.Unlock()
tbl, ok := tables[t]
if !ok {
return fmt.Errorf("invalid table name %s", t)
}
if len(fields) == 0 {
return fmt.Errorf("no field selected")
}
if err := checkTableFields(tbl, fields...); err != nil {
return err
}
// do concurrently
go func() {
defer close(res)
cache := tbl.data.Provide(p)
for i := range cache {
n := make([]Getter, len(fields))
for f := range fields {
if fields[f] == "" {
// this is a placeholder
continue
}
switch t := tbl.fields[fields[f]].typ.(type) {
case StringValuer:
n[f] = t.Value(cache[i])
case NumberValuer:
n[f] = t.Value(cache[i])
case BoolValuer:
n[f] = t.Value(cache[i])
case DefinitionValuer:
n[f] = t.Value(cache[i])
}
}
select {
case res <- n:
case <-quit:
return // whenever catch a close signal, exit from loop
}
}
}()
return nil
}