forked from gookit/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck.go
135 lines (118 loc) · 3 KB
/
check.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
package stdutil
import (
"reflect"
"strings"
"github.com/gookit/goutil/reflects"
)
// IsNil value check
func IsNil(v interface{}) bool {
if v == nil {
return true
}
return reflects.IsNil(reflect.ValueOf(v))
}
// IsEmpty value check
func IsEmpty(v interface{}) bool {
if v == nil {
return true
}
return reflects.IsEmpty(reflect.ValueOf(v))
}
// IsFunc value
func IsFunc(val interface{}) bool {
if val == nil {
return false
}
return reflect.TypeOf(val).Kind() == reflect.Func
}
// IsEqual determines if two objects are considered equal.
//
// TIP: cannot compare function type
func IsEqual(src, dst interface{}) bool {
if src == nil || dst == nil {
return src == dst
}
// cannot compare function type
if IsFunc(src) || IsFunc(dst) {
return false
}
return reflects.IsEqual(src, dst)
}
// Contains try loop over the data check if the data includes the element.
// alias of the IsContains
//
// TIP: only support types: string, map, array, slice
//
// map - check key exists
// string - check sub-string exists
// array,slice - check sub-element exists
func Contains(data, elem interface{}) bool {
_, found := CheckContains(data, elem)
return found
}
// IsContains try loop over the data check if the data includes the element.
//
// TIP: only support types: string, map, array, slice
//
// map - check key exists
// string - check sub-string exists
// array,slice - check sub-element exists
func IsContains(data, elem interface{}) bool {
_, found := CheckContains(data, elem)
return found
}
// CheckContains try loop over the data check if the data includes the element.
//
// TIP: only support types: string, map, array, slice
//
// map - check key exists
// string - check sub-string exists
// array,slice - check sub-element exists
//
// return (false, false) if impossible.
// return (true, false) if element was not found.
// return (true, true) if element was found.
func CheckContains(data, elem interface{}) (valid, found bool) {
dataRv := reflect.ValueOf(data)
dataRt := reflect.TypeOf(data)
if dataRt == nil {
return false, false
}
dataKind := dataRt.Kind()
// string
if dataKind == reflect.String {
return true, strings.Contains(dataRv.String(), reflect.ValueOf(elem).String())
}
// map
if dataKind == reflect.Map {
mapKeys := dataRv.MapKeys()
for i := 0; i < len(mapKeys); i++ {
if reflects.IsEqual(mapKeys[i].Interface(), elem) {
return true, true
}
}
return true, false
}
// array, slice - other return false
if dataKind != reflect.Slice && dataKind != reflect.Array {
return false, false
}
for i := 0; i < dataRv.Len(); i++ {
if reflects.IsEqual(dataRv.Index(i).Interface(), elem) {
return true, true
}
}
return true, false
}
// ValueIsEmpty reflect value check.
//
// Deprecated: please use reflects.IsEmpty()
func ValueIsEmpty(v reflect.Value) bool {
return reflects.IsEmpty(v)
}
// ValueLen get reflect value length
//
// Deprecated: please use reflects.Len()
func ValueLen(v reflect.Value) int {
return reflects.Len(v)
}