-
Notifications
You must be signed in to change notification settings - Fork 144
/
dump.go
105 lines (91 loc) · 2.81 KB
/
dump.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package venom
import (
"os"
"strings"
"github.com/fsamin/go-dump"
)
var preserveCase string
func init() {
preserveCase = os.Getenv("VENOM_PRESERVE_CASE")
if preserveCase == "" || preserveCase == "AUTO" {
preserveCase = "ON"
}
}
// 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
// TODO venom >= v1.2 update the PreserveCase behaviour
if preserveCase == "ON" {
e.ExtraFields.UseJSONTag = true
e.Formatters = []dump.KeyFormatterFunc{WithFormatterLowerFirstKey()}
} else {
e.Formatters = []dump.KeyFormatterFunc{dump.WithDefaultLowerCaseFormatter()}
}
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
// TODO venom >= v1.2 update the PreserveCase behaviour
if preserveCase == "ON" {
e.ExtraFields.UseJSONTag = true
e.Formatters = []dump.KeyFormatterFunc{WithFormatterLowerFirstKey()}
} else {
e.Formatters = []dump.KeyFormatterFunc{dump.WithDefaultLowerCaseFormatter()}
}
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
// TODO venom >= v1.2 update the PreserveCase behaviour
if preserveCase == "ON" {
e.ExtraFields.UseJSONTag = true
e.Formatters = []dump.KeyFormatterFunc{WithFormatterLowerFirstKey()}
} else {
e.Formatters = []dump.KeyFormatterFunc{dump.WithDefaultLowerCaseFormatter()}
}
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
if preserveCase == "ON" {
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)
}
}