08397fef by astaxie

Merge pull request #700 from fuxiaohei/develop

code style simplify
2 parents 1858f307 84da1c92
...@@ -214,7 +214,7 @@ func (c *IniConfigContainer) getdata(key string) string { ...@@ -214,7 +214,7 @@ func (c *IniConfigContainer) getdata(key string) string {
214 k = sectionKey[0] 214 k = sectionKey[0]
215 } 215 }
216 if v, ok := c.data[section]; ok { 216 if v, ok := c.data[section]; ok {
217 if vv, ok2 := v[k]; ok2 { 217 if vv, ok := v[k]; ok {
218 return vv 218 return vv
219 } 219 }
220 } 220 }
......
...@@ -29,13 +29,13 @@ func (js *JsonConfig) Parse(filename string) (ConfigContainer, error) { ...@@ -29,13 +29,13 @@ func (js *JsonConfig) Parse(filename string) (ConfigContainer, error) {
29 return nil, err 29 return nil, err
30 } 30 }
31 defer file.Close() 31 defer file.Close()
32 x := &JsonConfigContainer{
33 data: make(map[string]interface{}),
34 }
35 content, err := ioutil.ReadAll(file) 32 content, err := ioutil.ReadAll(file)
36 if err != nil { 33 if err != nil {
37 return nil, err 34 return nil, err
38 } 35 }
36 x := &JsonConfigContainer{
37 data: make(map[string]interface{}),
38 }
39 err = json.Unmarshal(content, &x.data) 39 err = json.Unmarshal(content, &x.data)
40 if err != nil { 40 if err != nil {
41 var wrappingArray []interface{} 41 var wrappingArray []interface{}
...@@ -57,72 +57,61 @@ type JsonConfigContainer struct { ...@@ -57,72 +57,61 @@ type JsonConfigContainer struct {
57 57
58 // Bool returns the boolean value for a given key. 58 // Bool returns the boolean value for a given key.
59 func (c *JsonConfigContainer) Bool(key string) (bool, error) { 59 func (c *JsonConfigContainer) Bool(key string) (bool, error) {
60 val := c.getdata(key) 60 val := c.getData(key)
61 if val != nil { 61 if val != nil {
62 if v, ok := val.(bool); ok { 62 if v, ok := val.(bool); ok {
63 return v, nil 63 return v, nil
64 } else {
65 return false, errors.New("not bool value")
66 } 64 }
67 } else { 65 return false, errors.New("not bool value")
68 return false, errors.New("not exist key:" + key)
69 } 66 }
67 return false, errors.New("not exist key:" + key)
70 } 68 }
71 69
72 // Int returns the integer value for a given key. 70 // Int returns the integer value for a given key.
73 func (c *JsonConfigContainer) Int(key string) (int, error) { 71 func (c *JsonConfigContainer) Int(key string) (int, error) {
74 val := c.getdata(key) 72 val := c.getData(key)
75 if val != nil { 73 if val != nil {
76 if v, ok := val.(float64); ok { 74 if v, ok := val.(float64); ok {
77 return int(v), nil 75 return int(v), nil
78 } else {
79 return 0, errors.New("not int value")
80 } 76 }
81 } else { 77 return 0, errors.New("not int value")
82 return 0, errors.New("not exist key:" + key)
83 } 78 }
79 return 0, errors.New("not exist key:" + key)
84 } 80 }
85 81
86 // Int64 returns the int64 value for a given key. 82 // Int64 returns the int64 value for a given key.
87 func (c *JsonConfigContainer) Int64(key string) (int64, error) { 83 func (c *JsonConfigContainer) Int64(key string) (int64, error) {
88 val := c.getdata(key) 84 val := c.getData(key)
89 if val != nil { 85 if val != nil {
90 if v, ok := val.(float64); ok { 86 if v, ok := val.(float64); ok {
91 return int64(v), nil 87 return int64(v), nil
92 } else {
93 return 0, errors.New("not int64 value")
94 } 88 }
95 } else { 89 return 0, errors.New("not int64 value")
96 return 0, errors.New("not exist key:" + key)
97 } 90 }
91 return 0, errors.New("not exist key:" + key)
98 } 92 }
99 93
100 // Float returns the float value for a given key. 94 // Float returns the float value for a given key.
101 func (c *JsonConfigContainer) Float(key string) (float64, error) { 95 func (c *JsonConfigContainer) Float(key string) (float64, error) {
102 val := c.getdata(key) 96 val := c.getData(key)
103 if val != nil { 97 if val != nil {
104 if v, ok := val.(float64); ok { 98 if v, ok := val.(float64); ok {
105 return v, nil 99 return v, nil
106 } else {
107 return 0.0, errors.New("not float64 value")
108 } 100 }
109 } else { 101 return 0.0, errors.New("not float64 value")
110 return 0.0, errors.New("not exist key:" + key)
111 } 102 }
103 return 0.0, errors.New("not exist key:" + key)
112 } 104 }
113 105
114 // String returns the string value for a given key. 106 // String returns the string value for a given key.
115 func (c *JsonConfigContainer) String(key string) string { 107 func (c *JsonConfigContainer) String(key string) string {
116 val := c.getdata(key) 108 val := c.getData(key)
117 if val != nil { 109 if val != nil {
118 if v, ok := val.(string); ok { 110 if v, ok := val.(string); ok {
119 return v 111 return v
120 } else {
121 return ""
122 } 112 }
123 } else {
124 return ""
125 } 113 }
114 return ""
126 } 115 }
127 116
128 // Strings returns the []string value for a given key. 117 // Strings returns the []string value for a given key.
...@@ -140,39 +129,37 @@ func (c *JsonConfigContainer) Set(key, val string) error { ...@@ -140,39 +129,37 @@ func (c *JsonConfigContainer) Set(key, val string) error {
140 129
141 // DIY returns the raw value by a given key. 130 // DIY returns the raw value by a given key.
142 func (c *JsonConfigContainer) DIY(key string) (v interface{}, err error) { 131 func (c *JsonConfigContainer) DIY(key string) (v interface{}, err error) {
143 val := c.getdata(key) 132 val := c.getData(key)
144 if val != nil { 133 if val != nil {
145 return val, nil 134 return val, nil
146 } else {
147 return nil, errors.New("not exist key")
148 } 135 }
136 return nil, errors.New("not exist key")
149 } 137 }
150 138
151 // section.key or key 139 // section.key or key
152 func (c *JsonConfigContainer) getdata(key string) interface{} { 140 func (c *JsonConfigContainer) getData(key string) interface{} {
153 c.RLock() 141 c.RLock()
154 defer c.RUnlock() 142 defer c.RUnlock()
155 if len(key) == 0 { 143 if len(key) == 0 {
156 return nil 144 return nil
157 } 145 }
158 sectionkey := strings.Split(key, "::") 146 sectionKey := strings.Split(key, "::")
159 if len(sectionkey) >= 2 { 147 if len(sectionKey) >= 2 {
160 cruval, ok := c.data[sectionkey[0]] 148 curValue, ok := c.data[sectionKey[0]]
161 if !ok { 149 if !ok {
162 return nil 150 return nil
163 } 151 }
164 for _, key := range sectionkey[1:] { 152 for _, key := range sectionKey[1:] {
165 if v, ok := cruval.(map[string]interface{}); !ok { 153 if v, ok := curValue.(map[string]interface{}); ok {
166 return nil 154 if v2, ok := v[key]; ok {
167 } else if cruval, ok = v[key]; !ok { 155 return v2
168 return nil 156 }
169 } 157 }
170 } 158 }
171 return cruval 159 return nil
172 } else { 160 }
173 if v, ok := c.data[key]; ok { 161 if v, ok := c.data[key]; ok {
174 return v 162 return v
175 }
176 } 163 }
177 return nil 164 return nil
178 } 165 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!