Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expression: quote json path if necessarily #37375

Merged
merged 3 commits into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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