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

nullable in MessageTemplateParser #1722

Merged
merged 1 commit into from Aug 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67 changes: 33 additions & 34 deletions src/Serilog/Parsing/MessageTemplateParser.cs
Expand Up @@ -12,8 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#nullable enable
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using Serilog.Core;
using Serilog.Events;
Expand Down Expand Up @@ -152,7 +154,7 @@ static MessageTemplateToken ParsePropertyToken(int startAt, string messageTempla
first);
}

static bool TrySplitTagContent(string tagContent, out string propertyNameAndDestructuring, out string format, out string alignment)
static bool TrySplitTagContent(string tagContent, [NotNullWhen(true)] out string? propertyNameAndDestructuring, out string? format, out string? alignment)
{
var formatDelim = tagContent.IndexOf(':');
var alignmentDelim = tagContent.IndexOf(',');
Expand All @@ -161,47 +163,44 @@ static bool TrySplitTagContent(string tagContent, out string propertyNameAndDest
propertyNameAndDestructuring = tagContent;
format = null;
alignment = null;
return true;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

i did some early returns to simplify the branching logic here. let me know if u prefer it back the previous way

Copy link
Member

Choose a reason for hiding this comment

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

LGTM 👍

}
else

if (alignmentDelim == -1 || (formatDelim != -1 && alignmentDelim > formatDelim))
{
propertyNameAndDestructuring = tagContent.Substring(0, formatDelim);
format = formatDelim == tagContent.Length - 1 ?
null :
tagContent.Substring(formatDelim + 1);
alignment = null;
return true;
}

propertyNameAndDestructuring = tagContent.Substring(0, alignmentDelim);
if (formatDelim == -1)
{
if (alignmentDelim == -1 || (formatDelim != -1 && alignmentDelim > formatDelim))
if (alignmentDelim == tagContent.Length - 1)
{
propertyNameAndDestructuring = tagContent.Substring(0, formatDelim);
format = formatDelim == tagContent.Length - 1 ?
null :
tagContent.Substring(formatDelim + 1);
alignment = null;
alignment = format = null;
return false;
}
else
{
propertyNameAndDestructuring = tagContent.Substring(0, alignmentDelim);
if (formatDelim == -1)
{
if (alignmentDelim == tagContent.Length - 1)
{
alignment = format = null;
return false;
}

format = null;
alignment = tagContent.Substring(alignmentDelim + 1);
}
else
{
if (alignmentDelim == formatDelim - 1)
{
alignment = format = null;
return false;
}
format = null;
alignment = tagContent.Substring(alignmentDelim + 1);
return true;
}

alignment = tagContent.Substring(alignmentDelim + 1, formatDelim - alignmentDelim - 1);
format = formatDelim == tagContent.Length - 1 ?
null :
tagContent.Substring(formatDelim + 1);
}
}
if (alignmentDelim == formatDelim - 1)
{
alignment = format = null;
return false;
}

alignment = tagContent.Substring(alignmentDelim + 1, formatDelim - alignmentDelim - 1);
format = formatDelim == tagContent.Length - 1 ?
null :
tagContent.Substring(formatDelim + 1);

return true;
}

Expand Down
18 changes: 9 additions & 9 deletions test/Serilog.Tests/Parsing/MessageTemplateParserTests.cs
Expand Up @@ -7,7 +7,7 @@ namespace Serilog.Tests.Parsing
{
public class MessageTemplateParserTests
{
static MessageTemplateToken[] Parse(string? messageTemplate)
static MessageTemplateToken[] Parse(string messageTemplate)
{
return new MessageTemplateParser().Parse(messageTemplate).Tokens.ToArray();
}
Expand All @@ -24,7 +24,7 @@ static void AssertParsedAs(string message, params MessageTemplateToken[] message
[Fact]
public void MessageTemplateIsRequired()
{
Assert.Throws<ArgumentNullException>(() => Parse(null));
Assert.Throws<ArgumentNullException>(() => Parse(null!));
}

[Fact]
Expand Down Expand Up @@ -88,7 +88,7 @@ public void DoubledRightBracketsAreParsedAsASingleBracket()
AssertParsedAs("Nice }}-: mo",
new TextToken("Nice }-: mo"));
}

[Fact]
public void DoubledRightBracketsAfterOneLeftIsParsedAPropertyTokenAndATextToken()
{
Expand All @@ -104,7 +104,7 @@ public void DoubledBracketsAreParsedAsASingleBracket()
{
AssertParsedAs("{{Hi}}",
new TextToken("{Hi}"));

AssertParsedAs("Hello, {{worl@d}}!",
new TextToken("Hello, {worl@d}!"));
}
Expand All @@ -117,7 +117,7 @@ public void AMalformedPropertyTagIsParsedAsText()

AssertParsedAs("{0 space",
new TextToken("{0 space"));

AssertParsedAs("{0_space",
new TextToken("{0_space"));
}
Expand All @@ -139,7 +139,7 @@ public void AMalformedPropertyTagIsParsedAsText3()
new PropertyToken("0_", "{0_}"),
new TextToken("}space}"));
}

[Fact]
public void AMessageWithAMalformedPropertyTagIsParsedAsManyTextTokens()
{
Expand Down Expand Up @@ -186,7 +186,7 @@ public void ManyIntegerPropertyNameIsParsedAsPositionalProperty()
Assert.True(prop1.IsPositional);
Assert.Equal(0, prop1.StartIndex);
Assert.Equal(3, prop1.Length);

var prop2 = (TextToken)parsed[1];
Assert.Equal(", ", prop2.Text);
Assert.Equal(3, prop2.StartIndex);
Expand All @@ -198,7 +198,7 @@ public void ManyIntegerPropertyNameIsParsedAsPositionalProperty()
Assert.True(prop3.IsPositional);
Assert.Equal(5, prop3.StartIndex);
Assert.Equal(3, prop3.Length);

var prop4 = (TextToken)parsed[3];
Assert.Equal(", ", prop4.Text);
Assert.Equal(8, prop4.StartIndex);
Expand Down Expand Up @@ -310,7 +310,7 @@ public void PropertiesCanHaveAlignmentAndFormat()
}

[Fact]
public void FormatInFrontOfAlignmentWillHaveTheAlignmentBeConsidredPartOfTheFormat()
public void FormatInFrontOfAlignmentWillHaveTheAlignmentBeConsideredPartOfTheFormat()
{
var prop1 = (PropertyToken)Parse("{Hello:000,-5}").Single();
Assert.Equal("Hello", prop1.PropertyName);
Expand Down