1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-17 16:12:42 +08:00
Files
go-mixed/configure/configure.go
2023-12-13 17:26:23 +08:00

47 lines
1.1 KiB
Go

package configure
import (
"github.com/charlienet/go-mixed/expr"
"github.com/spf13/viper"
)
type NotifyFunc func(Configure) error
type Configure interface {
Load(dataId string, v any, onChanged ...NotifyFunc) error
GetString(string, string) string
GetInt(key string, defaultValue int) int
}
type conf struct {
viper *viper.Viper //
nacos *nacos //
onChangeNotifies map[string][]NotifyFunc // 已经注册的配置变更通知
nacosOptions *NacosOptions //
useNacos bool //
}
func (c *conf) GetString(key string, defaultValue string) string {
if c.viper.IsSet(key) {
return c.viper.GetString(key)
}
return defaultValue
}
func (c *conf) GetInt(key string, defaultValue int) int {
return expr.Ternary(c.viper.IsSet(key), c.viper.GetInt(key), defaultValue)
}
func (c *conf) Load(dataId string, v any, onChanged ...NotifyFunc) error {
if err := c.nacos.Load(dataId, v); err != nil {
return err
}
if len(onChanged) > 0 {
c.onChangeNotifies[dataId] = onChanged
}
return nil
}