Commit c372328f authored by fuxiaohei's avatar fuxiaohei

code style simplify

parent a6ced644
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
// @license http://github.com/astaxie/beego/blob/master/LICENSE // @license http://github.com/astaxie/beego/blob/master/LICENSE
// //
// @authors astaxie // @authors astaxie
package config package yaml
import ( import (
"bytes" "bytes"
...@@ -24,39 +24,36 @@ import ( ...@@ -24,39 +24,36 @@ import (
) )
// YAMLConfig is a yaml config parser and implements Config interface. // 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. // Parse returns a ConfigContainer with parsed yaml config map.
func (yaml *YAMLConfig) Parse(filename string) (config.ConfigContainer, error) { func (yaml *YAMLConfig) Parse(filename string) (y config.ConfigContainer, err error) {
y := &YAMLConfigContainer{
data: make(map[string]interface{}),
}
cnf, err := ReadYmlReader(filename) cnf, err := ReadYmlReader(filename)
if err != nil { if err != nil {
return nil, err return
} }
y.data = cnf y = &YAMLConfigContainer{
return y, nil data: cnf,
}
return
} }
// Read yaml file to map. // Read yaml file to map.
// if json like, use json package, unless goyaml2 package. // if json like, use json package, unless goyaml2 package.
func ReadYmlReader(path string) (cnf map[string]interface{}, err error) { func ReadYmlReader(path string) (cnf map[string]interface{}, err error) {
err = nil
f, err := os.Open(path) f, err := os.Open(path)
if err != nil { if err != nil {
return return
} }
defer f.Close() defer f.Close()
err = nil
buf, err := ioutil.ReadAll(f) buf, err := ioutil.ReadAll(f)
if err != nil || len(buf) < 3 { if err != nil || len(buf) < 3 {
return return
} }
if string(buf[0:1]) == "{" { 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) err = json.Unmarshal(buf, &cnf)
if err == nil { if err == nil {
log.Println("It is Json Map") 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) {
} }
} }
_map, _err := goyaml2.Read(bytes.NewBuffer(buf)) data, err := goyaml2.Read(bytes.NewBuffer(buf))
if _err != nil { if err != nil {
log.Println("Goyaml2 ERR>", string(buf), _err) log.Println("Goyaml2 ERR>", string(buf), err)
//err = goyaml.Unmarshal(buf, &cnf)
err = _err
return return
} }
if _map == nil {
if data == nil {
log.Println("Goyaml2 output nil? Pls report bug\n" + string(buf)) 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 { if !ok {
log.Println("Not a Map? >> ", string(buf), _map) log.Println("Not a Map? >> ", string(buf), data)
cnf = nil cnf = nil
} }
return return
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
// @license http://github.com/astaxie/beego/blob/master/LICENSE // @license http://github.com/astaxie/beego/blob/master/LICENSE
// //
// @authors astaxie // @authors astaxie
package config package yaml
import ( import (
"os" "os"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment