From 4fc27f6509c91292134e11a5c79a895b59489d6a Mon Sep 17 00:00:00 2001 From: huangqian Date: Tue, 15 Feb 2022 22:08:51 +0800 Subject: [PATCH] Implemented gjson Example 1. ExampleLoad 2.ExampleLoadJson 3.ExampleLoadXml 4.ExampleLoadIni 5.ExampleLoadYaml 6.ExampleLoadToml 7.ExampleLoadContent 8.ExampleLoadContentType 9.ExampleIsValidDataType --- encoding/gjson/gjson_z_example_load_test.go | 150 ++++++++++++++++++-- encoding/gjson/gjson_z_example_new_test.go | 89 ++++-------- encoding/gjson/testdata/toml/data1.toml | 3 + encoding/gjson/testdata/yaml/data1.yaml | 3 + 4 files changed, 173 insertions(+), 72 deletions(-) create mode 100644 encoding/gjson/testdata/toml/data1.toml create mode 100644 encoding/gjson/testdata/yaml/data1.yaml diff --git a/encoding/gjson/gjson_z_example_load_test.go b/encoding/gjson/gjson_z_example_load_test.go index 84deb1fee..fe5e48fe7 100644 --- a/encoding/gjson/gjson_z_example_load_test.go +++ b/encoding/gjson/gjson_z_example_load_test.go @@ -13,11 +13,149 @@ import ( "github.com/gogf/gf/v2/encoding/gjson" ) -func Example_loadJson() { +func ExampleLoad() { jsonFilePath := gdebug.TestDataPath("json", "data1.json") j, _ := gjson.Load(jsonFilePath) fmt.Println(j.Get("name")) fmt.Println(j.Get("score")) + + // Output: + // john + // 100 +} + +func ExampleLoadJson() { + jsonContent := `{"name":"john", "score":"100"}` + j, _ := gjson.LoadJson(jsonContent) + fmt.Println(j.Get("name")) + fmt.Println(j.Get("score")) + + // Output: + // john + // 100 +} + +func ExampleLoadXml() { + xmlContent := ` + + john + 100 + ` + j, _ := gjson.LoadXml(xmlContent) + fmt.Println(j.Get("base.name")) + fmt.Println(j.Get("base.score")) + + // Output: + // john + // 100 +} + +func ExampleLoadIni() { + iniContent := ` + [base] + name = john + score = 100 + ` + j, _ := gjson.LoadIni(iniContent) + fmt.Println(j.Get("base.name")) + fmt.Println(j.Get("base.score")) + + // Output: + // john + // 100 +} + +func ExampleLoadYaml() { + yamlContent := + `base: + name: john + score: 100` + + j, _ := gjson.LoadYaml(yamlContent) + fmt.Println(j.Get("base.name")) + fmt.Println(j.Get("base.score")) + + // Output: + // john + // 100 +} + +func ExampleLoadToml() { + tomlContent := + `[base] + name = "john" + score = 100` + + j, _ := gjson.LoadToml(tomlContent) + fmt.Println(j.Get("base.name")) + fmt.Println(j.Get("base.score")) + + // Output: + // john + // 100 +} + +func ExampleLoadContent() { + jsonContent := `{"name":"john", "score":"100"}` + xmlContent := ` + + john + 100 + ` + + j, _ := gjson.LoadContent(jsonContent) + x, _ := gjson.LoadContent(xmlContent) + + fmt.Println(j.Get("name")) + fmt.Println(j.Get("score")) + fmt.Println(x.Get("base.name")) + fmt.Println(x.Get("base.score")) + + // Output: + // john + // 100 + // john + // 100 +} + +func ExampleLoadContentType() { + jsonContent := `{"name":"john", "score":"100"}` + xmlContent := ` + + john + 100 + ` + + j, _ := gjson.LoadContentType("json", jsonContent) + x, _ := gjson.LoadContentType("xml", xmlContent) + + fmt.Println(j.Get("name")) + fmt.Println(j.Get("score")) + fmt.Println(x.Get("base.name")) + fmt.Println(x.Get("base.score")) + + // Output: + // john + // 100 + // john + // 100 +} + +func ExampleIsValidDataType() { + fmt.Println(gjson.IsValidDataType("json")) + fmt.Println(gjson.IsValidDataType("yml")) + fmt.Println(gjson.IsValidDataType("js")) + fmt.Println(gjson.IsValidDataType("mp4")) + fmt.Println(gjson.IsValidDataType("xsl")) + fmt.Println(gjson.IsValidDataType("txt")) + + // Output: + // true + // true + // true + // false + // false + // false } func Example_loadXml() { @@ -26,13 +164,3 @@ func Example_loadXml() { fmt.Println(j.Get("doc.name")) fmt.Println(j.Get("doc.score")) } - -func Example_loadContent() { - jsonContent := `{"name":"john", "score":"100"}` - j, _ := gjson.LoadContent(jsonContent) - fmt.Println(j.Get("name")) - fmt.Println(j.Get("score")) - // Output: - // john - // 100 -} diff --git a/encoding/gjson/gjson_z_example_new_test.go b/encoding/gjson/gjson_z_example_new_test.go index a077f280c..242ea95ab 100644 --- a/encoding/gjson/gjson_z_example_new_test.go +++ b/encoding/gjson/gjson_z_example_new_test.go @@ -12,67 +12,6 @@ import ( "github.com/gogf/gf/v2/encoding/gjson" ) -func Example_newFromJson() { - jsonContent := `{"name":"john", "score":"100"}` - j := gjson.New(jsonContent) - fmt.Println(j.Get("name")) - fmt.Println(j.Get("score")) - // Output: - // john - // 100 -} - -func Example_newFromXml() { - jsonContent := `john100` - j := gjson.New(jsonContent) - // Note that there's root node in the XML content. - fmt.Println(j.Get("doc.name")) - fmt.Println(j.Get("doc.score")) - // Output: - // john - // 100 -} - -func Example_newFromStruct() { - type Me struct { - Name string `json:"name"` - Score int `json:"score"` - } - me := Me{ - Name: "john", - Score: 100, - } - j := gjson.New(me) - fmt.Println(j.Get("name")) - fmt.Println(j.Get("score")) - // Output: - // john - // 100 -} - -func Example_newFromStructWithTag() { - type Me struct { - Name string `tag:"name"` - Score int `tag:"score"` - Title string - } - me := Me{ - Name: "john", - Score: 100, - Title: "engineer", - } - // The parameter `tags` specifies custom priority tags for struct conversion to map, - // multiple tags joined with char ','. - j := gjson.NewWithTag(me, "tag") - fmt.Println(j.Get("name")) - fmt.Println(j.Get("score")) - fmt.Println(j.Get("Title")) - // Output: - // john - // 100 - // engineer -} - func ExampleNew() { jsonContent := `{"name":"john", "score":"100"}` j := gjson.New(jsonContent) @@ -130,3 +69,31 @@ func ExampleNewWithOptions() { // 100 // engineer } + +func Example_newFromXml() { + jsonContent := `john100` + j := gjson.New(jsonContent) + // Note that there's root node in the XML content. + fmt.Println(j.Get("doc.name")) + fmt.Println(j.Get("doc.score")) + // Output: + // john + // 100 +} + +func Example_newFromStruct() { + type Me struct { + Name string `json:"name"` + Score int `json:"score"` + } + me := Me{ + Name: "john", + Score: 100, + } + j := gjson.New(me) + fmt.Println(j.Get("name")) + fmt.Println(j.Get("score")) + // Output: + // john + // 100 +} diff --git a/encoding/gjson/testdata/toml/data1.toml b/encoding/gjson/testdata/toml/data1.toml new file mode 100644 index 000000000..21e3889c6 --- /dev/null +++ b/encoding/gjson/testdata/toml/data1.toml @@ -0,0 +1,3 @@ +[base] + name = "john" + score = 100 \ No newline at end of file diff --git a/encoding/gjson/testdata/yaml/data1.yaml b/encoding/gjson/testdata/yaml/data1.yaml new file mode 100644 index 000000000..5a428f55d --- /dev/null +++ b/encoding/gjson/testdata/yaml/data1.yaml @@ -0,0 +1,3 @@ +base: + name: john + score: 100 \ No newline at end of file