mirror of
https://github.com/astaxie/beego.git
synced 2024-11-29 12:21:27 +00:00
Clean json config. Fix DefaultStrings
This commit is contained in:
parent
22671c524e
commit
572508ddd8
@ -17,13 +17,10 @@ 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.
|
||||||
@ -41,13 +38,19 @@ func (js *JsonConfig) Parse(filename string) (ConfigContainer, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return js.ParseData(content)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseData returns a ConfigContainer with json string
|
||||||
|
func (js *JsonConfig) ParseData(data []byte) (ConfigContainer, error) {
|
||||||
x := &JsonConfigContainer{
|
x := &JsonConfigContainer{
|
||||||
data: make(map[string]interface{}),
|
data: make(map[string]interface{}),
|
||||||
}
|
}
|
||||||
err = json.Unmarshal(content, &x.data)
|
err := json.Unmarshal(data, &x.data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
var wrappingArray []interface{}
|
var wrappingArray []interface{}
|
||||||
err2 := json.Unmarshal(content, &wrappingArray)
|
err2 := json.Unmarshal(data, &wrappingArray)
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -56,16 +59,6 @@ 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 {
|
||||||
@ -88,11 +81,10 @@ func (c *JsonConfigContainer) Bool(key string) (bool, error) {
|
|||||||
// DefaultBool return the bool value if has no error
|
// DefaultBool return the bool value if has no error
|
||||||
// otherwise return the defaultval
|
// otherwise return the defaultval
|
||||||
func (c *JsonConfigContainer) DefaultBool(key string, defaultval bool) bool {
|
func (c *JsonConfigContainer) DefaultBool(key string, defaultval bool) bool {
|
||||||
if v, err := c.Bool(key); err != nil {
|
if v, err := c.Bool(key); err == nil {
|
||||||
return defaultval
|
|
||||||
} else {
|
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
return defaultval
|
||||||
}
|
}
|
||||||
|
|
||||||
// Int returns the integer value for a given key.
|
// Int returns the integer value for a given key.
|
||||||
@ -110,11 +102,10 @@ func (c *JsonConfigContainer) Int(key string) (int, error) {
|
|||||||
// DefaultInt returns the integer value for a given key.
|
// DefaultInt returns the integer value for a given key.
|
||||||
// if err != nil return defaltval
|
// if err != nil return defaltval
|
||||||
func (c *JsonConfigContainer) DefaultInt(key string, defaultval int) int {
|
func (c *JsonConfigContainer) DefaultInt(key string, defaultval int) int {
|
||||||
if v, err := c.Int(key); err != nil {
|
if v, err := c.Int(key); err == nil {
|
||||||
return defaultval
|
|
||||||
} else {
|
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
return defaultval
|
||||||
}
|
}
|
||||||
|
|
||||||
// Int64 returns the int64 value for a given key.
|
// Int64 returns the int64 value for a given key.
|
||||||
@ -132,11 +123,10 @@ func (c *JsonConfigContainer) Int64(key string) (int64, error) {
|
|||||||
// DefaultInt64 returns the int64 value for a given key.
|
// DefaultInt64 returns the int64 value for a given key.
|
||||||
// if err != nil return defaltval
|
// if err != nil return defaltval
|
||||||
func (c *JsonConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
|
func (c *JsonConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
|
||||||
if v, err := c.Int64(key); err != nil {
|
if v, err := c.Int64(key); err == nil {
|
||||||
return defaultval
|
|
||||||
} else {
|
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
return defaultval
|
||||||
}
|
}
|
||||||
|
|
||||||
// Float returns the float value for a given key.
|
// Float returns the float value for a given key.
|
||||||
@ -154,11 +144,10 @@ func (c *JsonConfigContainer) Float(key string) (float64, error) {
|
|||||||
// DefaultFloat returns the float64 value for a given key.
|
// DefaultFloat returns the float64 value for a given key.
|
||||||
// if err != nil return defaltval
|
// if err != nil return defaltval
|
||||||
func (c *JsonConfigContainer) DefaultFloat(key string, defaultval float64) float64 {
|
func (c *JsonConfigContainer) DefaultFloat(key string, defaultval float64) float64 {
|
||||||
if v, err := c.Float(key); err != nil {
|
if v, err := c.Float(key); err == nil {
|
||||||
return defaultval
|
|
||||||
} else {
|
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
return defaultval
|
||||||
}
|
}
|
||||||
|
|
||||||
// String returns the string value for a given key.
|
// String returns the string value for a given key.
|
||||||
@ -175,35 +164,37 @@ func (c *JsonConfigContainer) String(key string) string {
|
|||||||
// DefaultString returns the string value for a given key.
|
// DefaultString returns the string value for a given key.
|
||||||
// if err != nil return defaltval
|
// if err != nil return defaltval
|
||||||
func (c *JsonConfigContainer) DefaultString(key string, defaultval string) string {
|
func (c *JsonConfigContainer) DefaultString(key string, defaultval string) string {
|
||||||
if v := c.String(key); v == "" {
|
// TODO FIXME should not use "" to replace non existance
|
||||||
return defaultval
|
if v := c.String(key); v != "" {
|
||||||
} else {
|
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
return defaultval
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 {
|
||||||
|
stringVal := c.String(key)
|
||||||
|
if stringVal == "" {
|
||||||
|
return []string{}
|
||||||
|
}
|
||||||
return strings.Split(c.String(key), ";")
|
return strings.Split(c.String(key), ";")
|
||||||
}
|
}
|
||||||
|
|
||||||
// DefaultStrings returns the []string value for a given key.
|
// DefaultStrings returns the []string value for a given key.
|
||||||
// if err != nil return defaltval
|
// if err != nil return defaltval
|
||||||
func (c *JsonConfigContainer) DefaultStrings(key string, defaultval []string) []string {
|
func (c *JsonConfigContainer) DefaultStrings(key string, defaultval []string) []string {
|
||||||
if v := c.Strings(key); len(v) == 0 {
|
if v := c.Strings(key); len(v) > 0 {
|
||||||
return defaultval
|
|
||||||
} else {
|
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
return defaultval
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetSection returns map for the given section
|
// GetSection returns map for the given section
|
||||||
func (c *JsonConfigContainer) GetSection(section string) (map[string]string, error) {
|
func (c *JsonConfigContainer) GetSection(section string) (map[string]string, error) {
|
||||||
if v, ok := c.data[section]; ok {
|
if v, ok := c.data[section]; ok {
|
||||||
return v.(map[string]string), nil
|
return v.(map[string]string), nil
|
||||||
} else {
|
|
||||||
return nil, errors.New("not exist setction")
|
|
||||||
}
|
}
|
||||||
|
return nil, errors.New("nonexist section " + section)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SaveConfigFile save the config into file
|
// SaveConfigFile save the config into file
|
||||||
@ -222,7 +213,7 @@ func (c *JsonConfigContainer) SaveConfigFile(filename string) (err error) {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteValue writes a new value for key.
|
// Set 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()
|
||||||
@ -241,18 +232,20 @@ func (c *JsonConfigContainer) DIY(key string) (v interface{}, err error) {
|
|||||||
|
|
||||||
// section.key or key
|
// section.key or key
|
||||||
func (c *JsonConfigContainer) getData(key string) interface{} {
|
func (c *JsonConfigContainer) getData(key string) interface{} {
|
||||||
c.RLock()
|
|
||||||
defer c.RUnlock()
|
|
||||||
if len(key) == 0 {
|
if len(key) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
sectionKey := strings.Split(key, "::")
|
|
||||||
if len(sectionKey) >= 2 {
|
c.RLock()
|
||||||
curValue, ok := c.data[sectionKey[0]]
|
defer c.RUnlock()
|
||||||
|
|
||||||
|
sectionKeys := strings.Split(key, "::")
|
||||||
|
if len(sectionKeys) >= 2 {
|
||||||
|
curValue, ok := c.data[sectionKeys[0]]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
for _, key := range sectionKey[1:] {
|
for _, key := range sectionKeys[1:] {
|
||||||
if v, ok := curValue.(map[string]interface{}); ok {
|
if v, ok := curValue.(map[string]interface{}); ok {
|
||||||
if curValue, ok = v[key]; !ok {
|
if curValue, ok = v[key]; !ok {
|
||||||
return nil
|
return nil
|
||||||
|
@ -21,6 +21,7 @@ import (
|
|||||||
|
|
||||||
var jsoncontext = `{
|
var jsoncontext = `{
|
||||||
"appname": "beeapi",
|
"appname": "beeapi",
|
||||||
|
"testnames": "foo;bar",
|
||||||
"httpport": 8080,
|
"httpport": 8080,
|
||||||
"mysqlport": 3600,
|
"mysqlport": 3600,
|
||||||
"PI": 3.1415976,
|
"PI": 3.1415976,
|
||||||
@ -122,6 +123,12 @@ func TestJson(t *testing.T) {
|
|||||||
if jsonconf.String("runmode") != "dev" {
|
if jsonconf.String("runmode") != "dev" {
|
||||||
t.Fatal("runmode not equal to dev")
|
t.Fatal("runmode not equal to dev")
|
||||||
}
|
}
|
||||||
|
if v := jsonconf.Strings("unknown"); len(v) > 0 {
|
||||||
|
t.Fatal("unknown strings, the length should be 0")
|
||||||
|
}
|
||||||
|
if v := jsonconf.Strings("testnames"); len(v) != 2 {
|
||||||
|
t.Fatal("testnames length should be 2")
|
||||||
|
}
|
||||||
if v, err := jsonconf.Bool("autorender"); err != nil || v != false {
|
if v, err := jsonconf.Bool("autorender"); err != nil || v != false {
|
||||||
t.Error(v)
|
t.Error(v)
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@ -179,4 +186,8 @@ func TestJson(t *testing.T) {
|
|||||||
if _, err := jsonconf.Bool("unknown"); err == nil {
|
if _, err := jsonconf.Bool("unknown"); err == nil {
|
||||||
t.Error("unknown keys should return an error when expecting a Bool")
|
t.Error("unknown keys should return an error when expecting a Bool")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !jsonconf.DefaultBool("unknow", true) {
|
||||||
|
t.Error("unknown keys with default value wrong")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user