Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,12 @@ public class CustomizedMaskedLogs
[LogMasked(ShowFirst = 3, ShowLast = 3)]
public string? ShowFirstAndLastThreeAndDefaultMaskInTheMiddle { get; set; }

/// <summary>
/// 123456789 results in "123456789", no mask applied
/// </summary>
[LogMasked(ShowFirst = -1, ShowLast = -1)]
public string? ShowFirstAndLastInvalidValues { get; set; }

/// <summary>
/// 123456789 results in "123***789"
/// </summary>
Expand All @@ -278,7 +284,7 @@ public class CustomizedMaskedLogs
public string? ShowFirstAndLastThreeAndCustomMaskInTheMiddlePreservedLengthIgnored { get; set; }
}
```
<sup><a href='/src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs#L8-L127' title='Snippet source file'>snippet source</a> | <a href='#snippet-customizedmaskedlogs' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs#L8-L133' title='Snippet source file'>snippet source</a> | <a href='#snippet-customizedmaskedlogs' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

## 7. Masking a string property with regular expressions
Expand Down
25 changes: 25 additions & 0 deletions src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ public class CustomizedMaskedLogs
[LogMasked(ShowFirst = 3, ShowLast = 3)]
public string? ShowFirstAndLastThreeAndDefaultMaskInTheMiddle { get; set; }

/// <summary>
/// 123456789 results in "123456789", no mask applied
/// </summary>
[LogMasked(ShowFirst = -1, ShowLast = -1)]
public string? ShowFirstAndLastInvalidValues { get; set; }

/// <summary>
/// 123456789 results in "123***789"
/// </summary>
Expand Down Expand Up @@ -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();

Check notice

Code scanning / CodeQL

Inefficient use of ContainsKey

Inefficient use of 'ContainsKey' and [indexer](1).
props["ShowFirstAndLastInvalidValues"].LiteralValue().ShouldBe("123456789");
}

[Test]
public void LogMaskedAttribute_Shows_First_NChars_And_Last_NChars_Then_Replaces_All_Other_Chars_With_Custom_Mask()
{
Expand Down
22 changes: 10 additions & 12 deletions src/Destructurama.Attributed/Attributed/LogMaskedAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -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);

Expand All @@ -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;
Expand All @@ -104,8 +104,6 @@ private object FormatMaskedValue(string val)

return first + (mask ?? Text) + last;
}

return val;
}

/// <inheritdoc/>
Expand Down