1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-25 14:50:55 +00:00

config: add more method

DefaultString(key string, defaultval string) string      // support
section::key type in key string when using ini and json type;
Int,Int64,Bool,Float,DIY are same.
	DefaultStrings(key string, defaultval []string) []string //get string
slice
	DefaultInt(key string, defaultval int) int
	DefaultInt64(key string, defaultval int64) int64
	DefaultBool(key string, defaultval bool) bool
	DefaultFloat(key string, defaultval float64) float64
	DIY(key string) (interface{}, error)
	GetSection(section string) (map[string]string, error)
	SaveConfigFile(filename string) error
This commit is contained in:
astaxie 2014-08-07 17:24:21 +08:00
parent 1f6c5599aa
commit 2820f630c8
6 changed files with 503 additions and 0 deletions

View File

@ -22,12 +22,21 @@ type ConfigContainer interface {
Int64(key string) (int64, error) Int64(key string) (int64, error)
Bool(key string) (bool, error) Bool(key string) (bool, error)
Float(key string) (float64, error) Float(key string) (float64, error)
DefaultString(key string, defaultval string) string // support section::key type in key string when using ini and json type; Int,Int64,Bool,Float,DIY are same.
DefaultStrings(key string, defaultval []string) []string //get string slice
DefaultInt(key string, defaultval int) int
DefaultInt64(key string, defaultval int64) int64
DefaultBool(key string, defaultval bool) bool
DefaultFloat(key string, defaultval float64) float64
DIY(key string) (interface{}, error) DIY(key string) (interface{}, error)
GetSection(section string) (map[string]string, error)
SaveConfigFile(filename string) error
} }
// Config is the adapter interface for parsing config file to get raw data to ConfigContainer. // 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)
ParseData(data []byte) (ConfigContainer, error)
} }
var adapters = make(map[string]Config) var adapters = make(map[string]Config)
@ -54,3 +63,13 @@ func NewConfig(adapterName, fileaname string) (ConfigContainer, error) {
} }
return adapter.Parse(fileaname) return adapter.Parse(fileaname)
} }
// adapterName is ini/json/xml/yaml.
// data is the config data.
func NewConfigData(adapterName string, data []byte) (ConfigContainer, error) {
adapter, ok := adapters[adapterName]
if !ok {
return nil, fmt.Errorf("config: unknown adaptername %q (forgotten import?)", adapterName)
}
return adapter.ParseData(data)
}

View File

@ -32,26 +32,74 @@ func (c *fakeConfigContainer) String(key string) string {
return c.getData(key) return c.getData(key)
} }
func (c *fakeConfigContainer) DefaultString(key string, defaultval string) string {
if v := c.getData(key); v == "" {
return defaultval
} else {
return v
}
}
func (c *fakeConfigContainer) Strings(key string) []string { func (c *fakeConfigContainer) Strings(key string) []string {
return strings.Split(c.getData(key), ";") return strings.Split(c.getData(key), ";")
} }
func (c *fakeConfigContainer) DefaultStrings(key string, defaultval []string) []string {
if v := c.Strings(key); len(v) == 0 {
return defaultval
} else {
return v
}
}
func (c *fakeConfigContainer) Int(key string) (int, error) { func (c *fakeConfigContainer) Int(key string) (int, error) {
return strconv.Atoi(c.getData(key)) return strconv.Atoi(c.getData(key))
} }
func (c *fakeConfigContainer) DefaultInt(key string, defaultval int) int {
if v, err := c.Int(key); err != nil {
return defaultval
} else {
return v
}
}
func (c *fakeConfigContainer) Int64(key string) (int64, error) { func (c *fakeConfigContainer) Int64(key string) (int64, error) {
return strconv.ParseInt(c.getData(key), 10, 64) return strconv.ParseInt(c.getData(key), 10, 64)
} }
func (c *fakeConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
if v, err := c.Int64(key); err != nil {
return defaultval
} else {
return v
}
}
func (c *fakeConfigContainer) Bool(key string) (bool, error) { func (c *fakeConfigContainer) Bool(key string) (bool, error) {
return strconv.ParseBool(c.getData(key)) return strconv.ParseBool(c.getData(key))
} }
func (c *fakeConfigContainer) DefaultBool(key string, defaultval bool) bool {
if v, err := c.Bool(key); err != nil {
return defaultval
} else {
return v
}
}
func (c *fakeConfigContainer) Float(key string) (float64, error) { func (c *fakeConfigContainer) Float(key string) (float64, error) {
return strconv.ParseFloat(c.getData(key), 64) return strconv.ParseFloat(c.getData(key), 64)
} }
func (c *fakeConfigContainer) DefaultFloat(key string, defaultval float64) float64 {
if v, err := c.Float(key); err != nil {
return defaultval
} else {
return v
}
}
func (c *fakeConfigContainer) DIY(key string) (interface{}, error) { func (c *fakeConfigContainer) DIY(key string) (interface{}, error) {
if v, ok := c.data[strings.ToLower(key)]; ok { if v, ok := c.data[strings.ToLower(key)]; ok {
return v, nil return v, nil
@ -59,6 +107,14 @@ func (c *fakeConfigContainer) DIY(key string) (interface{}, error) {
return nil, errors.New("key not find") return nil, errors.New("key not find")
} }
func (c *fakeConfigContainer) GetSection(section string) (map[string]string, error) {
return nil, errors.New("not implement in the fakeConfigContainer")
}
func (c *fakeConfigContainer) SaveConfigFile(filename string) error {
return errors.New("not implement in the fakeConfigContainer")
}
var _ ConfigContainer = new(fakeConfigContainer) var _ ConfigContainer = new(fakeConfigContainer)
func NewFakeConfig() ConfigContainer { func NewFakeConfig() ConfigContainer {

View File

@ -13,11 +13,15 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"errors" "errors"
"fmt"
"io" "io"
"io/ioutil"
"os" "os"
"path"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"time"
"unicode" "unicode"
) )
@ -30,6 +34,7 @@ var (
bDQuote = []byte{'"'} // quote signal bDQuote = []byte{'"'} // quote signal
sectionStart = []byte{'['} // section start signal sectionStart = []byte{'['} // section start signal
sectionEnd = []byte{']'} // section end signal sectionEnd = []byte{']'} // section end signal
lineBreak = "\n"
) )
// IniConfig implements Config to parse ini file. // IniConfig implements Config to parse ini file.
@ -115,6 +120,16 @@ func (ini *IniConfig) Parse(name string) (ConfigContainer, error) {
return cfg, nil return cfg, nil
} }
func (ini *IniConfig) ParseData(data []byte) (ConfigContainer, error) {
// Save memory data to temporary file
tmpName := path.Join(os.TempDir(), "beego", fmt.Sprintf("%d", time.Now().Nanosecond()))
os.MkdirAll(path.Dir(tmpName), os.ModePerm)
if err := ioutil.WriteFile(tmpName, data, 0655); err != nil {
return nil, err
}
return ini.Parse(tmpName)
}
// A Config represents the ini configuration. // A Config represents the ini configuration.
// When set and get value, support key as section:name type. // When set and get value, support key as section:name type.
type IniConfigContainer struct { type IniConfigContainer struct {
@ -130,32 +145,154 @@ func (c *IniConfigContainer) Bool(key string) (bool, error) {
return strconv.ParseBool(c.getdata(strings.ToLower(key))) return strconv.ParseBool(c.getdata(strings.ToLower(key)))
} }
// DefaultBool returns the boolean value for a given key.
// if err != nil return defaltval
func (c *IniConfigContainer) DefaultBool(key string, defaultval bool) bool {
if v, err := c.Bool(key); err != nil {
return defaultval
} else {
return v
}
}
// Int returns the integer value for a given key. // Int returns the integer value for a given key.
func (c *IniConfigContainer) Int(key string) (int, error) { func (c *IniConfigContainer) Int(key string) (int, error) {
return strconv.Atoi(c.getdata(strings.ToLower(key))) return strconv.Atoi(c.getdata(strings.ToLower(key)))
} }
// DefaultInt returns the integer value for a given key.
// if err != nil return defaltval
func (c *IniConfigContainer) DefaultInt(key string, defaultval int) int {
if v, err := c.Int(key); err != nil {
return defaultval
} else {
return v
}
}
// Int64 returns the int64 value for a given 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) {
return strconv.ParseInt(c.getdata(strings.ToLower(key)), 10, 64) return strconv.ParseInt(c.getdata(strings.ToLower(key)), 10, 64)
} }
// DefaultInt64 returns the int64 value for a given key.
// if err != nil return defaltval
func (c *IniConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
if v, err := c.Int64(key); err != nil {
return defaultval
} else {
return v
}
}
// Float returns the float value for a given key. // Float returns the float value for a given key.
func (c *IniConfigContainer) Float(key string) (float64, error) { func (c *IniConfigContainer) Float(key string) (float64, error) {
return strconv.ParseFloat(c.getdata(strings.ToLower(key)), 64) return strconv.ParseFloat(c.getdata(strings.ToLower(key)), 64)
} }
// DefaultFloat returns the float64 value for a given key.
// if err != nil return defaltval
func (c *IniConfigContainer) DefaultFloat(key string, defaultval float64) float64 {
if v, err := c.Float(key); err != nil {
return defaultval
} else {
return v
}
}
// String returns the string value for a given key. // String returns the string value for a given key.
func (c *IniConfigContainer) String(key string) string { func (c *IniConfigContainer) String(key string) string {
key = strings.ToLower(key) key = strings.ToLower(key)
return c.getdata(strings.ToLower(key)) return c.getdata(strings.ToLower(key))
} }
// DefaultString returns the string value for a given key.
// if err != nil return defaltval
func (c *IniConfigContainer) DefaultString(key string, defaultval string) string {
if v := c.String(key); v == "" {
return defaultval
} else {
return v
}
}
// Strings returns the []string value for a given key. // Strings returns the []string value for a given key.
func (c *IniConfigContainer) Strings(key string) []string { func (c *IniConfigContainer) Strings(key string) []string {
return strings.Split(c.String(key), ";") return strings.Split(c.String(key), ";")
} }
// DefaultStrings returns the []string value for a given key.
// if err != nil return defaltval
func (c *IniConfigContainer) DefaultStrings(key string, defaultval []string) []string {
if v := c.Strings(key); len(v) == 0 {
return defaultval
} else {
return v
}
}
// GetSection returns map for the given section
func (c *IniConfigContainer) GetSection(section string) (map[string]string, error) {
if v, ok := c.data[section]; ok {
return v, nil
} else {
return nil, errors.New("not exist setction")
}
}
// SaveConfigFile save the config into file
func (c *IniConfigContainer) SaveConfigFile(filename string) (err error) {
// Write configuration file by filename.
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
buf := bytes.NewBuffer(nil)
for section, dt := range c.data {
// Write section comments.
if v, ok := c.sectionComment[section]; ok {
if _, err = buf.WriteString(string(bNumComment) + v + lineBreak); err != nil {
return err
}
}
if section != DEFAULT_SECTION {
// Write section name.
if _, err = buf.WriteString(string(sectionStart) + section + string(sectionEnd) + lineBreak); err != nil {
return err
}
}
for key, val := range dt {
if key != " " {
// Write key comments.
if v, ok := c.keyComment[key]; ok {
if _, err = buf.WriteString(string(bNumComment) + v + lineBreak); err != nil {
return err
}
}
// Write key and value.
if _, err = buf.WriteString(key + string(bEqual) + val + lineBreak); err != nil {
return err
}
}
}
// Put a line between sections.
if _, err = buf.WriteString(lineBreak); err != nil {
return err
}
}
if _, err = buf.WriteTo(f); err != nil {
return err
}
return nil
}
// 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 write to one section, the key need be "section::key".
// if the section is not existed, it panics. // if the section is not existed, it panics.

View File

@ -12,10 +12,13 @@ package config
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"path"
"strings" "strings"
"sync" "sync"
"time"
) )
// JsonConfig is a json config parser and implements Config interface. // JsonConfig is a json config parser and implements Config interface.
@ -48,6 +51,16 @@ func (js *JsonConfig) Parse(filename string) (ConfigContainer, error) {
return x, nil return x, nil
} }
func (js *JsonConfig) ParseData(data []byte) (ConfigContainer, error) {
// Save memory data to temporary file
tmpName := path.Join(os.TempDir(), "beego", fmt.Sprintf("%d", time.Now().Nanosecond()))
os.MkdirAll(path.Dir(tmpName), os.ModePerm)
if err := ioutil.WriteFile(tmpName, data, 0655); err != nil {
return nil, err
}
return js.Parse(tmpName)
}
// A Config represents the json configuration. // A Config represents the json configuration.
// Only when get value, support key as section:name type. // Only when get value, support key as section:name type.
type JsonConfigContainer struct { type JsonConfigContainer struct {
@ -67,6 +80,16 @@ func (c *JsonConfigContainer) Bool(key string) (bool, error) {
return false, errors.New("not exist key:" + key) return false, errors.New("not exist key:" + key)
} }
// DefaultBool return the bool value if has no error
// otherwise return the defaultval
func (c *JsonConfigContainer) DefaultBool(key string, defaultval bool) bool {
if v, err := c.Bool(key); err != nil {
return defaultval
} else {
return v
}
}
// Int returns the integer value for a given key. // 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)
@ -79,6 +102,16 @@ func (c *JsonConfigContainer) Int(key string) (int, error) {
return 0, errors.New("not exist key:" + key) return 0, errors.New("not exist key:" + key)
} }
// DefaultInt returns the integer value for a given key.
// if err != nil return defaltval
func (c *JsonConfigContainer) DefaultInt(key string, defaultval int) int {
if v, err := c.Int(key); err != nil {
return defaultval
} else {
return v
}
}
// Int64 returns the int64 value for a given key. // 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)
@ -91,6 +124,16 @@ func (c *JsonConfigContainer) Int64(key string) (int64, error) {
return 0, errors.New("not exist key:" + key) return 0, errors.New("not exist key:" + key)
} }
// DefaultInt64 returns the int64 value for a given key.
// if err != nil return defaltval
func (c *JsonConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
if v, err := c.Int64(key); err != nil {
return defaultval
} else {
return v
}
}
// Float returns the float value for a given key. // 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)
@ -103,6 +146,16 @@ func (c *JsonConfigContainer) Float(key string) (float64, error) {
return 0.0, errors.New("not exist key:" + key) return 0.0, errors.New("not exist key:" + key)
} }
// DefaultFloat returns the float64 value for a given key.
// if err != nil return defaltval
func (c *JsonConfigContainer) DefaultFloat(key string, defaultval float64) float64 {
if v, err := c.Float(key); err != nil {
return defaultval
} else {
return v
}
}
// String returns the string value for a given key. // 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)
@ -114,11 +167,56 @@ func (c *JsonConfigContainer) String(key string) string {
return "" return ""
} }
// DefaultString returns the string value for a given key.
// if err != nil return defaltval
func (c *JsonConfigContainer) DefaultString(key string, defaultval string) string {
if v := c.String(key); v == "" {
return defaultval
} else {
return v
}
}
// Strings returns the []string value for a given key. // Strings returns the []string value for a given key.
func (c *JsonConfigContainer) Strings(key string) []string { func (c *JsonConfigContainer) Strings(key string) []string {
return strings.Split(c.String(key), ";") return strings.Split(c.String(key), ";")
} }
// DefaultStrings returns the []string value for a given key.
// if err != nil return defaltval
func (c *JsonConfigContainer) DefaultStrings(key string, defaultval []string) []string {
if v := c.Strings(key); len(v) == 0 {
return defaultval
} else {
return v
}
}
// GetSection returns map for the given section
func (c *JsonConfigContainer) GetSection(section string) (map[string]string, error) {
if v, ok := c.data[section]; ok {
return v.(map[string]string), nil
} else {
return nil, errors.New("not exist setction")
}
}
// SaveConfigFile save the config into file
func (c *JsonConfigContainer) SaveConfigFile(filename string) (err error) {
// Write configuration file by filename.
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
b, err := json.MarshalIndent(c.data, "", " ")
if err != nil {
return err
}
_, err = f.Write(b)
return err
}
// WriteValue writes a new value for key. // 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()

View File

@ -10,12 +10,16 @@
package xml package xml
import ( import (
"encoding/xml"
"errors" "errors"
"fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"path"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"time"
"github.com/astaxie/beego/config" "github.com/astaxie/beego/config"
"github.com/beego/x2j" "github.com/beego/x2j"
@ -49,6 +53,16 @@ func (xc *XMLConfig) Parse(filename string) (config.ConfigContainer, error) {
return x, nil return x, nil
} }
func (x *XMLConfig) ParseData(data []byte) (config.ConfigContainer, error) {
// Save memory data to temporary file
tmpName := path.Join(os.TempDir(), "beego", fmt.Sprintf("%d", time.Now().Nanosecond()))
os.MkdirAll(path.Dir(tmpName), os.ModePerm)
if err := ioutil.WriteFile(tmpName, data, 0655); err != nil {
return nil, err
}
return x.Parse(tmpName)
}
// A Config represents the xml configuration. // A Config represents the xml configuration.
type XMLConfigContainer struct { type XMLConfigContainer struct {
data map[string]interface{} data map[string]interface{}
@ -60,21 +74,61 @@ func (c *XMLConfigContainer) Bool(key string) (bool, error) {
return strconv.ParseBool(c.data[key].(string)) return strconv.ParseBool(c.data[key].(string))
} }
// DefaultBool return the bool value if has no error
// otherwise return the defaultval
func (c *XMLConfigContainer) DefaultBool(key string, defaultval bool) bool {
if v, err := c.Bool(key); err != nil {
return defaultval
} else {
return v
}
}
// Int returns the integer value for a given key. // 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))
} }
// DefaultInt returns the integer value for a given key.
// if err != nil return defaltval
func (c *XMLConfigContainer) DefaultInt(key string, defaultval int) int {
if v, err := c.Int(key); err != nil {
return defaultval
} else {
return v
}
}
// Int64 returns the int64 value for a given key. // 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)
} }
// DefaultInt64 returns the int64 value for a given key.
// if err != nil return defaltval
func (c *XMLConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
if v, err := c.Int64(key); err != nil {
return defaultval
} else {
return v
}
}
// Float returns the float value for a given key. // 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)
} }
// DefaultFloat returns the float64 value for a given key.
// if err != nil return defaltval
func (c *XMLConfigContainer) DefaultFloat(key string, defaultval float64) float64 {
if v, err := c.Float(key); err != nil {
return defaultval
} else {
return v
}
}
// String returns the string value for a given key. // 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 {
@ -83,11 +137,56 @@ func (c *XMLConfigContainer) String(key string) string {
return "" return ""
} }
// DefaultString returns the string value for a given key.
// if err != nil return defaltval
func (c *XMLConfigContainer) DefaultString(key string, defaultval string) string {
if v := c.String(key); v == "" {
return defaultval
} else {
return v
}
}
// Strings returns the []string value for a given key. // Strings returns the []string value for a given key.
func (c *XMLConfigContainer) Strings(key string) []string { func (c *XMLConfigContainer) Strings(key string) []string {
return strings.Split(c.String(key), ";") return strings.Split(c.String(key), ";")
} }
// DefaultStrings returns the []string value for a given key.
// if err != nil return defaltval
func (c *XMLConfigContainer) DefaultStrings(key string, defaultval []string) []string {
if v := c.Strings(key); len(v) == 0 {
return defaultval
} else {
return v
}
}
// GetSection returns map for the given section
func (c *XMLConfigContainer) GetSection(section string) (map[string]string, error) {
if v, ok := c.data[section]; ok {
return v.(map[string]string), nil
} else {
return nil, errors.New("not exist setction")
}
}
// SaveConfigFile save the config into file
func (c *XMLConfigContainer) SaveConfigFile(filename string) (err error) {
// Write configuration file by filename.
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
b, err := xml.MarshalIndent(c.data, " ", " ")
if err != nil {
return err
}
_, err = f.Write(b)
return err
}
// WriteValue writes a new value for key. // 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()

View File

@ -13,11 +13,14 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
"path"
"strings" "strings"
"sync" "sync"
"time"
"github.com/astaxie/beego/config" "github.com/astaxie/beego/config"
"github.com/beego/goyaml2" "github.com/beego/goyaml2"
@ -38,6 +41,16 @@ func (yaml *YAMLConfig) Parse(filename string) (y config.ConfigContainer, err er
return return
} }
func (yaml *YAMLConfig) ParseData(data []byte) (config.ConfigContainer, error) {
// Save memory data to temporary file
tmpName := path.Join(os.TempDir(), "beego", fmt.Sprintf("%d", time.Now().Nanosecond()))
os.MkdirAll(path.Dir(tmpName), os.ModePerm)
if err := ioutil.WriteFile(tmpName, data, 0655); err != nil {
return nil, err
}
return yaml.Parse(tmpName)
}
// Read yaml file to map. // Read yaml file to map.
// if json like, use json package, unless goyaml2 package. // 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) {
@ -93,6 +106,16 @@ func (c *YAMLConfigContainer) Bool(key string) (bool, error) {
return false, errors.New("not bool value") return false, errors.New("not bool value")
} }
// DefaultBool return the bool value if has no error
// otherwise return the defaultval
func (c *YAMLConfigContainer) DefaultBool(key string, defaultval bool) bool {
if v, err := c.Bool(key); err != nil {
return defaultval
} else {
return v
}
}
// Int returns the integer value for a given key. // 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 {
@ -101,6 +124,16 @@ func (c *YAMLConfigContainer) Int(key string) (int, error) {
return 0, errors.New("not int value") return 0, errors.New("not int value")
} }
// DefaultInt returns the integer value for a given key.
// if err != nil return defaltval
func (c *YAMLConfigContainer) DefaultInt(key string, defaultval int) int {
if v, err := c.Int(key); err != nil {
return defaultval
} else {
return v
}
}
// Int64 returns the int64 value for a given key. // 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 {
@ -109,6 +142,16 @@ func (c *YAMLConfigContainer) Int64(key string) (int64, error) {
return 0, errors.New("not bool value") return 0, errors.New("not bool value")
} }
// DefaultInt64 returns the int64 value for a given key.
// if err != nil return defaltval
func (c *YAMLConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
if v, err := c.Int64(key); err != nil {
return defaultval
} else {
return v
}
}
// Float returns the float value for a given key. // 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 {
@ -117,6 +160,16 @@ func (c *YAMLConfigContainer) Float(key string) (float64, error) {
return 0.0, errors.New("not float64 value") return 0.0, errors.New("not float64 value")
} }
// DefaultFloat returns the float64 value for a given key.
// if err != nil return defaltval
func (c *YAMLConfigContainer) DefaultFloat(key string, defaultval float64) float64 {
if v, err := c.Float(key); err != nil {
return defaultval
} else {
return v
}
}
// String returns the string value for a given key. // 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 {
@ -125,11 +178,52 @@ func (c *YAMLConfigContainer) String(key string) string {
return "" return ""
} }
// DefaultString returns the string value for a given key.
// if err != nil return defaltval
func (c *YAMLConfigContainer) DefaultString(key string, defaultval string) string {
if v := c.String(key); v == "" {
return defaultval
} else {
return v
}
}
// Strings returns the []string value for a given key. // Strings returns the []string value for a given key.
func (c *YAMLConfigContainer) Strings(key string) []string { func (c *YAMLConfigContainer) Strings(key string) []string {
return strings.Split(c.String(key), ";") return strings.Split(c.String(key), ";")
} }
// DefaultStrings returns the []string value for a given key.
// if err != nil return defaltval
func (c *YAMLConfigContainer) DefaultStrings(key string, defaultval []string) []string {
if v := c.Strings(key); len(v) == 0 {
return defaultval
} else {
return v
}
}
// GetSection returns map for the given section
func (c *YAMLConfigContainer) GetSection(section string) (map[string]string, error) {
if v, ok := c.data[section]; ok {
return v.(map[string]string), nil
} else {
return nil, errors.New("not exist setction")
}
}
// SaveConfigFile save the config into file
func (c *YAMLConfigContainer) SaveConfigFile(filename string) (err error) {
// Write configuration file by filename.
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
err = goyaml2.Write(f, c.data)
return err
}
// WriteValue writes a new value for key. // 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()