c372328f by fuxiaohei

code style simplify

1 parent a6ced644
...@@ -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"
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!