-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathhelpers.go
More file actions
99 lines (84 loc) · 1.89 KB
/
Copy pathhelpers.go
File metadata and controls
99 lines (84 loc) · 1.89 KB
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
package admin
import (
"crypto/rand"
"reflect"
"strconv"
"strings"
)
func parseInt(s string) (int, error) {
i64, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return 0, err
}
return int(i64), nil
}
// parseTag parses admin tags used in model structs.
// TODO: Report errors
func parseTag(s string) (map[string]string, error) {
res := map[string]string{}
inQuotes := false
inKey := true
var key string
start := 0 // Where next key / value starts
end := 0
for i, c := range s {
// Skip ahead if needed
if i < start {
continue
}
if inKey && c == '=' {
// Key is complete, store it and look for value
inKey = !inKey
key = s[start:i]
start = i + 1
} else if c == '\'' && s[i-1] != '\'' {
// For multi word values
inQuotes = !inQuotes
if inQuotes {
start += 1
}
end = i
}
if (c == ' ' || i == len(s)-1) && !inQuotes {
// Insert key and value. If only a key was found, insert as key with empty value.
key = strings.TrimSpace(key)
// If value is in quotes, end it one character earlier
if end == 0 {
end = i + 1
}
val := strings.TrimSpace(s[start:end])
if len(key) == 0 {
res[strings.TrimSpace(val)] = ""
} else {
res[key] = val
}
// Reset before starting to look for next pair
start = i + 1
end = 0
key = ""
inKey = true
}
}
return res, nil
}
func randString(n int) string {
const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
var bytes = make([]byte, n)
rand.Read(bytes)
for i, b := range bytes {
bytes[i] = alphanum[b%byte(len(alphanum))]
}
return string(bytes)
}
func typeToName(t reflect.Type) string {
parts := strings.Split(t.String(), ".")
return parts[len(parts)-1]
}
func typeToTableName(t reflect.Type, nameTransform NameTransformFunc) string {
name := typeToName(t)
if nameTransform != nil {
return nameTransform(name)
} else {
return name
}
}