1
0
mirror of https://github.com/astaxie/beego.git synced 2025-06-14 19:20:40 +00:00

Depracated config module and recommend using pkg/config

This commit is contained in:
Ming Deng
2020-08-08 13:26:30 +00:00
parent f9a3eae9d5
commit 2e192e1ed0
7 changed files with 121 additions and 16 deletions

View File

@ -46,9 +46,11 @@ import (
// Config 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.
// Deprecated: using pkg/config, we will delete this in v2.1.0
type Config struct{}
// Parse returns a ConfigContainer with parsed xml config map.
// Deprecated: using pkg/config, we will delete this in v2.1.0
func (xc *Config) Parse(filename string) (config.Configer, error) {
context, err := ioutil.ReadFile(filename)
if err != nil {
@ -59,6 +61,7 @@ func (xc *Config) Parse(filename string) (config.Configer, error) {
}
// ParseData xml data
// Deprecated: using pkg/config, we will delete this in v2.1.0
func (xc *Config) ParseData(data []byte) (config.Configer, error) {
x := &ConfigContainer{data: make(map[string]interface{})}
@ -73,12 +76,14 @@ func (xc *Config) ParseData(data []byte) (config.Configer, error) {
}
// ConfigContainer A Config represents the xml configuration.
// Deprecated: using pkg/config, we will delete this in v2.1.0
type ConfigContainer struct {
data map[string]interface{}
sync.Mutex
}
// Bool returns the boolean value for a given key.
// Deprecated: using pkg/config, we will delete this in v2.1.0
func (c *ConfigContainer) Bool(key string) (bool, error) {
if v := c.data[key]; v != nil {
return config.ParseBool(v)
@ -88,6 +93,7 @@ func (c *ConfigContainer) Bool(key string) (bool, error) {
// DefaultBool return the bool value if has no error
// otherwise return the defaultval
// Deprecated: using pkg/config, we will delete this in v2.1.0
func (c *ConfigContainer) DefaultBool(key string, defaultval bool) bool {
v, err := c.Bool(key)
if err != nil {
@ -97,12 +103,14 @@ func (c *ConfigContainer) DefaultBool(key string, defaultval bool) bool {
}
// Int returns the integer value for a given key.
// Deprecated: using pkg/config, we will delete this in v2.1.0
func (c *ConfigContainer) Int(key string) (int, error) {
return strconv.Atoi(c.data[key].(string))
}
// DefaultInt returns the integer value for a given key.
// if err != nil return defaultval
// Deprecated: using pkg/config, we will delete this in v2.1.0
func (c *ConfigContainer) DefaultInt(key string, defaultval int) int {
v, err := c.Int(key)
if err != nil {
@ -112,12 +120,14 @@ func (c *ConfigContainer) DefaultInt(key string, defaultval int) int {
}
// Int64 returns the int64 value for a given key.
// Deprecated: using pkg/config, we will delete this in v2.1.0
func (c *ConfigContainer) Int64(key string) (int64, error) {
return strconv.ParseInt(c.data[key].(string), 10, 64)
}
// DefaultInt64 returns the int64 value for a given key.
// if err != nil return defaultval
// Deprecated: using pkg/config, we will delete this in v2.1.0
func (c *ConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
v, err := c.Int64(key)
if err != nil {
@ -128,12 +138,14 @@ func (c *ConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
}
// Float returns the float value for a given key.
// Deprecated: using pkg/config, we will delete this in v2.1.0
func (c *ConfigContainer) Float(key string) (float64, error) {
return strconv.ParseFloat(c.data[key].(string), 64)
}
// DefaultFloat returns the float64 value for a given key.
// if err != nil return defaultval
// Deprecated: using pkg/config, we will delete this in v2.1.0
func (c *ConfigContainer) DefaultFloat(key string, defaultval float64) float64 {
v, err := c.Float(key)
if err != nil {
@ -143,6 +155,7 @@ func (c *ConfigContainer) DefaultFloat(key string, defaultval float64) float64 {
}
// String returns the string value for a given key.
// Deprecated: using pkg/config, we will delete this in v2.1.0
func (c *ConfigContainer) String(key string) string {
if v, ok := c.data[key].(string); ok {
return v
@ -152,6 +165,7 @@ func (c *ConfigContainer) String(key string) string {
// DefaultString returns the string value for a given key.
// if err != nil return defaultval
// Deprecated: using pkg/config, we will delete this in v2.1.0
func (c *ConfigContainer) DefaultString(key string, defaultval string) string {
v := c.String(key)
if v == "" {
@ -161,6 +175,7 @@ func (c *ConfigContainer) DefaultString(key string, defaultval string) string {
}
// Strings returns the []string value for a given key.
// Deprecated: using pkg/config, we will delete this in v2.1.0
func (c *ConfigContainer) Strings(key string) []string {
v := c.String(key)
if v == "" {
@ -171,6 +186,7 @@ func (c *ConfigContainer) Strings(key string) []string {
// DefaultStrings returns the []string value for a given key.
// if err != nil return defaultval
// Deprecated: using pkg/config, we will delete this in v2.1.0
func (c *ConfigContainer) DefaultStrings(key string, defaultval []string) []string {
v := c.Strings(key)
if v == nil {
@ -180,6 +196,7 @@ func (c *ConfigContainer) DefaultStrings(key string, defaultval []string) []stri
}
// GetSection returns map for the given section
// Deprecated: using pkg/config, we will delete this in v2.1.0
func (c *ConfigContainer) GetSection(section string) (map[string]string, error) {
if v, ok := c.data[section].(map[string]interface{}); ok {
mapstr := make(map[string]string)
@ -192,6 +209,7 @@ func (c *ConfigContainer) GetSection(section string) (map[string]string, error)
}
// SaveConfigFile save the config into file
// Deprecated: using pkg/config, we will delete this in v2.1.0
func (c *ConfigContainer) SaveConfigFile(filename string) (err error) {
// Write configuration file by filename.
f, err := os.Create(filename)
@ -208,6 +226,7 @@ func (c *ConfigContainer) SaveConfigFile(filename string) (err error) {
}
// Set writes a new value for key.
// Deprecated: using pkg/config, we will delete this in v2.1.0
func (c *ConfigContainer) Set(key, val string) error {
c.Lock()
defer c.Unlock()
@ -216,6 +235,7 @@ func (c *ConfigContainer) Set(key, val string) error {
}
// DIY returns the raw value by a given key.
// Deprecated: using pkg/config, we will delete this in v2.1.0
func (c *ConfigContainer) DIY(key string) (v interface{}, err error) {
if v, ok := c.data[key]; ok {
return v, nil