-
Notifications
You must be signed in to change notification settings - Fork 34
/
mapper_setting.go
104 lines (91 loc) · 2.48 KB
/
mapper_setting.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
package mapper
// Option 用于设置 Config 结构体的字段。
type Option func(*Setting)
type Setting struct {
EnabledTypeChecking bool
EnabledMapperStructField bool
EnabledAutoTypeConvert bool
EnabledMapperTag bool
EnabledJsonTag bool
EnabledCustomTag bool
CustomTagName string
// in the version < 0.7.8, we use field name as the key when mapping structs if field tag is "-"
// from 0.7.8, we add switch enableIgnoreFieldTag which is false in default
// if caller enable this flag, the field will be ignored in the mapping process
EnableFieldIgnoreTag bool
}
// getDefaultSetting return default mapper setting
// Use default value:
//
// EnabledTypeChecking: false,
// EnabledMapperStructField: true,
// EnabledAutoTypeConvert: true,
// EnabledMapperTag: true,
// EnabledJsonTag: true,
// EnabledCustomTag: false,
// EnableFieldIgnoreTag: false
func getDefaultSetting() *Setting {
return &Setting{
EnabledTypeChecking: false,
EnabledMapperStructField: true,
EnabledAutoTypeConvert: true,
EnabledMapperTag: true,
EnabledJsonTag: true,
EnabledCustomTag: false,
EnableFieldIgnoreTag: false, // 保留老版本默认行为:对于tag = “-”的字段使用FieldName
}
}
// NewSetting create new setting with multi option
func NewSetting(opts ...Option) *Setting {
cfg := getDefaultSetting()
for _, opt := range opts {
opt(cfg)
}
return cfg
}
// CTypeChecking set EnabledTypeChecking value
//
// Default value: false
func CTypeChecking(isEnabled bool) Option {
return func(c *Setting) {
c.EnabledTypeChecking = isEnabled
}
}
// CMapperTag set EnabledMapperTag value
//
// Default value: true
func CMapperTag(isEnabled bool) Option {
return func(c *Setting) {
c.EnabledMapperTag = isEnabled
}
}
func CJsonTag(isEnabled bool) Option {
return func(c *Setting) {
c.EnabledJsonTag = isEnabled
}
}
func CAutoTypeConvert(isEnabled bool) Option {
return func(c *Setting) {
c.EnabledAutoTypeConvert = isEnabled
}
}
func CMapperStructField(isEnabled bool) Option {
return func(c *Setting) {
c.EnabledMapperStructField = isEnabled
}
}
func CCustomTag(isEnabled bool) Option {
return func(c *Setting) {
c.EnabledCustomTag = isEnabled
}
}
func CFieldIgnoreTag(isEnabled bool) Option {
return func(c *Setting) {
c.EnableFieldIgnoreTag = isEnabled
}
}
func CCustomTagName(tagName string) Option {
return func(c *Setting) {
c.CustomTagName = tagName
}
}