From 3a0b2e3b953705e4e21e300602d0b1f5ade2a36e Mon Sep 17 00:00:00 2001 From: astaxie Date: Tue, 10 Dec 2013 18:09:58 +0800 Subject: [PATCH] beego config module json support get data like key::key::key --- config/json.go | 101 +++++++++++++++++++++++++++++++++++--------- config/json_test.go | 17 +++++++- 2 files changed, 98 insertions(+), 20 deletions(-) diff --git a/config/json.go b/config/json.go index ea662df8..b401e1b3 100644 --- a/config/json.go +++ b/config/json.go @@ -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() { diff --git a/config/json_test.go b/config/json_test.go index 4d3b760f..e27f6932 100644 --- a/config/json_test.go +++ b/config/json_test.go @@ -18,7 +18,12 @@ var jsoncontext = `{ "port": "port", "database": "database", "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" { 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 { t.Fatal(err) } else if m, ok := db.(map[string]interface{}); !ok { + t.Log(db) t.Fatal("db not map[string]interface{}") } else { if m["host"].(string) != "host" {