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

breaking: no escape at all in triple quotes #290

Merged
merged 1 commit into from
Mar 4, 2024

Conversation

zmstone
Copy link
Member

@zmstone zmstone commented Mar 3, 2024

Prior to this breaking change, backslashes inside triple-quotes are treated as escape sequence.
This design allowed the last string character to be " as long as it's escaped.

key = """\"string value with quotes\""""

Which had its advantages however lead to the need of escaping backslash itself.

The need of escaping backslash inside triple-quote string
not only violate the HOCON spec, but also make it hard for users
to hand-craft multe-line string values.

For example, when writing EMQX rule-engine SQL program:

sql = """SELECT tokens(payload, b'\n')"""

would actually require users to write

sql = """SELECT tokens(payload, b'\\n')"""

-- which is quite counter counterintuitive.

The cost of having to escape \ will probably be higher
than the gain of supporting escape sequence in tripe-quote strings.

After this change, 4-quotes is syntax error, and tripe-quote as value is no longer possible (due to lack of escape sequence).

However, there are workarounds:

  • A string ending with " can be wrapped with """~ and ~""" instead.

    e.g.

    key = """~
        "string value with quotes"~"""
    
  • Use normal quotes with escape sequence: "\"\"\""

  • Make use of string concatenation feature: quote the proceeding characters using tripe-quotes, quote """ as "\"\"\"", then quote the following characters with triple-quotes again.
    e.g.

    key = """~
        line1
    ~"""
    "line2\"\"\"\n"
    """~
        line3
    ~"""
    

@zmstone zmstone force-pushed the no-escape-for-multiline-str branch from 2d6a497 to be182f9 Compare March 3, 2024 20:07
Prior to this breaking change, backslashes inside triple-quotes
are treated as escape sequence.

This design allowed the last string character to be " as long
as it's escaped.

key = """\"string value with quotes\""""

Which lead to the need of escaping backslash itself.

The need of escaping backslash inside triple-quote string
not only violate the HOCON spec, but also make it hard for users
to hand-craft multe-line string values.

For example, when writing EMQX rule-engine SQL program:

sql = """SELECT tokens(payload, b'\n')"""

would actually require users to write

sql = """SELECT tokens(payload, b'\\n')"""

this is quite counter counterintuitive.

The cost of having to escape \ is probably higher
than the gain of supporting escape sequence in tripe-quote strings.

After this change, 4-quotes is syntax error, and tripe-quote as value is
no longer possible (due to lack of escape sequence).

However, there are work arounds:

- A string ending with " can be wrapped with """~ and ~""" instead.
  e.g.
  key = """~
    "string value with quotes"~"""
- Use normal quotes with escape sequence: "\"\"\""
- Make use of string concatenation feature: quote the proceeding
  characters using tripe-quotes, quote """ as "\"\"\"", then quote the
  following characters with triple-quotes again.
  e.g.

  key = """~
        line1
      ~"""
      "line2\"\"\"\n"
      """~
          line3
      ~"""
@zmstone zmstone force-pushed the no-escape-for-multiline-str branch from be182f9 to c4c540c Compare March 4, 2024 08:24
Copy link
Contributor

@zhongwencool zhongwencool left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@zhongwencool zhongwencool merged commit 5e5721b into emqx:master Mar 4, 2024
3 checks passed
@zmstone zmstone deleted the no-escape-for-multiline-str branch March 4, 2024 09:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants