-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathutils.go
More file actions
44 lines (39 loc) · 852 Bytes
/
utils.go
File metadata and controls
44 lines (39 loc) · 852 Bytes
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
package utils
import (
"encoding/json"
"strings"
)
func DefaultStringIfEmpty(str string, defaultString string) string {
if str != "" {
return str
}
return defaultString
}
type SlugifyOptions struct {
KeepHyphen bool
KeepColon bool
KeepHash bool
}
func Slugify(str string, options SlugifyOptions) string {
str = strings.ToLower(str)
str = strings.ReplaceAll(str, " ", "_")
if !options.KeepHyphen {
str = strings.ReplaceAll(str, "-", "_")
}
if !options.KeepColon {
str = strings.ReplaceAll(str, ":", "_")
}
if !options.KeepHash {
str = strings.ReplaceAll(str, "#", "_")
}
return str
}
func StructToStringMap(s interface{}) (map[string]string, error) {
var myMap map[string]string
data, _ := json.Marshal(s)
err := json.Unmarshal(data, &myMap)
if err != nil {
return map[string]string{}, err
}
return myMap, nil
}