mirror of
https://gitee.com/johng/gf
synced 2026-06-07 10:22:11 +08:00
配置管理
This commit is contained in:
@ -8,7 +8,7 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
gDEFAULT_CONFIG_NAME = "config" // 默认的配置管理文件名称
|
||||
gDEFAULT_CONFIG_FILE = "config" // 默认的配置管理文件名称
|
||||
)
|
||||
|
||||
// 配置管理对象
|
||||
@ -26,81 +26,84 @@ func New(path string) *Config {
|
||||
}
|
||||
|
||||
// 判断从哪个配置文件中获取内容
|
||||
func (c *Config) name(names []string) string {
|
||||
name := gDEFAULT_CONFIG_NAME
|
||||
if len(names) > 0 {
|
||||
name = names[0]
|
||||
func (c *Config) file(files []string) string {
|
||||
file := gDEFAULT_CONFIG_FILE
|
||||
if len(files) > 0 {
|
||||
file = files[0]
|
||||
}
|
||||
return name
|
||||
return file
|
||||
}
|
||||
|
||||
// 添加配置文件到配置管理器中,第二个参数为非必须,如果不输入表示添加进入默认的配置名称中
|
||||
func (c *Config) Add(file string, names...string) error {
|
||||
func (c *Config) getJson(files []string) *gjson.Json {
|
||||
file := c.file(files)
|
||||
if r := c.jsons.Get(file); r != nil {
|
||||
return r.(*gjson.Json)
|
||||
}
|
||||
path := c.path + gfile.Separator + file
|
||||
if j, err := gjson.Load(path); err == nil {
|
||||
c.jsons.Set(c.name(names), j)
|
||||
return nil
|
||||
} else {
|
||||
return err
|
||||
c.jsons.Set(file, j)
|
||||
return j
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 获取配置项,当不存在时返回nil
|
||||
func (c *Config) Get(pattern string, names...string) interface{} {
|
||||
if r := c.jsons.Get(c.name(names)); r != nil {
|
||||
return r.(*gjson.Json).Get(pattern)
|
||||
func (c *Config) Get(pattern string, files...string) interface{} {
|
||||
if j := c.getJson(files); j != nil {
|
||||
return j.Get(pattern)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 获得一个键值对关联数组/哈希表,方便操作,不需要自己做类型转换
|
||||
// 注意,如果获取的值不存在,或者类型与json类型不匹配,那么将会返回nil
|
||||
func (c *Config) GetMap(pattern string, names...string) map[string]interface{} {
|
||||
if r := c.jsons.Get(c.name(names)); r != nil {
|
||||
return r.(*gjson.Json).GetMap(pattern)
|
||||
func (c *Config) GetMap(pattern string, files...string) map[string]interface{} {
|
||||
if j := c.getJson(files); j != nil {
|
||||
return j.GetMap(pattern)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 获得一个数组[]interface{},方便操作,不需要自己做类型转换
|
||||
// 注意,如果获取的值不存在,或者类型与json类型不匹配,那么将会返回nil
|
||||
func (c *Config) GetArray(pattern string, names...string) []interface{} {
|
||||
if r := c.jsons.Get(c.name(names)); r != nil {
|
||||
return r.(*gjson.Json).GetArray(pattern)
|
||||
func (c *Config) GetArray(pattern string, files...string) []interface{} {
|
||||
if j := c.getJson(files); j != nil {
|
||||
return j.GetArray(pattern)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 返回指定json中的string
|
||||
func (c *Config) GetString(pattern string, names...string) string {
|
||||
if r := c.jsons.Get(c.name(names)); r != nil {
|
||||
return r.(*gjson.Json).GetString(pattern)
|
||||
func (c *Config) GetString(pattern string, files...string) string {
|
||||
if j := c.getJson(files); j != nil {
|
||||
return j.GetString(pattern)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// 返回指定json中的bool
|
||||
func (c *Config) GetBool(pattern string, names...string) bool {
|
||||
if r := c.jsons.Get(c.name(names)); r != nil {
|
||||
return r.(*gjson.Json).GetBool(pattern)
|
||||
func (c *Config) GetBool(pattern string, files...string) bool {
|
||||
if j := c.getJson(files); j != nil {
|
||||
return j.GetBool(pattern)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// 返回指定json中的float64
|
||||
func (c *Config) GetFloat64(pattern string, names...string) float64 {
|
||||
if r := c.jsons.Get(c.name(names)); r != nil {
|
||||
return r.(*gjson.Json).GetFloat64(pattern)
|
||||
func (c *Config) GetFloat64(pattern string, files...string) float64 {
|
||||
if j := c.getJson(files); j != nil {
|
||||
return j.GetFloat64(pattern)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// 返回指定json中的float64->int
|
||||
func (c *Config) GetInt(pattern string, names...string) int {
|
||||
func (c *Config) GetInt(pattern string, files...string) int {
|
||||
return int(c.GetFloat64(pattern))
|
||||
}
|
||||
|
||||
// 返回指定json中的float64->int64
|
||||
func (c *Config) GetInt64(pattern string, names...string) int64 {
|
||||
func (c *Config) GetInt64(pattern string, files...string) int64 {
|
||||
return int64(c.GetFloat64(pattern))
|
||||
}
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
{
|
||||
"viewpath" : "/home/john/Workspace/Go/GOPATH/src/gitee.com/johng/gf/geg/frame/mvc/view",
|
||||
"database" : {
|
||||
"configpath" : "",
|
||||
"viewpath" : "/home/john/Workspace/Go/GOPATH/src/gitee.com/johng/gf/geg/frame/mvc/view",
|
||||
"database" : {
|
||||
"default" : [
|
||||
"root@127.0.0.1:3306,123456,test,mysql",
|
||||
"root@127.0.0.1:3306,123456,test,mysql",
|
||||
|
||||
Reference in New Issue
Block a user