1 Star 0 Fork 0

后端组 / mvc

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
configure.go 3.19 KB
一键复制 编辑 原始数据 按行查看 历史
heweiosc 提交于 2020-04-21 03:34 . add redis servide
package components
import (
"gitee.com/nuokwan_backend_group/mvc/Base"
"github.com/tietang/props/kvs"
"regexp"
"strings"
"time"
)
type ConfigureWrapper struct {
instance kvs.ConfigSource
}
func (c *ConfigureWrapper) Strings(key string) []string {
return c.instance.Strings(key)
}
func (c *ConfigureWrapper) Ints(key string) []int {
return c.instance.Ints(key)
}
func (c *ConfigureWrapper) Float64s(key string) []float64 {
return c.instance.Float64s(key)
}
func (c *ConfigureWrapper) Durations(key string) []time.Duration {
return c.instance.Durations(key)
}
func (c *ConfigureWrapper) Get(key string) (string, error) {
return c.instance.Get(key)
}
func (c *ConfigureWrapper) GetDefault(key string, defaultValue string) string {
return c.instance.GetDefault(key, defaultValue)
}
func (c *ConfigureWrapper) GetInt(key string) (int, error) {
return c.instance.GetInt(key)
}
func (c *ConfigureWrapper) GetIntDefault(key string, defaultValue int) int {
return c.instance.GetIntDefault(key, defaultValue)
}
func (c *ConfigureWrapper) GetDuration(key string) (time.Duration, error) {
return c.instance.GetDuration(key)
}
func (c *ConfigureWrapper) GetDurationDefault(key string, defaultValue time.Duration) time.Duration {
return c.instance.GetDurationDefault(key, defaultValue)
}
func (c *ConfigureWrapper) GetBool(key string) (bool, error) {
return c.instance.GetBool(key)
}
func (c *ConfigureWrapper) GetBoolDefault(key string, defaultValue bool) bool {
return c.instance.GetBoolDefault(key, defaultValue)
}
func (c *ConfigureWrapper) GetFloat64(key string) (float64, error) {
return c.instance.GetFloat64(key)
}
func (c *ConfigureWrapper) GetFloat64Default(key string, defaultValue float64) float64 {
return c.instance.GetFloat64Default(key, defaultValue)
}
func (c *ConfigureWrapper) Set(key string, value string) {
c.instance.Set(key, value)
}
func (c *ConfigureWrapper) SetAll(data map[string]string) {
c.instance.SetAll(data)
}
func (c *ConfigureWrapper) Keys() []string {
return c.instance.Keys()
}
func (c *ConfigureWrapper) Unmarshal(t interface{}) error {
return c.instance.Unmarshal(t)
}
func (c *ConfigureWrapper) Create() interface{} {
return kvs.NewProperties()
}
func (c *ConfigureWrapper) Prefix(prefix string, withOutPrefix ...bool) Base.ConfigureFace {
var (
withOutPrefixBool = true
conf = c.Create().(kvs.ConfigSource)
reg, _ = regexp.Compile(`^` + prefix + `\.`)
face = Base.NewConfigure(NewConfigure(conf))
)
if len(withOutPrefix) > 0 {
withOutPrefixBool = withOutPrefix[0]
}
for _, key := range c.Keys() {
if !reg.MatchString(key) {
continue
}
if v, err := c.Get(key); err == nil {
if withOutPrefixBool {
if !strings.Contains(key, ".") {
key = strings.Replace(key, prefix, "", 1)
} else {
key = strings.Replace(key, prefix+".", "", 1)
}
}
// fmt.Println(prefix, key, v)
face.Set(key, v)
}
}
return face
}
func NewConfigure(instance ...kvs.ConfigSource) Base.IConfiguration {
if len(instance) == 0 {
return &ConfigureWrapper{instance: kvs.NewProperties()}
}
return &ConfigureWrapper{instance: instance[0]}
}
1
https://gitee.com/nuokwan_backend_group/mvc.git
git@gitee.com:nuokwan_backend_group/mvc.git
nuokwan_backend_group
mvc
mvc
1bf86c47ef7b

搜索帮助

53164aa7 5694891 3bd8fe86 5694891