forked from Oskang09/goloquent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
257 lines (227 loc) · 5.37 KB
/
utils.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
package goloquent
import (
"fmt"
"math/rand"
"net/url"
"strconv"
"strings"
"time"
"unsafe"
"cloud.google.com/go/datastore"
)
// Dictionary :
type dictionary map[string]bool
func newDictionary(v []string) dictionary {
l := make(map[string]bool)
for _, vv := range v {
vv = strings.TrimSpace(vv)
if vv == "" {
continue
}
l[vv] = true
}
return dictionary(l)
}
func (d dictionary) add(k string) {
if !d.has(k) {
d[k] = true
}
}
// Has :
func (d dictionary) has(k string) bool {
return d[k]
}
// Delete :
func (d dictionary) delete(k string) {
delete(d, k)
}
// Keys :
func (d dictionary) keys() []string {
arr := make([]string, 0, len(d))
for k := range d {
arr = append(arr, k)
}
return arr
}
func b2s(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
// StringKey :
func StringKey(key *datastore.Key) string {
if key == nil {
return ""
}
if key.Name != "" {
return key.Name
}
return strconv.FormatInt(key.ID, 10)
}
// minimum and maximum value for random seed
const (
minSeed = int64(100000000)
maxSeed = int64(999999999)
)
// newPrimaryKey will generate a new key if the key provided was incomplete
// and it will ensure the key will not be incomplete
func newPrimaryKey(table string, parentKey *datastore.Key) *datastore.Key {
if parentKey != nil && ((parentKey.Kind == table && parentKey.Name != "") ||
(parentKey.Kind == table && parentKey.ID > 0)) {
return parentKey
}
rand.Seed(time.Now().UnixNano())
strID := strconv.FormatInt(time.Now().Unix(), 10) + strconv.FormatInt(rand.Int63n(maxSeed-minSeed)+minSeed, 10)
id, _ := strconv.ParseInt(strID, 10, 64)
if parentKey != nil && parentKey.Kind == table {
parentKey.ID = id
return parentKey
}
key := new(datastore.Key)
key.Kind = table
key.ID = id
if parentKey != nil {
key.Parent = parentKey
}
return key
}
func isNameKey(strKey string) bool {
if strKey == "" {
return false
}
if strings.HasPrefix(strKey, "name=") {
return true
}
_, err := strconv.ParseInt(strKey, 10, 64)
if err != nil {
return true
}
paths := strings.Split(strKey, "/")
if len(paths) != 2 {
return strings.HasPrefix(strKey, "'") && strings.HasSuffix(strKey, "'")
}
lastPath := strings.Split(paths[len(paths)-1], ",")[1]
return strings.HasPrefix(lastPath, "'") || strings.HasSuffix(lastPath, "'")
}
// ParseKey :
func ParseKey(str string) (*datastore.Key, error) {
return parseKey(str)
}
// parseKey will parse any key string to *datastore.Key,
// it will return null *datastore.Key if the key string is empty
func parseKey(str string) (*datastore.Key, error) {
str = strings.Trim(strings.TrimSpace(str), `"`)
if str == "" {
var k *datastore.Key
return k, nil
}
paths := strings.Split(strings.Trim(str, "/"), "/")
parentKey := new(datastore.Key)
for _, p := range paths {
path := strings.Split(p, ",")
if len(path) != 2 {
return nil, fmt.Errorf("goloquent: incorrect key value: %q, suppose %q", p, "table,value")
}
kind, value := path[0], path[1]
if kind == "" || value == "" {
return nil, fmt.Errorf("goloquent: invalid key value format: %q, suppose %q", p, "table,value")
}
key := new(datastore.Key)
key.Kind = kind
if isNameKey(value) {
name, err := url.PathUnescape(strings.Trim(value, `'`))
if err != nil {
return nil, err
}
key.Name = name
} else {
n, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return nil, fmt.Errorf("goloquent: incorrect key id, %v", value)
}
key.ID = n
}
if !parentKey.Incomplete() {
key.Parent = parentKey
}
parentKey = key
}
return parentKey, nil
}
// StringifyKey :
func StringifyKey(key *datastore.Key) string {
return stringifyKey(key)
}
// stringifyKey, will transform key to either string or empty string
func stringifyKey(key *datastore.Key) string {
paths := make([]string, 0)
parentKey := key
for parentKey != nil {
var k string
if parentKey.Name != "" {
name := url.PathEscape(parentKey.Name)
k = parentKey.Kind + ",'" + name + "'"
} else {
k = parentKey.Kind + "," + strconv.FormatInt(parentKey.ID, 10)
}
paths = append([]string{k}, paths...)
parentKey = parentKey.Parent
}
if len(paths) > 0 {
return strings.Join(paths, "/")
}
return ""
}
func splitKey(k *datastore.Key) (key string, parent string) {
if k == nil {
return "", ""
}
if k.ID > 0 {
if isPkSimple {
return strconv.FormatInt(k.ID, 10), stringifyKey(k.Parent)
}
return k.Kind + "," + strconv.FormatInt(k.ID, 10), stringifyKey(k.Parent)
}
name := url.PathEscape(k.Name)
if isPkSimple {
return "'" + name + "'", stringifyKey(k.Parent)
}
return k.Kind + ",'" + name + "'", stringifyKey(k.Parent)
}
func stringPk(k *datastore.Key) string {
kk, pp := splitKey(k)
return strings.Trim(pp+keyDelimeter+kk, keyDelimeter)
}
// compareVersion: is compare using semantic versioning
// if a > b, result will be -1
// if b < a, result will be 1
// if a = b, result will be 0
func compareVersion(a, b string) (ret int) {
as := strings.Split(a, ".")
bs := strings.Split(b, ".")
loopMax := len(bs)
if len(as) > len(bs) {
loopMax = len(as)
}
for i := 0; i < loopMax; i++ {
var x, y string
if len(as) > i {
x = as[i]
}
if len(bs) > i {
y = bs[i]
}
xi, _ := strconv.Atoi(x)
yi, _ := strconv.Atoi(y)
if xi > yi {
ret = -1
} else if xi < yi {
ret = 1
}
if ret != 0 {
break
}
}
return
}
func escapeSingleQuote(v string) string {
return strings.Replace(v, `'`, `''`, -1)
}