1
0
mirror of https://github.com/astaxie/beego.git synced 2025-07-06 02:10:18 +00:00

beego config module json support get data like key:🔑:key

This commit is contained in:
astaxie
2013-12-10 18:09:58 +08:00
parent 34ba7a8e6c
commit 3a0b2e3b95
2 changed files with 98 additions and 20 deletions

View File

@ -5,6 +5,7 @@ import (
"errors"
"io/ioutil"
"os"
"strings"
"sync"
)
@ -33,42 +34,73 @@ func (js *JsonConfig) Parse(filename string) (ConfigContainer, error) {
type JsonConfigContainer struct {
data map[string]interface{}
sync.Mutex
sync.RWMutex
}
func (c *JsonConfigContainer) Bool(key string) (bool, error) {
if v, ok := c.data[key].(bool); ok {
return v, nil
val := c.getdata(key)
if val != nil {
if v, ok := val.(bool); ok {
return v, nil
} else {
return false, errors.New("not bool value")
}
} else {
return false, errors.New("not exist key:" + key)
}
return false, errors.New("not bool value")
}
func (c *JsonConfigContainer) Int(key string) (int, error) {
if v, ok := c.data[key].(float64); ok {
return int(v), nil
val := c.getdata(key)
if val != nil {
if v, ok := val.(float64); ok {
return int(v), nil
} else {
return 0, errors.New("not int value")
}
} else {
return 0, errors.New("not exist key:" + key)
}
return 0, errors.New("not int value")
}
func (c *JsonConfigContainer) Int64(key string) (int64, error) {
if v, ok := c.data[key].(float64); ok {
return int64(v), nil
val := c.getdata(key)
if val != nil {
if v, ok := val.(float64); ok {
return int64(v), nil
} else {
return 0, errors.New("not int64 value")
}
} else {
return 0, errors.New("not exist key:" + key)
}
return 0, errors.New("not int64 value")
}
func (c *JsonConfigContainer) Float(key string) (float64, error) {
if v, ok := c.data[key].(float64); ok {
return v, nil
val := c.getdata(key)
if val != nil {
if v, ok := val.(float64); ok {
return v, nil
} else {
return 0.0, errors.New("not float64 value")
}
} else {
return 0.0, errors.New("not exist key:" + key)
}
return 0.0, errors.New("not float64 value")
}
func (c *JsonConfigContainer) String(key string) string {
if v, ok := c.data[key].(string); ok {
return v
val := c.getdata(key)
if val != nil {
if v, ok := val.(string); ok {
return v
} else {
return ""
}
} else {
return ""
}
return ""
}
func (c *JsonConfigContainer) Set(key, val string) error {
@ -79,10 +111,41 @@ func (c *JsonConfigContainer) Set(key, val string) error {
}
func (c *JsonConfigContainer) DIY(key string) (v interface{}, err error) {
if v, ok := c.data[key]; ok {
return v, nil
val := c.getdata(key)
if val != nil {
return val, nil
} else {
return nil, errors.New("not exist key")
}
return nil, errors.New("not exist key")
}
//section.key or key
func (c *JsonConfigContainer) getdata(key string) interface{} {
c.RLock()
defer c.RUnlock()
if len(key) == 0 {
return nil
}
sectionkey := strings.Split(key, "::")
if len(sectionkey) >= 2 {
cruval, ok := c.data[sectionkey[0]]
if !ok {
return nil
}
for _, key := range sectionkey[1:] {
if v, ok := cruval.(map[string]interface{}); !ok {
return nil
} else if cruval, ok = v[key]; !ok {
return nil
}
}
return cruval
} else {
if v, ok := c.data[key]; ok {
return v
}
}
return nil
}
func init() {