Skip to content

Commit

Permalink
Merge branch 'master' into dependabot/nuget/coverlet.msbuild-6.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
sungam3r committed Mar 10, 2024
2 parents a25125a + 1ae5089 commit 44e1025
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 33 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class PersonalData
public string? Name { get; set; }
}
```
<sup><a href='/src/Destructurama.Attributed.Tests/LogWithNameAttributeTests.cs#L28-L34' title='Snippet source file'>snippet source</a> | <a href='#snippet-logwithname' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Destructurama.Attributed.Tests/LogWithNameAttributeTests.cs#L63-L69' title='Snippet source file'>snippet source</a> | <a href='#snippet-logwithname' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

## 2. Ignoring a property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class ApiApprovalTests
{
/// <summary>Check for changes to the public APIs.</summary>
/// <param name="type">The type used as a marker for the assembly whose public API change you want to check.</param>
[TestCase(typeof(LoggerConfigurationAppSettingsExtensions))]
[TestCase(typeof(LoggerConfigurationAttributedExtensions))]
public void PublicApi_Should_Not_Change_Unintentionally(Type type)
{
string publicApi = type.Assembly.GeneratePublicApi(new()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ namespace Destructurama.Attributed
}
namespace Destructurama
{
public static class LoggerConfigurationAppSettingsExtensions
public static class LoggerConfigurationAttributedExtensions
{
public static Serilog.LoggerConfiguration UsingAttributes(this Serilog.Configuration.LoggerDestructuringConfiguration configuration) { }
public static Serilog.LoggerConfiguration UsingAttributes(this Serilog.Configuration.LoggerDestructuringConfiguration configuration, System.Action<Destructurama.Attributed.AttributedDestructuringPolicyOptions> configure) { }
Expand Down
44 changes: 40 additions & 4 deletions src/Destructurama.Attributed.Tests/LogWithNameAttributeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ namespace Destructurama.Attributed.Tests;
[TestFixture]
public class LogWithNameAttributeTests
{
[Test]
public void AttributesAreConsultedWhenDestructuring()
[TestCase("John Doe")]
[TestCase(null)]
public void AttributesAreConsultedWhenDestructuring(string name)
{
var customized = new PersonalData
{
Name = "John Doe"
Name = name
};

var evt = DelegatingSink.Execute(customized);
Expand All @@ -22,7 +23,42 @@ public void AttributesAreConsultedWhenDestructuring()
var props = sv.Properties.ToDictionary(p => p.Name, p => p.Value);

var literalValue = props["FullName"].LiteralValue();
literalValue.ShouldBe("John Doe");
literalValue.ShouldBe(name);
}

// https://github.com/destructurama/attributed/issues/65
[Test]
public void Issue65()
{
var customized = new MessageBase
{
Context = new ContextClass(),
};
var evt = DelegatingSink.Execute(customized);
var sv = (StructureValue)evt.Properties["Customized"];
sv.Properties.Count.ShouldBe(1);
sv.Properties[0].Name.ShouldBe("messageContext");
var sv2 = sv.Properties[0].Value.ShouldBeOfType<StructureValue>();
sv2.Properties.Count.ShouldBe(2);
sv2.Properties[0].Name.ShouldBe("Foo");
sv2.Properties[1].Name.ShouldBe("Bar");
sv2.Properties[0].Value.LiteralValue().ShouldBe("MyFoo");
sv2.Properties[1].Value.LiteralValue().ShouldBe("MyBar");
}

public class MessageBase
{
[LogWithName("messageContext")]
public ContextClass? Context { get; set; }
}

public class ContextClass
{
public string Foo { get; set; } = "MyFoo";

public string Bar { get; set; } = "MyBar";

public override string ToString() => "ContextClass ToString Output";
}

#region LogWithName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,4 @@ public static LogEvent Execute(object obj, string messageTemplate = "Here is {@C

return evt;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ public LogAsScalarAttribute(bool isMutable = false)
}

/// <inheritdoc/>
public LogEventPropertyValue CreateLogEventPropertyValue(object? value, ILogEventPropertyValueFactory propertyValueFactory) =>
new ScalarValue(_isMutable ? value?.ToString() : value);
public LogEventPropertyValue CreateLogEventPropertyValue(object? value, ILogEventPropertyValueFactory propertyValueFactory)
{
var v = _isMutable ? value?.ToString() : value;
return v == null ? ScalarValue.Null : new ScalarValue(v);
}

/// <inheritdoc/>
public bool TryCreateLogEventProperty(string name, object? value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventProperty property)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private LogEventPropertyValue CreateValue(object? value)
{
IEnumerable<string> strings => new SequenceValue(strings.Select(s => new ScalarValue(FormatMaskedValue(s)))),
string s => new ScalarValue(FormatMaskedValue(s)),
_ => new ScalarValue(null)
_ => ScalarValue.Null
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public bool TryCreateLogEventProperty(string name, object? value, ILogEventPrope
{
if (value == null)
{
property = new(name, new ScalarValue(value));
property = new(name, ScalarValue.Null);
return true;
}

Expand Down
19 changes: 1 addition & 18 deletions src/Destructurama.Attributed/Attributed/LogWithNameAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,7 @@ public LogWithNameAttribute(string newName)
/// <inheritdoc/>
public bool TryCreateLogEventProperty(string name, object? value, ILogEventPropertyValueFactory propertyValueFactory, [NotNullWhen(true)] out LogEventProperty? property)
{
var propValue = propertyValueFactory.CreatePropertyValue(value);

LogEventPropertyValue? logEventPropVal = propValue switch
{
ScalarValue scalar => new ScalarValue(scalar.Value),
DictionaryValue dictionary => new DictionaryValue(dictionary.Elements),
SequenceValue sequence => new SequenceValue(sequence.Elements),
StructureValue structure => new StructureValue(structure.Properties),
_ => null
};

if (logEventPropVal is null)
{
property = null;
return false;
}

property = new LogEventProperty(_newName, logEventPropVal);
property = new LogEventProperty(_newName, propertyValueFactory.CreatePropertyValue(value, destructureObjects: true));
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Serilog" Version="[2.10.0,4.0.0)" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="PolySharp" Version="1.14.1" PrivateAssets="All" />
<InternalsVisibleTo Include="Destructurama.Attributed.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100638a43140e8a1271c1453df1379e64b40b67a1f333864c1aef5ac318a0fa2008545c3d35a82ef005edf0de1ad1e1ea155722fe289df0e462f78c40a668cbc96d7be1d487faef5714a54bb4e57909c86b3924c2db6d55ccf59939b99eb0cab6e8a91429ba0ce630c08a319b323bddcbbd509f1afe4ae77a6cbb8b447f588febc3" />
<InternalsVisibleTo Include="Benchmarks, PublicKey=0024000004800000940000000602000000240000525341310004000001000100638a43140e8a1271c1453df1379e64b40b67a1f333864c1aef5ac318a0fa2008545c3d35a82ef005edf0de1ad1e1ea155722fe289df0e462f78c40a668cbc96d7be1d487faef5714a54bb4e57909c86b3924c2db6d55ccf59939b99eb0cab6e8a91429ba0ce630c08a319b323bddcbbd509f1afe4ae77a6cbb8b447f588febc3" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Destructurama;
/// <summary>
/// Adds the Destructure.UsingAttributes() extension to <see cref="LoggerConfiguration"/>.
/// </summary>
public static class LoggerConfigurationAppSettingsExtensions
public static class LoggerConfigurationAttributedExtensions
{
/// <summary>
/// Adds a custom <see cref="IDestructuringPolicy"/> to enable manipulation of how objects
Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<None Include="..\..\assets\icon.png" Pack="true" PackagePath="\" Visible="false" />
<None Include="..\..\README.md" Pack="true" PackagePath="\" Visible="false" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
<PackageReference Include="MinVer" Version="4.3.0" PrivateAssets="All" />
<PackageReference Include="MinVer" Version="5.0.0" PrivateAssets="All" />
</ItemGroup>

</Project>

0 comments on commit 44e1025

Please sign in to comment.