From d68a4d7d955f85a201b38073aede473a47d32791 Mon Sep 17 00:00:00 2001 From: Robin Tang Date: Tue, 30 May 2023 17:25:00 -0700 Subject: [PATCH] Escaping strings correctly (#110) --- lib/stringutil/strings.go | 4 +++- lib/stringutil/strings_test.go | 8 +++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/stringutil/strings.go b/lib/stringutil/strings.go index 3bfd76389..eeef76872 100644 --- a/lib/stringutil/strings.go +++ b/lib/stringutil/strings.go @@ -21,9 +21,11 @@ func Wrap(colVal interface{}, noQuotes bool) string { colVal = strings.ReplaceAll(fmt.Sprint(colVal), `\`, `\\`) // The normal string escape is to do for O'Reilly is O\\'Reilly, but Snowflake escapes via \' if noQuotes { - return strings.ReplaceAll(fmt.Sprint(colVal), "'", `\'`) + return fmt.Sprint(colVal) } + // When there is quote wrapping `foo -> 'foo'`, we'll need to escape `'` so the value compiles. + // However, if there are no quote wrapping, we should not need to escape. return fmt.Sprintf("'%s'", strings.ReplaceAll(fmt.Sprint(colVal), "'", `\'`)) } diff --git a/lib/stringutil/strings_test.go b/lib/stringutil/strings_test.go index 01b79b3ce..f72b2bc05 100644 --- a/lib/stringutil/strings_test.go +++ b/lib/stringutil/strings_test.go @@ -26,6 +26,12 @@ func TestWrap(t *testing.T) { noQuotes: true, expectedString: "hello", }, + { + name: "string (no quotes)", + colVal: "bobby o'reilly", + noQuotes: true, + expectedString: "bobby o'reilly", + }, { name: "string that requires escaping", colVal: "bobby o'reilly", @@ -34,7 +40,7 @@ func TestWrap(t *testing.T) { { name: "string that requires escaping (no quotes)", colVal: "bobby o'reilly", - expectedString: `bobby o\'reilly`, + expectedString: `bobby o'reilly`, noQuotes: true, }, {