代码拉取完成,页面将自动刷新
package common
import (
"fmt"
"strings"
"github.com/spf13/viper"
)
func GetString(conf *viper.Viper, node, key string) string {
nodes := makeNodes(node)
nodes = append(nodes, key)
return conf.GetString(makeKey(nodes))
}
func GetSliceNodeString(conf *viper.Viper, nodes []string, key string) string {
nodes = append(nodes, key)
return conf.GetString(makeKey(nodes))
}
func GetInt(conf *viper.Viper, node, key string) int {
nodes := makeNodes(node)
nodes = append(nodes, key)
return conf.GetInt(makeKey(nodes))
}
func GetSliceNodeInt(conf *viper.Viper, nodes []string, key string) int {
nodes = append(nodes, key)
return conf.GetInt(makeKey(nodes))
}
func GetStringSlice(conf *viper.Viper, node, key string) []string {
nodes := makeNodes(node)
nodes = append(nodes, key)
return conf.GetStringSlice(makeKey(nodes))
}
func GetSliceNodeStringSlice(conf *viper.Viper, nodes []string, key string) []string {
nodes = append(nodes, key)
return conf.GetStringSlice(makeKey(nodes))
}
func GetBool(conf *viper.Viper, node, key string) bool {
nodes := makeNodes(node)
nodes = append(nodes, key)
return conf.GetBool(makeKey(nodes))
}
func GetSliceNodeBool(conf *viper.Viper, nodes []string, key string) bool {
nodes = append(nodes, key)
return conf.GetBool(makeKey(nodes))
}
func makeKey(keys []string) string {
k := make([]string, 0, len(keys))
for i := range keys {
if len(keys[i]) != 0 {
k = append(k, keys[i])
}
}
return strings.Join(k, ".")
}
func makeNodes(node string) []string {
nodes := make([]string, 0)
if _n := strings.Split(node, "."); len(_n) >= 1 {
nodes = append(nodes, _n...)
} else {
nodes = append(nodes, node)
}
return nodes
}
func InitConfig(configId, configPath string) (*viper.Viper, error) {
conf := viper.New()
conf.SetConfigType("toml")
conf.SetConfigName(strings.ToLower(configId))
conf.AddConfigPath(configPath)
err := conf.ReadInConfig()
if err != nil {
return nil, fmt.Errorf("initialize config error, ConfigId: %s, Error:%w", configId, err)
}
return conf, nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。