/
strutil.go
47 lines (43 loc) · 964 Bytes
/
strutil.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
package server
import (
"bufio"
"fmt"
"strings"
)
// ArrayContains returns if the array contains the string
func ArrayContains(arr []string, str string) bool {
for _, x := range arr {
if x == str {
return true
}
}
return false
}
// Words breaks the line into words separated by whitespace
func Words(in string) []string {
scn := bufio.NewScanner(strings.NewReader(in))
scn.Split(bufio.ScanWords)
out := make([]string, 0)
for scn.Scan() {
out = append(out, scn.Text())
}
return out
}
// MapYaml converts map[interface{}]interface{} to map[string]interface{}
func MapYaml(in interface{}) interface{} {
if arr, ok := in.([]interface{}); ok {
out := make([]interface{}, 0, len(arr))
for _, x := range arr {
out = append(out, MapYaml(x))
}
return out
}
if m, ok := in.(map[interface{}]interface{}); ok {
out := map[string]interface{}{}
for k, v := range m {
out[fmt.Sprint(k)] = MapYaml(v)
}
return out
}
return in
}