Skip to content

Commit

Permalink
LogAsScalarAttribute can be applied to structs (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
sungam3r authored Jan 22, 2024
1 parent aae13dc commit 1988894
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Destructurama.Attributed
{
Serilog.Events.LogEventPropertyValue CreateLogEventPropertyValue(object? value, Serilog.Core.ILogEventPropertyValueFactory propertyValueFactory);
}
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Property)]
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Struct | System.AttributeTargets.Property)]
public class LogAsScalarAttribute : System.Attribute, Destructurama.Attributed.IPropertyDestructuringAttribute, Destructurama.Attributed.ITypeDestructuringAttribute
{
public LogAsScalarAttribute(bool isMutable = false) { }
Expand Down
29 changes: 25 additions & 4 deletions src/Destructurama.Attributed.Tests/AttributedDestructuringTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public void AttributesAreConsultedWhenDestructuring()
Assert.IsInstanceOf<StructureValue>(props["NotAScalar"]);
Assert.IsFalse(props.ContainsKey("Ignored"));
Assert.IsInstanceOf<NotAScalar>(props["ScalarAnyway"].LiteralValue());
Assert.IsInstanceOf<Struct1>(props["Struct1"].LiteralValue());
Assert.IsInstanceOf<Struct2>(props["Struct2"].LiteralValue());

var str = sv.ToString();
Assert.That(str.Contains("This is a username"));
Expand All @@ -64,23 +66,42 @@ public class NotAScalar

public class Customized
{
// ReSharper disable UnusedAutoPropertyAccessor.Global
public ImmutableScalar? ImmutableScalar { get; set; }
public MutableScalar? MutableScalar { get; set; }
public NotAScalar? NotAScalar { get; set; }

[NotLogged] public string? Ignored { get; set; }
[NotLogged]
public string? Ignored { get; set; }

[LogAsScalar] public NotAScalar? ScalarAnyway { get; set; }
[LogAsScalar]
public NotAScalar? ScalarAnyway { get; set; }

public UserAuthData? AuthData { get; set; }

[LogAsScalar]
public Struct1 Struct1 { get; set; }

public Struct2 Struct2 { get; set; }
}

public class UserAuthData
{
public string? Username { get; set; }

[NotLogged] public string? Password { get; set; }
[NotLogged]
public string? Password { get; set; }
}

public struct Struct1
{
public int SomeProperty { get; set; }
public override string ToString() => "AAA";
}

[LogAsScalar]
public struct Struct2
{
public int SomeProperty { get; set; }
public override string ToString() => "BBB";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Destructurama.Attributed;
/// Specified that the type or property it is applied to should never be
/// destructured; instead it should be logged as an atomic value.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property)]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Property)]
public class LogAsScalarAttribute : Attribute, ITypeDestructuringAttribute, IPropertyDestructuringAttribute
{
private readonly bool _isMutable;
Expand Down

0 comments on commit 1988894

Please sign in to comment.