From f96eec6deab631090be9da09b9d7b62e87aa8957 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=82=85=E5=B0=8F=E9=BB=91?= Date: Sun, 22 Dec 2013 15:31:49 +0800 Subject: [PATCH 1/4] fix a code broken when documenting --- cache/file.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/cache/file.go b/cache/file.go index a4e59c06..12982e26 100644 --- a/cache/file.go +++ b/cache/file.go @@ -34,9 +34,9 @@ type FileCacheItem struct { var ( FileCachePath string = "cache" // cache directory - FileCacheFileSuffix string = ".bin" // cache file suffix - FileCacheDirectoryLevel int = 2 // cache file deep level if auto generated cache files. - FileCacheEmbedExpiry int64 = 0 // cache expire time, default is no expire forever. + FileCacheFileSuffix string = ".bin" // cache file suffix + FileCacheDirectoryLevel int = 2 // cache file deep level if auto generated cache files. + FileCacheEmbedExpiry int64 = 0 // cache expire time, default is no expire forever. ) // FileCache is cache adapter for file storage. @@ -47,10 +47,11 @@ type FileCache struct { EmbedExpiry int } -// Create new file cache with default directory and suffix. +// Create new file cache with no config. // the level and expiry need set in method StartAndGC as config string. func NewFileCache() *FileCache { - return &FileCache{CachePath:FileCachePath, FileSuffix:FileCacheFileSuffix} + // return &FileCache{CachePath:FileCachePath, FileSuffix:FileCacheFileSuffix} + return &FileCache{} } // Start and begin gc for file cache. @@ -142,7 +143,7 @@ func (this *FileCache) Get(key string) interface{} { } // Put value into file cache. -// timeout means how long to keep this file, unit of second. +// timeout means how long to keep this file, unit of ms. func (this *FileCache) Put(key string, val interface{}, timeout int64) error { filename := this.getCacheFileName(key) var item FileCacheItem From 5b1afcdb5a5d25953ea134f72186a180000fcfcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=82=85=E5=B0=8F=E9=BB=91?= Date: Tue, 24 Dec 2013 21:56:48 +0800 Subject: [PATCH 2/4] add timeout description for file and memory cache. --- cache/file.go | 3 ++- cache/memory.go | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/cache/file.go b/cache/file.go index 12982e26..a80f3678 100644 --- a/cache/file.go +++ b/cache/file.go @@ -144,12 +144,13 @@ func (this *FileCache) Get(key string) interface{} { // Put value into file cache. // timeout means how long to keep this file, unit of ms. +// if timeout equals FileCacheEmbedExpiry(default is 0), cache this item forever. func (this *FileCache) Put(key string, val interface{}, timeout int64) error { filename := this.getCacheFileName(key) var item FileCacheItem item.Data = val if timeout == FileCacheEmbedExpiry { - item.Expired = time.Now().Unix() + (86400 * 365 * 10) //10年 + item.Expired = time.Now().Unix() + (86400 * 365 * 10) // ten years } else { item.Expired = time.Now().Unix() + timeout } diff --git a/cache/memory.go b/cache/memory.go index 839fed72..e1284b79 100644 --- a/cache/memory.go +++ b/cache/memory.go @@ -26,7 +26,7 @@ type MemoryCache struct { lock sync.RWMutex dur time.Duration items map[string]*MemoryItem - Every int // run an expiration check Every cloc; time + Every int // run an expiration check Every clock time } // NewMemoryCache returns a new MemoryCache. @@ -52,6 +52,7 @@ func (bc *MemoryCache) Get(name string) interface{} { } // Put cache to memory. +// if expired is 0, it will be cleaned by next gc operation ( default gc clock is 1 minute). func (bc *MemoryCache) Put(name string, value interface{}, expired int64) error { bc.lock.Lock() defer bc.lock.Unlock() From 0183608a59c407f76bee5358a3d3ba480c74bbcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=82=85=E5=B0=8F=E9=BB=91?= Date: Tue, 24 Dec 2013 21:57:33 +0800 Subject: [PATCH 3/4] add comments for config package. --- config/config.go | 10 ++++++---- config/ini.go | 31 ++++++++++++++++++------------- config/json.go | 20 ++++++++++++++++++-- config/xml.go | 13 ++++++++++++- config/yaml.go | 13 ++++++++++++- 5 files changed, 66 insertions(+), 21 deletions(-) diff --git a/config/config.go b/config/config.go index 101bc66d..63ca7c48 100644 --- a/config/config.go +++ b/config/config.go @@ -4,9 +4,10 @@ import ( "fmt" ) +// ConfigContainer defines how to get and set value from configuration raw data. type ConfigContainer interface { - Set(key, val string) error - String(key string) string + Set(key, val string) error // support section::key type in given key when using ini type. + String(key string) string // support section::key type in key string when using ini and json type; Int,Int64,Bool,Float,DIY are same. Int(key string) (int, error) Int64(key string) (int64, error) Bool(key string) (bool, error) @@ -14,6 +15,7 @@ type ConfigContainer interface { DIY(key string) (interface{}, error) } +// Config is the adapter interface for parsing config file to get raw data to ConfigContainer. type Config interface { Parse(key string) (ConfigContainer, error) } @@ -33,8 +35,8 @@ func Register(name string, adapter Config) { adapters[name] = adapter } -// adapterNamer is ini/json/xml/yaml -// filename is the config file path +// adapterName is ini/json/xml/yaml. +// filename is the config file path. func NewConfig(adapterName, fileaname string) (ConfigContainer, error) { adapter, ok := adapters[adapterName] if !ok { diff --git a/config/ini.go b/config/ini.go index 4739824d..2ca4b030 100644 --- a/config/ini.go +++ b/config/ini.go @@ -13,21 +13,21 @@ import ( ) var ( - DEFAULT_SECTION = "default" - bNumComment = []byte{'#'} // number sign - bSemComment = []byte{';'} // semicolon + DEFAULT_SECTION = "default" // default section means if some ini items not in a section, make them in default section, + bNumComment = []byte{'#'} // number signal + bSemComment = []byte{';'} // semicolon signal bEmpty = []byte{} - bEqual = []byte{'='} - bDQuote = []byte{'"'} - sectionStart = []byte{'['} - sectionEnd = []byte{']'} + bEqual = []byte{'='} // equal signal + bDQuote = []byte{'"'} // quote signal + sectionStart = []byte{'['} // section start signal + sectionEnd = []byte{']'} // section end signal ) +// IniConfig implements Config to parse ini file. type IniConfig struct { } -// ParseFile creates a new Config and parses the file configuration from the -// named file. +// ParseFile creates a new Config and parses the file configuration from the named file. func (ini *IniConfig) Parse(name string) (ConfigContainer, error) { file, err := os.Open(name) if err != nil { @@ -106,11 +106,12 @@ func (ini *IniConfig) Parse(name string) (ConfigContainer, error) { return cfg, nil } -// A Config represents the configuration. +// A Config represents the ini configuration. +// When set and get value, support key as section:name type. type IniConfigContainer struct { filename string - data map[string]map[string]string //section=> key:val - sectionComment map[string]string //sction : comment + data map[string]map[string]string // section=> key:val + sectionComment map[string]string // section : comment keycomment map[string]string // id: []{comment, key...}; id 1 is for main comment. sync.RWMutex } @@ -127,6 +128,7 @@ func (c *IniConfigContainer) Int(key string) (int, error) { return strconv.Atoi(c.getdata(key)) } +// Int64 returns the int64 value for a given key. func (c *IniConfigContainer) Int64(key string) (int64, error) { key = strings.ToLower(key) return strconv.ParseInt(c.getdata(key), 10, 64) @@ -145,6 +147,8 @@ func (c *IniConfigContainer) String(key string) string { } // WriteValue writes a new value for key. +// if write to one section, the key need be "section::key". +// if the section is not existed, it panics. func (c *IniConfigContainer) Set(key, value string) error { c.Lock() defer c.Unlock() @@ -166,6 +170,7 @@ func (c *IniConfigContainer) Set(key, value string) error { return nil } +// DIY returns the raw value by a given key. func (c *IniConfigContainer) DIY(key string) (v interface{}, err error) { key = strings.ToLower(key) if v, ok := c.data[key]; ok { @@ -174,7 +179,7 @@ func (c *IniConfigContainer) DIY(key string) (v interface{}, err error) { return v, errors.New("key not find") } -//section.key or key +// section.key or key func (c *IniConfigContainer) getdata(key string) string { c.RLock() defer c.RUnlock() diff --git a/config/json.go b/config/json.go index b401e1b3..a74ac7df 100644 --- a/config/json.go +++ b/config/json.go @@ -9,9 +9,11 @@ import ( "sync" ) +// JsonConfig is a json config parser and implements Config interface. type JsonConfig struct { } +// Parse returns a ConfigContainer with parsed json config map. func (js *JsonConfig) Parse(filename string) (ConfigContainer, error) { file, err := os.Open(filename) if err != nil { @@ -32,11 +34,14 @@ func (js *JsonConfig) Parse(filename string) (ConfigContainer, error) { return x, nil } +// A Config represents the json configuration. +// Only when get value, support key as section:name type. type JsonConfigContainer struct { data map[string]interface{} sync.RWMutex } +// Bool returns the boolean value for a given key. func (c *JsonConfigContainer) Bool(key string) (bool, error) { val := c.getdata(key) if val != nil { @@ -48,9 +53,10 @@ func (c *JsonConfigContainer) Bool(key string) (bool, error) { } else { return false, errors.New("not exist key:" + key) } - + return false,nil } +// Int returns the integer value for a given key. func (c *JsonConfigContainer) Int(key string) (int, error) { val := c.getdata(key) if val != nil { @@ -62,8 +68,10 @@ func (c *JsonConfigContainer) Int(key string) (int, error) { } else { return 0, errors.New("not exist key:" + key) } + return 0,nil } +// Int64 returns the int64 value for a given key. func (c *JsonConfigContainer) Int64(key string) (int64, error) { val := c.getdata(key) if val != nil { @@ -75,8 +83,10 @@ func (c *JsonConfigContainer) Int64(key string) (int64, error) { } else { return 0, errors.New("not exist key:" + key) } + return 0,nil } +// Float returns the float value for a given key. func (c *JsonConfigContainer) Float(key string) (float64, error) { val := c.getdata(key) if val != nil { @@ -88,8 +98,10 @@ func (c *JsonConfigContainer) Float(key string) (float64, error) { } else { return 0.0, errors.New("not exist key:" + key) } + return 0.0,nil } +// String returns the string value for a given key. func (c *JsonConfigContainer) String(key string) string { val := c.getdata(key) if val != nil { @@ -101,8 +113,10 @@ func (c *JsonConfigContainer) String(key string) string { } else { return "" } + return "" } +// WriteValue writes a new value for key. func (c *JsonConfigContainer) Set(key, val string) error { c.Lock() defer c.Unlock() @@ -110,6 +124,7 @@ func (c *JsonConfigContainer) Set(key, val string) error { return nil } +// DIY returns the raw value by a given key. func (c *JsonConfigContainer) DIY(key string) (v interface{}, err error) { val := c.getdata(key) if val != nil { @@ -117,9 +132,10 @@ func (c *JsonConfigContainer) DIY(key string) (v interface{}, err error) { } else { return nil, errors.New("not exist key") } + return nil,nil } -//section.key or key +// section.key or key func (c *JsonConfigContainer) getdata(key string) interface{} { c.RLock() defer c.RUnlock() diff --git a/config/xml.go b/config/xml.go index 7f3807ef..7bc0ec02 100644 --- a/config/xml.go +++ b/config/xml.go @@ -1,4 +1,3 @@ -//xml parse should incluce in tags package config @@ -12,9 +11,13 @@ import ( "github.com/beego/x2j" ) +// 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 { } +// Parse returns a ConfigContainer with parsed xml config map. func (xmls *XMLConfig) Parse(filename string) (ConfigContainer, error) { file, err := os.Open(filename) if err != nil { @@ -36,27 +39,33 @@ func (xmls *XMLConfig) Parse(filename string) (ConfigContainer, error) { return x, nil } +// A Config represents the xml configuration. type XMLConfigContainer struct { data map[string]interface{} sync.Mutex } +// Bool returns the boolean value for a given key. func (c *XMLConfigContainer) Bool(key string) (bool, error) { return strconv.ParseBool(c.data[key].(string)) } +// Int returns the integer value for a given key. func (c *XMLConfigContainer) Int(key string) (int, error) { return strconv.Atoi(c.data[key].(string)) } +// Int64 returns the int64 value for a given key. func (c *XMLConfigContainer) Int64(key string) (int64, error) { return strconv.ParseInt(c.data[key].(string), 10, 64) } +// Float returns the float value for a given key. func (c *XMLConfigContainer) Float(key string) (float64, error) { return strconv.ParseFloat(c.data[key].(string), 64) } +// String returns the string value for a given key. func (c *XMLConfigContainer) String(key string) string { if v, ok := c.data[key].(string); ok { return v @@ -64,6 +73,7 @@ func (c *XMLConfigContainer) String(key string) string { return "" } +// WriteValue writes a new value for key. func (c *XMLConfigContainer) Set(key, val string) error { c.Lock() defer c.Unlock() @@ -71,6 +81,7 @@ func (c *XMLConfigContainer) Set(key, val string) error { return nil } +// DIY returns the raw value by a given key. func (c *XMLConfigContainer) DIY(key string) (v interface{}, err error) { if v, ok := c.data[key]; ok { return v, nil diff --git a/config/yaml.go b/config/yaml.go index 04bb0940..394cb3b2 100644 --- a/config/yaml.go +++ b/config/yaml.go @@ -12,9 +12,11 @@ import ( "github.com/beego/goyaml2" ) +// YAMLConfig is a yaml config parser and implements Config interface. type YAMLConfig struct { } +// Parse returns a ConfigContainer with parsed yaml config map. func (yaml *YAMLConfig) Parse(filename string) (ConfigContainer, error) { y := &YAMLConfigContainer{ data: make(map[string]interface{}), @@ -27,7 +29,8 @@ func (yaml *YAMLConfig) Parse(filename string) (ConfigContainer, error) { return y, nil } -// 从Reader读取YAML +// 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) @@ -68,11 +71,13 @@ func ReadYmlReader(path string) (cnf map[string]interface{}, err error) { return } +// A Config represents the yaml configuration. type YAMLConfigContainer struct { data map[string]interface{} sync.Mutex } +// Bool returns the boolean value for a given key. func (c *YAMLConfigContainer) Bool(key string) (bool, error) { if v, ok := c.data[key].(bool); ok { return v, nil @@ -80,6 +85,7 @@ func (c *YAMLConfigContainer) Bool(key string) (bool, error) { return false, errors.New("not bool value") } +// Int returns the integer value for a given key. func (c *YAMLConfigContainer) Int(key string) (int, error) { if v, ok := c.data[key].(int64); ok { return int(v), nil @@ -87,6 +93,7 @@ func (c *YAMLConfigContainer) Int(key string) (int, error) { return 0, errors.New("not int value") } +// Int64 returns the int64 value for a given key. func (c *YAMLConfigContainer) Int64(key string) (int64, error) { if v, ok := c.data[key].(int64); ok { return v, nil @@ -94,6 +101,7 @@ func (c *YAMLConfigContainer) Int64(key string) (int64, error) { return 0, errors.New("not bool value") } +// Float returns the float value for a given key. func (c *YAMLConfigContainer) Float(key string) (float64, error) { if v, ok := c.data[key].(float64); ok { return v, nil @@ -101,6 +109,7 @@ func (c *YAMLConfigContainer) Float(key string) (float64, error) { return 0.0, errors.New("not float64 value") } +// String returns the string value for a given key. func (c *YAMLConfigContainer) String(key string) string { if v, ok := c.data[key].(string); ok { return v @@ -108,6 +117,7 @@ func (c *YAMLConfigContainer) String(key string) string { return "" } +// WriteValue writes a new value for key. func (c *YAMLConfigContainer) Set(key, val string) error { c.Lock() defer c.Unlock() @@ -115,6 +125,7 @@ func (c *YAMLConfigContainer) Set(key, val string) error { return nil } +// DIY returns the raw value by a given key. func (c *YAMLConfigContainer) DIY(key string) (v interface{}, err error) { if v, ok := c.data[key]; ok { return v, nil From a1f6039d82951a2f6f0f918768a996b4b31b2747 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=82=85=E5=B0=8F=E9=BB=91?= Date: Tue, 24 Dec 2013 21:59:00 +0800 Subject: [PATCH 4/4] gofmt code --- cache/file.go | 6 +++--- config/config.go | 2 +- config/ini.go | 2 +- config/json.go | 10 +++++----- config/xml.go | 1 - 5 files changed, 10 insertions(+), 11 deletions(-) diff --git a/cache/file.go b/cache/file.go index a80f3678..3807fa7c 100644 --- a/cache/file.go +++ b/cache/file.go @@ -34,9 +34,9 @@ type FileCacheItem struct { var ( FileCachePath string = "cache" // cache directory - FileCacheFileSuffix string = ".bin" // cache file suffix - FileCacheDirectoryLevel int = 2 // cache file deep level if auto generated cache files. - FileCacheEmbedExpiry int64 = 0 // cache expire time, default is no expire forever. + FileCacheFileSuffix string = ".bin" // cache file suffix + FileCacheDirectoryLevel int = 2 // cache file deep level if auto generated cache files. + FileCacheEmbedExpiry int64 = 0 // cache expire time, default is no expire forever. ) // FileCache is cache adapter for file storage. diff --git a/config/config.go b/config/config.go index 63ca7c48..5fb0dd81 100644 --- a/config/config.go +++ b/config/config.go @@ -7,7 +7,7 @@ import ( // ConfigContainer defines how to get and set value from configuration raw data. type ConfigContainer interface { Set(key, val string) error // support section::key type in given key when using ini type. - String(key string) string // support section::key type in key string when using ini and json type; Int,Int64,Bool,Float,DIY are same. + String(key string) string // support section::key type in key string when using ini and json type; Int,Int64,Bool,Float,DIY are same. Int(key string) (int, error) Int64(key string) (int64, error) Bool(key string) (bool, error) diff --git a/config/ini.go b/config/ini.go index 2ca4b030..6ac79295 100644 --- a/config/ini.go +++ b/config/ini.go @@ -13,7 +13,7 @@ import ( ) var ( - DEFAULT_SECTION = "default" // default section means if some ini items not in a section, make them in default section, + DEFAULT_SECTION = "default" // default section means if some ini items not in a section, make them in default section, bNumComment = []byte{'#'} // number signal bSemComment = []byte{';'} // semicolon signal bEmpty = []byte{} diff --git a/config/json.go b/config/json.go index a74ac7df..883e0674 100644 --- a/config/json.go +++ b/config/json.go @@ -53,7 +53,7 @@ func (c *JsonConfigContainer) Bool(key string) (bool, error) { } else { return false, errors.New("not exist key:" + key) } - return false,nil + return false, nil } // Int returns the integer value for a given key. @@ -68,7 +68,7 @@ func (c *JsonConfigContainer) Int(key string) (int, error) { } else { return 0, errors.New("not exist key:" + key) } - return 0,nil + return 0, nil } // Int64 returns the int64 value for a given key. @@ -83,7 +83,7 @@ func (c *JsonConfigContainer) Int64(key string) (int64, error) { } else { return 0, errors.New("not exist key:" + key) } - return 0,nil + return 0, nil } // Float returns the float value for a given key. @@ -98,7 +98,7 @@ func (c *JsonConfigContainer) Float(key string) (float64, error) { } else { return 0.0, errors.New("not exist key:" + key) } - return 0.0,nil + return 0.0, nil } // String returns the string value for a given key. @@ -132,7 +132,7 @@ func (c *JsonConfigContainer) DIY(key string) (v interface{}, err error) { } else { return nil, errors.New("not exist key") } - return nil,nil + return nil, nil } // section.key or key diff --git a/config/xml.go b/config/xml.go index 7bc0ec02..35f19336 100644 --- a/config/xml.go +++ b/config/xml.go @@ -1,4 +1,3 @@ - package config import (