Merge pull request #412 from fuxiaohei/master
add comment in config package.
Showing
7 changed files
with
74 additions
and
27 deletions
| ... | @@ -47,10 +47,11 @@ type FileCache struct { | ... | @@ -47,10 +47,11 @@ type FileCache struct { |
| 47 | EmbedExpiry int | 47 | EmbedExpiry int |
| 48 | } | 48 | } |
| 49 | 49 | ||
| 50 | // Create new file cache with default directory and suffix. | 50 | // Create new file cache with no config. |
| 51 | // the level and expiry need set in method StartAndGC as config string. | 51 | // the level and expiry need set in method StartAndGC as config string. |
| 52 | func NewFileCache() *FileCache { | 52 | func NewFileCache() *FileCache { |
| 53 | return &FileCache{CachePath:FileCachePath, FileSuffix:FileCacheFileSuffix} | 53 | // return &FileCache{CachePath:FileCachePath, FileSuffix:FileCacheFileSuffix} |
| 54 | return &FileCache{} | ||
| 54 | } | 55 | } |
| 55 | 56 | ||
| 56 | // Start and begin gc for file cache. | 57 | // Start and begin gc for file cache. |
| ... | @@ -142,13 +143,14 @@ func (this *FileCache) Get(key string) interface{} { | ... | @@ -142,13 +143,14 @@ func (this *FileCache) Get(key string) interface{} { |
| 142 | } | 143 | } |
| 143 | 144 | ||
| 144 | // Put value into file cache. | 145 | // Put value into file cache. |
| 145 | // timeout means how long to keep this file, unit of second. | 146 | // timeout means how long to keep this file, unit of ms. |
| 147 | // if timeout equals FileCacheEmbedExpiry(default is 0), cache this item forever. | ||
| 146 | func (this *FileCache) Put(key string, val interface{}, timeout int64) error { | 148 | func (this *FileCache) Put(key string, val interface{}, timeout int64) error { |
| 147 | filename := this.getCacheFileName(key) | 149 | filename := this.getCacheFileName(key) |
| 148 | var item FileCacheItem | 150 | var item FileCacheItem |
| 149 | item.Data = val | 151 | item.Data = val |
| 150 | if timeout == FileCacheEmbedExpiry { | 152 | if timeout == FileCacheEmbedExpiry { |
| 151 | item.Expired = time.Now().Unix() + (86400 * 365 * 10) //10年 | 153 | item.Expired = time.Now().Unix() + (86400 * 365 * 10) // ten years |
| 152 | } else { | 154 | } else { |
| 153 | item.Expired = time.Now().Unix() + timeout | 155 | item.Expired = time.Now().Unix() + timeout |
| 154 | } | 156 | } | ... | ... |
| ... | @@ -26,7 +26,7 @@ type MemoryCache struct { | ... | @@ -26,7 +26,7 @@ type MemoryCache struct { |
| 26 | lock sync.RWMutex | 26 | lock sync.RWMutex |
| 27 | dur time.Duration | 27 | dur time.Duration |
| 28 | items map[string]*MemoryItem | 28 | items map[string]*MemoryItem |
| 29 | Every int // run an expiration check Every cloc; time | 29 | Every int // run an expiration check Every clock time |
| 30 | } | 30 | } |
| 31 | 31 | ||
| 32 | // NewMemoryCache returns a new MemoryCache. | 32 | // NewMemoryCache returns a new MemoryCache. |
| ... | @@ -52,6 +52,7 @@ func (bc *MemoryCache) Get(name string) interface{} { | ... | @@ -52,6 +52,7 @@ func (bc *MemoryCache) Get(name string) interface{} { |
| 52 | } | 52 | } |
| 53 | 53 | ||
| 54 | // Put cache to memory. | 54 | // Put cache to memory. |
| 55 | // if expired is 0, it will be cleaned by next gc operation ( default gc clock is 1 minute). | ||
| 55 | func (bc *MemoryCache) Put(name string, value interface{}, expired int64) error { | 56 | func (bc *MemoryCache) Put(name string, value interface{}, expired int64) error { |
| 56 | bc.lock.Lock() | 57 | bc.lock.Lock() |
| 57 | defer bc.lock.Unlock() | 58 | defer bc.lock.Unlock() | ... | ... |
| ... | @@ -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() |
| ... | @@ -169,6 +173,7 @@ func (c *IniConfigContainer) Set(key, value string) error { | ... | @@ -169,6 +173,7 @@ func (c *IniConfigContainer) Set(key, value string) error { |
| 169 | return nil | 173 | return nil |
| 170 | } | 174 | } |
| 171 | 175 | ||
| 176 | // DIY returns the raw value by a given key. | ||
| 172 | func (c *IniConfigContainer) DIY(key string) (v interface{}, err error) { | 177 | func (c *IniConfigContainer) DIY(key string) (v interface{}, err error) { |
| 173 | key = strings.ToLower(key) | 178 | key = strings.ToLower(key) |
| 174 | if v, ok := c.data[key]; ok { | 179 | if v, ok := c.data[key]; ok { |
| ... | @@ -177,7 +182,7 @@ func (c *IniConfigContainer) DIY(key string) (v interface{}, err error) { | ... | @@ -177,7 +182,7 @@ func (c *IniConfigContainer) DIY(key string) (v interface{}, err error) { |
| 177 | return v, errors.New("key not find") | 182 | return v, errors.New("key not find") |
| 178 | } | 183 | } |
| 179 | 184 | ||
| 180 | //section.key or key | 185 | // section.key or key |
| 181 | func (c *IniConfigContainer) getdata(key string) string { | 186 | func (c *IniConfigContainer) getdata(key string) string { |
| 182 | c.RLock() | 187 | c.RLock() |
| 183 | defer c.RUnlock() | 188 | 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 | |||
| 3 | package config | 1 | package config |
| 4 | 2 | ||
| 5 | import ( | 3 | import ( |
| ... | @@ -12,9 +10,13 @@ import ( | ... | @@ -12,9 +10,13 @@ import ( |
| 12 | "github.com/beego/x2j" | 10 | "github.com/beego/x2j" |
| 13 | ) | 11 | ) |
| 14 | 12 | ||
| 13 | // XmlConfig is a xml config parser and implements Config interface. | ||
| 14 | // xml configurations should be included in <config></config> tag. | ||
| 15 | // only support key/value pair as <key>value</key> as each item. | ||
| 15 | type XMLConfig struct { | 16 | type XMLConfig struct { |
| 16 | } | 17 | } |
| 17 | 18 | ||
| 19 | // Parse returns a ConfigContainer with parsed xml config map. | ||
| 18 | func (xmls *XMLConfig) Parse(filename string) (ConfigContainer, error) { | 20 | func (xmls *XMLConfig) Parse(filename string) (ConfigContainer, error) { |
| 19 | file, err := os.Open(filename) | 21 | file, err := os.Open(filename) |
| 20 | if err != nil { | 22 | if err != nil { |
| ... | @@ -36,27 +38,33 @@ func (xmls *XMLConfig) Parse(filename string) (ConfigContainer, error) { | ... | @@ -36,27 +38,33 @@ func (xmls *XMLConfig) Parse(filename string) (ConfigContainer, error) { |
| 36 | return x, nil | 38 | return x, nil |
| 37 | } | 39 | } |
| 38 | 40 | ||
| 41 | // A Config represents the xml configuration. | ||
| 39 | type XMLConfigContainer struct { | 42 | type XMLConfigContainer struct { |
| 40 | data map[string]interface{} | 43 | data map[string]interface{} |
| 41 | sync.Mutex | 44 | sync.Mutex |
| 42 | } | 45 | } |
| 43 | 46 | ||
| 47 | // Bool returns the boolean value for a given key. | ||
| 44 | func (c *XMLConfigContainer) Bool(key string) (bool, error) { | 48 | func (c *XMLConfigContainer) Bool(key string) (bool, error) { |
| 45 | return strconv.ParseBool(c.data[key].(string)) | 49 | return strconv.ParseBool(c.data[key].(string)) |
| 46 | } | 50 | } |
| 47 | 51 | ||
| 52 | // Int returns the integer value for a given key. | ||
| 48 | func (c *XMLConfigContainer) Int(key string) (int, error) { | 53 | func (c *XMLConfigContainer) Int(key string) (int, error) { |
| 49 | return strconv.Atoi(c.data[key].(string)) | 54 | return strconv.Atoi(c.data[key].(string)) |
| 50 | } | 55 | } |
| 51 | 56 | ||
| 57 | // Int64 returns the int64 value for a given key. | ||
| 52 | func (c *XMLConfigContainer) Int64(key string) (int64, error) { | 58 | func (c *XMLConfigContainer) Int64(key string) (int64, error) { |
| 53 | return strconv.ParseInt(c.data[key].(string), 10, 64) | 59 | return strconv.ParseInt(c.data[key].(string), 10, 64) |
| 54 | } | 60 | } |
| 55 | 61 | ||
| 62 | // Float returns the float value for a given key. | ||
| 56 | func (c *XMLConfigContainer) Float(key string) (float64, error) { | 63 | func (c *XMLConfigContainer) Float(key string) (float64, error) { |
| 57 | return strconv.ParseFloat(c.data[key].(string), 64) | 64 | return strconv.ParseFloat(c.data[key].(string), 64) |
| 58 | } | 65 | } |
| 59 | 66 | ||
| 67 | // String returns the string value for a given key. | ||
| 60 | func (c *XMLConfigContainer) String(key string) string { | 68 | func (c *XMLConfigContainer) String(key string) string { |
| 61 | if v, ok := c.data[key].(string); ok { | 69 | if v, ok := c.data[key].(string); ok { |
| 62 | return v | 70 | return v |
| ... | @@ -64,6 +72,7 @@ func (c *XMLConfigContainer) String(key string) string { | ... | @@ -64,6 +72,7 @@ func (c *XMLConfigContainer) String(key string) string { |
| 64 | return "" | 72 | return "" |
| 65 | } | 73 | } |
| 66 | 74 | ||
| 75 | // WriteValue writes a new value for key. | ||
| 67 | func (c *XMLConfigContainer) Set(key, val string) error { | 76 | func (c *XMLConfigContainer) Set(key, val string) error { |
| 68 | c.Lock() | 77 | c.Lock() |
| 69 | defer c.Unlock() | 78 | defer c.Unlock() |
| ... | @@ -71,6 +80,7 @@ func (c *XMLConfigContainer) Set(key, val string) error { | ... | @@ -71,6 +80,7 @@ func (c *XMLConfigContainer) Set(key, val string) error { |
| 71 | return nil | 80 | return nil |
| 72 | } | 81 | } |
| 73 | 82 | ||
| 83 | // DIY returns the raw value by a given key. | ||
| 74 | func (c *XMLConfigContainer) DIY(key string) (v interface{}, err error) { | 84 | func (c *XMLConfigContainer) DIY(key string) (v interface{}, err error) { |
| 75 | if v, ok := c.data[key]; ok { | 85 | if v, ok := c.data[key]; ok { |
| 76 | return v, nil | 86 | 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 | ... | ... |
-
Please register or sign in to post a comment