diff --git a/g/encoding/gjson/gjson_api_new_load.go b/g/encoding/gjson/gjson_api_new_load.go index f6be8a423..220790e69 100644 --- a/g/encoding/gjson/gjson_api_new_load.go +++ b/g/encoding/gjson/gjson_api_new_load.go @@ -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 . 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 "" diff --git a/g/frame/gins/gins_config_test.go b/g/frame/gins/gins_config_test.go index cc13260bf..21f36f18d 100644 --- a/g/frame/gins/gins_config_test.go +++ b/g/frame/gins/gins_config_test.go @@ -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") + }) +} diff --git a/g/os/gcfg/gcfg.go b/g/os/gcfg/gcfg.go index adb3fa570..70c31fbe8 100644 --- a/g/os/gcfg/gcfg.go +++ b/g/os/gcfg/gcfg.go @@ -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 content. +// getJson returns a *gjson.Json object for the specified content. // It would print error if file reading fails. // If any error occurs, it return nil. func (c *Config) getJson(file ...string) *gjson.Json { diff --git a/g/os/gcfg/gcfg_z_unit_test.go b/g/os/gcfg/gcfg_z_unit_test.go index 3955383b4..cc9963daf 100644 --- a/g/os/gcfg/gcfg_z_unit_test.go +++ b/g/os/gcfg/gcfg_z_unit_test.go @@ -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 diff --git a/geg/other/test.go b/geg/other/test.go index 07fc62b41..f71bc27b8 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -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")) }