Skip to content

fix: support escape sequences in single-quoted literals in struct tags - #467

Open
Pastalikek65 wants to merge 1 commit into
alecthomas:masterfrom
Pastalikek65:fix-tag-char-escapes
Open

fix: support escape sequences in single-quoted literals in struct tags#467
Pastalikek65 wants to merge 1 commit into
alecthomas:masterfrom
Pastalikek65:fix-tag-char-escapes

Conversation

@Pastalikek65

Copy link
Copy Markdown
Contributor

Fixes #249.

Problem

parser:"'\n'" (and '\t', '\'', '\\', ...) failed to build with:

Key: <input>:1:2: literal not terminated

Root cause: the tag lexer (tagLexer) is based on text/scanner, whose char literals do not support escape sequences — so a literal newline inside '...' (produced by Go's own escape processing of "\n") caused a scanner error. The equivalent double-quoted form "\n" worked, which is the exact inconsistency reported in the issue.

Fix

struct.go: before scanning, quoteEscapedCharLiterals rewrites any single-quoted literal containing escape sequences or control characters as a double-quoted string, with control characters re-escaped so text/scanner accepts them:

  • '\n' (real newline) → "\n"
  • '\t'"\t"
  • '\''"'"
  • '\\'"\\"
  • 'a', '=', ... (plain single-char literals) are left untouched

Both forms now behave identically, and Unquote-style semantics are preserved (the value is the character itself).

One new test: TestEscapedCharLiteralInTag (also covers '\t' and '\\'). Verified against the repro from the issue — TestLiteralNotTerminatedBad now builds without error.

text/scanner only supports single-character char literals, so tags like
`parser:"'\n'"` (a literal newline after Go's escape processing) failed
with 'literal not terminated', while the equivalent `"\n"` worked.

Before scanning a struct tag, rewrite single-quoted literals containing
escape sequences or control characters (eg. '\t', '\n', '\'', '\\')
as double-quoted strings (with control characters re-escaped for
text/scanner). Single-character literals like '=' are left untouched.

Fixes alecthomas#249.
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.

possible bug in tag syntax

1 participant