From 937f8e69190920ef147508f9b3b84b3c7e1c57d7 Mon Sep 17 00:00:00 2001 From: chenall Date: Sat, 25 Jul 2020 10:57:40 +0800 Subject: [PATCH 1/2] fix configfile with UTF8-BOM issue --- encoding/gjson/gjson_api_new_load.go | 9 ++++++++- os/gcfg/gcfg_z_unit_test.go | 15 +++++++++++++-- os/gcfg/testdata/cfg-with-utf8-bom.toml | 4 ++++ 3 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 os/gcfg/testdata/cfg-with-utf8-bom.toml diff --git a/encoding/gjson/gjson_api_new_load.go b/encoding/gjson/gjson_api_new_load.go index 9caf40081..52a5374d7 100644 --- a/encoding/gjson/gjson_api_new_load.go +++ b/encoding/gjson/gjson_api_new_load.go @@ -10,9 +10,10 @@ import ( "bytes" "errors" "fmt" - "github.com/gogf/gf/internal/json" "reflect" + "github.com/gogf/gf/internal/json" + "github.com/gogf/gf/encoding/gini" "github.com/gogf/gf/encoding/gtoml" "github.com/gogf/gf/encoding/gxml" @@ -188,6 +189,12 @@ func LoadContent(data interface{}, safe ...bool) (*Json, error) { if len(content) == 0 { return New(nil, safe...), nil } + + //ignore UTF8-BOM + if content[0] == 0xEF && content[1] == 0xBB && content[2] == 0xBF { + content = content[3:] + } + return doLoadContent(checkDataType(content), content, safe...) } diff --git a/os/gcfg/gcfg_z_unit_test.go b/os/gcfg/gcfg_z_unit_test.go index 2c81ca6a0..71653d516 100644 --- a/os/gcfg/gcfg_z_unit_test.go +++ b/os/gcfg/gcfg_z_unit_test.go @@ -9,11 +9,12 @@ package gcfg_test import ( - "github.com/gogf/gf/debug/gdebug" - "github.com/gogf/gf/os/gtime" "os" "testing" + "github.com/gogf/gf/debug/gdebug" + "github.com/gogf/gf/os/gtime" + "github.com/gogf/gf/encoding/gjson" "github.com/gogf/gf/frame/g" "github.com/gogf/gf/os/gcfg" @@ -475,3 +476,13 @@ func TestCfg_Config(t *testing.T) { t.Assert(gcfg.GetContent("name"), "") }) } + +func TestCfg_With_UTF8_BOM(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + cfg := g.Cfg() + t.Assert(cfg.SetPath("testdata"), nil) + cfg.SetFileName("cfg-with-utf8-bom.toml") + t.Assert(cfg.GetInt("test.testInt"), 1) + t.Assert(cfg.GetString("test.testStr"), "test") + }) +} diff --git a/os/gcfg/testdata/cfg-with-utf8-bom.toml b/os/gcfg/testdata/cfg-with-utf8-bom.toml new file mode 100644 index 000000000..19f8005ff --- /dev/null +++ b/os/gcfg/testdata/cfg-with-utf8-bom.toml @@ -0,0 +1,4 @@ + +[test] +testInt=1 +testStr="test" From 437fc046205a46a5f2450e1887d8572d9ce79bd6 Mon Sep 17 00:00:00 2001 From: chenall Date: Sat, 25 Jul 2020 14:07:33 +0800 Subject: [PATCH 2/2] improve testCfg_With_UTF8_BOM unit_test --- os/gcfg/gcfg_z_unit_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/os/gcfg/gcfg_z_unit_test.go b/os/gcfg/gcfg_z_unit_test.go index 71653d516..3cec43324 100644 --- a/os/gcfg/gcfg_z_unit_test.go +++ b/os/gcfg/gcfg_z_unit_test.go @@ -479,7 +479,7 @@ func TestCfg_Config(t *testing.T) { func TestCfg_With_UTF8_BOM(t *testing.T) { gtest.C(t, func(t *gtest.T) { - cfg := g.Cfg() + cfg := g.Cfg("test-cfg-with-utf8-bom") t.Assert(cfg.SetPath("testdata"), nil) cfg.SetFileName("cfg-with-utf8-bom.toml") t.Assert(cfg.GetInt("test.testInt"), 1)