-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhelper.go
91 lines (79 loc) · 2.14 KB
/
helper.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
package dump
import (
"reflect"
"strings"
)
// KeyFormatterFunc is a type for key formatting
type KeyFormatterFunc func(s string, level int) string
// WithLowerCaseFormatter formats keys in lowercase
func WithLowerCaseFormatter() KeyFormatterFunc {
return func(s string, level int) string {
return strings.ToLower(s)
}
}
// WithDefaultLowerCaseFormatter formats keys in lowercase and apply default formatting
func WithDefaultLowerCaseFormatter() KeyFormatterFunc {
f := WithDefaultFormatter()
return func(s string, level int) string {
return strings.ToLower(f(s, level))
}
}
// WithDefaultUpperCaseFormatter formats keys in uppercase and apply default formatting
func WithDefaultUpperCaseFormatter() KeyFormatterFunc {
f := WithDefaultFormatter()
return func(s string, level int) string {
return strings.ToUpper(f(s, level))
}
}
// WithDefaultFormatter is the default formatter
func WithDefaultFormatter() KeyFormatterFunc {
return func(s string, level int) string {
s = strings.Replace(s, " ", "_", -1)
s = strings.Replace(s, "/", "_", -1)
s = strings.Replace(s, ":", "_", -1)
return s
}
}
// NoFormatter doesn't do anything, so to be sure to avoid keys formatting, use only this formatter
func NoFormatter() KeyFormatterFunc {
return func(s string, level int) string {
return s
}
}
func valueFromInterface(i interface{}) reflect.Value {
var f reflect.Value
if reflect.ValueOf(i).Kind() == reflect.Ptr {
f = reflect.ValueOf(i).Elem()
} else {
f = reflect.ValueOf(i)
}
if f.Kind() == reflect.Interface {
if reflect.ValueOf(f.Interface()).Kind() == reflect.Ptr {
f = reflect.ValueOf(f.Interface()).Elem()
} else {
f = reflect.ValueOf(f.Interface())
}
}
return f
}
func validAndNotEmpty(v reflect.Value) bool {
if v.IsValid() && v.CanInterface() {
if v.Kind() == reflect.String {
return v.String() != ""
}
return true
}
return false
}
func sliceFormat(s []string, formatters []KeyFormatterFunc) []string {
for i := range s {
s[i] = format(s[i], formatters, i)
}
return s
}
func format(s string, formatters []KeyFormatterFunc, level int) string {
for _, f := range formatters {
s = f(s, level)
}
return s
}