Skip to content

Commit

Permalink
optimize try parse logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tinohager committed Jan 15, 2025
1 parent f986dfa commit 51caec6
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public void TryParse_InvalidDmarcString2_ReturnsTrueAndPopulatesDmarcRecord()
}

[TestMethod]
public void TryParse_CorruptDmarcString1_ReturnsTrueAndParseErrors()
public void TryParse_CorruptDmarcString1_ReturnsFalseAndParseErrors()
{
var isSuccessful = DmarcRecordParser.TryParse("verification=123456789", out var dmarcDataFragment, out var parseErrors);

Assert.IsTrue(isSuccessful);
Assert.IsFalse(isSuccessful);
Assert.IsNotNull(dmarcDataFragment);
Assert.IsNotNull(parseErrors, "ParseErrors is null");
Assert.IsTrue(parseErrors.Length == 2);
Expand Down
8 changes: 6 additions & 2 deletions src/Nager.EmailAuthentication/DmarcRecordParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ public static bool TryParse(
}
};

var mappingFound = false;

foreach (var keyValue in parseResult.KeyValues)
{
if (string.IsNullOrEmpty(keyValue.Key))
Expand All @@ -176,7 +178,9 @@ public static bool TryParse(
errors.AddRange([.. handler.Validate(new ValidateRequest { Field = keyValue.Key, Value = keyValue.Value })]);
}
handler.Map(keyValue.Value ?? "");


mappingFound = true;

continue;
}

Expand All @@ -190,7 +194,7 @@ public static bool TryParse(
parseErrors = errors.Count == 0 ? null : [.. errors];
dmarcDataFragment = dataFragment;

return true;
return mappingFound;
}

private static ParseError[] ValidateDomainPolicy(ValidateRequest validateRequest)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

<TargetFrameworks>net8.0;net9.0</TargetFrameworks>

<Version>1.1.0</Version>
<Version>1.2.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 51caec6

Please sign in to comment.