-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
49 lines (41 loc) · 933 Bytes
/
util.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
package seedr
import (
"fmt"
"unicode"
)
func panicf(s string, v ...interface{}) {
panic(fmt.Sprintf(s, v...))
}
func toString(v interface{}) string {
s, _ := v.(string)
return s
}
func validString(s string, msg string) string {
if s == "" {
panic(msg)
}
return s
}
// toSnake convert the given string to snake case following the Golang format:
// acronyms are converted to lower-case and preceded by an underscore.
func toSnake(in string) string {
runes := []rune(in)
length := len(runes)
var out []rune
for i := 0; i < length; i++ {
if i > 0 && unicode.IsUpper(runes[i]) && ((i+1 < length && unicode.IsLower(runes[i+1])) || unicode.IsLower(runes[i-1])) {
out = append(out, '_')
}
out = append(out, unicode.ToLower(runes[i]))
}
return string(out)
}
type stringSice []string
func (s stringSice) contains(str string) bool {
for _, v := range s {
if v == str {
return true
}
}
return false
}