beego config module json support get data like key:
:key
Showing
2 changed files
with
91 additions
and
13 deletions
| ... | @@ -5,6 +5,7 @@ import ( | ... | @@ -5,6 +5,7 @@ import ( |
| 5 | "errors" | 5 | "errors" |
| 6 | "io/ioutil" | 6 | "io/ioutil" |
| 7 | "os" | 7 | "os" |
| 8 | "strings" | ||
| 8 | "sync" | 9 | "sync" |
| 9 | ) | 10 | ) |
| 10 | 11 | ||
| ... | @@ -33,42 +34,73 @@ func (js *JsonConfig) Parse(filename string) (ConfigContainer, error) { | ... | @@ -33,42 +34,73 @@ func (js *JsonConfig) Parse(filename string) (ConfigContainer, error) { |
| 33 | 34 | ||
| 34 | type JsonConfigContainer struct { | 35 | type JsonConfigContainer struct { |
| 35 | data map[string]interface{} | 36 | data map[string]interface{} |
| 36 | sync.Mutex | 37 | sync.RWMutex |
| 37 | } | 38 | } |
| 38 | 39 | ||
| 39 | func (c *JsonConfigContainer) Bool(key string) (bool, error) { | 40 | func (c *JsonConfigContainer) Bool(key string) (bool, error) { |
| 40 | if v, ok := c.data[key].(bool); ok { | 41 | val := c.getdata(key) |
| 42 | if val != nil { | ||
| 43 | if v, ok := val.(bool); ok { | ||
| 41 | return v, nil | 44 | return v, nil |
| 42 | } | 45 | } else { |
| 43 | return false, errors.New("not bool value") | 46 | return false, errors.New("not bool value") |
| 47 | } | ||
| 48 | } else { | ||
| 49 | return false, errors.New("not exist key:" + key) | ||
| 50 | } | ||
| 51 | |||
| 44 | } | 52 | } |
| 45 | 53 | ||
| 46 | func (c *JsonConfigContainer) Int(key string) (int, error) { | 54 | func (c *JsonConfigContainer) Int(key string) (int, error) { |
| 47 | if v, ok := c.data[key].(float64); ok { | 55 | val := c.getdata(key) |
| 56 | if val != nil { | ||
| 57 | if v, ok := val.(float64); ok { | ||
| 48 | return int(v), nil | 58 | return int(v), nil |
| 49 | } | 59 | } else { |
| 50 | return 0, errors.New("not int value") | 60 | return 0, errors.New("not int value") |
| 61 | } | ||
| 62 | } else { | ||
| 63 | return 0, errors.New("not exist key:" + key) | ||
| 64 | } | ||
| 51 | } | 65 | } |
| 52 | 66 | ||
| 53 | func (c *JsonConfigContainer) Int64(key string) (int64, error) { | 67 | func (c *JsonConfigContainer) Int64(key string) (int64, error) { |
| 54 | if v, ok := c.data[key].(float64); ok { | 68 | val := c.getdata(key) |
| 69 | if val != nil { | ||
| 70 | if v, ok := val.(float64); ok { | ||
| 55 | return int64(v), nil | 71 | return int64(v), nil |
| 56 | } | 72 | } else { |
| 57 | return 0, errors.New("not int64 value") | 73 | return 0, errors.New("not int64 value") |
| 74 | } | ||
| 75 | } else { | ||
| 76 | return 0, errors.New("not exist key:" + key) | ||
| 77 | } | ||
| 58 | } | 78 | } |
| 59 | 79 | ||
| 60 | func (c *JsonConfigContainer) Float(key string) (float64, error) { | 80 | func (c *JsonConfigContainer) Float(key string) (float64, error) { |
| 61 | if v, ok := c.data[key].(float64); ok { | 81 | val := c.getdata(key) |
| 82 | if val != nil { | ||
| 83 | if v, ok := val.(float64); ok { | ||
| 62 | return v, nil | 84 | return v, nil |
| 63 | } | 85 | } else { |
| 64 | return 0.0, errors.New("not float64 value") | 86 | return 0.0, errors.New("not float64 value") |
| 87 | } | ||
| 88 | } else { | ||
| 89 | return 0.0, errors.New("not exist key:" + key) | ||
| 90 | } | ||
| 65 | } | 91 | } |
| 66 | 92 | ||
| 67 | func (c *JsonConfigContainer) String(key string) string { | 93 | func (c *JsonConfigContainer) String(key string) string { |
| 68 | if v, ok := c.data[key].(string); ok { | 94 | val := c.getdata(key) |
| 95 | if val != nil { | ||
| 96 | if v, ok := val.(string); ok { | ||
| 69 | return v | 97 | return v |
| 98 | } else { | ||
| 99 | return "" | ||
| 70 | } | 100 | } |
| 101 | } else { | ||
| 71 | return "" | 102 | return "" |
| 103 | } | ||
| 72 | } | 104 | } |
| 73 | 105 | ||
| 74 | func (c *JsonConfigContainer) Set(key, val string) error { | 106 | func (c *JsonConfigContainer) Set(key, val string) error { |
| ... | @@ -79,10 +111,41 @@ func (c *JsonConfigContainer) Set(key, val string) error { | ... | @@ -79,10 +111,41 @@ func (c *JsonConfigContainer) Set(key, val string) error { |
| 79 | } | 111 | } |
| 80 | 112 | ||
| 81 | func (c *JsonConfigContainer) DIY(key string) (v interface{}, err error) { | 113 | func (c *JsonConfigContainer) DIY(key string) (v interface{}, err error) { |
| 114 | val := c.getdata(key) | ||
| 115 | if val != nil { | ||
| 116 | return val, nil | ||
| 117 | } else { | ||
| 118 | return nil, errors.New("not exist key") | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | //section.key or key | ||
| 123 | func (c *JsonConfigContainer) getdata(key string) interface{} { | ||
| 124 | c.RLock() | ||
| 125 | defer c.RUnlock() | ||
| 126 | if len(key) == 0 { | ||
| 127 | return nil | ||
| 128 | } | ||
| 129 | sectionkey := strings.Split(key, "::") | ||
| 130 | if len(sectionkey) >= 2 { | ||
| 131 | cruval, ok := c.data[sectionkey[0]] | ||
| 132 | if !ok { | ||
| 133 | return nil | ||
| 134 | } | ||
| 135 | for _, key := range sectionkey[1:] { | ||
| 136 | if v, ok := cruval.(map[string]interface{}); !ok { | ||
| 137 | return nil | ||
| 138 | } else if cruval, ok = v[key]; !ok { | ||
| 139 | return nil | ||
| 140 | } | ||
| 141 | } | ||
| 142 | return cruval | ||
| 143 | } else { | ||
| 82 | if v, ok := c.data[key]; ok { | 144 | if v, ok := c.data[key]; ok { |
| 83 | return v, nil | 145 | return v |
| 84 | } | 146 | } |
| 85 | return nil, errors.New("not exist key") | 147 | } |
| 148 | return nil | ||
| 86 | } | 149 | } |
| 87 | 150 | ||
| 88 | func init() { | 151 | func init() { | ... | ... |
| ... | @@ -18,7 +18,12 @@ var jsoncontext = `{ | ... | @@ -18,7 +18,12 @@ var jsoncontext = `{ |
| 18 | "port": "port", | 18 | "port": "port", |
| 19 | "database": "database", | 19 | "database": "database", |
| 20 | "username": "username", | 20 | "username": "username", |
| 21 | "password": "password" | 21 | "password": "password", |
| 22 | "conns":{ | ||
| 23 | "maxconnection":12, | ||
| 24 | "autoconnect":true, | ||
| 25 | "connectioninfo":"info" | ||
| 26 | } | ||
| 22 | } | 27 | } |
| 23 | }` | 28 | }` |
| 24 | 29 | ||
| ... | @@ -70,9 +75,19 @@ func TestJson(t *testing.T) { | ... | @@ -70,9 +75,19 @@ func TestJson(t *testing.T) { |
| 70 | if jsonconf.String("name") != "astaxie" { | 75 | if jsonconf.String("name") != "astaxie" { |
| 71 | t.Fatal("get name error") | 76 | t.Fatal("get name error") |
| 72 | } | 77 | } |
| 78 | if jsonconf.String("database::host") != "host" { | ||
| 79 | t.Fatal("get database::host error") | ||
| 80 | } | ||
| 81 | if jsonconf.String("database::conns::connectioninfo") != "info" { | ||
| 82 | t.Fatal("get database::conns::connectioninfo error") | ||
| 83 | } | ||
| 84 | if maxconnection, err := jsonconf.Int("database::conns::maxconnection"); err != nil || maxconnection != 12 { | ||
| 85 | t.Fatal("get database::conns::maxconnection error") | ||
| 86 | } | ||
| 73 | if db, err := jsonconf.DIY("database"); err != nil { | 87 | if db, err := jsonconf.DIY("database"); err != nil { |
| 74 | t.Fatal(err) | 88 | t.Fatal(err) |
| 75 | } else if m, ok := db.(map[string]interface{}); !ok { | 89 | } else if m, ok := db.(map[string]interface{}); !ok { |
| 90 | t.Log(db) | ||
| 76 | t.Fatal("db not map[string]interface{}") | 91 | t.Fatal("db not map[string]interface{}") |
| 77 | } else { | 92 | } else { |
| 78 | if m["host"].(string) != "host" { | 93 | if m["host"].(string) != "host" { | ... | ... |
-
Please register or sign in to post a comment