Fix panic when key starts at-sign

This commit is contained in:
tidwall 2019-07-15 07:54:31 -07:00
parent dea71f728d
commit c5e72cdf74
2 changed files with 9 additions and 1 deletions

View File

@ -1841,9 +1841,11 @@ func Get(json, path string) Result {
if path[0] == '@' { if path[0] == '@' {
// possible modifier // possible modifier
var ok bool var ok bool
var npath string
var rjson string var rjson string
path, rjson, ok = execModifier(json, path) npath, rjson, ok = execModifier(json, path)
if ok { if ok {
path = npath
if len(path) > 0 && (path[0] == '|' || path[0] == '.') { if len(path) > 0 && (path[0] == '|' || path[0] == '.') {
res := Get(rjson, path[1:]) res := Get(rjson, path[1:])
res.Index = 0 res.Index = 0

View File

@ -1984,3 +1984,9 @@ func TestParentSubQuery(t *testing.T) {
// should return two instances // should return two instances
assert(t, res.String() == `["1.2.3","1.2.2"]`) assert(t, res.String() == `["1.2.3","1.2.2"]`)
} }
func TestSingleModifier(t *testing.T) {
var data = `{"@key": "value"}`
assert(t, Get(data, "@key").String() == "value")
assert(t, Get(data, "\\@key").String() == "value")
}