-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
167 lines (148 loc) · 3.26 KB
/
util.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
package ormx
import (
"database/sql"
"encoding/json"
"errors"
"reflect"
"strconv"
"strings"
"time"
sb "github.com/huandu/go-sqlbuilder"
)
func Any2Time(i any) (time.Time, bool) {
if i == nil {
return time.Time{}, false
}
var num int64
switch value := i.(type) {
case string:
if n, err := strconv.ParseInt(value, 10, 64); err == nil {
num = n
}
case float64:
num = int64(value)
case float32:
num = int64(value)
case int64:
num = int64(value)
case int32:
num = int64(value)
case int:
num = int64(value)
case uint64:
num = int64(value)
case uint32:
num = int64(value)
case uint:
num = int64(value)
case json.Number:
if n, err := value.Int64(); err == nil {
num = n
}
}
if num == 0 {
return time.Time{}, false
}
for num >= 9000000000 {
num /= 10
}
return time.Unix(num, 0), true
}
func dereferencedValue(v reflect.Value) reflect.Value {
for k := v.Kind(); k == reflect.Ptr || k == reflect.Interface; k = v.Kind() {
v = v.Elem()
}
return v
}
func dereferencedType(t reflect.Type) reflect.Type {
for k := t.Kind(); k == reflect.Ptr || k == reflect.Interface; k = t.Kind() {
t = t.Elem()
}
return t
}
func dereferencedElemType(t reflect.Type) reflect.Type {
for k := t.Kind(); k == reflect.Ptr || k == reflect.Interface || k == reflect.Slice; k = t.Kind() {
t = t.Elem()
}
return t
}
func Any2Slice(data any) []any {
v := reflect.ValueOf(data)
t := reflect.TypeOf(data)
if t.Kind() != reflect.Slice {
return []any{data}
}
var iface []interface{}
for i := 0; i < v.Len(); i++ {
iface = append(iface, v.Index(i).Interface())
}
return iface
}
type M map[string]any
type KV struct {
Key string
Value any
Extra string
}
type KVs []KV
// KVsFromMap generate KVs from map
func KVsFromMap(dst KVs, filter map[string]any) KVs {
for k, v := range filter {
switch reflect.TypeOf(v).Kind() {
case reflect.Slice:
dst = append(dst, KV{Key: k, Value: v, Extra: "in"})
default:
dst = append(dst, KV{Key: k, Value: v, Extra: "e"})
}
}
return dst
}
// IsNotFound 判断查询错误是否是 未找到错误
func IsNotFound(err error) bool {
return errors.Is(err, sql.ErrNoRows)
}
// IsDuplicate 判断查询错误是否是 未找到错误
func IsDuplicate(err error) bool {
if err == nil {
return false
}
return strings.Contains(err.Error(), "Error 1062: Duplicate")
}
// ParseOptionStr will decode key-value data from a string which format like k1:v1,k2:v2,k3:v3.
// it will always return a non-nil value map
// such as:
// - k1:v1,k2:v2 will parsed to {"k1":"v1","k2":"v2"}
// - k1,k2 will parsed to {"k1":"","k2":""}
// - k1:v2,k2 will parsed to {"k1":"v2","k2":""}
func ParseOptionStr(str string) map[string]string {
options := map[string]string{}
kb, vb, stage := &strings.Builder{}, &strings.Builder{}, 'k'
for i := 0; i < len(str); i++ {
b := kb
if stage == 'v' {
b = vb
}
if str[i] == '\\' && i < len(str)-1 && str[i+1] == ',' {
b.WriteByte(',')
i++
}
if str[i] == ':' {
stage = 'v'
continue
} else if str[i] == ',' {
if k, v := kb.String(), vb.String(); k != "" {
options[k] = v
}
stage = 'k'
kb.Reset()
vb.Reset()
} else {
b.WriteByte(str[i])
}
}
if k, v := kb.String(), vb.String(); k != "" {
options[k] = v
}
return options
}
var Raw = sb.Raw