1
|
|
|
package internal |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"reflect" |
5
|
|
|
"strings" |
6
|
|
|
|
7
|
|
|
"github.com/gotilty/gotil/internal/errs" |
8
|
|
|
) |
9
|
|
|
|
10
|
|
|
// It checks whether the given value exists in the given parameter. |
11
|
|
|
// data := []float64{5, 10.5, 10, 20, 10.75, 100, 4.23, 5.15, 5.99, 100.0001} |
12
|
|
|
// result, _ := Includes(data, "gotil") |
13
|
|
|
// // Output: false |
14
|
|
|
func Includes(a interface{}, val interface{}) (bool, error) { |
15
|
|
|
state_arr := false |
16
|
|
|
state_map := false |
17
|
|
|
value := reflect.ValueOf(a) |
18
|
|
|
switch value.Kind() { |
19
|
|
|
case reflect.Slice, reflect.Array: |
20
|
|
|
for i := 0; i < value.Len(); i++ { |
21
|
|
|
value_i := value.Index(i) |
22
|
|
|
if reflect.DeepEqual(value_i.Interface(), val) { |
23
|
|
|
return true, nil |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
return state_arr, nil |
27
|
|
|
case reflect.Map, reflect.Struct: |
28
|
|
|
keys := value.MapKeys() |
29
|
|
|
FindBy(keys, func(find interface{}, i int) bool { |
30
|
|
|
slice_in_map := value.MapIndex(find.(reflect.Value)) |
31
|
|
|
if includes_in_slice, err := Includes(slice_in_map.Interface(), val); err == nil && includes_in_slice { |
32
|
|
|
state_map = includes_in_slice |
33
|
|
|
} |
34
|
|
|
return state_map |
35
|
|
|
}) |
36
|
|
|
return state_map || state_arr, nil |
37
|
|
|
|
38
|
|
|
case reflect.String, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64: |
39
|
|
|
a_str, _ := ToString(a) |
40
|
|
|
val_str, _ := ToString(val) |
41
|
|
|
return (strings.Contains(a_str, val_str)), nil |
42
|
|
|
} |
43
|
|
|
return false, errs.NewUnsupportedTypeError(value.Kind().String()) |
44
|
|
|
} |
45
|
|
|
|