2014-04-12 05:18:18 +00:00
|
|
|
// Beego (http://beego.me/)
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-04-12 05:18:18 +00:00
|
|
|
// @description beego is an open-source, high-performance web framework for the Go programming language.
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-04-12 05:18:18 +00:00
|
|
|
// @link http://github.com/astaxie/beego for the canonical source repository
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-04-12 05:18:18 +00:00
|
|
|
// @license http://github.com/astaxie/beego/blob/master/LICENSE
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-04-12 05:18:18 +00:00
|
|
|
// @authors astaxie
|
2014-01-11 06:28:11 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type fakeConfigContainer struct {
|
|
|
|
data map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *fakeConfigContainer) getData(key string) string {
|
2014-07-12 08:03:14 +00:00
|
|
|
return c.data[strings.ToLower(key)]
|
2014-01-11 06:28:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *fakeConfigContainer) Set(key, val string) error {
|
2014-07-12 08:03:14 +00:00
|
|
|
c.data[strings.ToLower(key)] = val
|
2014-01-11 06:28:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *fakeConfigContainer) String(key string) string {
|
|
|
|
return c.getData(key)
|
|
|
|
}
|
|
|
|
|
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
2014-08-07 09:24:21 +00:00
|
|
|
func (c *fakeConfigContainer) DefaultString(key string, defaultval string) string {
|
|
|
|
if v := c.getData(key); v == "" {
|
|
|
|
return defaultval
|
|
|
|
} else {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-15 09:19:03 +00:00
|
|
|
func (c *fakeConfigContainer) Strings(key string) []string {
|
|
|
|
return strings.Split(c.getData(key), ";")
|
|
|
|
}
|
|
|
|
|
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
2014-08-07 09:24:21 +00:00
|
|
|
func (c *fakeConfigContainer) DefaultStrings(key string, defaultval []string) []string {
|
|
|
|
if v := c.Strings(key); len(v) == 0 {
|
|
|
|
return defaultval
|
|
|
|
} else {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-11 06:28:11 +00:00
|
|
|
func (c *fakeConfigContainer) Int(key string) (int, error) {
|
|
|
|
return strconv.Atoi(c.getData(key))
|
|
|
|
}
|
|
|
|
|
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
2014-08-07 09:24:21 +00:00
|
|
|
func (c *fakeConfigContainer) DefaultInt(key string, defaultval int) int {
|
|
|
|
if v, err := c.Int(key); err != nil {
|
|
|
|
return defaultval
|
|
|
|
} else {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-11 06:28:11 +00:00
|
|
|
func (c *fakeConfigContainer) Int64(key string) (int64, error) {
|
|
|
|
return strconv.ParseInt(c.getData(key), 10, 64)
|
|
|
|
}
|
|
|
|
|
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
2014-08-07 09:24:21 +00:00
|
|
|
func (c *fakeConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
|
|
|
|
if v, err := c.Int64(key); err != nil {
|
|
|
|
return defaultval
|
|
|
|
} else {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-11 06:28:11 +00:00
|
|
|
func (c *fakeConfigContainer) Bool(key string) (bool, error) {
|
|
|
|
return strconv.ParseBool(c.getData(key))
|
|
|
|
}
|
|
|
|
|
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
2014-08-07 09:24:21 +00:00
|
|
|
func (c *fakeConfigContainer) DefaultBool(key string, defaultval bool) bool {
|
|
|
|
if v, err := c.Bool(key); err != nil {
|
|
|
|
return defaultval
|
|
|
|
} else {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-11 06:28:11 +00:00
|
|
|
func (c *fakeConfigContainer) Float(key string) (float64, error) {
|
|
|
|
return strconv.ParseFloat(c.getData(key), 64)
|
|
|
|
}
|
|
|
|
|
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
2014-08-07 09:24:21 +00:00
|
|
|
func (c *fakeConfigContainer) DefaultFloat(key string, defaultval float64) float64 {
|
|
|
|
if v, err := c.Float(key); err != nil {
|
|
|
|
return defaultval
|
|
|
|
} else {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-11 06:28:11 +00:00
|
|
|
func (c *fakeConfigContainer) DIY(key string) (interface{}, error) {
|
2014-07-12 08:03:14 +00:00
|
|
|
if v, ok := c.data[strings.ToLower(key)]; ok {
|
2014-01-11 06:28:11 +00:00
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
return nil, errors.New("key not find")
|
|
|
|
}
|
|
|
|
|
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
2014-08-07 09:24:21 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2014-01-11 06:28:11 +00:00
|
|
|
var _ ConfigContainer = new(fakeConfigContainer)
|
|
|
|
|
|
|
|
func NewFakeConfig() ConfigContainer {
|
|
|
|
return &fakeConfigContainer{
|
|
|
|
data: make(map[string]string),
|
|
|
|
}
|
|
|
|
}
|