-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
64 lines (60 loc) · 1.32 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
package array
import (
"reflect"
"strings"
)
func ToSnake(name string) string {
var convert []byte
for i, asc := range name {
if asc >= 65 && asc <= 90 {
asc += 32
if i > 0 {
convert = append(convert, 95)
}
}
convert = append(convert, uint8(asc))
}
return string(convert)
}
func CheckIsStructSlice(dest any) bool {
typeOf := reflect.TypeOf(dest)
valueOf := reflect.ValueOf(dest)
if typeOf.Kind() == reflect.Pointer {
typeOf = typeOf.Elem()
valueOf = valueOf.Elem()
}
if typeOf.Kind() != reflect.Slice {
return false
}
tmpSlice := reflect.MakeSlice(typeOf, 1, 1)
if tmpSlice.Index(0).Kind() == reflect.Pointer {
pointerKind := tmpSlice.Index(0).Elem().Kind()
if pointerKind == reflect.Struct {
return true
}
if pointerKind == reflect.Invalid {
return false
//unknown := tmpSlice.Index(0).Type().String()
//switch unknown {
//case `int`, `int32`, `int64`, `float64`, `float32`, `string`, `*int`, `*int32`, `*int64`, `*float64`, `*float32`, `*string`:
// return false
//case `bool`, `*bool`:
// return false
//default:
// return true
//}
}
}
if tmpSlice.Index(0).Kind() != reflect.Struct {
return false
}
return true
}
func Part(str, sep string, n int) string {
parts := strings.Split(str, sep)
n--
if n < 0 || n >= len(parts) {
return ""
}
return parts[n]
}