e52386b5 by fuxiaohei

code style simplify

1 parent 024817aa
...@@ -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{}
...@@ -61,12 +61,10 @@ func (c *JsonConfigContainer) Bool(key string) (bool, error) { ...@@ -61,12 +61,10 @@ func (c *JsonConfigContainer) Bool(key string) (bool, error) {
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.
...@@ -75,12 +73,10 @@ func (c *JsonConfigContainer) Int(key string) (int, error) { ...@@ -75,12 +73,10 @@ func (c *JsonConfigContainer) Int(key string) (int, error) {
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.
...@@ -89,12 +85,10 @@ func (c *JsonConfigContainer) Int64(key string) (int64, error) { ...@@ -89,12 +85,10 @@ func (c *JsonConfigContainer) Int64(key string) (int64, error) {
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.
...@@ -103,12 +97,10 @@ func (c *JsonConfigContainer) Float(key string) (float64, error) { ...@@ -103,12 +97,10 @@ func (c *JsonConfigContainer) Float(key string) (float64, error) {
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.
...@@ -117,12 +109,9 @@ func (c *JsonConfigContainer) String(key string) string { ...@@ -117,12 +109,9 @@ func (c *JsonConfigContainer) String(key string) string {
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.
...@@ -143,9 +132,8 @@ func (c *JsonConfigContainer) DIY(key string) (v interface{}, err error) { ...@@ -143,9 +132,8 @@ 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
...@@ -155,24 +143,21 @@ func (c *JsonConfigContainer) getdata(key string) interface{} { ...@@ -155,24 +143,21 @@ func (c *JsonConfigContainer) getdata(key string) interface{} {
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 if curValue, ok := c.data[sectionKey[0]]; ok {
161 if !ok { 149 for _, key := range sectionKey[1:] {
162 return nil 150 if v, ok := curValue.(map[string]interface{}); ok {
163 } 151 if v2, ok := v[key]; ok {
164 for _, key := range sectionkey[1:] { 152 return v2
165 if v, ok := cruval.(map[string]interface{}); !ok { 153 }
166 return nil 154 }
167 } else if cruval, ok = v[key]; !ok {
168 return nil
169 } 155 }
170 } 156 }
171 return cruval 157 return nil
172 } else { 158 }
173 if v, ok := c.data[key]; ok { 159 if v, ok := c.data[key]; ok {
174 return v 160 return v
175 }
176 } 161 }
177 return nil 162 return nil
178 } 163 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!