Skip to content

Commit

Permalink
Refactor LogWithNameAttribute (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
sungam3r authored Feb 27, 2024
1 parent 211e495 commit 5fb68fe
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 23 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#L29-L35' 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 @@ -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,7 @@ 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);
}

#region LogWithName
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));
return true;
}
}

0 comments on commit 5fb68fe

Please sign in to comment.