-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdump.go
More file actions
74 lines (66 loc) · 2.09 KB
/
dump.go
File metadata and controls
74 lines (66 loc) · 2.09 KB
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
package venom
import (
"strings"
"github.com/fsamin/go-dump"
)
// Dump dumps v as a map[string]interface{}.
func DumpWithPrefix(va interface{}, prefix string) (map[string]interface{}, error) {
e := dump.NewDefaultEncoder()
e.ExtraFields.Len = true
e.ExtraFields.Type = true
e.ExtraFields.DetailedStruct = true
e.ExtraFields.DetailedMap = true
e.ExtraFields.DetailedArray = true
e.Prefix = prefix
e.ExtraFields.UseJSONTag = true
e.Formatters = []dump.KeyFormatterFunc{WithFormatterLowerFirstKey()}
return e.ToMap(va)
}
// Dump dumps v as a map[string]interface{}.
func Dump(va interface{}) (map[string]interface{}, error) {
e := dump.NewDefaultEncoder()
e.ExtraFields.Len = true
e.ExtraFields.Type = true
e.ExtraFields.DetailedStruct = true
e.ExtraFields.DetailedMap = true
e.ExtraFields.DetailedArray = true
e.ExtraFields.UseJSONTag = true
e.Formatters = []dump.KeyFormatterFunc{WithFormatterLowerFirstKey()}
return e.ToMap(va)
}
// DumpString dumps v as a map[string]string{}, key in lowercase
func DumpString(va interface{}) (map[string]string, error) {
e := dump.NewDefaultEncoder()
e.ExtraFields.Len = true
e.ExtraFields.Type = true
e.ExtraFields.DetailedStruct = true
e.ExtraFields.DetailedMap = true
e.ExtraFields.DetailedArray = true
e.ExtraFields.UseJSONTag = true
e.Formatters = []dump.KeyFormatterFunc{WithFormatterLowerFirstKey()}
return e.ToStringMap(va)
}
// DumpStringPreserveCase dumps v as a map[string]string{}
func DumpStringPreserveCase(va interface{}) (map[string]string, error) {
e := dump.NewDefaultEncoder()
e.ExtraFields.Len = true
e.ExtraFields.Type = true
e.ExtraFields.DetailedStruct = true
e.ExtraFields.DetailedMap = true
e.ExtraFields.DetailedArray = true
e.ExtraFields.UseJSONTag = true
return e.ToStringMap(va)
}
func WithFormatterLowerFirstKey() dump.KeyFormatterFunc {
f := dump.WithDefaultFormatter()
return func(s string, level int) string {
if level == 0 && strings.Contains(s, ".") {
pos := strings.Index(s, ".")
return strings.ToLower(s[0:pos]) + s[pos:]
}
if level == 0 {
return strings.ToLower(f(s, level))
}
return f(s, level)
}
}