Skip to content

Commit

Permalink
Merge branch 'master' into logname
Browse files Browse the repository at this point in the history
  • Loading branch information
sungam3r committed Jan 29, 2024
2 parents 9aef69f + c0c2812 commit 7e9fd08
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 45 deletions.
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
14 changes: 14 additions & 0 deletions src/Destructurama.Attributed.Tests/AttributedDestructuringTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public void AttributesAreConsultedWhenDestructuring()
props["ScalarAnyway"].LiteralValue().ShouldBeOfType<NotAScalar>();
props["Struct1"].LiteralValue().ShouldBeOfType<Struct1>();
props["Struct2"].LiteralValue().ShouldBeOfType<Struct2>();
props["StructReturningNull"].LiteralValue().ShouldBeNull();
props["StructNull"].LiteralValue().ShouldBeNull();

var str = sv.ToString();
str.Contains("This is a username").ShouldBeTrue();
Expand Down Expand Up @@ -152,6 +154,12 @@ public class Customized
public Struct1 Struct1 { get; set; }

public Struct2 Struct2 { get; set; }

[LogAsScalar(isMutable: true)]
public StructReturningNull StructReturningNull { get; set; }

[LogAsScalar(isMutable: true)]
public StructReturningNull? StructNull { get; set; }
}

public class UserAuthData
Expand All @@ -174,4 +182,10 @@ public struct Struct2
public int SomeProperty { get; set; }
public override string ToString() => "BBB";
}

public struct StructReturningNull
{
public int SomeProperty { get; set; }
public override string ToString() => null!;
}
}
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();
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: 22 additions & 0 deletions src/Destructurama.Attributed.Tests/ReplacedAttributeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public class CustomizedRegexLogs
/// </summary>
[LogReplaced(REGEX_WITH_VERTICAL_BARS, "***|$2|****")]
public string? RegexReplaceFirstThird { get; set; }

/// <summary>
/// LogReplaced works only for string properties.
/// </summary>
[LogReplaced("does not matter", "does not matter")]
public int RegexReplaceForInt { get; set; }
}

[TestFixture]
Expand Down Expand Up @@ -134,4 +140,20 @@ public void LogReplacedAttribute_Replaces_First_And_Third()
props.ContainsKey("RegexReplaceThird").ShouldBeTrue();
props["RegexReplaceThird"].LiteralValue().ShouldBe("123|456|***");
}

[Test]
public void LogReplacedAttribute_Should_Work_Only_For_String_Properties()
{
var customized = new CustomizedRegexLogs
{
RegexReplaceForInt = 42,
};

var evt = DelegatingSink.Execute(customized);

var sv = (StructureValue)evt.Properties["Customized"];
var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value);

props.ContainsKey("RegexReplaceForInt").ShouldBeFalse();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,27 @@ private static IEnumerable<PropertyInfo> GetPropertiesRecursive(Type type)

private CacheEntry CreateCacheEntry(Type type)
{
var classDestructurer = type.GetCustomAttribute<ITypeDestructuringAttribute>();
static T GetCustomAttribute<T>(PropertyInfo propertyInfo) => propertyInfo.GetCustomAttributes().OfType<T>().FirstOrDefault();

var classDestructurer = type.GetCustomAttributes().OfType<ITypeDestructuringAttribute>().FirstOrDefault();
if (classDestructurer != null)
return new(classDestructurer.CreateLogEventPropertyValue);

var properties = GetPropertiesRecursive(type).ToList();
if (!_options.IgnoreNullProperties && properties.All(pi =>
pi.GetCustomAttribute<IPropertyDestructuringAttribute>() == null
&& pi.GetCustomAttribute<IPropertyOptionalIgnoreAttribute>() == null))
GetCustomAttribute<IPropertyDestructuringAttribute>(pi) == null
&& GetCustomAttribute<IPropertyOptionalIgnoreAttribute>(pi) == null))
{
return CacheEntry.Ignore;
}

var optionalIgnoreAttributes = properties
.Select(pi => new { pi, Attribute = pi.GetCustomAttribute<IPropertyOptionalIgnoreAttribute>() })
.Select(pi => new { pi, Attribute = GetCustomAttribute<IPropertyOptionalIgnoreAttribute>(pi) })
.Where(o => o.Attribute != null)
.ToDictionary(o => o.pi, o => o.Attribute);

var destructuringAttributes = properties
.Select(pi => new { pi, Attribute = pi.GetCustomAttribute<IPropertyDestructuringAttribute>() })
.Select(pi => new { pi, Attribute = GetCustomAttribute<IPropertyDestructuringAttribute>(pi) })
.Where(o => o.Attribute != null)
.ToDictionary(o => o.pi, o => o.Attribute);

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
26 changes: 0 additions & 26 deletions src/Destructurama.Attributed/Util/AttributeFinder.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Destructurama.Attributed/Util/CacheEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace Destructurama.Util;

internal struct CacheEntry
internal readonly struct CacheEntry
{
public CacheEntry(Func<object, ILogEventPropertyValueFactory, LogEventPropertyValue> destructureFunc)
{
Expand Down

0 comments on commit 7e9fd08

Please sign in to comment.