Skip to content

Commit

Permalink
expression: quote json path if necessarily (#37375)
Browse files Browse the repository at this point in the history
close #25579
  • Loading branch information
xiongjiwei authored Aug 25, 2022
1 parent a89ef1f commit b4b5223
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion types/json/binary_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func quoteString(s string) string {
ret.WriteString(s[start:])
}

if hasEscaped {
if hasEscaped || !isEcmascriptIdentifier(s) {
ret.WriteByte('"')
return ret.String()
}
Expand Down
8 changes: 4 additions & 4 deletions types/json/binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,15 @@ func TestQuoteString(t *testing.T) {
raw string
quoted string
}{
{raw: "3", quoted: `3`},
{raw: "3", quoted: `"3"`},
{raw: "hello, \"escaped quotes\" world", quoted: `"hello, \"escaped quotes\" world"`},
{raw: "你", quoted: `你`},
{raw: "true", quoted: `true`},
{raw: "null", quoted: `null`},
{raw: `"`, quoted: `"\""`},
{raw: `'`, quoted: `'`},
{raw: `''`, quoted: `''`},
{raw: ``, quoted: ``},
{raw: `'`, quoted: `"'"`},
{raw: `''`, quoted: `"''"`},
{raw: ``, quoted: `""`},
{raw: "\\ \" \b \f \n \r \t", quoted: `"\\ \" \b \f \n \r \t"`},
}

Expand Down
6 changes: 5 additions & 1 deletion types/json/path_expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,11 @@ func (pe PathExpression) String() string {
}
case pathLegKey:
s.WriteString(".")
s.WriteString(quoteString(leg.dotKey))
if leg.dotKey == "*" {
s.WriteString(leg.dotKey)
} else {
s.WriteString(quoteString(leg.dotKey))
}
case pathLegDoubleAsterisk:
s.WriteString("**")
}
Expand Down
2 changes: 2 additions & 0 deletions types/json/path_expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ func TestPathExprToString(t *testing.T) {
{"$.*[2]"},
{"$**.a[3]"},
{`$."\"hello\""`},
{`$."a b"`},
{`$."one potato"`},
}
for _, test := range tests {
// copy iterator variable into a new variable, see issue #27779
Expand Down

0 comments on commit b4b5223

Please sign in to comment.