-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtool.go
132 lines (114 loc) · 2.91 KB
/
tool.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
package gofilter
import (
"errors"
"reflect"
)
func AnyFind[T comparable](arr []T, item T) bool {
for _, v := range arr {
if v == item {
return true
}
//if reflect.DeepEqual(v, item) {
// return true
//}
}
return false
}
func AnyEqual[T comparable](arr []T, item T) (bool, error) {
if len(arr) != 1 {
return true, errors.New("params length must be 1")
}
element := arr[0]
if element == item {
return true, nil
}
return false, nil
}
func AnyLessThan[T comparable](arr []T, item T) (bool, error) {
if len(arr) != 1 {
return true, errors.New("params length must be 1")
}
element := arr[0]
elementValue := reflect.ValueOf(element)
itemValue := reflect.ValueOf(item)
switch elementValue.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if itemValue.Int() < elementValue.Int() {
return true, nil
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if uint64(itemValue.Uint()) < elementValue.Uint() {
return true, nil
}
case reflect.Float32, reflect.Float64:
if float64(itemValue.Float()) < elementValue.Float() {
return true, nil
}
case reflect.Bool:
return reflect.ValueOf(item).Bool(), nil
default: // string/map/slice ...
return false, nil
}
return false, nil
}
func AnyGreaterThan[T comparable](arr []T, item T) (bool, error) {
if len(arr) != 1 {
return true, errors.New("params length must be 1")
}
element := arr[0]
elementValue := reflect.ValueOf(element)
itemValue := reflect.ValueOf(item)
switch elementValue.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if itemValue.Int() > elementValue.Int() {
return true, nil
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if uint64(itemValue.Uint()) > elementValue.Uint() {
return true, nil
}
case reflect.Float32, reflect.Float64:
if float64(itemValue.Float()) > elementValue.Float() {
return true, nil
}
case reflect.Bool:
return reflect.ValueOf(item).Bool(), nil
default: // string/map/slice ...
return false, nil
}
return false, nil
}
func dealStructPtr(i any, ctype string) (string, any, bool) {
var flag = false
value := reflect.ValueOf(i)
if value.Kind() != reflect.Ptr {
return "", nil, false
}
flag = true
value = value.Elem()
if value.Kind() == reflect.Struct {
typeOfValue := value.Type()
for i := 0; i < value.NumField(); i++ {
fieldType := typeOfValue.Field(i)
if fieldType.Name != ctype {
continue
}
return ctype, value.Field(i).Interface(), flag
}
}
return "", nil, flag
}
func dealStruct(i any, ctype string) (string, any) {
value := reflect.ValueOf(i)
if value.Kind() == reflect.Struct {
typeOfValue := value.Type()
for i := 0; i < value.NumField(); i++ {
fieldType := typeOfValue.Field(i)
if fieldType.Name != ctype {
continue
}
return ctype, value.Field(i).Interface()
}
}
return "", nil
}