1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-25 22:01:29 +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" "errors"
"io/ioutil" "io/ioutil"
"os" "os"
"strings"
"sync" "sync"
) )
@ -33,43 +34,74 @@ func (js *JsonConfig) Parse(filename string) (ConfigContainer, error) {
type JsonConfigContainer struct { type JsonConfigContainer struct {
data map[string]interface{} data map[string]interface{}
sync.Mutex sync.RWMutex
} }
func (c *JsonConfigContainer) Bool(key string) (bool, error) { func (c *JsonConfigContainer) Bool(key string) (bool, error) {
if v, ok := c.data[key].(bool); ok { val := c.getdata(key)
if val != nil {
if v, ok := val.(bool); ok {
return v, nil return v, nil
} } else {
return false, errors.New("not bool value") return false, errors.New("not bool value")
} }
} else {
return false, errors.New("not exist key:" + key)
}
}
func (c *JsonConfigContainer) Int(key string) (int, error) { func (c *JsonConfigContainer) Int(key string) (int, error) {
if v, ok := c.data[key].(float64); ok { val := c.getdata(key)
if val != nil {
if v, ok := val.(float64); ok {
return int(v), nil return int(v), nil
} } else {
return 0, errors.New("not int value") return 0, errors.New("not int value")
} }
} else {
return 0, errors.New("not exist key:" + key)
}
}
func (c *JsonConfigContainer) Int64(key string) (int64, error) { func (c *JsonConfigContainer) Int64(key string) (int64, error) {
if v, ok := c.data[key].(float64); ok { val := c.getdata(key)
if val != nil {
if v, ok := val.(float64); ok {
return int64(v), nil return int64(v), nil
} } else {
return 0, errors.New("not int64 value") return 0, errors.New("not int64 value")
} }
} else {
return 0, errors.New("not exist key:" + key)
}
}
func (c *JsonConfigContainer) Float(key string) (float64, error) { func (c *JsonConfigContainer) Float(key string) (float64, error) {
if v, ok := c.data[key].(float64); ok { val := c.getdata(key)
if val != nil {
if v, ok := val.(float64); ok {
return v, nil return v, nil
} } else {
return 0.0, errors.New("not float64 value") return 0.0, errors.New("not float64 value")
} }
} else {
return 0.0, errors.New("not exist key:" + key)
}
}
func (c *JsonConfigContainer) String(key string) string { func (c *JsonConfigContainer) String(key string) string {
if v, ok := c.data[key].(string); ok { val := c.getdata(key)
if val != nil {
if v, ok := val.(string); ok {
return v return v
} } else {
return "" return ""
} }
} else {
return ""
}
}
func (c *JsonConfigContainer) Set(key, val string) error { func (c *JsonConfigContainer) Set(key, val string) error {
c.Lock() c.Lock()
@ -79,11 +111,42 @@ func (c *JsonConfigContainer) Set(key, val string) error {
} }
func (c *JsonConfigContainer) DIY(key string) (v interface{}, err error) { func (c *JsonConfigContainer) DIY(key string) (v interface{}, err error) {
if v, ok := c.data[key]; ok { val := c.getdata(key)
return v, nil 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() { func init() {
Register("json", &JsonConfig{}) Register("json", &JsonConfig{})

View File

@ -18,7 +18,12 @@ var jsoncontext = `{
"port": "port", "port": "port",
"database": "database", "database": "database",
"username": "username", "username": "username",
"password": "password" "password": "password",
"conns":{
"maxconnection":12,
"autoconnect":true,
"connectioninfo":"info"
}
} }
}` }`
@ -70,9 +75,19 @@ func TestJson(t *testing.T) {
if jsonconf.String("name") != "astaxie" { if jsonconf.String("name") != "astaxie" {
t.Fatal("get name error") t.Fatal("get name error")
} }
if jsonconf.String("database::host") != "host" {
t.Fatal("get database::host error")
}
if jsonconf.String("database::conns::connectioninfo") != "info" {
t.Fatal("get database::conns::connectioninfo error")
}
if maxconnection, err := jsonconf.Int("database::conns::maxconnection"); err != nil || maxconnection != 12 {
t.Fatal("get database::conns::maxconnection error")
}
if db, err := jsonconf.DIY("database"); err != nil { if db, err := jsonconf.DIY("database"); err != nil {
t.Fatal(err) t.Fatal(err)
} else if m, ok := db.(map[string]interface{}); !ok { } else if m, ok := db.(map[string]interface{}); !ok {
t.Log(db)
t.Fatal("db not map[string]interface{}") t.Fatal("db not map[string]interface{}")
} else { } else {
if m["host"].(string) != "host" { if m["host"].(string) != "host" {