From a9d7dc68a35ec6e2032131f04dd09e710b7f355a Mon Sep 17 00:00:00 2001 From: John Guo Date: Mon, 27 Sep 2021 23:05:46 +0800 Subject: [PATCH] remove usage of package gparser --- .example/database/gdb/mysql/gdb_json_xml.go | 6 +- .example/encoding/gparser/config.yaml | 44 ---- .example/encoding/gparser/gparser.go | 217 ------------------ .example/encoding/gparser/gparser_xml.go | 21 -- .../encoding/gparser/gparser_yaml_issue336.go | 77 ------- encoding/gtoml/gtoml_test.go | 19 +- encoding/gxml/gxml_test.go | 4 +- encoding/gyaml/gyaml_test.go | 5 +- net/ghttp/ghttp_response_write.go | 1 - 9 files changed, 11 insertions(+), 383 deletions(-) delete mode 100644 .example/encoding/gparser/config.yaml delete mode 100644 .example/encoding/gparser/gparser.go delete mode 100644 .example/encoding/gparser/gparser_xml.go delete mode 100644 .example/encoding/gparser/gparser_yaml_issue336.go diff --git a/.example/database/gdb/mysql/gdb_json_xml.go b/.example/database/gdb/mysql/gdb_json_xml.go index 8be1b9163..a5445ad53 100644 --- a/.example/database/gdb/mysql/gdb_json_xml.go +++ b/.example/database/gdb/mysql/gdb_json_xml.go @@ -2,10 +2,10 @@ package main import ( "fmt" + "github.com/gogf/gf/encoding/gjson" "github.com/gogf/gf/os/gctx" "github.com/gogf/gf/database/gdb" - "github.com/gogf/gf/encoding/gparser" "github.com/gogf/gf/frame/g" ) @@ -34,8 +34,8 @@ func main() { fmt.Println(one.Xml()) // 自定义方法方法转换为json/xml - jsonContent, _ := gparser.VarToJson(one.Map()) + jsonContent, _ := gjson.New(one.Map()).ToJson() fmt.Println(string(jsonContent)) - xmlContent, _ := gparser.VarToXml(one.Map()) + xmlContent, _ := gjson.New(one.Map()).ToJson() fmt.Println(string(xmlContent)) } diff --git a/.example/encoding/gparser/config.yaml b/.example/encoding/gparser/config.yaml deleted file mode 100644 index 7fe5f397a..000000000 --- a/.example/encoding/gparser/config.yaml +++ /dev/null @@ -1,44 +0,0 @@ - -broker: - ip: "127.0.0.1" - tcpport: "4222" - httpport: "8222" - user: "alfxnats" - pwd: "alfxnats" - clusterid: "alfxnats" -database: - ip: "110.1.1.227" - port: "27017" - user: "alfxdev" - pwd: "alfxdev" - dbname: "alfxdev" -scheduler: - a2rparallel: 4 - sleeptime: 300 -worker: - name: "worker1" - domains: - officenet: 3 - producnet: 3 - free: 3 - temppath: "/home/alfx/proc" -common: - filepath: "/home/alfx/file" - logpath: "home/alfx/log" - logdebug: true - logtrace: true -report: - localip: "127.0.0.1" - localport: "" - - - - - - - - - - - - diff --git a/.example/encoding/gparser/gparser.go b/.example/encoding/gparser/gparser.go deleted file mode 100644 index df42f1ffd..000000000 --- a/.example/encoding/gparser/gparser.go +++ /dev/null @@ -1,217 +0,0 @@ -package main - -import ( - "fmt" - - "github.com/gogf/gf/encoding/gparser" - "github.com/gogf/gf/os/glog" -) - -func getWithPattern1() { - data := - `{ - "users" : { - "count" : 100, - "list" : [ - {"name" : "Ming", "score" : 60}, - {"name" : "John", "score" : 99.5} - ] - } - }` - - if p, e := gparser.LoadContent([]byte(data)); e != nil { - glog.Error(e) - } else { - fmt.Println("John Score:", p.GetFloat32("users.list.1.score")) - } -} - -func getWithPattern2() { - data := - ` - - Tove - Jani - Reminder - Don't forget me this weekend! - ` - - if p, e := gparser.LoadContent([]byte(data)); e != nil { - glog.Error(e) - } else { - fmt.Println("Heading:", p.GetString("note.heading")) - } -} - -// 当键名存在"."号时,检索优先级:键名->层级,因此不会引起歧义 -func multiDots1() { - data := - `{ - "users" : { - "count" : 100 - }, - "users.count" : 101 - }` - if p, e := gparser.LoadContent([]byte(data)); e != nil { - glog.Error(e) - } else { - fmt.Println("Users Count:", p.Get("users.count")) - } -} - -func multiDots2() { - data := - `{ - "users" : { - "count" : { - "type1" : 1, - "type2" : 2 - }, - "count.type1" : 100 - } - }` - if p, e := gparser.LoadContent([]byte(data)); e != nil { - glog.Error(e) - } else { - fmt.Println("Users Count:", p.Get("users.count.type1")) - fmt.Println("Users Count:", p.Get("users.count.type2")) - } -} - -// 设置数据 -func set1() { - data := - ` -
- 10 - gf article1gf content1 - gf article2gf content2 - gf article3gf content3 -
` - if p, e := gparser.LoadContent([]byte(data)); e != nil { - glog.Error(e) - } else { - p.Set("article.list.0", nil) - c, _ := p.ToJson() - fmt.Println(string(c)) - // {"article":{"count":"10","list":[{"content":"gf content2","title":"gf article2"},{"content":"gf content3","title":"gf article3"}]}} - } -} - -func set2() { - data := - `{ - "users" : { - "count" : 100 - } - }` - if p, e := gparser.LoadContent([]byte(data)); e != nil { - glog.Error(e) - } else { - p.Set("users.count", 1) - p.Set("users.list", []string{"John", "小明"}) - c, _ := p.ToJson() - fmt.Println(string(c)) - } -} - -func makeXml1() { - p := gparser.New(nil) - p.Set("name", "john") - p.Set("age", 18) - p.Set("scores", map[string]int{ - "语文": 100, - "数学": 100, - "英语": 100, - }) - c, _ := p.ToXmlIndent("simple-xml") - fmt.Println(string(c)) -} - -func makeJson1() { - type Order struct { - Id int `json:"id"` - Price float32 `json:"price"` - } - p := gparser.New(nil) - p.Set("orders.list.0", Order{1, 100}) - p.Set("orders.list.1", Order{2, 666}) - p.Set("orders.list.2", Order{3, 999.99}) - fmt.Println("Order 1 Price:", p.Get("orders.list.1.price")) - c, _ := p.ToJson() - fmt.Println(string(c)) -} - -func makeJson2() { - p := gparser.New(map[string]string{ - "k1": "v1", - "k2": "v2", - }) - p.Set("k1.1", []int{1, 2, 3}) - //p.Set("0.0.1", []int{1,2,3}) - c, _ := p.ToJson() - fmt.Println(string(c)) -} - -func makeJson3() { - p := gparser.New([]string{"a"}) - p.Set("0.0.0", []int{1, 2, 3}) - c, _ := p.ToJson() - fmt.Println(string(c)) -} - -func toStruct1() { - type Info struct { - Name string - Url string - } - o := Info{} - p := gparser.New(map[string]string{ - "Name": "gf", - "Url": "https://gitee.com/johng", - }) - p.ToStruct(&o) - fmt.Println("Name:", o.Name) - fmt.Println("Url :", o.Url) -} - -func convert() { - p := gparser.New(map[string]string{ - "name": "gf", - "site": "https://gitee.com/johng", - }) - c, _ := p.ToJson() - fmt.Println("JSON:") - fmt.Println(string(c)) - fmt.Println("======================") - - fmt.Println("XML:") - c, _ = p.ToXmlIndent() - fmt.Println(string(c)) - fmt.Println("======================") - - fmt.Println("YAML:") - c, _ = p.ToYaml() - fmt.Println(string(c)) - fmt.Println("======================") - - fmt.Println("TOML:") - c, _ = p.ToToml() - fmt.Println(string(c)) - -} - -func remove1() { - p := gparser.New(map[string]string{ - "k1": "v1", - "k2": "v2", - }) - p.Set("0.0.0.0.0.0.0.0", []int{1, 2, 3}) - p.Remove("0.0") - c, _ := p.ToJson() - fmt.Println(string(c)) -} - -func main() { - toStruct1() -} diff --git a/.example/encoding/gparser/gparser_xml.go b/.example/encoding/gparser/gparser_xml.go deleted file mode 100644 index 6d3ce48bb..000000000 --- a/.example/encoding/gparser/gparser_xml.go +++ /dev/null @@ -1,21 +0,0 @@ -package main - -import "github.com/gogf/gf/encoding/gparser" - -func main() { - xml := ` - - - 0 - 1 - 2 - 3 - - - ` - p, err := gparser.LoadContent([]byte(xml)) - if err != nil { - panic(err) - } - p.Dump() -} diff --git a/.example/encoding/gparser/gparser_yaml_issue336.go b/.example/encoding/gparser/gparser_yaml_issue336.go deleted file mode 100644 index ee796d16d..000000000 --- a/.example/encoding/gparser/gparser_yaml_issue336.go +++ /dev/null @@ -1,77 +0,0 @@ -package main - -import ( - "fmt" - - "github.com/gogf/gf/encoding/gparser" -) - -type Conf struct { - Broker Broker - Database Database - Scheduler Scheduler - Worker Worker - Common Common - Report Report -} - -type Broker struct { - Ip string - Tcport string - Httpport string - User string - Pwd string - Clusterid string -} - -type Database struct { - Ip string - Port string - User string - Pwd string - Dbname string -} - -type Scheduler struct { - SleepTime int - Pidfilepath string - A2rparallel int -} - -type Worker struct { - Name string - Domains map[string]interface{} - Temppath string - Pidfilepath string -} - -type Common struct { - Filepath string - Logpath string - Logdebug bool - Logtrace bool -} - -type Report struct { - Localip string - Localport string //暂不启用 -} - -func main() { - _, err := gparser.Load("config.yaml") - if err != nil { - fmt.Println("oops,read config.yaml err:", err) - } - - //fmt.Println("yaml.v3读取yaml文件") - //f, err := os.Open("config.yaml") - //if err != nil { - // panic(err) - //} - //var conf Conf - //err = yaml.NewDecoder(f).Decode(&conf) - //if err != nil { - // panic(err) - //} - //fmt.Println(conf) -} diff --git a/encoding/gtoml/gtoml_test.go b/encoding/gtoml/gtoml_test.go index 1074600b5..53d9f78fa 100644 --- a/encoding/gtoml/gtoml_test.go +++ b/encoding/gtoml/gtoml_test.go @@ -6,11 +6,10 @@ package gtoml_test import ( - "testing" - - "github.com/gogf/gf/encoding/gparser" + "github.com/gogf/gf/encoding/gjson" "github.com/gogf/gf/encoding/gtoml" "github.com/gogf/gf/test/gtest" + "testing" ) var tomlStr string = ` @@ -43,13 +42,7 @@ func TestEncode(t *testing.T) { return } - p, err := gparser.LoadContent(res) - if err != nil { - t.Errorf("parser failed. %v", err) - return - } - - t.Assert(p.GetString("toml"), tomlStr) + t.Assert(gjson.New(res).Get("toml").String(), tomlStr) }) gtest.C(t, func(t *gtest.T) { @@ -120,11 +113,7 @@ func TestToJson(t *testing.T) { return } - p, err := gparser.LoadContent(res) - if err != nil { - t.Errorf("parser failed. %v", err) - return - } + p := gjson.New(res) expectJson, err := p.ToJson() if err != nil { t.Errorf("parser ToJson failed. %v", err) diff --git a/encoding/gxml/gxml_test.go b/encoding/gxml/gxml_test.go index cf901cd12..495d9fa5a 100644 --- a/encoding/gxml/gxml_test.go +++ b/encoding/gxml/gxml_test.go @@ -8,11 +8,11 @@ package gxml_test import ( "bytes" + "github.com/gogf/gf/encoding/gjson" "strings" "testing" "github.com/gogf/gf/encoding/gcharset" - "github.com/gogf/gf/encoding/gparser" "github.com/gogf/gf/encoding/gxml" "github.com/gogf/gf/test/gtest" ) @@ -35,7 +35,7 @@ func buildXml(charset string, str string) (string, string) { head := `` srcXml := strings.Replace(head, "UTF-8", charset, -1) - srcParser := gparser.New(nil) + srcParser := gjson.New(nil) srcParser.Set("name", str) srcParser.Set("age", "12") diff --git a/encoding/gyaml/gyaml_test.go b/encoding/gyaml/gyaml_test.go index 072349c37..f41afaeed 100644 --- a/encoding/gyaml/gyaml_test.go +++ b/encoding/gyaml/gyaml_test.go @@ -7,11 +7,10 @@ package gyaml_test import ( + "github.com/gogf/gf/encoding/gjson" "github.com/gogf/gf/internal/json" "testing" - "github.com/gogf/gf/encoding/gparser" - "github.com/gogf/gf/frame/g" "github.com/gogf/gf/encoding/gyaml" @@ -113,7 +112,7 @@ func Test_ToJson(t *testing.T) { return } - p, err := gparser.LoadContent(res) + p := gjson.New(res) if err != nil { t.Errorf("parser failed. %v", err) return diff --git a/net/ghttp/ghttp_response_write.go b/net/ghttp/ghttp_response_write.go index eee3743a4..c2c2f0471 100644 --- a/net/ghttp/ghttp_response_write.go +++ b/net/ghttp/ghttp_response_write.go @@ -181,7 +181,6 @@ func (r *Response) WriteXml(content interface{}, rootTag ...string) error { r.Write(gconv.String(content)) return nil } - // Else use gparser.VarToXml function to encode the parameter. if b, err := gjson.New(content).ToXml(rootTag...); err != nil { return err } else {