-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Add diagnostic for Logging Generator: can't have malformed format strings #52040
Conversation
…rly brackets, etc.)
Tagging subscribers to this area: @maryamariyan |
src/libraries/Microsoft.Extensions.Logging/gen/LoggerMessageGenerator.Parser.cs
Outdated
Show resolved
Hide resolved
…nerator.Parser.cs
else | ||
{ | ||
// dangling }, fail | ||
throw new ArgumentException($"Dangling }} in format string at position {pos}", nameof(format)); |
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.
I assume these strings will be shown to the developer. If so, we should put them in a resource so they can be localized.
src/libraries/Microsoft.Extensions.Logging/gen/LoggerMessageGenerator.Parser.cs
Show resolved
Hide resolved
ReadOnlySpan<char> format = msg.AsSpan(); | ||
var builder = new StringBuilder(msg.Length); | ||
var pos = 0; | ||
var len = format.Length; |
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.
// classic composite format string | ||
|
||
pos++; | ||
if (pos == len || (ch = format[pos]) < '0' || ch > '9') |
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.
{ | ||
// we need a template name | ||
throw new ArgumentException($"Missing template name in format string at position {pos}", nameof(format)); | ||
} |
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.
you can move this block before the condition
if (templates == null) so you don't have to repeat the same code
} | ||
|
||
// skip whitespace | ||
while (pos < len && (ch = format[pos]) == ' ') |
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.
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.
you need to do that in other places to check space char?
} | ||
} | ||
|
||
private static bool ValidTemplateNameChar(char ch, bool first) |
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.
/// <summary> | ||
/// Parses the message template and throws exception for malformed messages. | ||
/// </summary> | ||
internal static string Parse(this string msg, List<string>? templates) |
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.
@maryamariyan is the PR still active, or is it stale now due to conflicts? |
A follow up PR for: #51064
Took parsing logic from https://github.com/geeknoid/FastFormatting/blob/8878f2b829b2ebc023ddabc5b12f584d024d2591/Text.Formatting/CompositeFormat.cs#L204-L486
cc: @geeknoid @eerhardt
Fixes #52226