Skip to content

Commit

Permalink
* Fixed a bug where the Type Descriptor Cach did not account for IMem…
Browse files Browse the repository at this point in the history
…berProvider - using the discovered members of a type in all future Dumps regardless of the values in IMemberProvider
  • Loading branch information
MoaidHathot committed Apr 14, 2023
1 parent d30ea50 commit 4c4068c
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 26 deletions.
36 changes: 21 additions & 15 deletions src/Dumpify.Playground/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ void TestSingle()

//var map = new Dictionary<string, int>() { ["One"] = 1, ["Two"] = 2, ["Three"] = 3 }.DumpConsole();

//new AdditionValue(1, 10).Dump(members: new MembersConfig { IncludeFields = true, IncludeNonPublicMembers = true });
new AdditionValue(1, 10).Dump(members: new MembersConfig { IncludeFields = true, IncludeNonPublicMembers = true, IncludeProperties = true});
new AdditionValue(1, 10).Dump(members: new MembersConfig { IncludeFields = true, IncludeNonPublicMembers = false, IncludeProperties = false});
new AdditionValue(1, 10).Dump(members: new MembersConfig { IncludeFields = true, IncludeNonPublicMembers = false, IncludeProperties = true});
new AdditionValue(1, 10).Dump(members: new MembersConfig { IncludeFields = true, IncludeNonPublicMembers = true, IncludeProperties = true, IncludePublicMembers = false});

Enumerable.Range(1, 3).Select((i, index) => new { i = i, index = index }).ToArray().Dump();
//Enumerable.Range(1, 3).Select((i, index) => new { i = i, index = index }).ToArray().Dump();
}

void ShowEverything()
Expand Down Expand Up @@ -142,27 +145,30 @@ void ShowEverything()

new Exception("This is an exception", new ArgumentNullException("blaParam", "This is inner exception")).Dump();

//arr.Dump();
//moaid.Dump();
new AdditionValue(1, 10).Dump(members: new () { IncludeFields = true, IncludeNonPublicMembers = true, IncludeProperties = false });
new AdditionValue(1, 10).Dump(members: new() { IncludeFields = true, IncludeNonPublicMembers = true });

//arr.Dump();
//moaid.Dump();

//family.Dump(maxDepth: 2);

//Console.WriteLine(JsonSerializer.Serialize(moaid));
//family.Dump(maxDepth: 2);

//Console.WriteLine(JsonSerializer.Serialize(moaid));

//moaid.Dump();
//arr2d.Dump();

//moaid.Dump(maxDepth: 2);
//family.Dump(maxDepth: 2);
//arr.Dump();
//arr2d.Dump();
//((object)null).Dump();
//moaid.Dump();
//arr2d.Dump();

//moaid.Dump(maxDepth: 2);
//family.Dump(maxDepth: 2);
//arr.Dump();
//arr2d.Dump();
//((object)null).Dump();

//var result = DumpConfig.Default.Generator.Generate(family.GetType(), null);
//var result = DumpConfig.Default.Generator.Generate(family.GetType(), null);

//JsonSerializer.Serialize(result, new JsonSerializerOptions { WriteIndented = true });
//JsonSerializer.Serialize(result, new JsonSerializerOptions { WriteIndented = true });
}
#pragma warning restore CS8321

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Dumpify.Descriptors.Generators;

internal class CompositeDescriptorGenerator : IDescriptorGenerator
{
private readonly ConcurrentDictionary<(RuntimeTypeHandle, RuntimeTypeHandle?, string?), IDescriptor> _descriptorsCache = new();
private readonly ConcurrentDictionary<(RuntimeTypeHandle, RuntimeTypeHandle?, string?, IMemberProvider), IDescriptor> _descriptorsCache = new();

private readonly IDescriptorGenerator[] _generatorsChain;

Expand All @@ -22,7 +22,7 @@ public CompositeDescriptorGenerator(ConcurrentDictionary<RuntimeTypeHandle, Func

public IDescriptor? Generate(Type type, IValueProvider? valueProvider, IMemberProvider memberProvider)
{
var cacheKey = CreateCacheKey(type, valueProvider);
var cacheKey = CreateCacheKey(type, valueProvider, memberProvider);

if (_descriptorsCache.TryGetValue(cacheKey, out IDescriptor? cachedDescriptor))
{
Expand Down Expand Up @@ -51,8 +51,8 @@ public CompositeDescriptorGenerator(ConcurrentDictionary<RuntimeTypeHandle, Func
return generatedDescriptor;
}

(RuntimeTypeHandle, RuntimeTypeHandle?, string?) CreateCacheKey(Type type, IValueProvider? valueProvider)
=> (type.TypeHandle, valueProvider?.Info.DeclaringType?.TypeHandle, valueProvider?.Name);
(RuntimeTypeHandle, RuntimeTypeHandle?, string?, IMemberProvider) CreateCacheKey(Type type, IValueProvider? valueProvider, IMemberProvider memberProvider)
=> (type.TypeHandle, valueProvider?.Info.DeclaringType?.TypeHandle, valueProvider?.Name, memberProvider);

private IDescriptor? GenerateDescriptor(Type type, IValueProvider? valueProvider, IMemberProvider memberProvider)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Dumpify/Descriptors/ValueProviders/IMemberProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Dumpify.Descriptors.ValueProviders;

public interface IMemberProvider
public interface IMemberProvider : IEquatable<IMemberProvider>
{
IEnumerable<IValueProvider> GetMembers(Type type);
}
15 changes: 14 additions & 1 deletion src/Dumpify/Descriptors/ValueProviders/MemberProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Dumpify.Descriptors.ValueProviders;

internal class MemberProvider : IMemberProvider
internal sealed record MemberProvider : IMemberProvider
{
private readonly bool _includeProperties;
private readonly bool _includeFields;
Expand Down Expand Up @@ -52,4 +52,17 @@ public IEnumerable<IValueProvider> GetMembers(Type type)

return members;
}

public bool Equals(IMemberProvider? provider)
{
if (provider is not MemberProvider other)
{
return false;
}

return _includeProperties == other._includeProperties && _includeFields == other._includeFields && _includePublicMembers == other._includePublicMembers && _includeNonPublicMembers == other._includeNonPublicMembers;
}

public override int GetHashCode()
=> HashCode.Combine(_includeProperties, _includeFields, _includePublicMembers, _includeNonPublicMembers);
}
6 changes: 1 addition & 5 deletions src/Dumpify/Renderers/RendererConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@

namespace Dumpify.Renderers;

public record struct RendererConfig
public record RendererConfig
{
public RendererConfig()
{
}

public string? Label { get; init; } = null;
public int? MaxDepth { get; init; } = null;

Expand Down

0 comments on commit 4c4068c

Please sign in to comment.