-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
126 lines (105 loc) · 2.66 KB
/
list.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
package list
import (
"encoding/json"
"regexp"
"strings"
"github.com/spf13/cast"
)
var cachedPatterns = map[string]*regexp.Regexp{}
// ExistInSlice checks whether a comparable element exists in a slice of the same type.
func ExistInSlice[T comparable](item T, list []T) bool {
if len(list) == 0 {
return false
}
for _, v := range list {
if v == item {
return true
}
}
return false
}
// ExistInSliceWithRegex checks whether a string exists in a slice
// either by direct match, or by a regular expression (eg. `^\w+$`).
//
// _Note: Only list items starting with '^' and ending with '$' are treated as regular expressions!_
func ExistInSliceWithRegex(str string, list []string) bool {
for _, field := range list {
isRegex := strings.HasPrefix(field, "^") && strings.HasSuffix(field, "$")
if !isRegex {
// check for direct match
if str == field {
return true
}
continue
}
// check for regex match
pattern, ok := cachedPatterns[field]
if !ok {
var err error
pattern, err = regexp.Compile(field)
if err != nil {
continue
}
// "cache" the pattern to avoid compiling it every time
cachedPatterns[field] = pattern
}
if pattern != nil && pattern.MatchString(str) {
return true
}
}
return false
}
// ToInterfaceSlice converts a generic slice to slice of interfaces.
func ToInterfaceSlice[T any](list []T) []any {
result := make([]any, len(list))
for i := range list {
result[i] = list[i]
}
return result
}
// NonzeroUniques returns only the nonzero unique values from a slice.
func NonzeroUniques[T comparable](list []T) []T {
result := make([]T, 0, len(list))
existMap := make(map[T]struct{}, len(list))
var zeroVal T
for _, val := range list {
if val == zeroVal {
continue
}
if _, ok := existMap[val]; ok {
continue
}
existMap[val] = struct{}{}
result = append(result, val)
}
return result
}
// ToUniqueStringSlice casts `value` to a slice of non-zero unique strings.
func ToUniqueStringSlice(value any) (result []string) {
switch val := value.(type) {
case nil:
// nothing to cast
case []string:
result = val
case string:
if val == "" {
break
}
// check if it is a json encoded array of strings
if strings.Contains(val, "[") {
if err := json.Unmarshal([]byte(val), &result); err != nil {
// not a json array, just add the string as single array element
result = append(result, val)
}
} else {
// just add the string as single array element
result = append(result, val)
}
case json.Marshaler: // eg. JsonArray
raw, _ := val.MarshalJSON()
_ = json.Unmarshal(raw, &result)
default:
result = cast.ToStringSlice(value)
}
return NonzeroUniques(result)
}