diff --git a/README.MD b/README.MD index c626ca8c7..f3230b1c2 100644 --- a/README.MD +++ b/README.MD @@ -5,7 +5,7 @@ [![Go Doc](https://godoc.org/github.com/gogf/gf?status.svg)](https://godoc.org/github.com/gogf/gf) [![GoFrame CI](https://github.com/gogf/gf/actions/workflows/gf.yml/badge.svg)](https://github.com/gogf/gf/actions/workflows/gf.yml) [![Go Report](https://goreportcard.com/badge/github.com/gogf/gf?v=1)](https://goreportcard.com/report/github.com/gogf/gf) -[![Code Coverage](https://codecov.io/gh/gogf/gf/branch/master/graph/badge.svg)](https://codecov.io/gh/gogf/gf/branch/master) +[![Code Coverage](https://codecov.io/gh/gogf/gf/branch/master/graph/badge.svg)](https://codecov.io/gh/gogf/gf) [![Production Ready](https://img.shields.io/badge/production-ready-blue.svg)](https://github.com/gogf/gf) [![License](https://img.shields.io/github/license/gogf/gf.svg?style=flat)](https://github.com/gogf/gf) diff --git a/text/gstr/gstr_parse.go b/text/gstr/gstr_parse.go index 2f5573c87..52878bc70 100644 --- a/text/gstr/gstr_parse.go +++ b/text/gstr/gstr_parse.go @@ -40,9 +40,11 @@ func Parse(s string) (result map[string]interface{}, err error) { err = gerror.Wrapf(err, `url.QueryUnescape failed for string "%s"`, part[:pos]) return nil, err } - for key[0] == ' ' { + + for len(key) > 0 && key[0] == ' ' { key = key[1:] } + if key == "" || key[0] == '[' { continue } @@ -129,7 +131,6 @@ func build(result map[string]interface{}, keys []string, value interface{}) erro result[key] = append(children, value) return nil } - // The end is slice + map. like v[][a] if keys[1] == "" && length > 2 && keys[2] != "" { val, ok := result[key] diff --git a/text/gstr/gstr_z_unit_parse_test.go b/text/gstr/gstr_z_unit_parse_test.go index 9d4bd90cd..bc4d457e9 100644 --- a/text/gstr/gstr_z_unit_parse_test.go +++ b/text/gstr/gstr_z_unit_parse_test.go @@ -18,6 +18,74 @@ import ( ) func Test_Parse(t *testing.T) { + // cover test + gtest.C(t, func(t *gtest.T) { + // empty + m, err := gstr.Parse("") + t.AssertNil(err) + t.Assert(m, nil) + // invalid + m, err = gstr.Parse("a&b") + t.AssertNil(err) + t.Assert(m, make(map[string]interface{})) + // special key + m, err = gstr.Parse(" =1& b=2& c =3") + t.AssertNil(err) + t.Assert(m, map[string]interface{}{"b": "2", "c_": "3"}) + m, err = gstr.Parse("c[=3") + t.AssertNil(err) + t.Assert(m, map[string]interface{}{"c_": "3"}) + m, err = gstr.Parse("v[a][a]a=m") + t.AssertNil(err) + t.Assert(m, g.Map{ + "v": g.Map{ + "a": g.Map{ + "a": "m", + }, + }, + }) + // v[][a]=m&v[][b]=b => map["v"]:[{"a":"m","b":"b"}] + m, err = gstr.Parse("v[][a]=m&v[][b]=b") + t.AssertNil(err) + t.Assert(m, g.Map{ + "v": g.Slice{ + g.Map{ + "a": "m", + "b": "b", + }, + }, + }) + // v[][a]=m&v[][a]=b => map["v"]:[{"a":"m"},{"a":"b"}] + m, err = gstr.Parse("v[][a]=m&v[][a]=b") + t.AssertNil(err) + t.Assert(m, g.Map{ + "v": g.Slice{ + g.Map{ + "a": "m", + }, + g.Map{ + "a": "b", + }, + }, + }) + // error + m, err = gstr.Parse("v=111&v[]=m&v[]=a&v[]=b") + t.Log(err) + t.AssertNE(err, nil) + m, err = gstr.Parse("v=111&v[a]=m&v[a]=a") + t.Log(err) + t.AssertNE(err, nil) + _, err = gstr.Parse("%Q=%Q&b") + t.Log(err) + t.AssertNE(err, nil) + _, err = gstr.Parse("a=%Q&b") + t.Log(err) + t.AssertNE(err, nil) + _, err = gstr.Parse("v[a][a]=m&v[][a]=b") + t.Log(err) + t.AssertNE(err, nil) + }) + // url gtest.C(t, func(t *gtest.T) { s := "goframe.org/index?name=john&score=100"