From 750db282f8b0b91a111478fc23fb17f5eb86d77f Mon Sep 17 00:00:00 2001 From: xiongjiwei Date: Thu, 25 Aug 2022 13:40:44 +0800 Subject: [PATCH] add quote Signed-off-by: xiongjiwei --- types/json/binary_functions.go | 2 +- types/json/binary_test.go | 8 ++++---- types/json/path_expr.go | 6 +++++- types/json/path_expr_test.go | 2 ++ 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/types/json/binary_functions.go b/types/json/binary_functions.go index 32b414cafb93b..53b4fe6a08363 100644 --- a/types/json/binary_functions.go +++ b/types/json/binary_functions.go @@ -211,7 +211,7 @@ func quoteString(s string) string { ret.WriteString(s[start:]) } - if hasEscaped { + if hasEscaped || !isEcmascriptIdentifier(s) { ret.WriteByte('"') return ret.String() } diff --git a/types/json/binary_test.go b/types/json/binary_test.go index 6a7b09c90996d..76d75d29bd784 100644 --- a/types/json/binary_test.go +++ b/types/json/binary_test.go @@ -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"`}, } diff --git a/types/json/path_expr.go b/types/json/path_expr.go index 46773ed2e9b16..e8b304c67a283 100644 --- a/types/json/path_expr.go +++ b/types/json/path_expr.go @@ -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("**") } diff --git a/types/json/path_expr_test.go b/types/json/path_expr_test.go index c0aa716aa9145..fb863d48050d3 100644 --- a/types/json/path_expr_test.go +++ b/types/json/path_expr_test.go @@ -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