-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxdataset.go
582 lines (530 loc) · 17 KB
/
xdataset.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
package xcore
import (
"fmt"
"sort"
"strconv"
"strings"
"time"
)
// XDatasetDef is a special interface to implement a set of data that can be scanned recursively (by XTemplate for instance)
// to search data into it, Stringify it, and set/get/del entries of data
// The get* methods must accept a path id>id>id...
type XDatasetDef interface {
// Stringify will dump the content into a human readable string
fmt.Stringer // please implement String()
fmt.GoStringer // Please implement GoString()
// Set will associate the data to the key. If it already exists, it will be replaced
Set(key string, data interface{})
// Get will return the value associated to the key if it exists, or bool = false
Get(key string) (interface{}, bool)
// Same as Get but will return the value associated to the key as a XDatasetDef if it exists, or bool = false
GetDataset(key string) (XDatasetDef, bool)
// Same as Get but will return the value associated to the key as a XDatasetCollectionDef if it exists, or bool = false
GetCollection(key string) (XDatasetCollectionDef, bool)
// Same as Get but will return the value associated to the key as a string if it exists, or bool = false
GetString(key string) (string, bool)
// Same as Get but will return the value associated to the key as a bool if it exists, or bool = false
GetBool(key string) (bool, bool)
// Same as Get but will return the value associated to the key as an int if it exists, or bool = false
GetInt(key string) (int, bool)
// Same as Get but will return the value associated to the key as a float64 if it exists, or bool = false
GetFloat(key string) (float64, bool)
// Same as Get but will return the value associated to the key as a Time if it exists, or bool = false
GetTime(key string) (time.Time, bool)
// Same as Get but will return the value associated to the key as a []String if it exists, or bool = false
GetStringCollection(key string) ([]string, bool)
// Same as Get but will return the value associated to the key as a []bool if it exists, or bool = false
GetBoolCollection(key string) ([]bool, bool)
// Same as Get but will return the value associated to the key as a []int if it exists, or bool = false
GetIntCollection(key string) ([]int, bool)
// Same as Get but will return the value associated to the key as a []float64 if it exists, or bool = false
GetFloatCollection(key string) ([]float64, bool)
// Same as Get but will return the value associated to the key as a []Time if it exists, or bool = false
GetTimeCollection(key string) ([]time.Time, bool)
// Del will delete the data associated to the key and deletes the key entry
Del(key string)
// Clone the object
Clone() XDatasetDef
}
// =====================
// XDataset
// =====================
// XDataset is the basic interface dataset interface
type XDataset map[string]interface{}
// String will transform the XDataset into a readable string for humans
func (d *XDataset) String() string {
sdata := []string{}
for key, val := range *d {
sdata = append(sdata, key+":"+fmt.Sprintf("%v", val))
}
sort.Strings(sdata) // Lets be sure the print is always the same presentation
return "xcore.XDataset{" + strings.Join(sdata, " ") + "}"
}
// GoString will transform the XDataset into a readable string for humans
func (d *XDataset) GoString() string {
sdata := []string{}
for key, val := range *d {
sdata = append(sdata, key+":"+fmt.Sprintf("%#v", val))
}
sort.Strings(sdata) // Lets be sure the print is always the same presentation
return "#xcore.XDataset{" + strings.Join(sdata, " ") + "}"
}
// Set will add a variable key with value data to the XDataset
func (d *XDataset) Set(key string, data interface{}) {
(*d)[key] = data
}
// Get will read the value of the key variable
func (d *XDataset) Get(key string) (interface{}, bool) {
xid := strings.Split(key, ">")
if len(xid) > 1 {
subset, ok := (*d)[xid[0]]
if !ok {
return nil, false
}
if ds, ok := subset.(XDatasetDef); ok {
return ds.Get(strings.Join(xid[1:], ">"))
}
if dsc, ok := subset.(XDatasetCollectionDef); ok {
entry, err := strconv.Atoi(xid[1])
if err != nil {
return nil, false
}
ds, _ := dsc.Get(entry)
if ds == nil {
return nil, false
}
if len(xid) == 2 {
return ds, true
}
return ds.Get(strings.Join(xid[2:], ">"))
}
}
data, ok := (*d)[key]
if ok {
return data, true
}
return nil, false
}
// GetDataset will read the value of the key variable as a XDatasetDef cast type
func (d *XDataset) GetDataset(key string) (XDatasetDef, bool) {
if val, ok := d.Get(key); ok {
if val2, ok2 := val.(XDatasetDef); ok2 {
return val2, true
}
}
return nil, false
}
// GetCollection will read the value of the key variable as a XDatasetCollection cast type
func (d *XDataset) GetCollection(key string) (XDatasetCollectionDef, bool) {
if val, ok := d.Get(key); ok {
if val2, ok2 := val.(XDatasetCollectionDef); ok2 {
return val2, true
}
}
return nil, false
}
// GetString will read the value of the key variable as a string cast type
func (d *XDataset) GetString(key string) (string, bool) {
if val, ok := d.Get(key); ok {
if val == nil {
return "", true
}
return fmt.Sprint(val), true
}
return "", false
}
// GetBool will read the value of the key variable as a boolean cast type
// If the value is int, float, it will be convert with the rule 0: false, != 0: true
// If the value is anything else and it exists, it will return true if it's not nil
func (d *XDataset) GetBool(key string) (bool, bool) {
if val, ok := d.Get(key); ok {
if val2, ok2 := val.(bool); ok2 {
return val2, true
}
if val2, ok2 := val.(int); ok2 {
return val2 != 0, true
}
if val2, ok2 := val.(int8); ok2 {
return val2 != 0, true
}
if val2, ok2 := val.(int16); ok2 {
return val2 != 0, true
}
if val2, ok2 := val.(int32); ok2 {
return val2 != 0, true
}
if val2, ok2 := val.(int64); ok2 {
return val2 != 0, true
}
if val2, ok2 := val.(uint); ok2 {
return val2 != 0, true
}
if val2, ok2 := val.(uint8); ok2 {
return val2 != 0, true
}
if val2, ok2 := val.(uint16); ok2 {
return val2 != 0, true
}
if val2, ok2 := val.(uint32); ok2 {
return val2 != 0, true
}
if val2, ok2 := val.(uint64); ok2 {
return val2 != 0, true
}
if val2, ok2 := val.(byte); ok2 {
return val2 != 0, true
}
if val2, ok2 := val.(float32); ok2 {
return val2 != 0, true
}
if val2, ok2 := val.(float64); ok2 {
return val2 != 0, true
}
if val != nil {
return true, true
}
}
return false, false
}
// GetInt will read the value of the key variable as an integer cast type
// If the value is bool, will return 0/1
// If the value is float, will return integer part of value
func (d *XDataset) GetInt(key string) (int, bool) {
if val, ok := d.Get(key); ok {
if val2, ok2 := val.(bool); ok2 {
if val2 {
return 1, true
}
return 0, true
}
if val2, ok2 := val.(int); ok2 {
return val2, true
}
if val2, ok2 := val.(int8); ok2 {
return int(val2), true
}
if val2, ok2 := val.(int16); ok2 {
return int(val2), true
}
if val2, ok2 := val.(int32); ok2 {
return int(val2), true
}
if val2, ok2 := val.(int64); ok2 {
return int(val2), true
}
if val2, ok2 := val.(uint); ok2 {
return int(val2), true
}
if val2, ok2 := val.(uint8); ok2 {
return int(val2), true
}
if val2, ok2 := val.(uint16); ok2 {
return int(val2), true
}
if val2, ok2 := val.(uint32); ok2 {
return int(val2), true
}
if val2, ok2 := val.(uint64); ok2 {
return int(val2), true
}
if val2, ok2 := val.(byte); ok2 {
return int(val2), true
}
if val2, ok2 := val.(float32); ok2 {
return int(val2), true
}
if val2, ok2 := val.(float64); ok2 {
return int(val2), true
}
}
return 0, false
}
// GetFloat will read the value of the key variable as a float64 cast type
func (d *XDataset) GetFloat(key string) (float64, bool) {
if val, ok := d.Get(key); ok {
if val2, ok2 := val.(bool); ok2 {
if val2 {
return 1.0, true
}
return 0.0, true
}
if val2, ok2 := val.(int); ok2 {
return float64(val2), true
}
if val2, ok2 := val.(int8); ok2 {
return float64(val2), true
}
if val2, ok2 := val.(int16); ok2 {
return float64(val2), true
}
if val2, ok2 := val.(int32); ok2 {
return float64(val2), true
}
if val2, ok2 := val.(int64); ok2 {
return float64(val2), true
}
if val2, ok2 := val.(uint); ok2 {
return float64(val2), true
}
if val2, ok2 := val.(uint8); ok2 {
return float64(val2), true
}
if val2, ok2 := val.(uint16); ok2 {
return float64(val2), true
}
if val2, ok2 := val.(uint32); ok2 {
return float64(val2), true
}
if val2, ok2 := val.(uint64); ok2 {
return float64(val2), true
}
if val2, ok2 := val.(byte); ok2 {
return float64(val2), true
}
if val2, ok2 := val.(float32); ok2 {
return float64(val2), true
}
if val2, ok2 := val.(float64); ok2 {
return val2, true
}
}
return 0.0, false
}
// GetTime will read the value of the key variable as a time cast type
func (d *XDataset) GetTime(key string) (time.Time, bool) {
if val, ok := d.Get(key); ok {
if val2, ok2 := val.(time.Time); ok2 {
return val2, true
}
}
return time.Time{}, false
}
// GetStringCollection will read the value of the key variable as a collection of strings cast type
func (d *XDataset) GetStringCollection(key string) ([]string, bool) {
if val, ok := d.Get(key); ok {
if val2, ok2 := val.([]string); ok2 {
return val2, true
}
}
return nil, false
}
// GetBoolCollection will read the value of the key variable as a collection of bool cast type
func (d *XDataset) GetBoolCollection(key string) ([]bool, bool) {
if val, ok := d.Get(key); ok {
if val2, ok2 := val.([]bool); ok2 {
return val2, true
}
}
return nil, false
}
// GetIntCollection will read the value of the key variable as a collection of int cast type
func (d *XDataset) GetIntCollection(key string) ([]int, bool) {
if val, ok := d.Get(key); ok {
if val2, ok2 := val.([]int); ok2 {
return val2, true
}
}
return nil, false
}
// GetFloatCollection will read the value of the key variable as a collection of float cast type
func (d *XDataset) GetFloatCollection(key string) ([]float64, bool) {
if val, ok := d.Get(key); ok {
if val2, ok2 := val.([]float64); ok2 {
return val2, true
}
}
return nil, false
}
// GetTimeCollection will read the value of the key variable as a collection of time cast type
func (d *XDataset) GetTimeCollection(key string) ([]time.Time, bool) {
if val, ok := d.Get(key); ok {
if val2, ok2 := val.([]time.Time); ok2 {
return val2, true
}
}
return nil, false
}
// Del will deletes the variable
func (d *XDataset) Del(key string) {
delete(*d, key)
}
// Clone will creates a totally new data memory cloned from this object
func (d *XDataset) Clone() XDatasetDef {
cloned := &XDataset{}
for id, val := range *d {
clonedval := val
// If the object is also cloneable, we clone it
if cloneable1, ok := val.(interface{ Clone() XDatasetDef }); ok {
clonedval = cloneable1.Clone()
}
if cloneable2, ok := val.(interface{ Clone() XDatasetCollectionDef }); ok {
clonedval = cloneable2.Clone()
}
cloned.Set(id, clonedval)
}
return cloned
}
// XDatasetCollectionDef is the definition of a collection of XDatasetDefs
type XDatasetCollectionDef interface {
// Stringify will dump the content into a human readable string
fmt.Stringer // please implement String()
fmt.GoStringer // Please implement GoString()
// Will add a datasetdef to the beginning of the collection
Unshift(data XDatasetDef)
// Will remove the first datasetdef of the collection and return it
Shift() XDatasetDef
// Will add a datasetdef to the end of the collection
Push(data XDatasetDef)
// Will remove the last datasetdef of the collection and return it
Pop() XDatasetDef
// Will count the quantity of entries
Count() int
// Will get the entry by the index and let it in the collection
Get(index int) (XDatasetDef, bool)
// Will search for the data associated to the key by priority (last entry is the most important)
// returns bool = false if nothing has been found
GetData(key string) (interface{}, bool)
// Same as GetData but will convert the result to a string if possible
// returns bool = false if nothing has been found
GetDataString(key string) (string, bool)
// Same as GetData but will convert the result to an int if possible
// returns bool = false if nothing has been found
GetDataInt(key string) (int, bool)
// Same as GetData but will convert the result to a boolean if possible
// returns second bool = false if nothing has been found
GetDataBool(key string) (bool, bool)
// Same as GetData but will convert the result to a float if possible
// returns bool = false if nothing has been found
GetDataFloat(key string) (float64, bool)
// Same as GetData but will convert the result to a Time if possible
// returns bool = false if nothing has been found
GetDataTime(key string) (time.Time, bool)
// Same as GetData but will convert the result to a XDatasetCollectionDef of data if possible
// returns bool = false if nothing has been found
GetCollection(key string) (XDatasetCollectionDef, bool)
// Clone the object
Clone() XDatasetCollectionDef
}
// =====================
// XDatasetConnection
// =====================
// XDatasetCollection is the basic collection of XDatasetDefs
type XDatasetCollection []XDatasetDef
// String will transform the XDataset into a readable string
func (d *XDatasetCollection) String() string {
str := "XDatasetCollection["
for key, val := range *d {
str += strconv.Itoa(key) + ":" + fmt.Sprint(val) + " "
}
str += "]"
return str
}
// GoString will transform the XDataset into a readable string for humans
func (d *XDatasetCollection) GoString() string {
return d.String()
}
// Unshift will adds a XDatasetDef at the beginning of the collection
func (d *XDatasetCollection) Unshift(data XDatasetDef) {
*d = append([]XDatasetDef{data}, (*d)...)
}
// Shift will remove the element at the beginning of the collection
func (d *XDatasetCollection) Shift() XDatasetDef {
data := (*d)[0]
*d = (*d)[1:]
return data
}
// Push will adds a XDatasetDef at the end of the collection
func (d *XDatasetCollection) Push(data XDatasetDef) {
*d = append(*d, data)
}
// Pop will remove the element at the end of the collection
func (d *XDatasetCollection) Pop() XDatasetDef {
data := (*d)[len(*d)-1]
*d = (*d)[:len(*d)-1]
return data
}
// Count will return the quantity of elements into the collection
func (d *XDatasetCollection) Count() int {
return len(*d)
}
// Get will retrieve an element by index from the collection
func (d *XDatasetCollection) Get(index int) (XDatasetDef, bool) {
if index < 0 || index >= len(*d) {
return nil, false
}
return (*d)[index], true
}
// GetData will retrieve the first available data identified by key from the collection ordered by index
func (d *XDatasetCollection) GetData(key string) (interface{}, bool) {
for i := len(*d) - 1; i >= 0; i-- {
val, ok := (*d)[i].Get(key)
if ok {
return val, true
}
}
return nil, false
}
// GetDataString will retrieve the first available data identified by key from the collection ordered by index and return it as a string
func (d *XDatasetCollection) GetDataString(key string) (string, bool) {
v, ok := d.GetData(key)
if ok {
if v == nil {
return "", true
}
return fmt.Sprint(v), true
}
return "", false
}
// GetDataBool will retrieve the first available data identified by key from the collection ordered by index and return it as a boolean
func (d *XDatasetCollection) GetDataBool(key string) (bool, bool) {
if val, ok := d.GetData(key); ok {
if val2, ok2 := val.(bool); ok2 {
return val2, true
}
}
return false, false
}
// GetDataInt will retrieve the first available data identified by key from the collection ordered by index and return it as an integer
func (d *XDatasetCollection) GetDataInt(key string) (int, bool) {
if val, ok := d.GetData(key); ok {
if val2, ok2 := val.(int); ok2 {
return val2, true
}
}
return 0, false
}
// GetDataFloat will retrieve the first available data identified by key from the collection ordered by index and return it as a float
func (d *XDatasetCollection) GetDataFloat(key string) (float64, bool) {
if val, ok := d.GetData(key); ok {
if val2, ok2 := val.(float64); ok2 {
return val2, true
}
}
return 0, false
}
// GetDataTime will retrieve the first available data identified by key from the collection ordered by index and return it as a time
func (d *XDatasetCollection) GetDataTime(key string) (time.Time, bool) {
if val, ok := d.GetData(key); ok {
if val2, ok2 := val.(time.Time); ok2 {
return val2, true
}
}
return time.Time{}, false
}
// GetCollection will retrieve a collection from the XDatasetCollection
func (d *XDatasetCollection) GetCollection(key string) (XDatasetCollectionDef, bool) {
v, ok := d.GetData(key)
// Verify v IS actually a XDatasetCollectionDef to avoid the error
if ok {
return v.(XDatasetCollectionDef), true
}
return nil, false
}
// Clone will make a full copy of the object into memory
func (d *XDatasetCollection) Clone() XDatasetCollectionDef {
cloned := &XDatasetCollection{}
for _, val := range *d {
*cloned = append(*cloned, val.Clone())
}
return cloned
}