Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for MetadataTypeAttribute #91

Closed
wants to merge 6 commits into from
Closed
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
43 changes: 43 additions & 0 deletions src/Destructurama.Attributed.Tests/MetadataTypeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.ComponentModel.DataAnnotations;
using Destructurama.Attributed.Tests.Support;
using NUnit.Framework;
using Serilog.Events;
using Shouldly;

namespace Destructurama.Attributed.Tests;

[TestFixture]
public class MetadataTypeTests
{
[Test]
public void MetadataType_Should_Be_Respected()
{
var customized = new Dto
{
Private = "secret",
Public = "not_Secret"
};

var evt = DelegatingSink.Execute(customized);

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

props.Count.ShouldBe(1);
props["Public"].LiteralValue().ShouldBe("not_Secret");
}

[MetadataType(typeof(DtoMetadata))]
public partial class Dto
{
public string Private { get; set; }

Check warning on line 33 in src/Destructurama.Attributed.Tests/MetadataTypeTests.cs

View workflow job for this annotation

GitHub Actions / ubuntu-latest

Non-nullable property 'Private' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 33 in src/Destructurama.Attributed.Tests/MetadataTypeTests.cs

View workflow job for this annotation

GitHub Actions / ubuntu-latest

Non-nullable property 'Private' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 33 in src/Destructurama.Attributed.Tests/MetadataTypeTests.cs

View workflow job for this annotation

GitHub Actions / ubuntu-latest

Non-nullable property 'Private' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 33 in src/Destructurama.Attributed.Tests/MetadataTypeTests.cs

View workflow job for this annotation

GitHub Actions / ubuntu-latest

Non-nullable property 'Private' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 33 in src/Destructurama.Attributed.Tests/MetadataTypeTests.cs

View workflow job for this annotation

GitHub Actions / analyze

Non-nullable property 'Private' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 33 in src/Destructurama.Attributed.Tests/MetadataTypeTests.cs

View workflow job for this annotation

GitHub Actions / analyze

Non-nullable property 'Private' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

public string Public { get; set; }

Check warning on line 35 in src/Destructurama.Attributed.Tests/MetadataTypeTests.cs

View workflow job for this annotation

GitHub Actions / ubuntu-latest

Non-nullable property 'Public' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 35 in src/Destructurama.Attributed.Tests/MetadataTypeTests.cs

View workflow job for this annotation

GitHub Actions / ubuntu-latest

Non-nullable property 'Public' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 35 in src/Destructurama.Attributed.Tests/MetadataTypeTests.cs

View workflow job for this annotation

GitHub Actions / ubuntu-latest

Non-nullable property 'Public' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 35 in src/Destructurama.Attributed.Tests/MetadataTypeTests.cs

View workflow job for this annotation

GitHub Actions / ubuntu-latest

Non-nullable property 'Public' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 35 in src/Destructurama.Attributed.Tests/MetadataTypeTests.cs

View workflow job for this annotation

GitHub Actions / analyze

Non-nullable property 'Public' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 35 in src/Destructurama.Attributed.Tests/MetadataTypeTests.cs

View workflow job for this annotation

GitHub Actions / analyze

Non-nullable property 'Public' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}

internal class DtoMetadata
{
[NotLogged]
public object Private { get; set; }

Check warning on line 41 in src/Destructurama.Attributed.Tests/MetadataTypeTests.cs

View workflow job for this annotation

GitHub Actions / ubuntu-latest

Non-nullable property 'Private' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 41 in src/Destructurama.Attributed.Tests/MetadataTypeTests.cs

View workflow job for this annotation

GitHub Actions / ubuntu-latest

Non-nullable property 'Private' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 41 in src/Destructurama.Attributed.Tests/MetadataTypeTests.cs

View workflow job for this annotation

GitHub Actions / ubuntu-latest

Non-nullable property 'Private' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 41 in src/Destructurama.Attributed.Tests/MetadataTypeTests.cs

View workflow job for this annotation

GitHub Actions / ubuntu-latest

Non-nullable property 'Private' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 41 in src/Destructurama.Attributed.Tests/MetadataTypeTests.cs

View workflow job for this annotation

GitHub Actions / analyze

Non-nullable property 'Private' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 41 in src/Destructurama.Attributed.Tests/MetadataTypeTests.cs

View workflow job for this annotation

GitHub Actions / analyze

Non-nullable property 'Private' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ private CacheEntry CreateCacheEntry(Type type)
if (classDestructurer != null)
return new(classDestructurer.CreateLogEventPropertyValue);

// TODO: fetch ti.GetCustomAttribute<MetadataTypeAttribute>(); and bind to properties

var properties = GetPropertiesRecursive(type).ToList();
if (!_options.IgnoreNullProperties && properties.All(pi =>
GetCustomAttribute<IPropertyDestructuringAttribute>(pi) == null
Expand Down
3 changes: 2 additions & 1 deletion src/Destructurama.Attributed/Destructurama.Attributed.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<Description>Use attributes to control how complex types are logged to Serilog.</Description>
<RootNamespace>Destructurama</RootNamespace>
<PolySharpIncludeRuntimeSupportedAttributes>true</PolySharpIncludeRuntimeSupportedAttributes>
Expand All @@ -10,6 +10,7 @@
<ItemGroup>
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="PolySharp" Version="1.14.1" PrivateAssets="All" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" Condition="'$(TargetFramework)' == 'netstandard2.1'" />
<InternalsVisibleTo Include="Destructurama.Attributed.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100638a43140e8a1271c1453df1379e64b40b67a1f333864c1aef5ac318a0fa2008545c3d35a82ef005edf0de1ad1e1ea155722fe289df0e462f78c40a668cbc96d7be1d487faef5714a54bb4e57909c86b3924c2db6d55ccf59939b99eb0cab6e8a91429ba0ce630c08a319b323bddcbbd509f1afe4ae77a6cbb8b447f588febc3" />
<InternalsVisibleTo Include="Benchmarks, PublicKey=0024000004800000940000000602000000240000525341310004000001000100638a43140e8a1271c1453df1379e64b40b67a1f333864c1aef5ac318a0fa2008545c3d35a82ef005edf0de1ad1e1ea155722fe289df0e462f78c40a668cbc96d7be1d487faef5714a54bb4e57909c86b3924c2db6d55ccf59939b99eb0cab6e8a91429ba0ce630c08a319b323bddcbbd509f1afe4ae77a6cbb8b447f588febc3" />
</ItemGroup>
Expand Down
Loading