fix issue in gcfg for config content loading

This commit is contained in:
john
2019-07-22 21:03:54 +08:00
parent c71b9bc122
commit e61bd174c8
5 changed files with 41 additions and 8 deletions

View File

@ -156,6 +156,9 @@ func doLoadContent(dataType string, data []byte, unsafe ...bool) (*Json, error)
if len(data) == 0 {
return New(nil, unsafe...), nil
}
if dataType == "" {
dataType = checkDataType(data)
}
switch dataType {
case "json", ".json":
@ -203,14 +206,15 @@ func LoadContent(data interface{}, unsafe ...bool) (*Json, error) {
}
// checkDataType automatically checks and returns the data type for <content>.
func checkDataType(content []byte) string {
if json.Valid(content) {
return "json"
} else if gregex.IsMatch(`^<.+>[\S\s]+<.+>$`, content) {
return "xml"
} else if gregex.IsMatch(`^[\s\t]*\w+\s*:\s*.+`, content) || gregex.IsMatch(`\n[\s\t]*\w+\s*:\s*.+`, content) {
} else if gregex.IsMatch(`^[\s\t]*[\w\-]+\s*:\s*.+`, content) || gregex.IsMatch(`\n[\s\t]*[\w\-]+\s*:\s*.+`, content) {
return "yml"
} else if gregex.IsMatch(`^[\s\t]*\w+\s*=\s*.+`, content) || gregex.IsMatch(`\n[\s\t]*\w+\s*=\s*.+`, content) {
} else if gregex.IsMatch(`^[\s\t]*[\w\-]+\s*=\s*.+`, content) || gregex.IsMatch(`\n[\s\t]*[\w\-]+\s*=\s*.+`, content) {
return "toml"
} else {
return ""

View File

@ -8,6 +8,7 @@ package gins_test
import (
"fmt"
"github.com/gogf/gf/g/os/gcfg"
"testing"
"time"
@ -167,3 +168,17 @@ test = "v=1"
gtest.Assert(gins.Config("test").Get("redis.disk"), "127.0.0.1:6379,0")
})
}
func Test_Basic2(t *testing.T) {
config := `log-path = "logs"`
gtest.Case(t, func() {
path := gcfg.DEFAULT_CONFIG_FILE
err := gfile.PutContents(path, config)
gtest.Assert(err, nil)
defer func() {
_ = gfile.Remove(path)
}()
gtest.Assert(gins.Config().Get("log-path"), "logs")
})
}

View File

@ -36,8 +36,7 @@ type Config struct {
name *gtype.String // Default configuration file name.
paths *garray.StringArray // Searching path array.
jsons *gmap.StrAnyMap // The pared JSON objects for configuration files.
vc *gtype.Bool // Whether do violence check in value index searching.
// It affects the performance when set true(false in default).
vc *gtype.Bool // Whether do violence check in value index searching. It affects the performance when set true(false in default).
}
// New returns a new configuration management object.
@ -259,7 +258,7 @@ func (c *Config) GetFileName() string {
return c.name.Val()
}
// getJson returns a gjson.Json object for the specified <file> content.
// getJson returns a *gjson.Json object for the specified <file> content.
// It would print error if file reading fails.
// If any error occurs, it return nil.
func (c *Config) getJson(file ...string) *gjson.Json {

View File

@ -24,7 +24,7 @@ func init() {
os.Setenv("GF_GCFG_ERRORPRINT", "false")
}
func Test_Basic(t *testing.T) {
func Test_Basic1(t *testing.T) {
config := `
v1 = 1
v2 = "true"
@ -87,6 +87,21 @@ array = [1,2,3]
})
}
func Test_Basic2(t *testing.T) {
config := `log-path = "logs"`
gtest.Case(t, func() {
path := gcfg.DEFAULT_CONFIG_FILE
err := gfile.PutContents(path, config)
gtest.Assert(err, nil)
defer func() {
_ = gfile.Remove(path)
}()
c := gcfg.New()
gtest.Assert(c.Get("log-path"), "logs")
})
}
func Test_Content(t *testing.T) {
content := `
v1 = 1

View File

@ -1,10 +1,10 @@
package main
import (
"encoding/json"
"fmt"
"github.com/gogf/gf/g"
)
func main() {
fmt.Println(json.Marshal(nil))
fmt.Println(g.Config().Get("log-path"))
}