fix: support escape sequences in single-quoted literals in struct tags - #467
Open
Pastalikek65 wants to merge 1 commit into
Open
fix: support escape sequences in single-quoted literals in struct tags#467Pastalikek65 wants to merge 1 commit into
Pastalikek65 wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #249.
Problem
parser:"'\n'"(and'\t','\'','\\', ...) failed to build with:Root cause: the tag lexer (
tagLexer) is based ontext/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,quoteEscapedCharLiteralsrewrites any single-quoted literal containing escape sequences or control characters as a double-quoted string, with control characters re-escaped sotext/scanneraccepts them:'\n'(real newline) →"\n"'\t'→"\t"'\''→"'"'\\'→"\\"'a','=', ... (plain single-char literals) are left untouchedBoth 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 —TestLiteralNotTerminatedBadnow builds without error.