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