-
Notifications
You must be signed in to change notification settings - Fork 0
/
conf.go
62 lines (55 loc) · 1.41 KB
/
conf.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
package xinviper
import (
"github.com/fsnotify/fsnotify"
"github.com/gz4z2b/xinconf/types"
logertypes "github.com/gz4z2b/xinlog/types"
"github.com/spf13/viper"
)
type ViperConf struct {
viper *viper.Viper
watchKeys map[string][]onChangeDeal
logger logertypes.XinLogger
}
type onChangeDeal struct {
dataType any
deal func(after any)
}
func NewViperConf(viper *viper.Viper, logger logertypes.XinLogger) types.XinConf {
return &ViperConf{
viper: viper,
watchKeys: make(map[string][]onChangeDeal),
logger: logger,
}
}
// Unmarshal 对配置解析成对象
func (v *ViperConf) Unmarshal(key string, val any) error {
err := v.viper.UnmarshalKey(key, val)
return err
}
// ConfigOnChange key配置变更时对应的操作
func (v *ViperConf) ConfigKeyOnChange(key string, dataType any, deal func(after any)) {
if _, exist := v.watchKeys[key]; exist {
v.watchKeys[key] = append(v.watchKeys[key], onChangeDeal{
dataType: dataType,
deal: deal,
})
} else {
v.watchKeys[key] = []onChangeDeal{
{
dataType: dataType,
deal: deal,
},
}
}
v.viper.OnConfigChange(func(in fsnotify.Event) {
for key, deals := range v.watchKeys {
for _, deal := range deals {
err := v.viper.UnmarshalKey(key, &deal.dataType)
if err != nil {
v.logger.Error("配置变更会写数据格式错误", logertypes.NewField("err", err))
}
deal.deal(deal.dataType)
}
}
})
}