0183608a by 傅小黑

add comments for config package.

1 parent 5b1afcdb
...@@ -4,9 +4,10 @@ import ( ...@@ -4,9 +4,10 @@ import (
4 "fmt" 4 "fmt"
5 ) 5 )
6 6
7 // ConfigContainer defines how to get and set value from configuration raw data.
7 type ConfigContainer interface { 8 type ConfigContainer interface {
8 Set(key, val string) error 9 Set(key, val string) error // support section::key type in given key when using ini type.
9 String(key string) string 10 String(key string) string // support section::key type in key string when using ini and json type; Int,Int64,Bool,Float,DIY are same.
10 Int(key string) (int, error) 11 Int(key string) (int, error)
11 Int64(key string) (int64, error) 12 Int64(key string) (int64, error)
12 Bool(key string) (bool, error) 13 Bool(key string) (bool, error)
...@@ -14,6 +15,7 @@ type ConfigContainer interface { ...@@ -14,6 +15,7 @@ type ConfigContainer interface {
14 DIY(key string) (interface{}, error) 15 DIY(key string) (interface{}, error)
15 } 16 }
16 17
18 // Config is the adapter interface for parsing config file to get raw data to ConfigContainer.
17 type Config interface { 19 type Config interface {
18 Parse(key string) (ConfigContainer, error) 20 Parse(key string) (ConfigContainer, error)
19 } 21 }
...@@ -33,8 +35,8 @@ func Register(name string, adapter Config) { ...@@ -33,8 +35,8 @@ func Register(name string, adapter Config) {
33 adapters[name] = adapter 35 adapters[name] = adapter
34 } 36 }
35 37
36 // adapterNamer is ini/json/xml/yaml 38 // adapterName is ini/json/xml/yaml.
37 // filename is the config file path 39 // filename is the config file path.
38 func NewConfig(adapterName, fileaname string) (ConfigContainer, error) { 40 func NewConfig(adapterName, fileaname string) (ConfigContainer, error) {
39 adapter, ok := adapters[adapterName] 41 adapter, ok := adapters[adapterName]
40 if !ok { 42 if !ok {
......
...@@ -13,21 +13,21 @@ import ( ...@@ -13,21 +13,21 @@ import (
13 ) 13 )
14 14
15 var ( 15 var (
16 DEFAULT_SECTION = "default" 16 DEFAULT_SECTION = "default" // default section means if some ini items not in a section, make them in default section,
17 bNumComment = []byte{'#'} // number sign 17 bNumComment = []byte{'#'} // number signal
18 bSemComment = []byte{';'} // semicolon 18 bSemComment = []byte{';'} // semicolon signal
19 bEmpty = []byte{} 19 bEmpty = []byte{}
20 bEqual = []byte{'='} 20 bEqual = []byte{'='} // equal signal
21 bDQuote = []byte{'"'} 21 bDQuote = []byte{'"'} // quote signal
22 sectionStart = []byte{'['} 22 sectionStart = []byte{'['} // section start signal
23 sectionEnd = []byte{']'} 23 sectionEnd = []byte{']'} // section end signal
24 ) 24 )
25 25
26 // IniConfig implements Config to parse ini file.
26 type IniConfig struct { 27 type IniConfig struct {
27 } 28 }
28 29
29 // ParseFile creates a new Config and parses the file configuration from the 30 // ParseFile creates a new Config and parses the file configuration from the named file.
30 // named file.
31 func (ini *IniConfig) Parse(name string) (ConfigContainer, error) { 31 func (ini *IniConfig) Parse(name string) (ConfigContainer, error) {
32 file, err := os.Open(name) 32 file, err := os.Open(name)
33 if err != nil { 33 if err != nil {
...@@ -106,11 +106,12 @@ func (ini *IniConfig) Parse(name string) (ConfigContainer, error) { ...@@ -106,11 +106,12 @@ func (ini *IniConfig) Parse(name string) (ConfigContainer, error) {
106 return cfg, nil 106 return cfg, nil
107 } 107 }
108 108
109 // A Config represents the configuration. 109 // A Config represents the ini configuration.
110 // When set and get value, support key as section:name type.
110 type IniConfigContainer struct { 111 type IniConfigContainer struct {
111 filename string 112 filename string
112 data map[string]map[string]string //section=> key:val 113 data map[string]map[string]string // section=> key:val
113 sectionComment map[string]string //sction : comment 114 sectionComment map[string]string // section : comment
114 keycomment map[string]string // id: []{comment, key...}; id 1 is for main comment. 115 keycomment map[string]string // id: []{comment, key...}; id 1 is for main comment.
115 sync.RWMutex 116 sync.RWMutex
116 } 117 }
...@@ -127,6 +128,7 @@ func (c *IniConfigContainer) Int(key string) (int, error) { ...@@ -127,6 +128,7 @@ func (c *IniConfigContainer) Int(key string) (int, error) {
127 return strconv.Atoi(c.getdata(key)) 128 return strconv.Atoi(c.getdata(key))
128 } 129 }
129 130
131 // Int64 returns the int64 value for a given key.
130 func (c *IniConfigContainer) Int64(key string) (int64, error) { 132 func (c *IniConfigContainer) Int64(key string) (int64, error) {
131 key = strings.ToLower(key) 133 key = strings.ToLower(key)
132 return strconv.ParseInt(c.getdata(key), 10, 64) 134 return strconv.ParseInt(c.getdata(key), 10, 64)
...@@ -145,6 +147,8 @@ func (c *IniConfigContainer) String(key string) string { ...@@ -145,6 +147,8 @@ func (c *IniConfigContainer) String(key string) string {
145 } 147 }
146 148
147 // WriteValue writes a new value for key. 149 // WriteValue writes a new value for key.
150 // if write to one section, the key need be "section::key".
151 // if the section is not existed, it panics.
148 func (c *IniConfigContainer) Set(key, value string) error { 152 func (c *IniConfigContainer) Set(key, value string) error {
149 c.Lock() 153 c.Lock()
150 defer c.Unlock() 154 defer c.Unlock()
...@@ -166,6 +170,7 @@ func (c *IniConfigContainer) Set(key, value string) error { ...@@ -166,6 +170,7 @@ func (c *IniConfigContainer) Set(key, value string) error {
166 return nil 170 return nil
167 } 171 }
168 172
173 // DIY returns the raw value by a given key.
169 func (c *IniConfigContainer) DIY(key string) (v interface{}, err error) { 174 func (c *IniConfigContainer) DIY(key string) (v interface{}, err error) {
170 key = strings.ToLower(key) 175 key = strings.ToLower(key)
171 if v, ok := c.data[key]; ok { 176 if v, ok := c.data[key]; ok {
...@@ -174,7 +179,7 @@ func (c *IniConfigContainer) DIY(key string) (v interface{}, err error) { ...@@ -174,7 +179,7 @@ func (c *IniConfigContainer) DIY(key string) (v interface{}, err error) {
174 return v, errors.New("key not find") 179 return v, errors.New("key not find")
175 } 180 }
176 181
177 //section.key or key 182 // section.key or key
178 func (c *IniConfigContainer) getdata(key string) string { 183 func (c *IniConfigContainer) getdata(key string) string {
179 c.RLock() 184 c.RLock()
180 defer c.RUnlock() 185 defer c.RUnlock()
......
...@@ -9,9 +9,11 @@ import ( ...@@ -9,9 +9,11 @@ import (
9 "sync" 9 "sync"
10 ) 10 )
11 11
12 // JsonConfig is a json config parser and implements Config interface.
12 type JsonConfig struct { 13 type JsonConfig struct {
13 } 14 }
14 15
16 // Parse returns a ConfigContainer with parsed json config map.
15 func (js *JsonConfig) Parse(filename string) (ConfigContainer, error) { 17 func (js *JsonConfig) Parse(filename string) (ConfigContainer, error) {
16 file, err := os.Open(filename) 18 file, err := os.Open(filename)
17 if err != nil { 19 if err != nil {
...@@ -32,11 +34,14 @@ func (js *JsonConfig) Parse(filename string) (ConfigContainer, error) { ...@@ -32,11 +34,14 @@ func (js *JsonConfig) Parse(filename string) (ConfigContainer, error) {
32 return x, nil 34 return x, nil
33 } 35 }
34 36
37 // A Config represents the json configuration.
38 // Only when get value, support key as section:name type.
35 type JsonConfigContainer struct { 39 type JsonConfigContainer struct {
36 data map[string]interface{} 40 data map[string]interface{}
37 sync.RWMutex 41 sync.RWMutex
38 } 42 }
39 43
44 // Bool returns the boolean value for a given key.
40 func (c *JsonConfigContainer) Bool(key string) (bool, error) { 45 func (c *JsonConfigContainer) Bool(key string) (bool, error) {
41 val := c.getdata(key) 46 val := c.getdata(key)
42 if val != nil { 47 if val != nil {
...@@ -48,9 +53,10 @@ func (c *JsonConfigContainer) Bool(key string) (bool, error) { ...@@ -48,9 +53,10 @@ func (c *JsonConfigContainer) Bool(key string) (bool, error) {
48 } else { 53 } else {
49 return false, errors.New("not exist key:" + key) 54 return false, errors.New("not exist key:" + key)
50 } 55 }
51 56 return false,nil
52 } 57 }
53 58
59 // Int returns the integer value for a given key.
54 func (c *JsonConfigContainer) Int(key string) (int, error) { 60 func (c *JsonConfigContainer) Int(key string) (int, error) {
55 val := c.getdata(key) 61 val := c.getdata(key)
56 if val != nil { 62 if val != nil {
...@@ -62,8 +68,10 @@ func (c *JsonConfigContainer) Int(key string) (int, error) { ...@@ -62,8 +68,10 @@ func (c *JsonConfigContainer) Int(key string) (int, error) {
62 } else { 68 } else {
63 return 0, errors.New("not exist key:" + key) 69 return 0, errors.New("not exist key:" + key)
64 } 70 }
71 return 0,nil
65 } 72 }
66 73
74 // Int64 returns the int64 value for a given key.
67 func (c *JsonConfigContainer) Int64(key string) (int64, error) { 75 func (c *JsonConfigContainer) Int64(key string) (int64, error) {
68 val := c.getdata(key) 76 val := c.getdata(key)
69 if val != nil { 77 if val != nil {
...@@ -75,8 +83,10 @@ func (c *JsonConfigContainer) Int64(key string) (int64, error) { ...@@ -75,8 +83,10 @@ func (c *JsonConfigContainer) Int64(key string) (int64, error) {
75 } else { 83 } else {
76 return 0, errors.New("not exist key:" + key) 84 return 0, errors.New("not exist key:" + key)
77 } 85 }
86 return 0,nil
78 } 87 }
79 88
89 // Float returns the float value for a given key.
80 func (c *JsonConfigContainer) Float(key string) (float64, error) { 90 func (c *JsonConfigContainer) Float(key string) (float64, error) {
81 val := c.getdata(key) 91 val := c.getdata(key)
82 if val != nil { 92 if val != nil {
...@@ -88,8 +98,10 @@ func (c *JsonConfigContainer) Float(key string) (float64, error) { ...@@ -88,8 +98,10 @@ func (c *JsonConfigContainer) Float(key string) (float64, error) {
88 } else { 98 } else {
89 return 0.0, errors.New("not exist key:" + key) 99 return 0.0, errors.New("not exist key:" + key)
90 } 100 }
101 return 0.0,nil
91 } 102 }
92 103
104 // String returns the string value for a given key.
93 func (c *JsonConfigContainer) String(key string) string { 105 func (c *JsonConfigContainer) String(key string) string {
94 val := c.getdata(key) 106 val := c.getdata(key)
95 if val != nil { 107 if val != nil {
...@@ -101,8 +113,10 @@ func (c *JsonConfigContainer) String(key string) string { ...@@ -101,8 +113,10 @@ func (c *JsonConfigContainer) String(key string) string {
101 } else { 113 } else {
102 return "" 114 return ""
103 } 115 }
116 return ""
104 } 117 }
105 118
119 // WriteValue writes a new value for key.
106 func (c *JsonConfigContainer) Set(key, val string) error { 120 func (c *JsonConfigContainer) Set(key, val string) error {
107 c.Lock() 121 c.Lock()
108 defer c.Unlock() 122 defer c.Unlock()
...@@ -110,6 +124,7 @@ func (c *JsonConfigContainer) Set(key, val string) error { ...@@ -110,6 +124,7 @@ func (c *JsonConfigContainer) Set(key, val string) error {
110 return nil 124 return nil
111 } 125 }
112 126
127 // DIY returns the raw value by a given key.
113 func (c *JsonConfigContainer) DIY(key string) (v interface{}, err error) { 128 func (c *JsonConfigContainer) DIY(key string) (v interface{}, err error) {
114 val := c.getdata(key) 129 val := c.getdata(key)
115 if val != nil { 130 if val != nil {
...@@ -117,9 +132,10 @@ func (c *JsonConfigContainer) DIY(key string) (v interface{}, err error) { ...@@ -117,9 +132,10 @@ func (c *JsonConfigContainer) DIY(key string) (v interface{}, err error) {
117 } else { 132 } else {
118 return nil, errors.New("not exist key") 133 return nil, errors.New("not exist key")
119 } 134 }
135 return nil,nil
120 } 136 }
121 137
122 //section.key or key 138 // section.key or key
123 func (c *JsonConfigContainer) getdata(key string) interface{} { 139 func (c *JsonConfigContainer) getdata(key string) interface{} {
124 c.RLock() 140 c.RLock()
125 defer c.RUnlock() 141 defer c.RUnlock()
......
1 //xml parse should incluce in <config></config> tags
2 1
3 package config 2 package config
4 3
...@@ -12,9 +11,13 @@ import ( ...@@ -12,9 +11,13 @@ import (
12 "github.com/beego/x2j" 11 "github.com/beego/x2j"
13 ) 12 )
14 13
14 // XmlConfig is a xml config parser and implements Config interface.
15 // xml configurations should be included in <config></config> tag.
16 // only support key/value pair as <key>value</key> as each item.
15 type XMLConfig struct { 17 type XMLConfig struct {
16 } 18 }
17 19
20 // Parse returns a ConfigContainer with parsed xml config map.
18 func (xmls *XMLConfig) Parse(filename string) (ConfigContainer, error) { 21 func (xmls *XMLConfig) Parse(filename string) (ConfigContainer, error) {
19 file, err := os.Open(filename) 22 file, err := os.Open(filename)
20 if err != nil { 23 if err != nil {
...@@ -36,27 +39,33 @@ func (xmls *XMLConfig) Parse(filename string) (ConfigContainer, error) { ...@@ -36,27 +39,33 @@ func (xmls *XMLConfig) Parse(filename string) (ConfigContainer, error) {
36 return x, nil 39 return x, nil
37 } 40 }
38 41
42 // A Config represents the xml configuration.
39 type XMLConfigContainer struct { 43 type XMLConfigContainer struct {
40 data map[string]interface{} 44 data map[string]interface{}
41 sync.Mutex 45 sync.Mutex
42 } 46 }
43 47
48 // Bool returns the boolean value for a given key.
44 func (c *XMLConfigContainer) Bool(key string) (bool, error) { 49 func (c *XMLConfigContainer) Bool(key string) (bool, error) {
45 return strconv.ParseBool(c.data[key].(string)) 50 return strconv.ParseBool(c.data[key].(string))
46 } 51 }
47 52
53 // Int returns the integer value for a given key.
48 func (c *XMLConfigContainer) Int(key string) (int, error) { 54 func (c *XMLConfigContainer) Int(key string) (int, error) {
49 return strconv.Atoi(c.data[key].(string)) 55 return strconv.Atoi(c.data[key].(string))
50 } 56 }
51 57
58 // Int64 returns the int64 value for a given key.
52 func (c *XMLConfigContainer) Int64(key string) (int64, error) { 59 func (c *XMLConfigContainer) Int64(key string) (int64, error) {
53 return strconv.ParseInt(c.data[key].(string), 10, 64) 60 return strconv.ParseInt(c.data[key].(string), 10, 64)
54 } 61 }
55 62
63 // Float returns the float value for a given key.
56 func (c *XMLConfigContainer) Float(key string) (float64, error) { 64 func (c *XMLConfigContainer) Float(key string) (float64, error) {
57 return strconv.ParseFloat(c.data[key].(string), 64) 65 return strconv.ParseFloat(c.data[key].(string), 64)
58 } 66 }
59 67
68 // String returns the string value for a given key.
60 func (c *XMLConfigContainer) String(key string) string { 69 func (c *XMLConfigContainer) String(key string) string {
61 if v, ok := c.data[key].(string); ok { 70 if v, ok := c.data[key].(string); ok {
62 return v 71 return v
...@@ -64,6 +73,7 @@ func (c *XMLConfigContainer) String(key string) string { ...@@ -64,6 +73,7 @@ func (c *XMLConfigContainer) String(key string) string {
64 return "" 73 return ""
65 } 74 }
66 75
76 // WriteValue writes a new value for key.
67 func (c *XMLConfigContainer) Set(key, val string) error { 77 func (c *XMLConfigContainer) Set(key, val string) error {
68 c.Lock() 78 c.Lock()
69 defer c.Unlock() 79 defer c.Unlock()
...@@ -71,6 +81,7 @@ func (c *XMLConfigContainer) Set(key, val string) error { ...@@ -71,6 +81,7 @@ func (c *XMLConfigContainer) Set(key, val string) error {
71 return nil 81 return nil
72 } 82 }
73 83
84 // DIY returns the raw value by a given key.
74 func (c *XMLConfigContainer) DIY(key string) (v interface{}, err error) { 85 func (c *XMLConfigContainer) DIY(key string) (v interface{}, err error) {
75 if v, ok := c.data[key]; ok { 86 if v, ok := c.data[key]; ok {
76 return v, nil 87 return v, nil
......
...@@ -12,9 +12,11 @@ import ( ...@@ -12,9 +12,11 @@ import (
12 "github.com/beego/goyaml2" 12 "github.com/beego/goyaml2"
13 ) 13 )
14 14
15 // YAMLConfig is a yaml config parser and implements Config interface.
15 type YAMLConfig struct { 16 type YAMLConfig struct {
16 } 17 }
17 18
19 // Parse returns a ConfigContainer with parsed yaml config map.
18 func (yaml *YAMLConfig) Parse(filename string) (ConfigContainer, error) { 20 func (yaml *YAMLConfig) Parse(filename string) (ConfigContainer, error) {
19 y := &YAMLConfigContainer{ 21 y := &YAMLConfigContainer{
20 data: make(map[string]interface{}), 22 data: make(map[string]interface{}),
...@@ -27,7 +29,8 @@ func (yaml *YAMLConfig) Parse(filename string) (ConfigContainer, error) { ...@@ -27,7 +29,8 @@ func (yaml *YAMLConfig) Parse(filename string) (ConfigContainer, error) {
27 return y, nil 29 return y, nil
28 } 30 }
29 31
30 // 从Reader读取YAML 32 // Read yaml file to map.
33 // if json like, use json package, unless goyaml2 package.
31 func ReadYmlReader(path string) (cnf map[string]interface{}, err error) { 34 func ReadYmlReader(path string) (cnf map[string]interface{}, err error) {
32 err = nil 35 err = nil
33 f, err := os.Open(path) 36 f, err := os.Open(path)
...@@ -68,11 +71,13 @@ func ReadYmlReader(path string) (cnf map[string]interface{}, err error) { ...@@ -68,11 +71,13 @@ func ReadYmlReader(path string) (cnf map[string]interface{}, err error) {
68 return 71 return
69 } 72 }
70 73
74 // A Config represents the yaml configuration.
71 type YAMLConfigContainer struct { 75 type YAMLConfigContainer struct {
72 data map[string]interface{} 76 data map[string]interface{}
73 sync.Mutex 77 sync.Mutex
74 } 78 }
75 79
80 // Bool returns the boolean value for a given key.
76 func (c *YAMLConfigContainer) Bool(key string) (bool, error) { 81 func (c *YAMLConfigContainer) Bool(key string) (bool, error) {
77 if v, ok := c.data[key].(bool); ok { 82 if v, ok := c.data[key].(bool); ok {
78 return v, nil 83 return v, nil
...@@ -80,6 +85,7 @@ func (c *YAMLConfigContainer) Bool(key string) (bool, error) { ...@@ -80,6 +85,7 @@ func (c *YAMLConfigContainer) Bool(key string) (bool, error) {
80 return false, errors.New("not bool value") 85 return false, errors.New("not bool value")
81 } 86 }
82 87
88 // Int returns the integer value for a given key.
83 func (c *YAMLConfigContainer) Int(key string) (int, error) { 89 func (c *YAMLConfigContainer) Int(key string) (int, error) {
84 if v, ok := c.data[key].(int64); ok { 90 if v, ok := c.data[key].(int64); ok {
85 return int(v), nil 91 return int(v), nil
...@@ -87,6 +93,7 @@ func (c *YAMLConfigContainer) Int(key string) (int, error) { ...@@ -87,6 +93,7 @@ func (c *YAMLConfigContainer) Int(key string) (int, error) {
87 return 0, errors.New("not int value") 93 return 0, errors.New("not int value")
88 } 94 }
89 95
96 // Int64 returns the int64 value for a given key.
90 func (c *YAMLConfigContainer) Int64(key string) (int64, error) { 97 func (c *YAMLConfigContainer) Int64(key string) (int64, error) {
91 if v, ok := c.data[key].(int64); ok { 98 if v, ok := c.data[key].(int64); ok {
92 return v, nil 99 return v, nil
...@@ -94,6 +101,7 @@ func (c *YAMLConfigContainer) Int64(key string) (int64, error) { ...@@ -94,6 +101,7 @@ func (c *YAMLConfigContainer) Int64(key string) (int64, error) {
94 return 0, errors.New("not bool value") 101 return 0, errors.New("not bool value")
95 } 102 }
96 103
104 // Float returns the float value for a given key.
97 func (c *YAMLConfigContainer) Float(key string) (float64, error) { 105 func (c *YAMLConfigContainer) Float(key string) (float64, error) {
98 if v, ok := c.data[key].(float64); ok { 106 if v, ok := c.data[key].(float64); ok {
99 return v, nil 107 return v, nil
...@@ -101,6 +109,7 @@ func (c *YAMLConfigContainer) Float(key string) (float64, error) { ...@@ -101,6 +109,7 @@ func (c *YAMLConfigContainer) Float(key string) (float64, error) {
101 return 0.0, errors.New("not float64 value") 109 return 0.0, errors.New("not float64 value")
102 } 110 }
103 111
112 // String returns the string value for a given key.
104 func (c *YAMLConfigContainer) String(key string) string { 113 func (c *YAMLConfigContainer) String(key string) string {
105 if v, ok := c.data[key].(string); ok { 114 if v, ok := c.data[key].(string); ok {
106 return v 115 return v
...@@ -108,6 +117,7 @@ func (c *YAMLConfigContainer) String(key string) string { ...@@ -108,6 +117,7 @@ func (c *YAMLConfigContainer) String(key string) string {
108 return "" 117 return ""
109 } 118 }
110 119
120 // WriteValue writes a new value for key.
111 func (c *YAMLConfigContainer) Set(key, val string) error { 121 func (c *YAMLConfigContainer) Set(key, val string) error {
112 c.Lock() 122 c.Lock()
113 defer c.Unlock() 123 defer c.Unlock()
...@@ -115,6 +125,7 @@ func (c *YAMLConfigContainer) Set(key, val string) error { ...@@ -115,6 +125,7 @@ func (c *YAMLConfigContainer) Set(key, val string) error {
115 return nil 125 return nil
116 } 126 }
117 127
128 // DIY returns the raw value by a given key.
118 func (c *YAMLConfigContainer) DIY(key string) (v interface{}, err error) { 129 func (c *YAMLConfigContainer) DIY(key string) (v interface{}, err error) {
119 if v, ok := c.data[key]; ok { 130 if v, ok := c.data[key]; ok {
120 return v, nil 131 return v, nil
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!