-
Notifications
You must be signed in to change notification settings - Fork 25
/
kits.go
190 lines (168 loc) · 3.66 KB
/
kits.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
package bun
import (
"context"
"github.com/uptrace/bun"
"reflect"
"strconv"
"strings"
)
// CreateSchema create tables
func CreateSchema(db *bun.DB, ms []interface{}) error {
ctx := context.Background()
for _, model := range ms {
if _, err := db.NewCreateTable().Model(model).Exec(ctx); err != nil {
return err
}
//err := db.Model(model).CreateTable(&orm.CreateTableOptions{Temp: false, IfNotExists: true})
//if err != nil {
// return err
//}
}
return nil
}
// SQLLikeEscapePostgres like escape
func SQLLikeEscapePostgres(param string) string {
return SQLLikeEscape(param, []rune{'%', '_'}, '\\')
}
// SQLLikeEscape like escape
func SQLLikeEscape(param string, special []rune, escape rune) string {
cs := []rune(param)
newcs := make([]rune, 0, len(cs)+10)
es := escape
for _, c := range cs {
for _, s := range special {
if c == s {
newcs = append(newcs, es)
break
}
}
newcs = append(newcs, c)
}
res := string(newcs)
return res
}
// ToMap convert fields of struct to map, key is Lowercase for the first letter of the field name
func ToMap(d interface{}, ex map[string]bool) map[string]string {
if ex == nil {
ex = map[string]bool{}
}
res := make(map[string]string)
mv := reflect.ValueOf(d).Elem()
mt := mv.Type()
for i := 0; i < mv.NumField(); i++ {
v := mv.Field(i)
fname := mt.Field(i).Name
lfname := ToFirstLower(fname)
if _, ok := ex[fname]; ok {
continue
}
if _, ok := ex[lfname]; ok {
continue
}
switch v.Kind() {
case reflect.Int64, reflect.Int32, reflect.Int, reflect.Int16:
res[lfname] = strconv.FormatInt(v.Int(), 10)
default:
res[lfname] = v.String()
}
}
return res
}
// ToFirstLower Lowercase for the first letter
func ToFirstLower(s string) string {
cs := []rune(s)
return strings.ToLower(string(cs[0])) + string(cs[1:])
}
// ToFirstUpper Uppercase for the first letter
func ToFirstUpper(s string) string {
cs := []rune(s)
return strings.ToUpper(string(cs[0])) + string(cs[1:])
}
// ///////////////////////////////////////////see github.com/go-pg/pg/internal
func isUpper(c byte) bool {
return c >= 'A' && c <= 'Z'
}
func isLower(c byte) bool {
return c >= 'a' && c <= 'z'
}
func toUpper(c byte) byte {
return c - 32
}
func toLower(c byte) byte {
return c + 32
}
// Underscore converts "CamelCasedString" to "camel_cased_string".
// Just work for ASCⅡ code
func Underscore(s string) string {
r := make([]byte, 0, len(s)+5)
for i := 0; i < len(s); i++ {
c := s[i]
if isUpper(c) {
if i > 0 && i+1 < len(s) && (isLower(s[i-1]) || isLower(s[i+1])) {
r = append(r, '_', toLower(c))
} else {
r = append(r, toLower(c))
}
} else {
r = append(r, c)
}
}
return string(r)
}
// CamelCased Just work for ASCⅡ code
func CamelCased(s string) string {
r := make([]byte, 0, len(s))
upperNext := true
for i := 0; i < len(s); i++ {
c := s[i]
if c == '_' {
upperNext = true
continue
}
if upperNext {
if isLower(c) {
c = toUpper(c)
}
upperNext = false
}
r = append(r, c)
}
return string(r)
}
// ToExported Just work for ASCⅡ code
func ToExported(s string) string {
if len(s) == 0 {
return s
}
if c := s[0]; isLower(c) {
b := []byte(s)
b[0] = toUpper(c)
return string(b)
}
return s
}
// UpperString Just work for ASCⅡ code
func UpperString(s string) string {
if isUpperString(s) {
return s
}
b := make([]byte, len(s))
for i := range b {
c := s[i]
if isLower(c) {
c = toUpper(c)
}
b[i] = c
}
return string(b)
}
func isUpperString(s string) bool {
for i := 0; i < len(s); i++ {
c := s[i]
if isLower(c) {
return false
}
}
return true
}
/////////////////////////////////////////////see github.com/go-pg/pg/internal