-
Notifications
You must be signed in to change notification settings - Fork 38
/
collection.go
43 lines (37 loc) · 945 Bytes
/
collection.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
package utils
import (
"fmt"
"strings"
)
// MergeMap will merge two map and return a new map
func MergeMap(args ...map[string]string) map[string]string {
m := map[string]string{}
for _, kv := range args {
for k, v := range kv {
m[k] = v
}
}
return m
}
// SetMapIfNotExists will set a key-value of the map if the key is not exists
func SetMapIfNotExists(m map[string]string, k string, v string) {
if _, ok := m[k]; !ok && v != "" {
m[k] = v
}
}
// IsStringIn will return if the value is contains by an array
func IsStringIn(val string, availables []string) bool {
for _, choice := range availables {
if val == choice {
return true
}
}
return false
}
// CheckStringIn will check if the value is contains by an array
func CheckStringIn(val string, availables []string) error {
if IsStringIn(val, availables) {
return nil
}
return fmt.Errorf("got %s, should be one of %s", val, strings.Join(availables, ","))
}