diff --git a/config/json.go b/config/json.go index 12b92e7d..7214a686 100644 --- a/config/json.go +++ b/config/json.go @@ -151,12 +151,12 @@ func (c *JsonConfigContainer) getData(key string) interface{} { } for _, key := range sectionKey[1:] { if v, ok := curValue.(map[string]interface{}); ok { - if v2, ok := v[key]; ok { - return v2 + if curValue, ok = v[key]; !ok { + return nil } } } - return nil + return curValue } if v, ok := c.data[key]; ok { return v diff --git a/config/xml/xml.go b/config/xml/xml.go index fbd15190..5345f69c 100644 --- a/config/xml/xml.go +++ b/config/xml/xml.go @@ -7,7 +7,7 @@ // @license http://github.com/astaxie/beego/blob/master/LICENSE // // @authors astaxie -package config +package xml import ( "errors" @@ -24,27 +24,27 @@ import ( // XmlConfig is a xml config parser and implements Config interface. // xml configurations should be included in tag. // only support key/value pair as value as each item. -type XMLConfig struct { -} +type XMLConfig struct{} // Parse returns a ConfigContainer with parsed xml config map. -func (xmls *XMLConfig) Parse(filename string) (config.ConfigContainer, error) { +func (xc *XMLConfig) Parse(filename string) (config.ConfigContainer, error) { file, err := os.Open(filename) if err != nil { return nil, err } defer file.Close() - x := &XMLConfigContainer{ - data: make(map[string]interface{}), - } + + x := &XMLConfigContainer{data: make(map[string]interface{})} content, err := ioutil.ReadAll(file) if err != nil { return nil, err } + d, err := x2j.DocToMap(string(content)) if err != nil { return nil, err } + x.data = d["config"].(map[string]interface{}) return x, nil } diff --git a/config/xml/xml_test.go b/config/xml/xml_test.go index dac1a527..564d6344 100644 --- a/config/xml/xml_test.go +++ b/config/xml/xml_test.go @@ -7,7 +7,7 @@ // @license http://github.com/astaxie/beego/blob/master/LICENSE // // @authors astaxie -package config +package xml import ( "os" diff --git a/config/yaml/yaml.go b/config/yaml/yaml.go index 5d2d2b84..e948dae2 100644 --- a/config/yaml/yaml.go +++ b/config/yaml/yaml.go @@ -7,7 +7,7 @@ // @license http://github.com/astaxie/beego/blob/master/LICENSE // // @authors astaxie -package config +package yaml import ( "bytes" @@ -24,39 +24,36 @@ import ( ) // YAMLConfig is a yaml config parser and implements Config interface. -type YAMLConfig struct { -} +type YAMLConfig struct{} // Parse returns a ConfigContainer with parsed yaml config map. -func (yaml *YAMLConfig) Parse(filename string) (config.ConfigContainer, error) { - y := &YAMLConfigContainer{ - data: make(map[string]interface{}), - } +func (yaml *YAMLConfig) Parse(filename string) (y config.ConfigContainer, err error) { cnf, err := ReadYmlReader(filename) if err != nil { - return nil, err + return } - y.data = cnf - return y, nil + y = &YAMLConfigContainer{ + data: cnf, + } + return } // Read yaml file to map. // if json like, use json package, unless goyaml2 package. func ReadYmlReader(path string) (cnf map[string]interface{}, err error) { - err = nil f, err := os.Open(path) if err != nil { return } defer f.Close() - err = nil + buf, err := ioutil.ReadAll(f) if err != nil || len(buf) < 3 { return } if string(buf[0:1]) == "{" { - log.Println("Look lile a Json, try it") + log.Println("Look like a Json, try json umarshal") err = json.Unmarshal(buf, &cnf) if err == nil { log.Println("It is Json Map") @@ -64,19 +61,19 @@ func ReadYmlReader(path string) (cnf map[string]interface{}, err error) { } } - _map, _err := goyaml2.Read(bytes.NewBuffer(buf)) - if _err != nil { - log.Println("Goyaml2 ERR>", string(buf), _err) - //err = goyaml.Unmarshal(buf, &cnf) - err = _err + data, err := goyaml2.Read(bytes.NewBuffer(buf)) + if err != nil { + log.Println("Goyaml2 ERR>", string(buf), err) return } - if _map == nil { + + if data == nil { log.Println("Goyaml2 output nil? Pls report bug\n" + string(buf)) + return } - cnf, ok := _map.(map[string]interface{}) + cnf, ok := data.(map[string]interface{}) if !ok { - log.Println("Not a Map? >> ", string(buf), _map) + log.Println("Not a Map? >> ", string(buf), data) cnf = nil } return diff --git a/config/yaml/yaml_test.go b/config/yaml/yaml_test.go index ca066c72..d960f501 100644 --- a/config/yaml/yaml_test.go +++ b/config/yaml/yaml_test.go @@ -7,7 +7,7 @@ // @license http://github.com/astaxie/beego/blob/master/LICENSE // // @authors astaxie -package config +package yaml import ( "os"