-
Notifications
You must be signed in to change notification settings - Fork 1
/
xrecords.go
143 lines (121 loc) · 2.64 KB
/
xrecords.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
package xdominion
import (
"fmt"
"strconv"
"time"
"github.com/webability-go/xcore/v2"
)
/*
The XRecords is based on the xcore/XDatasetCollectionDef (basically same funcions) and also a Record Interface
*/
type XRecordsDef interface {
xcore.XDatasetCollectionDef
}
type XRecords []XRecordDef
// =====================
// XRecords
// =====================
func (r *XRecords) String() string {
str := "XRecords[\n"
for key, val := range *r {
str += " " + strconv.Itoa(key) + ": " + fmt.Sprint(val) + "\n"
}
str += "]\n"
return str
}
func (r *XRecords) GoString() string {
return r.String()
}
func (d *XRecords) Unshift(data xcore.XDatasetDef) {
*d = append([]XRecordDef{data.(*XRecord)}, (*d)...)
}
func (d *XRecords) Shift() xcore.XDatasetDef {
data := (*d)[0]
*d = (*d)[1:]
return data
}
func (r *XRecords) Push(data xcore.XDatasetDef) {
*r = append(*r, data.(*XRecord))
}
func (r *XRecords) Pop() xcore.XDatasetDef {
data := (*r)[len(*r)-1]
*r = (*r)[:len(*r)-1]
return data
}
func (r *XRecords) Count() int {
return len(*r)
}
func (r *XRecords) Get(index int) (xcore.XDatasetDef, bool) {
if index < 0 || index >= len(*r) {
return nil, false
}
return (*r)[index], true
}
func (r *XRecords) GetData(key string) (interface{}, bool) {
for i := len(*r) - 1; i >= 0; i-- {
val, ok := (*r)[i].Get(key)
if ok {
return val, true
}
}
return nil, false
}
func (r *XRecords) GetDataString(key string) (string, bool) {
v, ok := r.GetData(key)
if ok {
return fmt.Sprint(v), true
}
return "", false
}
func (d *XRecords) GetDataBool(key string) (bool, bool) {
if val, ok := d.GetData(key); ok {
switch val.(type) {
case bool:
return val.(bool), true
}
}
return false, false
}
func (d *XRecords) GetDataInt(key string) (int, bool) {
if val, ok := d.GetData(key); ok {
switch val.(type) {
case int:
return val.(int), true
}
}
return 0, false
}
func (d *XRecords) GetDataFloat(key string) (float64, bool) {
if val, ok := d.GetData(key); ok {
switch val.(type) {
case float64:
return val.(float64), true
}
}
return 0, false
}
func (d *XRecords) GetDataTime(key string) (time.Time, bool) {
if val, ok := d.GetData(key); ok {
switch val.(type) {
case time.Time:
return val.(time.Time), true
}
}
return time.Time{}, false
}
func (r *XRecords) GetCollection(key string) (xcore.XDatasetCollectionDef, bool) {
if val, ok := r.GetData(key); ok {
switch val.(type) {
case *XRecords:
return val.(*XRecords), true
}
}
return nil, false
}
func (r *XRecords) Clone() xcore.XDatasetCollectionDef {
cloned := &XRecords{}
for _, val := range *r {
*cloned = append(*cloned, val.Clone())
}
return cloned
}