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

Add test for set-only property #102

Merged
merged 2 commits into from
Jan 28, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,45 @@

var evt = DelegatingSink.Execute(customized);

// Verify
var sv = (StructureValue)evt.Properties["Customized"];
sv.Properties.Count.ShouldBe(1);
sv.Properties[0].Name.ShouldBe("BadProperty");
sv.Properties[0].Value.ShouldBeOfType<ScalarValue>().Value.ShouldBe("***");
}

[Test]
public void Only_Settable_Accessor_Should_Be_Handled()
{
var customized = new ClassWithOnlySetters();

var evt = DelegatingSink.Execute(customized);

var sv = (StructureValue)evt.Properties["Customized"];
sv.Properties.Count.ShouldBe(0);
}

[Test]
public void Private_Property_Should_Be_Handled()
{
var customized = new ClassWithPrivateProperty();

var evt = DelegatingSink.Execute(customized);

var sv = (StructureValue)evt.Properties["Customized"];
sv.Properties.Count.ShouldBe(0);
}

[Test]
public void Indexer_Should_Be_Handled()
{
var customized = new ClassWithIndexer();

var evt = DelegatingSink.Execute(customized);

var sv = (StructureValue)evt.Properties["Customized"];
sv.Properties.Count.ShouldBe(0);
}

[Test]
public void AttributesAreConsultedWhenDestructuring()
{
Expand Down Expand Up @@ -63,6 +95,31 @@
public string? BadProperty => throw new FormatException("oops");
}

public class ClassWithOnlySetters
{
[LogMasked]
public string? Name { set { } }
Fixed Show fixed Hide fixed
Dismissed Show dismissed Hide dismissed

[LogAsScalar]
public Struct1 Struct1 { set { } }
Fixed Show fixed Hide fixed
Dismissed Show dismissed Hide dismissed
}

public class ClassWithPrivateProperty
{
[LogMasked]
private string? Name { get; set; } = "Tom";
}

public class ClassWithIndexer
{
[LogMasked]
public string? this[int index]
{
get => "Tom";
set { }
Dismissed Show dismissed Hide dismissed
}
}

[LogAsScalar]
public class ImmutableScalar
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,32 @@ public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyV
return cached.CanDestructure;
}

private static IEnumerable<PropertyInfo> GetPropertiesRecursive(Type type)
{
var seenNames = new HashSet<string>();

while (type != typeof(object))
{
var unseenProperties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
.Where(p => p.CanRead && p.GetMethod.IsPublic && p.GetIndexParameters().Length == 0 && !seenNames.Contains(p.Name));

foreach (var propertyInfo in unseenProperties)
{
seenNames.Add(propertyInfo.Name);
yield return propertyInfo;
}

type = type.BaseType;
}
}

private CacheEntry CreateCacheEntry(Type type)
{
var classDestructurer = type.GetCustomAttribute<ITypeDestructuringAttribute>();
if (classDestructurer != null)
return new(classDestructurer.CreateLogEventPropertyValue);

var properties = type.GetPropertiesRecursive().ToList();
var properties = GetPropertiesRecursive(type).ToList();
if (!_options.IgnoreNullProperties && properties.All(pi =>
pi.GetCustomAttribute<IPropertyDestructuringAttribute>() == null
&& pi.GetCustomAttribute<IPropertyOptionalIgnoreAttribute>() == null))
Expand Down
42 changes: 0 additions & 42 deletions src/Destructurama.Attributed/Util/GetablePropertyFinder.cs

This file was deleted.