Merge pull request #701 from fuxiaohei/develop
code style simplify
Showing
5 changed files
with
30 additions
and
33 deletions
| ... | @@ -151,12 +151,12 @@ func (c *JsonConfigContainer) getData(key string) interface{} { | ... | @@ -151,12 +151,12 @@ func (c *JsonConfigContainer) getData(key string) interface{} { |
| 151 | } | 151 | } |
| 152 | for _, key := range sectionKey[1:] { | 152 | for _, key := range sectionKey[1:] { |
| 153 | if v, ok := curValue.(map[string]interface{}); ok { | 153 | if v, ok := curValue.(map[string]interface{}); ok { |
| 154 | if v2, ok := v[key]; ok { | 154 | if curValue, ok = v[key]; !ok { |
| 155 | return v2 | 155 | return nil |
| 156 | } | 156 | } |
| 157 | } | 157 | } |
| 158 | } | 158 | } |
| 159 | return nil | 159 | return curValue |
| 160 | } | 160 | } |
| 161 | if v, ok := c.data[key]; ok { | 161 | if v, ok := c.data[key]; ok { |
| 162 | return v | 162 | return v | ... | ... |
| ... | @@ -7,7 +7,7 @@ | ... | @@ -7,7 +7,7 @@ |
| 7 | // @license http://github.com/astaxie/beego/blob/master/LICENSE | 7 | // @license http://github.com/astaxie/beego/blob/master/LICENSE |
| 8 | // | 8 | // |
| 9 | // @authors astaxie | 9 | // @authors astaxie |
| 10 | package config | 10 | package xml |
| 11 | 11 | ||
| 12 | import ( | 12 | import ( |
| 13 | "errors" | 13 | "errors" |
| ... | @@ -24,27 +24,27 @@ import ( | ... | @@ -24,27 +24,27 @@ import ( |
| 24 | // XmlConfig is a xml config parser and implements Config interface. | 24 | // XmlConfig is a xml config parser and implements Config interface. |
| 25 | // xml configurations should be included in <config></config> tag. | 25 | // xml configurations should be included in <config></config> tag. |
| 26 | // only support key/value pair as <key>value</key> as each item. | 26 | // only support key/value pair as <key>value</key> as each item. |
| 27 | type XMLConfig struct { | 27 | type XMLConfig struct{} |
| 28 | } | ||
| 29 | 28 | ||
| 30 | // Parse returns a ConfigContainer with parsed xml config map. | 29 | // Parse returns a ConfigContainer with parsed xml config map. |
| 31 | func (xmls *XMLConfig) Parse(filename string) (config.ConfigContainer, error) { | 30 | func (xc *XMLConfig) Parse(filename string) (config.ConfigContainer, error) { |
| 32 | file, err := os.Open(filename) | 31 | file, err := os.Open(filename) |
| 33 | if err != nil { | 32 | if err != nil { |
| 34 | return nil, err | 33 | return nil, err |
| 35 | } | 34 | } |
| 36 | defer file.Close() | 35 | defer file.Close() |
| 37 | x := &XMLConfigContainer{ | 36 | |
| 38 | data: make(map[string]interface{}), | 37 | x := &XMLConfigContainer{data: make(map[string]interface{})} |
| 39 | } | ||
| 40 | content, err := ioutil.ReadAll(file) | 38 | content, err := ioutil.ReadAll(file) |
| 41 | if err != nil { | 39 | if err != nil { |
| 42 | return nil, err | 40 | return nil, err |
| 43 | } | 41 | } |
| 42 | |||
| 44 | d, err := x2j.DocToMap(string(content)) | 43 | d, err := x2j.DocToMap(string(content)) |
| 45 | if err != nil { | 44 | if err != nil { |
| 46 | return nil, err | 45 | return nil, err |
| 47 | } | 46 | } |
| 47 | |||
| 48 | x.data = d["config"].(map[string]interface{}) | 48 | x.data = d["config"].(map[string]interface{}) |
| 49 | return x, nil | 49 | return x, nil |
| 50 | } | 50 | } | ... | ... |
| ... | @@ -7,7 +7,7 @@ | ... | @@ -7,7 +7,7 @@ |
| 7 | // @license http://github.com/astaxie/beego/blob/master/LICENSE | 7 | // @license http://github.com/astaxie/beego/blob/master/LICENSE |
| 8 | // | 8 | // |
| 9 | // @authors astaxie | 9 | // @authors astaxie |
| 10 | package config | 10 | package xml |
| 11 | 11 | ||
| 12 | import ( | 12 | import ( |
| 13 | "os" | 13 | "os" | ... | ... |
| ... | @@ -7,7 +7,7 @@ | ... | @@ -7,7 +7,7 @@ |
| 7 | // @license http://github.com/astaxie/beego/blob/master/LICENSE | 7 | // @license http://github.com/astaxie/beego/blob/master/LICENSE |
| 8 | // | 8 | // |
| 9 | // @authors astaxie | 9 | // @authors astaxie |
| 10 | package config | 10 | package yaml |
| 11 | 11 | ||
| 12 | import ( | 12 | import ( |
| 13 | "bytes" | 13 | "bytes" |
| ... | @@ -24,39 +24,36 @@ import ( | ... | @@ -24,39 +24,36 @@ import ( |
| 24 | ) | 24 | ) |
| 25 | 25 | ||
| 26 | // YAMLConfig is a yaml config parser and implements Config interface. | 26 | // YAMLConfig is a yaml config parser and implements Config interface. |
| 27 | type YAMLConfig struct { | 27 | type YAMLConfig struct{} |
| 28 | } | ||
| 29 | 28 | ||
| 30 | // Parse returns a ConfigContainer with parsed yaml config map. | 29 | // Parse returns a ConfigContainer with parsed yaml config map. |
| 31 | func (yaml *YAMLConfig) Parse(filename string) (config.ConfigContainer, error) { | 30 | func (yaml *YAMLConfig) Parse(filename string) (y config.ConfigContainer, err error) { |
| 32 | y := &YAMLConfigContainer{ | ||
| 33 | data: make(map[string]interface{}), | ||
| 34 | } | ||
| 35 | cnf, err := ReadYmlReader(filename) | 31 | cnf, err := ReadYmlReader(filename) |
| 36 | if err != nil { | 32 | if err != nil { |
| 37 | return nil, err | 33 | return |
| 38 | } | 34 | } |
| 39 | y.data = cnf | 35 | y = &YAMLConfigContainer{ |
| 40 | return y, nil | 36 | data: cnf, |
| 37 | } | ||
| 38 | return | ||
| 41 | } | 39 | } |
| 42 | 40 | ||
| 43 | // Read yaml file to map. | 41 | // Read yaml file to map. |
| 44 | // if json like, use json package, unless goyaml2 package. | 42 | // if json like, use json package, unless goyaml2 package. |
| 45 | func ReadYmlReader(path string) (cnf map[string]interface{}, err error) { | 43 | func ReadYmlReader(path string) (cnf map[string]interface{}, err error) { |
| 46 | err = nil | ||
| 47 | f, err := os.Open(path) | 44 | f, err := os.Open(path) |
| 48 | if err != nil { | 45 | if err != nil { |
| 49 | return | 46 | return |
| 50 | } | 47 | } |
| 51 | defer f.Close() | 48 | defer f.Close() |
| 52 | err = nil | 49 | |
| 53 | buf, err := ioutil.ReadAll(f) | 50 | buf, err := ioutil.ReadAll(f) |
| 54 | if err != nil || len(buf) < 3 { | 51 | if err != nil || len(buf) < 3 { |
| 55 | return | 52 | return |
| 56 | } | 53 | } |
| 57 | 54 | ||
| 58 | if string(buf[0:1]) == "{" { | 55 | if string(buf[0:1]) == "{" { |
| 59 | log.Println("Look lile a Json, try it") | 56 | log.Println("Look like a Json, try json umarshal") |
| 60 | err = json.Unmarshal(buf, &cnf) | 57 | err = json.Unmarshal(buf, &cnf) |
| 61 | if err == nil { | 58 | if err == nil { |
| 62 | log.Println("It is Json Map") | 59 | log.Println("It is Json Map") |
| ... | @@ -64,19 +61,19 @@ func ReadYmlReader(path string) (cnf map[string]interface{}, err error) { | ... | @@ -64,19 +61,19 @@ func ReadYmlReader(path string) (cnf map[string]interface{}, err error) { |
| 64 | } | 61 | } |
| 65 | } | 62 | } |
| 66 | 63 | ||
| 67 | _map, _err := goyaml2.Read(bytes.NewBuffer(buf)) | 64 | data, err := goyaml2.Read(bytes.NewBuffer(buf)) |
| 68 | if _err != nil { | 65 | if err != nil { |
| 69 | log.Println("Goyaml2 ERR>", string(buf), _err) | 66 | log.Println("Goyaml2 ERR>", string(buf), err) |
| 70 | //err = goyaml.Unmarshal(buf, &cnf) | ||
| 71 | err = _err | ||
| 72 | return | 67 | return |
| 73 | } | 68 | } |
| 74 | if _map == nil { | 69 | |
| 70 | if data == nil { | ||
| 75 | log.Println("Goyaml2 output nil? Pls report bug\n" + string(buf)) | 71 | log.Println("Goyaml2 output nil? Pls report bug\n" + string(buf)) |
| 72 | return | ||
| 76 | } | 73 | } |
| 77 | cnf, ok := _map.(map[string]interface{}) | 74 | cnf, ok := data.(map[string]interface{}) |
| 78 | if !ok { | 75 | if !ok { |
| 79 | log.Println("Not a Map? >> ", string(buf), _map) | 76 | log.Println("Not a Map? >> ", string(buf), data) |
| 80 | cnf = nil | 77 | cnf = nil |
| 81 | } | 78 | } |
| 82 | return | 79 | return | ... | ... |
| ... | @@ -7,7 +7,7 @@ | ... | @@ -7,7 +7,7 @@ |
| 7 | // @license http://github.com/astaxie/beego/blob/master/LICENSE | 7 | // @license http://github.com/astaxie/beego/blob/master/LICENSE |
| 8 | // | 8 | // |
| 9 | // @authors astaxie | 9 | // @authors astaxie |
| 10 | package config | 10 | package yaml |
| 11 | 11 | ||
| 12 | import ( | 12 | import ( |
| 13 | "os" | 13 | "os" | ... | ... |
-
Please register or sign in to post a comment