3a0b2e3b by astaxie

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

1 parent 34ba7a8e
...@@ -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)
41 return v, nil 42 if val != nil {
43 if v, ok := val.(bool); ok {
44 return v, nil
45 } else {
46 return false, errors.New("not bool value")
47 }
48 } else {
49 return false, errors.New("not exist key:" + key)
42 } 50 }
43 return false, errors.New("not bool value") 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)
48 return int(v), nil 56 if val != nil {
57 if v, ok := val.(float64); ok {
58 return int(v), nil
59 } else {
60 return 0, errors.New("not int value")
61 }
62 } else {
63 return 0, errors.New("not exist key:" + key)
49 } 64 }
50 return 0, errors.New("not int value")
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)
55 return int64(v), nil 69 if val != nil {
70 if v, ok := val.(float64); ok {
71 return int64(v), nil
72 } else {
73 return 0, errors.New("not int64 value")
74 }
75 } else {
76 return 0, errors.New("not exist key:" + key)
56 } 77 }
57 return 0, errors.New("not int64 value")
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)
62 return v, nil 82 if val != nil {
83 if v, ok := val.(float64); ok {
84 return v, nil
85 } else {
86 return 0.0, errors.New("not float64 value")
87 }
88 } else {
89 return 0.0, errors.New("not exist key:" + key)
63 } 90 }
64 return 0.0, errors.New("not float64 value")
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)
69 return v 95 if val != nil {
96 if v, ok := val.(string); ok {
97 return v
98 } else {
99 return ""
100 }
101 } else {
102 return ""
70 } 103 }
71 return ""
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) {
82 if v, ok := c.data[key]; ok { 114 val := c.getdata(key)
83 return v, nil 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 {
144 if v, ok := c.data[key]; ok {
145 return v
146 }
84 } 147 }
85 return nil, errors.New("not exist key") 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" {
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!