diff --git a/README.md b/README.md index d070a9d..d03bbea 100644 --- a/README.md +++ b/README.md @@ -259,6 +259,12 @@ public class CustomizedMaskedLogs [LogMasked(ShowFirst = 3, ShowLast = 3)] public string? ShowFirstAndLastThreeAndDefaultMaskInTheMiddle { get; set; } + /// + /// 123456789 results in "123456789", no mask applied + /// + [LogMasked(ShowFirst = -1, ShowLast = -1)] + public string? ShowFirstAndLastInvalidValues { get; set; } + /// /// 123456789 results in "123***789" /// @@ -278,7 +284,7 @@ public class CustomizedMaskedLogs public string? ShowFirstAndLastThreeAndCustomMaskInTheMiddlePreservedLengthIgnored { get; set; } } ``` -snippet source | anchor +snippet source | anchor ## 7. Masking a string property with regular expressions diff --git a/src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs b/src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs index 567ee1d..fc53f6f 100644 --- a/src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs +++ b/src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs @@ -105,6 +105,12 @@ public class CustomizedMaskedLogs [LogMasked(ShowFirst = 3, ShowLast = 3)] public string? ShowFirstAndLastThreeAndDefaultMaskInTheMiddle { get; set; } + /// + /// 123456789 results in "123456789", no mask applied + /// + [LogMasked(ShowFirst = -1, ShowLast = -1)] + public string? ShowFirstAndLastInvalidValues { get; set; } + /// /// 123456789 results in "123***789" /// @@ -359,6 +365,25 @@ public void LogMaskedAttribute_Shows_First_NChars_And_Last_NChars_Replaces_Value props["ShowFirstAndLastThreeAndDefaultMaskInTheMiddlePreservedLength"].LiteralValue().ShouldBe("123*456"); } + [Test] + public void LogMaskedAttribute_With_Invalid_Values_Should_Return_Value_As_Is() + { + // [LogMasked(ShowFirst = -1, ShowLast = -1)] + // -> "123456789", no mask applied + var customized = new CustomizedMaskedLogs + { + ShowFirstAndLastInvalidValues = "123456789" + }; + + var evt = DelegatingSink.Execute(customized); + + var sv = (StructureValue)evt.Properties["Customized"]; + var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value); + + props.ContainsKey("ShowFirstAndLastInvalidValues").ShouldBeTrue(); + props["ShowFirstAndLastInvalidValues"].LiteralValue().ShouldBe("123456789"); + } + [Test] public void LogMaskedAttribute_Shows_First_NChars_And_Last_NChars_Then_Replaces_All_Other_Chars_With_Custom_Mask() { diff --git a/src/Destructurama.Attributed/Attributed/LogMaskedAttribute.cs b/src/Destructurama.Attributed/Attributed/LogMaskedAttribute.cs index 6caaafc..c2b90f0 100644 --- a/src/Destructurama.Attributed/Attributed/LogMaskedAttribute.cs +++ b/src/Destructurama.Attributed/Attributed/LogMaskedAttribute.cs @@ -53,15 +53,16 @@ private object FormatMaskedValue(string val) if (string.IsNullOrEmpty(val)) return PreserveLength ? val : Text; - if (ShowFirst == 0 && ShowLast == 0) + return (ShowFirst, ShowLast) switch { - if (PreserveLength) - return new string(Text[0], val.Length); - - return Text; - } + (0, 0) => PreserveLength ? new string(Text[0], val.Length) : Text, + ( > 0, 0) => ShowOnlyFirst(), + (0, > 0) => ShowOnlyLast(), + ( > 0, > 0) => ShowFirstAndLast(), + _ => val + }; - if (ShowFirst > 0 && ShowLast == 0) + string ShowOnlyFirst() { var first = val.Substring(0, Math.Min(ShowFirst, val.Length)); @@ -73,10 +74,9 @@ private object FormatMaskedValue(string val) mask = new(Text[0], val.Length - ShowFirst); return first + mask; - } - if (ShowFirst == 0 && ShowLast > 0) + string ShowOnlyLast() { var last = ShowLast > val.Length ? val : val.Substring(val.Length - ShowLast); @@ -90,7 +90,7 @@ private object FormatMaskedValue(string val) return mask + last; } - if (ShowFirst > 0 && ShowLast > 0) + string ShowFirstAndLast() { if (ShowFirst + ShowLast >= val.Length) return val; @@ -104,8 +104,6 @@ private object FormatMaskedValue(string val) return first + (mask ?? Text) + last; } - - return val; } ///