-
Notifications
You must be signed in to change notification settings - Fork 114
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
env: fix errors on valid interpolation expressions #307
Conversation
func TestInvalid(t *testing.T) { | ||
invalidTemplates := []string{ | ||
"${", | ||
"$}", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is valid and is in the new test cases above.
❯ echo "$}"
$}
@milas can you sign-off your commit please 😂 ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@@ -31,7 +31,7 @@ var substitutionNamed = "[_a-z][_a-z0-9]*" | |||
var substitutionBraced = "[_a-z][_a-z0-9]*(?::?[-+?](.*}|[^}]*))?" | |||
|
|||
var patternString = fmt.Sprintf( | |||
"%s(?i:(?P<escaped>%s)|(?P<named>%s)|{(?P<braced>%s)}|(?P<invalid>))", | |||
"%s(?i:(?P<escaped>%s)|(?P<named>%s)|{(?:(?P<braced>%s)}|(?P<invalid>)))", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's a playground with the expanded regex and various cases: https://regex101.com/r/H5aQT8/1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The change here is to move the "invalid" named group (doesn't actually capture anything, used as a sentinel) to be part of the "braced" section so we can detect an unmatched brace, so there's an extra (non-capturing) group.
The parser was too strict here and would reject valid constructs that used an unescaped `$` that was not part of a variable expression (and not ambiguous). Now, only errors for unmatched braced expressions (e.g. `${FOO`) are returned, but other valid cases are ignored and the `$` will be treated literally, e.g. `a $ string` -> `a $ string`, which is the same as in POSIX. Signed-off-by: Milas Bowman <milas.bowman@docker.com>
74cfbe9
to
85f1221
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pretty awesome 4-character fix PR if I do say so myself!
Awesome! |
The parser was too strict here and would reject valid constructs that used an unescaped
$
that was not part of a variable expression (and not ambiguous).Now, only errors for unmatched braced expressions (e.g.
${FOO
) are returned, but other valid cases are ignored and the$
will be treated literally, e.g.a $ string
->a $ string
, which is the same as in POSIX.