Files
gf/encoding/gjson/gjson_z_unit_feature_load_test.go

419 lines
11 KiB
Go
Raw Permalink Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gjson_test
import (
2019-07-16 17:11:01 +08:00
"testing"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/test/gtest"
)
func Test_Load_JSON1(t *testing.T) {
2019-06-12 22:14:31 +08:00
data := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]}`)
// JSON
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-12 22:14:31 +08:00
j, err := gjson.LoadContent(data)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
t.Assert(j.Get("n").String(), "123456789")
t.Assert(j.Get("m").Map(), g.Map{"k": "v"})
t.Assert(j.Get("m.k").String(), "v")
t.Assert(j.Get("a").Slice(), g.Slice{1, 2, 3})
t.Assert(j.Get("a.1").Int(), 2)
2019-06-12 22:14:31 +08:00
})
// JSON
2022-02-26 21:26:30 +08:00
gtest.C(t, func(t *gtest.T) {
errData := []byte(`{"n":123456789, "m":{"k":"v"}, "a":[1,2,3]`)
_, err := gjson.LoadContentType("json", errData, true)
t.AssertNE(err, nil)
})
// JSON
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-12 22:14:31 +08:00
path := "test.json"
gfile.PutBytes(path, data)
2019-06-12 22:14:31 +08:00
defer gfile.Remove(path)
2022-02-26 21:26:30 +08:00
j, err := gjson.Load(path, true)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
t.Assert(j.Get("n").String(), "123456789")
t.Assert(j.Get("m").Map(), g.Map{"k": "v"})
t.Assert(j.Get("m.k").String(), "v")
t.Assert(j.Get("a").Slice(), g.Slice{1, 2, 3})
t.Assert(j.Get("a.1").Int(), 2)
2019-06-12 22:14:31 +08:00
})
}
func Test_Load_JSON2(t *testing.T) {
data := []byte(`{"n":123456789000000000000, "m":{"k":"v"}, "a":[1,2,3]}`)
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
j, err := gjson.LoadContent(data)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
t.Assert(j.Get("n").String(), "123456789000000000000")
t.Assert(j.Get("m").Map(), g.Map{"k": "v"})
t.Assert(j.Get("m.k").String(), "v")
t.Assert(j.Get("a").Slice(), g.Slice{1, 2, 3})
t.Assert(j.Get("a.1").Int(), 2)
})
}
func Test_Load_XML(t *testing.T) {
2019-06-12 22:14:31 +08:00
data := []byte(`<doc><a>1</a><a>2</a><a>3</a><m><k>v</k></m><n>123456789</n></doc>`)
// XML
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-12 22:14:31 +08:00
j, err := gjson.LoadContent(data)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
t.Assert(j.Get("doc.n").String(), "123456789")
t.Assert(j.Get("doc.m").Map(), g.Map{"k": "v"})
t.Assert(j.Get("doc.m.k").String(), "v")
t.Assert(j.Get("doc.a").Slice(), g.Slice{"1", "2", "3"})
t.Assert(j.Get("doc.a.1").Int(), 2)
2019-06-12 22:14:31 +08:00
})
// XML
2022-02-26 21:26:30 +08:00
gtest.C(t, func(t *gtest.T) {
j, err := gjson.LoadXml(data, true)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2022-02-26 21:26:30 +08:00
t.Assert(j.Get("doc.n").String(), "123456789")
t.Assert(j.Get("doc.m").Map(), g.Map{"k": "v"})
t.Assert(j.Get("doc.m.k").String(), "v")
t.Assert(j.Get("doc.a").Slice(), g.Slice{"1", "2", "3"})
t.Assert(j.Get("doc.a.1").Int(), 2)
})
// XML
gtest.C(t, func(t *gtest.T) {
errData := []byte(`<doc><a>1</a><a>2</a><a>3</a><m><k>v</k></m><n>123456789</n><doc>`)
_, err := gjson.LoadContentType("xml", errData, true)
t.AssertNE(err, nil)
})
// XML
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-12 22:14:31 +08:00
path := "test.xml"
gfile.PutBytes(path, data)
2019-06-12 22:14:31 +08:00
defer gfile.Remove(path)
j, err := gjson.Load(path)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
t.Assert(j.Get("doc.n").String(), "123456789")
t.Assert(j.Get("doc.m").Map(), g.Map{"k": "v"})
t.Assert(j.Get("doc.m.k").String(), "v")
t.Assert(j.Get("doc.a").Array(), g.Slice{"1", "2", "3"})
t.Assert(j.Get("doc.a.1").Int(), 2)
2019-06-12 22:14:31 +08:00
})
// XML
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
xml := `<?xml version="1.0"?>
<Output type="o">
<itotalSize>0</itotalSize>
<ipageSize>1</ipageSize>
<ipageIndex>2</ipageIndex>
<itotalRecords>GF框架</itotalRecords>
<nworkOrderDtos/>
<nworkOrderFrontXML/>
</Output>`
j, err := gjson.LoadContent(xml)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2020-03-19 22:56:12 +08:00
t.Assert(j.Get("Output.ipageIndex"), "2")
t.Assert(j.Get("Output.itotalRecords"), "GF框架")
})
}
func Test_Load_YAML1(t *testing.T) {
2019-06-12 22:14:31 +08:00
data := []byte(`
a:
- 1
- 2
- 3
m:
k: v
"n": 123456789
`)
2019-06-12 22:14:31 +08:00
// YAML
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-12 22:14:31 +08:00
j, err := gjson.LoadContent(data)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
t.Assert(j.Get("n").String(), "123456789")
t.Assert(j.Get("m").Map(), g.Map{"k": "v"})
t.Assert(j.Get("m.k").String(), "v")
t.Assert(j.Get("a").Slice(), g.Slice{1, 2, 3})
t.Assert(j.Get("a.1").Int(), 2)
2019-06-12 22:14:31 +08:00
})
// YAML
2022-02-26 21:26:30 +08:00
gtest.C(t, func(t *gtest.T) {
j, err := gjson.LoadYaml(data, true)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2022-02-26 21:26:30 +08:00
t.Assert(j.Get("n").String(), "123456789")
t.Assert(j.Get("m").Map(), g.Map{"k": "v"})
t.Assert(j.Get("m.k").String(), "v")
t.Assert(j.Get("a").Slice(), g.Slice{1, 2, 3})
t.Assert(j.Get("a.1").Int(), 2)
})
// YAML
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-12 22:14:31 +08:00
path := "test.yaml"
gfile.PutBytes(path, data)
2019-06-12 22:14:31 +08:00
defer gfile.Remove(path)
j, err := gjson.Load(path)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
t.Assert(j.Get("n").String(), "123456789")
t.Assert(j.Get("m").Map(), g.Map{"k": "v"})
t.Assert(j.Get("m.k").String(), "v")
t.Assert(j.Get("a").Slice(), g.Slice{1, 2, 3})
t.Assert(j.Get("a.1").Int(), 2)
2019-06-12 22:14:31 +08:00
})
}
func Test_Load_YAML2(t *testing.T) {
2019-06-12 22:14:31 +08:00
data := []byte("i : 123456789")
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-12 22:14:31 +08:00
j, err := gjson.LoadContent(data)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2020-03-19 22:56:12 +08:00
t.Assert(j.Get("i"), "123456789")
2019-06-12 22:14:31 +08:00
})
2022-02-26 21:26:30 +08:00
gtest.C(t, func(t *gtest.T) {
errData := []byte("i # 123456789")
_, err := gjson.LoadContentType("yaml", errData, true)
t.AssertNE(err, nil)
})
}
func Test_Load_TOML1(t *testing.T) {
2019-06-12 22:14:31 +08:00
data := []byte(`
a = ["1", "2", "3"]
2019-07-16 17:11:01 +08:00
n = 123456789
[m]
k = "v"
`)
2019-06-12 22:14:31 +08:00
// TOML
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-12 22:14:31 +08:00
j, err := gjson.LoadContent(data)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
t.Assert(j.Get("n").String(), "123456789")
t.Assert(j.Get("m").Map(), g.Map{"k": "v"})
t.Assert(j.Get("m.k").String(), "v")
t.Assert(j.Get("a").Slice(), g.Slice{"1", "2", "3"})
t.Assert(j.Get("a.1").Int(), 2)
2019-06-12 22:14:31 +08:00
})
// TOML
2022-02-26 21:26:30 +08:00
gtest.C(t, func(t *gtest.T) {
j, err := gjson.LoadToml(data, true)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2022-02-26 21:26:30 +08:00
t.Assert(j.Get("n").String(), "123456789")
t.Assert(j.Get("m").Map(), g.Map{"k": "v"})
t.Assert(j.Get("m.k").String(), "v")
t.Assert(j.Get("a").Slice(), g.Slice{"1", "2", "3"})
t.Assert(j.Get("a.1").Int(), 2)
})
// TOML
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-12 22:14:31 +08:00
path := "test.toml"
gfile.PutBytes(path, data)
2019-06-12 22:14:31 +08:00
defer gfile.Remove(path)
j, err := gjson.Load(path)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
t.Assert(j.Get("n").String(), "123456789")
t.Assert(j.Get("m").Map(), g.Map{"k": "v"})
t.Assert(j.Get("m.k").String(), "v")
t.Assert(j.Get("a").Slice(), g.Slice{"1", "2", "3"})
t.Assert(j.Get("a.1").Int(), 2)
2019-06-12 22:14:31 +08:00
})
}
func Test_Load_TOML2(t *testing.T) {
2019-06-12 22:14:31 +08:00
data := []byte("i=123456789")
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-06-12 22:14:31 +08:00
j, err := gjson.LoadContent(data)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2020-03-19 22:56:12 +08:00
t.Assert(j.Get("i"), "123456789")
2019-06-12 22:14:31 +08:00
})
2022-02-26 21:26:30 +08:00
gtest.C(t, func(t *gtest.T) {
errData := []byte("i : 123456789")
_, err := gjson.LoadContentType("toml", errData, true)
t.AssertNE(err, nil)
})
2019-06-12 22:14:31 +08:00
}
func Test_Load_Basic(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
j := gjson.New(nil)
t.Assert(j.Interface(), nil)
2019-06-12 22:14:31 +08:00
_, err := gjson.Decode(nil)
2020-03-19 22:56:12 +08:00
t.AssertNE(err, nil)
2019-06-12 22:14:31 +08:00
_, err = gjson.DecodeToJson(nil)
2020-03-19 22:56:12 +08:00
t.AssertNE(err, nil)
2019-06-12 22:14:31 +08:00
j, err = gjson.LoadContent(nil)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
t.Assert(j.Interface(), nil)
2019-06-12 22:14:31 +08:00
j, err = gjson.LoadContent(`{"name": "gf"}`)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2019-06-12 22:14:31 +08:00
j, err = gjson.LoadContent(`{"name": "gf"""}`)
2020-03-19 22:56:12 +08:00
t.AssertNE(err, nil)
2019-06-12 22:14:31 +08:00
j = gjson.New(&g.Map{"name": "gf"})
t.Assert(j.Get("name").String(), "gf")
2019-06-12 22:14:31 +08:00
})
}
2019-08-12 16:53:07 +08:00
func Test_Load_Ini(t *testing.T) {
var data = `
;注释
[addr]
2019-08-12 16:53:07 +08:00
ip = 127.0.0.1
port=9001
enable=true
[DBINFO]
type=mysql
user=root
password=password
`
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
j, err := gjson.LoadContent(data)
2019-08-12 16:53:07 +08:00
if err != nil {
gtest.Fatal(err)
}
t.Assert(j.Get("addr.ip").String(), "127.0.0.1")
t.Assert(j.Get("addr.port").String(), "9001")
t.Assert(j.Get("addr.enable").String(), "true")
t.Assert(j.Get("DBINFO.type").String(), "mysql")
t.Assert(j.Get("DBINFO.user").String(), "root")
t.Assert(j.Get("DBINFO.password").String(), "password")
2019-08-12 16:53:07 +08:00
_, err = j.ToIni()
2019-08-12 16:53:07 +08:00
if err != nil {
gtest.Fatal(err)
}
})
2022-02-26 21:26:30 +08:00
gtest.C(t, func(t *gtest.T) {
j, err := gjson.LoadIni(data, true)
if err != nil {
gtest.Fatal(err)
}
t.Assert(j.Get("addr.ip").String(), "127.0.0.1")
t.Assert(j.Get("addr.port").String(), "9001")
t.Assert(j.Get("addr.enable").String(), "true")
t.Assert(j.Get("DBINFO.type").String(), "mysql")
t.Assert(j.Get("DBINFO.user").String(), "root")
t.Assert(j.Get("DBINFO.password").String(), "password")
})
gtest.C(t, func(t *gtest.T) {
errData := []byte("i : 123456789")
_, err := gjson.LoadContentType("ini", errData, true)
t.AssertNE(err, nil)
})
2019-08-12 16:53:07 +08:00
}
func Test_Load_YamlWithV3(t *testing.T) {
content := `
# CLI tool, only in development environment.
# https://goframe.org/pages/viewpage.action?pageId=3673173
gfcli:
gen:
dao:
- path : "../../pkg/oss/oss/internal"
group : "oss"
stdTime : true
descriptionTag : true
noJsonTag : true
noModelComment : true
overwriteDao : true
modelFileForDao : "model_dao.go"
tablesEx : |
bpmn_info,
dlocker,
dlocker_detail,
message_table,
monitor_data,
resource_param_info,
version_info,
version_topology_info,
work_flow,
work_flow_step_info,
work_flow_undo_step_info
- path : "../../pkg/oss/workflow/internal"
group : "workflow"
stdTime : true
descriptionTag : true
noJsonTag : true
noModelComment : true
overwriteDao : true
modelFileForDao : "model_dao.go"
`
gtest.C(t, func(t *gtest.T) {
_, err := gjson.LoadContent(content)
t.AssertNil(err)
})
}
2022-05-12 17:05:02 +08:00
func Test_Load_Properties(t *testing.T) {
var data = `
#注释
addr.ip = 127.0.0.1
addr.port=9001
addr.enable=true
DBINFO.type=mysql
DBINFO.user=root
DBINFO.password=password
`
gtest.C(t, func(t *gtest.T) {
j, err := gjson.LoadContent(data)
if err != nil {
gtest.Fatal(err)
}
t.Assert(j.Get("addr.ip").String(), "127.0.0.1")
t.Assert(j.Get("addr.port").String(), "9001")
t.Assert(j.Get("addr.enable").String(), "true")
t.Assert(j.Get("DBINFO.type").String(), "mysql")
t.Assert(j.Get("DBINFO.user").String(), "root")
t.Assert(j.Get("DBINFO.password").String(), "password")
_, err = j.ToProperties()
if err != nil {
gtest.Fatal(err)
}
})
gtest.C(t, func(t *gtest.T) {
j, err := gjson.LoadProperties(data, true)
if err != nil {
gtest.Fatal(err)
}
t.Assert(j.Get("addr.ip").String(), "127.0.0.1")
t.Assert(j.Get("addr.port").String(), "9001")
t.Assert(j.Get("addr.enable").String(), "true")
t.Assert(j.Get("DBINFO.type").String(), "mysql")
t.Assert(j.Get("DBINFO.user").String(), "root")
t.Assert(j.Get("DBINFO.password").String(), "password")
})
gtest.C(t, func(t *gtest.T) {
errData := []byte("i\\u1 : 123456789")
_, err := gjson.LoadContentType("properties", errData, true)
t.AssertNE(err, nil)
})
}