mirror of
https://github.com/astaxie/beego.git
synced 2024-11-25 22:11:29 +00:00
add comments for config package.
This commit is contained in:
parent
5b1afcdb5a
commit
0183608a59
@ -4,9 +4,10 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ConfigContainer defines how to get and set value from configuration raw data.
|
||||||
type ConfigContainer interface {
|
type ConfigContainer interface {
|
||||||
Set(key, val string) error
|
Set(key, val string) error // support section::key type in given key when using ini type.
|
||||||
String(key string) string
|
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)
|
Int(key string) (int, error)
|
||||||
Int64(key string) (int64, error)
|
Int64(key string) (int64, error)
|
||||||
Bool(key string) (bool, error)
|
Bool(key string) (bool, error)
|
||||||
@ -14,6 +15,7 @@ type ConfigContainer interface {
|
|||||||
DIY(key string) (interface{}, error)
|
DIY(key string) (interface{}, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Config is the adapter interface for parsing config file to get raw data to ConfigContainer.
|
||||||
type Config interface {
|
type Config interface {
|
||||||
Parse(key string) (ConfigContainer, error)
|
Parse(key string) (ConfigContainer, error)
|
||||||
}
|
}
|
||||||
@ -33,8 +35,8 @@ func Register(name string, adapter Config) {
|
|||||||
adapters[name] = adapter
|
adapters[name] = adapter
|
||||||
}
|
}
|
||||||
|
|
||||||
// adapterNamer is ini/json/xml/yaml
|
// adapterName is ini/json/xml/yaml.
|
||||||
// filename is the config file path
|
// filename is the config file path.
|
||||||
func NewConfig(adapterName, fileaname string) (ConfigContainer, error) {
|
func NewConfig(adapterName, fileaname string) (ConfigContainer, error) {
|
||||||
adapter, ok := adapters[adapterName]
|
adapter, ok := adapters[adapterName]
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -13,21 +13,21 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
DEFAULT_SECTION = "default"
|
DEFAULT_SECTION = "default" // default section means if some ini items not in a section, make them in default section,
|
||||||
bNumComment = []byte{'#'} // number sign
|
bNumComment = []byte{'#'} // number signal
|
||||||
bSemComment = []byte{';'} // semicolon
|
bSemComment = []byte{';'} // semicolon signal
|
||||||
bEmpty = []byte{}
|
bEmpty = []byte{}
|
||||||
bEqual = []byte{'='}
|
bEqual = []byte{'='} // equal signal
|
||||||
bDQuote = []byte{'"'}
|
bDQuote = []byte{'"'} // quote signal
|
||||||
sectionStart = []byte{'['}
|
sectionStart = []byte{'['} // section start signal
|
||||||
sectionEnd = []byte{']'}
|
sectionEnd = []byte{']'} // section end signal
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// IniConfig implements Config to parse ini file.
|
||||||
type IniConfig struct {
|
type IniConfig struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseFile creates a new Config and parses the file configuration from the
|
// ParseFile creates a new Config and parses the file configuration from the named file.
|
||||||
// named file.
|
|
||||||
func (ini *IniConfig) Parse(name string) (ConfigContainer, error) {
|
func (ini *IniConfig) Parse(name string) (ConfigContainer, error) {
|
||||||
file, err := os.Open(name)
|
file, err := os.Open(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -106,11 +106,12 @@ func (ini *IniConfig) Parse(name string) (ConfigContainer, error) {
|
|||||||
return cfg, nil
|
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 {
|
type IniConfigContainer struct {
|
||||||
filename string
|
filename string
|
||||||
data map[string]map[string]string //section=> key:val
|
data map[string]map[string]string // section=> key:val
|
||||||
sectionComment map[string]string //sction : comment
|
sectionComment map[string]string // section : comment
|
||||||
keycomment map[string]string // id: []{comment, key...}; id 1 is for main comment.
|
keycomment map[string]string // id: []{comment, key...}; id 1 is for main comment.
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
}
|
}
|
||||||
@ -127,6 +128,7 @@ func (c *IniConfigContainer) Int(key string) (int, error) {
|
|||||||
return strconv.Atoi(c.getdata(key))
|
return strconv.Atoi(c.getdata(key))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Int64 returns the int64 value for a given key.
|
||||||
func (c *IniConfigContainer) Int64(key string) (int64, error) {
|
func (c *IniConfigContainer) Int64(key string) (int64, error) {
|
||||||
key = strings.ToLower(key)
|
key = strings.ToLower(key)
|
||||||
return strconv.ParseInt(c.getdata(key), 10, 64)
|
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.
|
// 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 {
|
func (c *IniConfigContainer) Set(key, value string) error {
|
||||||
c.Lock()
|
c.Lock()
|
||||||
defer c.Unlock()
|
defer c.Unlock()
|
||||||
@ -166,6 +170,7 @@ func (c *IniConfigContainer) Set(key, value string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DIY returns the raw value by a given key.
|
||||||
func (c *IniConfigContainer) DIY(key string) (v interface{}, err error) {
|
func (c *IniConfigContainer) DIY(key string) (v interface{}, err error) {
|
||||||
key = strings.ToLower(key)
|
key = strings.ToLower(key)
|
||||||
if v, ok := c.data[key]; ok {
|
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")
|
return v, errors.New("key not find")
|
||||||
}
|
}
|
||||||
|
|
||||||
//section.key or key
|
// section.key or key
|
||||||
func (c *IniConfigContainer) getdata(key string) string {
|
func (c *IniConfigContainer) getdata(key string) string {
|
||||||
c.RLock()
|
c.RLock()
|
||||||
defer c.RUnlock()
|
defer c.RUnlock()
|
||||||
|
@ -9,9 +9,11 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// JsonConfig is a json config parser and implements Config interface.
|
||||||
type JsonConfig struct {
|
type JsonConfig struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Parse returns a ConfigContainer with parsed json config map.
|
||||||
func (js *JsonConfig) Parse(filename string) (ConfigContainer, error) {
|
func (js *JsonConfig) Parse(filename string) (ConfigContainer, error) {
|
||||||
file, err := os.Open(filename)
|
file, err := os.Open(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -32,11 +34,14 @@ func (js *JsonConfig) Parse(filename string) (ConfigContainer, error) {
|
|||||||
return x, nil
|
return x, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A Config represents the json configuration.
|
||||||
|
// Only when get value, support key as section:name type.
|
||||||
type JsonConfigContainer struct {
|
type JsonConfigContainer struct {
|
||||||
data map[string]interface{}
|
data map[string]interface{}
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Bool returns the boolean value for a given key.
|
||||||
func (c *JsonConfigContainer) Bool(key string) (bool, error) {
|
func (c *JsonConfigContainer) Bool(key string) (bool, error) {
|
||||||
val := c.getdata(key)
|
val := c.getdata(key)
|
||||||
if val != nil {
|
if val != nil {
|
||||||
@ -48,9 +53,10 @@ func (c *JsonConfigContainer) Bool(key string) (bool, error) {
|
|||||||
} else {
|
} else {
|
||||||
return false, errors.New("not exist key:" + key)
|
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) {
|
func (c *JsonConfigContainer) Int(key string) (int, error) {
|
||||||
val := c.getdata(key)
|
val := c.getdata(key)
|
||||||
if val != nil {
|
if val != nil {
|
||||||
@ -62,8 +68,10 @@ func (c *JsonConfigContainer) Int(key string) (int, error) {
|
|||||||
} else {
|
} else {
|
||||||
return 0, errors.New("not exist key:" + key)
|
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) {
|
func (c *JsonConfigContainer) Int64(key string) (int64, error) {
|
||||||
val := c.getdata(key)
|
val := c.getdata(key)
|
||||||
if val != nil {
|
if val != nil {
|
||||||
@ -75,8 +83,10 @@ func (c *JsonConfigContainer) Int64(key string) (int64, error) {
|
|||||||
} else {
|
} else {
|
||||||
return 0, errors.New("not exist key:" + key)
|
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) {
|
func (c *JsonConfigContainer) Float(key string) (float64, error) {
|
||||||
val := c.getdata(key)
|
val := c.getdata(key)
|
||||||
if val != nil {
|
if val != nil {
|
||||||
@ -88,8 +98,10 @@ func (c *JsonConfigContainer) Float(key string) (float64, error) {
|
|||||||
} else {
|
} else {
|
||||||
return 0.0, errors.New("not exist key:" + key)
|
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 {
|
func (c *JsonConfigContainer) String(key string) string {
|
||||||
val := c.getdata(key)
|
val := c.getdata(key)
|
||||||
if val != nil {
|
if val != nil {
|
||||||
@ -101,8 +113,10 @@ func (c *JsonConfigContainer) String(key string) string {
|
|||||||
} else {
|
} else {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WriteValue writes a new value for key.
|
||||||
func (c *JsonConfigContainer) Set(key, val string) error {
|
func (c *JsonConfigContainer) Set(key, val string) error {
|
||||||
c.Lock()
|
c.Lock()
|
||||||
defer c.Unlock()
|
defer c.Unlock()
|
||||||
@ -110,6 +124,7 @@ func (c *JsonConfigContainer) Set(key, val string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DIY returns the raw value by a given key.
|
||||||
func (c *JsonConfigContainer) DIY(key string) (v interface{}, err error) {
|
func (c *JsonConfigContainer) DIY(key string) (v interface{}, err error) {
|
||||||
val := c.getdata(key)
|
val := c.getdata(key)
|
||||||
if val != nil {
|
if val != nil {
|
||||||
@ -117,9 +132,10 @@ func (c *JsonConfigContainer) DIY(key string) (v interface{}, err error) {
|
|||||||
} else {
|
} else {
|
||||||
return nil, errors.New("not exist key")
|
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{} {
|
func (c *JsonConfigContainer) getdata(key string) interface{} {
|
||||||
c.RLock()
|
c.RLock()
|
||||||
defer c.RUnlock()
|
defer c.RUnlock()
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
//xml parse should incluce in <config></config> tags
|
|
||||||
|
|
||||||
package config
|
package config
|
||||||
|
|
||||||
@ -12,9 +11,13 @@ import (
|
|||||||
"github.com/beego/x2j"
|
"github.com/beego/x2j"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// XmlConfig is a xml config parser and implements Config interface.
|
||||||
|
// xml configurations should be included in <config></config> tag.
|
||||||
|
// only support key/value pair as <key>value</key> as each item.
|
||||||
type XMLConfig struct {
|
type XMLConfig struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Parse returns a ConfigContainer with parsed xml config map.
|
||||||
func (xmls *XMLConfig) Parse(filename string) (ConfigContainer, error) {
|
func (xmls *XMLConfig) Parse(filename string) (ConfigContainer, error) {
|
||||||
file, err := os.Open(filename)
|
file, err := os.Open(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -36,27 +39,33 @@ func (xmls *XMLConfig) Parse(filename string) (ConfigContainer, error) {
|
|||||||
return x, nil
|
return x, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A Config represents the xml configuration.
|
||||||
type XMLConfigContainer struct {
|
type XMLConfigContainer struct {
|
||||||
data map[string]interface{}
|
data map[string]interface{}
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Bool returns the boolean value for a given key.
|
||||||
func (c *XMLConfigContainer) Bool(key string) (bool, error) {
|
func (c *XMLConfigContainer) Bool(key string) (bool, error) {
|
||||||
return strconv.ParseBool(c.data[key].(string))
|
return strconv.ParseBool(c.data[key].(string))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Int returns the integer value for a given key.
|
||||||
func (c *XMLConfigContainer) Int(key string) (int, error) {
|
func (c *XMLConfigContainer) Int(key string) (int, error) {
|
||||||
return strconv.Atoi(c.data[key].(string))
|
return strconv.Atoi(c.data[key].(string))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Int64 returns the int64 value for a given key.
|
||||||
func (c *XMLConfigContainer) Int64(key string) (int64, error) {
|
func (c *XMLConfigContainer) Int64(key string) (int64, error) {
|
||||||
return strconv.ParseInt(c.data[key].(string), 10, 64)
|
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) {
|
func (c *XMLConfigContainer) Float(key string) (float64, error) {
|
||||||
return strconv.ParseFloat(c.data[key].(string), 64)
|
return strconv.ParseFloat(c.data[key].(string), 64)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// String returns the string value for a given key.
|
||||||
func (c *XMLConfigContainer) String(key string) string {
|
func (c *XMLConfigContainer) String(key string) string {
|
||||||
if v, ok := c.data[key].(string); ok {
|
if v, ok := c.data[key].(string); ok {
|
||||||
return v
|
return v
|
||||||
@ -64,6 +73,7 @@ func (c *XMLConfigContainer) String(key string) string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WriteValue writes a new value for key.
|
||||||
func (c *XMLConfigContainer) Set(key, val string) error {
|
func (c *XMLConfigContainer) Set(key, val string) error {
|
||||||
c.Lock()
|
c.Lock()
|
||||||
defer c.Unlock()
|
defer c.Unlock()
|
||||||
@ -71,6 +81,7 @@ func (c *XMLConfigContainer) Set(key, val string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DIY returns the raw value by a given key.
|
||||||
func (c *XMLConfigContainer) DIY(key string) (v interface{}, err error) {
|
func (c *XMLConfigContainer) DIY(key string) (v interface{}, err error) {
|
||||||
if v, ok := c.data[key]; ok {
|
if v, ok := c.data[key]; ok {
|
||||||
return v, nil
|
return v, nil
|
||||||
|
@ -12,9 +12,11 @@ import (
|
|||||||
"github.com/beego/goyaml2"
|
"github.com/beego/goyaml2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// YAMLConfig is a yaml config parser and implements Config interface.
|
||||||
type YAMLConfig struct {
|
type YAMLConfig struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Parse returns a ConfigContainer with parsed yaml config map.
|
||||||
func (yaml *YAMLConfig) Parse(filename string) (ConfigContainer, error) {
|
func (yaml *YAMLConfig) Parse(filename string) (ConfigContainer, error) {
|
||||||
y := &YAMLConfigContainer{
|
y := &YAMLConfigContainer{
|
||||||
data: make(map[string]interface{}),
|
data: make(map[string]interface{}),
|
||||||
@ -27,7 +29,8 @@ func (yaml *YAMLConfig) Parse(filename string) (ConfigContainer, error) {
|
|||||||
return y, nil
|
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) {
|
func ReadYmlReader(path string) (cnf map[string]interface{}, err error) {
|
||||||
err = nil
|
err = nil
|
||||||
f, err := os.Open(path)
|
f, err := os.Open(path)
|
||||||
@ -68,11 +71,13 @@ func ReadYmlReader(path string) (cnf map[string]interface{}, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A Config represents the yaml configuration.
|
||||||
type YAMLConfigContainer struct {
|
type YAMLConfigContainer struct {
|
||||||
data map[string]interface{}
|
data map[string]interface{}
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Bool returns the boolean value for a given key.
|
||||||
func (c *YAMLConfigContainer) Bool(key string) (bool, error) {
|
func (c *YAMLConfigContainer) Bool(key string) (bool, error) {
|
||||||
if v, ok := c.data[key].(bool); ok {
|
if v, ok := c.data[key].(bool); ok {
|
||||||
return v, nil
|
return v, nil
|
||||||
@ -80,6 +85,7 @@ func (c *YAMLConfigContainer) Bool(key string) (bool, error) {
|
|||||||
return false, errors.New("not bool value")
|
return false, errors.New("not bool value")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Int returns the integer value for a given key.
|
||||||
func (c *YAMLConfigContainer) Int(key string) (int, error) {
|
func (c *YAMLConfigContainer) Int(key string) (int, error) {
|
||||||
if v, ok := c.data[key].(int64); ok {
|
if v, ok := c.data[key].(int64); ok {
|
||||||
return int(v), nil
|
return int(v), nil
|
||||||
@ -87,6 +93,7 @@ func (c *YAMLConfigContainer) Int(key string) (int, error) {
|
|||||||
return 0, errors.New("not int value")
|
return 0, errors.New("not int value")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Int64 returns the int64 value for a given key.
|
||||||
func (c *YAMLConfigContainer) Int64(key string) (int64, error) {
|
func (c *YAMLConfigContainer) Int64(key string) (int64, error) {
|
||||||
if v, ok := c.data[key].(int64); ok {
|
if v, ok := c.data[key].(int64); ok {
|
||||||
return v, nil
|
return v, nil
|
||||||
@ -94,6 +101,7 @@ func (c *YAMLConfigContainer) Int64(key string) (int64, error) {
|
|||||||
return 0, errors.New("not bool value")
|
return 0, errors.New("not bool value")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Float returns the float value for a given key.
|
||||||
func (c *YAMLConfigContainer) Float(key string) (float64, error) {
|
func (c *YAMLConfigContainer) Float(key string) (float64, error) {
|
||||||
if v, ok := c.data[key].(float64); ok {
|
if v, ok := c.data[key].(float64); ok {
|
||||||
return v, nil
|
return v, nil
|
||||||
@ -101,6 +109,7 @@ func (c *YAMLConfigContainer) Float(key string) (float64, error) {
|
|||||||
return 0.0, errors.New("not float64 value")
|
return 0.0, errors.New("not float64 value")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// String returns the string value for a given key.
|
||||||
func (c *YAMLConfigContainer) String(key string) string {
|
func (c *YAMLConfigContainer) String(key string) string {
|
||||||
if v, ok := c.data[key].(string); ok {
|
if v, ok := c.data[key].(string); ok {
|
||||||
return v
|
return v
|
||||||
@ -108,6 +117,7 @@ func (c *YAMLConfigContainer) String(key string) string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WriteValue writes a new value for key.
|
||||||
func (c *YAMLConfigContainer) Set(key, val string) error {
|
func (c *YAMLConfigContainer) Set(key, val string) error {
|
||||||
c.Lock()
|
c.Lock()
|
||||||
defer c.Unlock()
|
defer c.Unlock()
|
||||||
@ -115,6 +125,7 @@ func (c *YAMLConfigContainer) Set(key, val string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DIY returns the raw value by a given key.
|
||||||
func (c *YAMLConfigContainer) DIY(key string) (v interface{}, err error) {
|
func (c *YAMLConfigContainer) DIY(key string) (v interface{}, err error) {
|
||||||
if v, ok := c.data[key]; ok {
|
if v, ok := c.data[key]; ok {
|
||||||
return v, nil
|
return v, nil
|
||||||
|
Loading…
Reference in New Issue
Block a user