Skip to content

Commit

Permalink
Escaping strings correctly (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 authored May 31, 2023
1 parent aacf66d commit d68a4d7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/stringutil/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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), "'", `\'`))
}

Expand Down
8 changes: 7 additions & 1 deletion lib/stringutil/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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,
},
{
Expand Down

0 comments on commit d68a4d7

Please sign in to comment.