Skip to content

Commit

Permalink
vardump extensions add default options (#25)
Browse files Browse the repository at this point in the history
* - add default options override (see VarDumpExtensions.DefaultDumpOptions)
- fix AnonymousTypeVisitor (previously ignored option UseFullTypeName)
- fix failed unit tests for .net 8.0

* add reference to VarDump v 0.2.10
  • Loading branch information
ycherkes authored Dec 29, 2023
1 parent 4d92761 commit 3bfdcfd
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 76 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ VarDump is a utility for serialization runtime objects to C# or Visual Basic str

Developed as a free alternative of [ObjectDumper.NET](https://github.com/thomasgalliker/ObjectDumper), which is not free for commercial use.

[![nuget version](https://img.shields.io/badge/Nuget-v0.2.9-blue)](https://www.nuget.org/packages/VarDump)
[![nuget version](https://img.shields.io/badge/Nuget-v0.2.10-blue)](https://www.nuget.org/packages/VarDump)
[![nuget downloads](https://img.shields.io/nuget/dt/VarDump?label=Downloads)](https://www.nuget.org/packages/VarDump)

# Powered By
Expand Down
3 changes: 2 additions & 1 deletion src/VarDump.Extensions/DumpExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ namespace VarDump.Extensions;
public static class VarDumpExtensions
{
public static Func<DumpOptions, IDumper> VarDumpFactory { get; set; } = VarDumpFactories.CSharp;
public static DumpOptions DefaultDumpOptions { get; set; } = DumpOptions.Default;

public static string Dump(this object obj)
{
return VarDumpFactory(DumpOptions.Default).Dump(obj);
return VarDumpFactory(DefaultDumpOptions).Dump(obj);
}

public static string Dump(this object obj, DumpOptions options)
Expand Down
8 changes: 4 additions & 4 deletions src/VarDump.Extensions/VarDump.Extensions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<AssemblyName>VarDump.Extensions</AssemblyName>
<AssemblyVersion>0.2.9</AssemblyVersion>
<FileVersion>0.2.9</FileVersion>
<AssemblyVersion>0.2.10</AssemblyVersion>
<FileVersion>0.2.10</FileVersion>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<Copyright>Copyright $([System.DateTime]::Now.Year) Yevhen Cherkes</Copyright>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Title>VarDump.Extensions</Title>
<Version>0.2.9</Version>
<Version>0.2.10</Version>
<Description>VarDump is a utility for serialization runtime objects to C# and Visual Basic string.</Description>
<PackageProjectUrl>https://github.com/ycherkes/VarDump</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand All @@ -30,7 +30,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="VarDump" Version="0.2.9" />
<PackageReference Include="VarDump" Version="0.2.10" />
</ItemGroup>

</Project>
7 changes: 7 additions & 0 deletions src/VarDump/CodeDom/Common/CodeCastExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ public CodeCastExpression(CodeTypeReference targetType, CodeExpression expressio
Expression = expression;
}

public CodeCastExpression(CodeTypeReference targetType, CodeExpression expression, bool useSimpleParentheses)
{
TargetType = targetType;
Expression = expression;
SimpleParentheses = useSimpleParentheses;
}

public CodeCastExpression(string targetType, CodeExpression expression)
{
TargetType = new CodeTypeReference(targetType);
Expand Down
6 changes: 3 additions & 3 deletions src/VarDump/VarDump.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
<TargetFrameworks>net45;netstandard2.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<AssemblyName>VarDump</AssemblyName>
<AssemblyVersion>0.2.9</AssemblyVersion>
<FileVersion>0.2.9</FileVersion>
<AssemblyVersion>0.2.10</AssemblyVersion>
<FileVersion>0.2.10</FileVersion>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<Copyright>Copyright $([System.DateTime]::Now.Year) Yevhen Cherkes</Copyright>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Title>VarDump</Title>
<Version>0.2.9</Version>
<Version>0.2.10</Version>
<Description>VarDump is a utility for serialization runtime objects to C# and Visual Basic string.</Description>
<PackageProjectUrl>https://github.com/ycherkes/VarDump</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
9 changes: 7 additions & 2 deletions src/VarDump/Visitor/KnownTypes/AnonymousTypeVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ internal sealed class AnonymousTypeVisitor : IKnownObjectVisitor
{
private readonly IObjectVisitor _rootObjectVisitor;
private readonly IObjectDescriptor _anonymousObjectDescriptor;
private readonly CodeTypeReferenceOptions _typeReferenceOptions;

public AnonymousTypeVisitor(IObjectVisitor rootObjectVisitor, IObjectDescriptor anonymousObjectDescriptor)
public AnonymousTypeVisitor(DumpOptions options, IObjectVisitor rootObjectVisitor,
IObjectDescriptor anonymousObjectDescriptor)
{
_rootObjectVisitor = rootObjectVisitor;
_anonymousObjectDescriptor = anonymousObjectDescriptor;
_typeReferenceOptions = options.UseTypeFullName
? CodeTypeReferenceOptions.FullTypeName
: CodeTypeReferenceOptions.ShortTypeName;
}

public string Id => "Anonymous";
Expand All @@ -30,7 +35,7 @@ public CodeExpression Visit(object obj, Type objectType)
InitializeExpressions = new CodeExpressionCollection(_anonymousObjectDescriptor.Describe(obj, objectType)
.Select(pv => (CodeExpression)new CodeAssignExpression(
new CodePropertyReferenceExpression(null, pv.Name),
pv.Type.IsNullableType() || pv.Value == null ? new CodeCastExpression(pv.Type, _rootObjectVisitor.Visit(pv.Value), true) : _rootObjectVisitor.Visit(pv.Value)))
pv.Type.IsNullableType() || pv.Value == null ? new CodeCastExpression(new CodeTypeReference(pv.Type, _typeReferenceOptions), _rootObjectVisitor.Visit(pv.Value), true) : _rootObjectVisitor.Visit(pv.Value)))
.ToArray())
};

Expand Down
2 changes: 1 addition & 1 deletion src/VarDump/Visitor/ObjectVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public ObjectVisitor(DumpOptions options)
new DateOnlyVisitor(options),
new TimeOnlyVisitor(options),
new RecordVisitor(options, this),
new AnonymousTypeVisitor(this, anonymousObjectDescriptor),
new AnonymousTypeVisitor(options, this, anonymousObjectDescriptor),
new KeyValuePairVisitor(options, this),
new TupleVisitor(options, this),
new ValueTupleVisitor(this),
Expand Down
111 changes: 83 additions & 28 deletions test/VarDump.Extensions.UnitTests/VarDumpExtensionsSpec.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Collections.Generic;
using VarDump.Visitor;
using Xunit;

namespace VarDump.Extensions.UnitTests;
Expand All @@ -18,22 +20,73 @@ public void DumpAnonymousTypeCsharp()
var result = anonymous.Dump();

Assert.Equal(
@"var arrayOfAnonymousType = new []
{
new
{
Name = ""Steeve"",
Age = (int?)int.MaxValue,
Reference = ""Test reference""
},
new
{
Name = ""Peter"",
Age = (int?)null,
Reference = (string)null
"""
var arrayOfAnonymousType = new []
{
new
{
Name = "Steeve",
Age = (int?)int.MaxValue,
Reference = "Test reference"
},
new
{
Name = "Peter",
Age = (int?)null,
Reference = (string)null
}
};
""", result);
}
};
", result);

[Fact]
public void DumpAnonymousTypeCustomOptionsCsharp()
{
var anonymous = new[]
{
new
{
d = new Dictionary<string, string>
{
{"test", "test"}
}
},
new
{
d = (Dictionary<string, string>)null
}
};

VarDumpExtensions.VarDumpFactory = VarDumpFactories.CSharp;
VarDumpExtensions.DefaultDumpOptions = new DumpOptions
{
UseTypeFullName = true
};

var result = anonymous.Dump();

Assert.Equal(
"""
var arrayOfAnonymousType = new []
{
new
{
d = new System.Collections.Generic.Dictionary<string, string>
{
{
"test",
"test"
}
}
},
new
{
d = (System.Collections.Generic.Dictionary<string, string>)null
}
};
""", result);
}

[Fact]
Expand All @@ -50,18 +103,20 @@ public void DumpAnonymousTypeVb()
var result = anonymous.Dump();

Assert.Equal(
@"Dim arrayOfAnonymousType = {
New With {
.Name = ""Steeve"",
.Age = CType(Integer.MaxValue, Integer?),
.Reference = ""Test reference""
},
New With {
.Name = ""Peter"",
.Age = CType(Nothing, Integer?),
.Reference = CType(Nothing, String)
}
}
", result);
"""
Dim arrayOfAnonymousType = {
New With {
.Name = "Steeve",
.Age = CType(Integer.MaxValue, Integer?),
.Reference = "Test reference"
},
New With {
.Name = "Peter",
.Age = CType(Nothing, Integer?),
.Reference = CType(Nothing, String)
}
}
""", result);
}
}
Loading

0 comments on commit 3bfdcfd

Please sign in to comment.