-
Notifications
You must be signed in to change notification settings - Fork 55
/
options.go
212 lines (180 loc) · 5.14 KB
/
options.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package config
import (
"strings"
"dario.cat/mergo"
"github.com/gookit/goutil"
"github.com/mitchellh/mapstructure"
)
// there are some event names for config data changed.
const (
OnSetValue = "set.value"
OnSetData = "set.data"
OnLoadData = "load.data"
OnReloadData = "reload.data"
OnCleanData = "clean.data"
)
// HookFunc on config data changed.
type HookFunc func(event string, c *Config)
// Options config options
type Options struct {
// ParseEnv parse env in string value and default value. like: "${EnvName}" "${EnvName|default}"
ParseEnv bool
// ParseTime parses a duration string to time.Duration
// eg: 10s, 2m
ParseTime bool
// Readonly config is readonly
Readonly bool
// ParseDefault tag on binding data to struct. tag: default
ParseDefault bool
// EnableCache enable config data cache
EnableCache bool
// ParseKey parse key path, allow find value by key path. eg: 'key.sub' will find `map[key]sub`
ParseKey bool
// TagName tag name for binding data to struct
//
// Deprecated: please set tag name by DecoderConfig, or use SetTagName()
TagName string
// Delimiter the delimiter char for split key path, if `FindByPath=true`. default is '.'
Delimiter byte
// DumpFormat default write format
DumpFormat string
// ReadFormat default input format
ReadFormat string
// DecoderConfig setting for binding data to struct. such as: TagName
DecoderConfig *mapstructure.DecoderConfig
// HookFunc on data changed. you can do something...
HookFunc HookFunc
// MergeOptions settings for merge two data
MergeOptions []func(*mergo.Config)
// WatchChange bool
}
// OptionFn option func
type OptionFn func(*Options)
func newDefaultOption() *Options {
return &Options{
ParseKey: true,
TagName: defaultStructTag,
Delimiter: defaultDelimiter,
// for export
DumpFormat: JSON,
ReadFormat: JSON,
// struct decoder config
DecoderConfig: newDefaultDecoderConfig(""),
MergeOptions: []func(*mergo.Config){
mergo.WithOverride,
mergo.WithTypeCheck,
},
}
}
func newDefaultDecoderConfig(tagName string) *mapstructure.DecoderConfig {
if tagName == "" {
tagName = defaultStructTag
}
return &mapstructure.DecoderConfig{
// tag name for binding struct
TagName: tagName,
// will auto convert string to int/uint
WeaklyTypedInput: true,
}
}
// SetTagName for mapping data to struct
func (o *Options) SetTagName(tagName string) {
o.TagName = tagName
o.DecoderConfig.TagName = tagName
}
func (o *Options) shouldAddHookFunc() bool {
return o.ParseTime || o.ParseEnv
}
func (o *Options) makeDecoderConfig() *mapstructure.DecoderConfig {
var bindConf *mapstructure.DecoderConfig
if o.DecoderConfig == nil {
bindConf = newDefaultDecoderConfig(o.TagName)
} else {
// copy new config for each binding.
copyConf := *o.DecoderConfig
bindConf = ©Conf
// compatible with previous settings opts.TagName
if bindConf.TagName == "" {
bindConf.TagName = o.TagName
}
}
// add hook on decode value to struct
if bindConf.DecodeHook == nil && o.shouldAddHookFunc() {
bindConf.DecodeHook = ValDecodeHookFunc(o.ParseEnv, o.ParseTime)
}
return bindConf
}
/*************************************************************
* config setting
*************************************************************/
// WithTagName set tag name for export to struct
func WithTagName(tagName string) func(*Options) {
return func(opts *Options) {
opts.SetTagName(tagName)
}
}
// ParseEnv set parse env value
func ParseEnv(opts *Options) { opts.ParseEnv = true }
// ParseTime set parse time string.
func ParseTime(opts *Options) { opts.ParseTime = true }
// ParseDefault tag value on binding data to struct.
func ParseDefault(opts *Options) { opts.ParseDefault = true }
// Readonly set readonly
func Readonly(opts *Options) { opts.Readonly = true }
// Delimiter set delimiter char
func Delimiter(sep byte) func(*Options) {
return func(opts *Options) {
opts.Delimiter = sep
}
}
// SaveFileOnSet set hook func, will panic on save error
func SaveFileOnSet(fileName string, format string) func(options *Options) {
return func(opts *Options) {
opts.HookFunc = func(event string, c *Config) {
if strings.HasPrefix(event, "set.") {
goutil.PanicErr(c.DumpToFile(fileName, format))
}
}
}
}
// WithHookFunc set hook func
func WithHookFunc(fn HookFunc) func(*Options) {
return func(opts *Options) {
opts.HookFunc = fn
}
}
// EnableCache set readonly
func EnableCache(opts *Options) { opts.EnableCache = true }
// WithOptions with options
func WithOptions(opts ...OptionFn) { dc.WithOptions(opts...) }
// WithOptions apply some options
func (c *Config) WithOptions(opts ...OptionFn) *Config {
if !c.IsEmpty() {
panic("config: Cannot set options after data has been loaded")
}
// apply options
for _, opt := range opts {
opt(c.opts)
}
return c
}
// GetOptions get options
func GetOptions() *Options { return dc.Options() }
// Options get
func (c *Config) Options() *Options {
return c.opts
}
// With apply some options
func (c *Config) With(fn func(c *Config)) *Config {
fn(c)
return c
}
// Readonly disable set data to config.
//
// Usage:
//
// config.LoadFiles(a, b, c)
// config.Readonly()
func (c *Config) Readonly() {
c.opts.Readonly = true
}