Skip to content

Commit

Permalink
Refactoring object visitor (#16)
Browse files Browse the repository at this point in the history
* Refactoring existing generic purpose ObjectVisitor class to set of specific Visitors.

* + ability to configure known types.

* remove unnecessary file

* code cleanup

* v++

* v++

* to file scoped namespace

* to file scoped namespace

* remove unnecessary code
  • Loading branch information
ycherkes authored Sep 9, 2023
1 parent 8122f95 commit 5da43d3
Show file tree
Hide file tree
Showing 110 changed files with 5,218 additions and 4,229 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.5-blue)](https://www.nuget.org/packages/VarDump)
[![nuget version](https://img.shields.io/badge/Nuget-v0.2.6-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
67 changes: 33 additions & 34 deletions src/CSharpDumper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,48 @@
using VarDump.Utils;
using VarDump.Visitor;

namespace VarDump
{
public class CSharpDumper : IDumper
{
private readonly DumpOptions _options;
namespace VarDump;

public CSharpDumper()
{
_options = DumpOptions.Default;
}
public class CSharpDumper : IDumper
{
private readonly DumpOptions _options;

public CSharpDumper(DumpOptions options)
{
_options = options?.Clone() ?? throw new ArgumentNullException(nameof(options));
}
public CSharpDumper()
{
_options = DumpOptions.Default;
}

public string Dump(object obj)
{
var objectVisitor = new ObjectVisitor(_options);
public CSharpDumper(DumpOptions options)
{
_options = options?.Clone() ?? throw new ArgumentNullException(nameof(options));
}

var expression = objectVisitor.Visit(obj);
public string Dump(object obj)
{
var objectVisitor = new ObjectVisitor(_options);

CodeObject codeObject = _options.GenerateVariableInitializer
? new CodeVariableDeclarationStatement(new CodeImplicitlyTypedTypeReference(),
obj != null ? ReflectionUtils.ComposeCsharpVariableName(obj.GetType()) : "nullValue")
{
InitExpression = expression
}
: expression;
var expression = objectVisitor.Visit(obj);

var codeGeneratorOptions = new CodeGeneratorOptions
CodeObject codeObject = _options.GenerateVariableInitializer
? new CodeVariableDeclarationStatement(new CodeImplicitlyTypedTypeReference(),
obj != null ? ReflectionUtils.ComposeCsharpVariableName(obj.GetType()) : "nullValue")
{
BracingStyle = "C"
};
InitExpression = expression
}
: expression;

var codeGeneratorOptions = new CodeGeneratorOptions
{
BracingStyle = "C"
};

ICodeGenerator generator = new CSharpCodeGenerator();
using var sourceWriter = new StringWriter();
ICodeGenerator generator = new CSharpCodeGenerator();
using var sourceWriter = new StringWriter();

generator.GenerateCode(codeObject, sourceWriter, codeGeneratorOptions);
generator.GenerateCode(codeObject, sourceWriter, codeGeneratorOptions);

var csCodeString = sourceWriter.ToString();
var csCodeString = sourceWriter.ToString();

return csCodeString;
}
return csCodeString;
}
}
}
9 changes: 4 additions & 5 deletions src/CodeDom/Common/CodeAnonymousTypeReference.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace VarDump.CodeDom.Common
namespace VarDump.CodeDom.Common;

internal class CodeAnonymousTypeReference : CodeEmptyTypeReference
{
internal class CodeAnonymousTypeReference : CodeEmptyTypeReference
{
}
}
}
149 changes: 74 additions & 75 deletions src/CodeDom/Common/CodeArrayCreateExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,81 +4,80 @@

using System;

namespace VarDump.CodeDom.Common
namespace VarDump.CodeDom.Common;

internal class CodeArrayCreateExpression : CodeExpression
{
internal class CodeArrayCreateExpression : CodeExpression
private readonly CodeExpressionCollection _initializers = new CodeExpressionCollection();
private CodeTypeReference _createType;

public CodeArrayCreateExpression()
{
}

public CodeArrayCreateExpression(CodeTypeReference createType, params CodeExpression[] initializers)
{
_createType = createType;
_initializers.AddRange(initializers);
}

public CodeArrayCreateExpression(string createType, params CodeExpression[] initializers)
{
_createType = new CodeTypeReference(createType);
_initializers.AddRange(initializers);
}

public CodeArrayCreateExpression(Type createType, params CodeExpression[] initializers)
{
_createType = new CodeTypeReference(createType);
_initializers.AddRange(initializers);
}

public CodeArrayCreateExpression(CodeTypeReference createType, int size)
{
private readonly CodeExpressionCollection _initializers = new CodeExpressionCollection();
private CodeTypeReference _createType;

public CodeArrayCreateExpression()
{
}

public CodeArrayCreateExpression(CodeTypeReference createType, params CodeExpression[] initializers)
{
_createType = createType;
_initializers.AddRange(initializers);
}

public CodeArrayCreateExpression(string createType, params CodeExpression[] initializers)
{
_createType = new CodeTypeReference(createType);
_initializers.AddRange(initializers);
}

public CodeArrayCreateExpression(Type createType, params CodeExpression[] initializers)
{
_createType = new CodeTypeReference(createType);
_initializers.AddRange(initializers);
}

public CodeArrayCreateExpression(CodeTypeReference createType, int size)
{
_createType = createType;
Size = size;
}

public CodeArrayCreateExpression(string createType, int size)
{
_createType = new CodeTypeReference(createType);
Size = size;
}

public CodeArrayCreateExpression(Type createType, int size)
{
_createType = new CodeTypeReference(createType);
Size = size;
}

public CodeArrayCreateExpression(CodeTypeReference createType, CodeExpression size)
{
_createType = createType;
SizeExpression = size;
}

public CodeArrayCreateExpression(string createType, CodeExpression size)
{
_createType = new CodeTypeReference(createType);
SizeExpression = size;
}

public CodeArrayCreateExpression(Type createType, CodeExpression size)
{
_createType = new CodeTypeReference(createType);
SizeExpression = size;
}

public CodeTypeReference CreateType
{
get => _createType ??= new CodeTypeReference("");
set => _createType = value;
}

public CodeExpressionCollection Initializers => _initializers;

public int Size { get; set; }

public CodeExpression SizeExpression { get; set; }
_createType = createType;
Size = size;
}
}

public CodeArrayCreateExpression(string createType, int size)
{
_createType = new CodeTypeReference(createType);
Size = size;
}

public CodeArrayCreateExpression(Type createType, int size)
{
_createType = new CodeTypeReference(createType);
Size = size;
}

public CodeArrayCreateExpression(CodeTypeReference createType, CodeExpression size)
{
_createType = createType;
SizeExpression = size;
}

public CodeArrayCreateExpression(string createType, CodeExpression size)
{
_createType = new CodeTypeReference(createType);
SizeExpression = size;
}

public CodeArrayCreateExpression(Type createType, CodeExpression size)
{
_createType = new CodeTypeReference(createType);
SizeExpression = size;
}

public CodeTypeReference CreateType
{
get => _createType ??= new CodeTypeReference("");
set => _createType = value;
}

public CodeExpressionCollection Initializers => _initializers;

public int Size { get; set; }

public CodeExpression SizeExpression { get; set; }
}
17 changes: 8 additions & 9 deletions src/CodeDom/Common/CodeArrayDimensionExpression.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
namespace VarDump.CodeDom.Common
namespace VarDump.CodeDom.Common;

internal class CodeArrayDimensionExpression : CodeExpression
{
internal class CodeArrayDimensionExpression : CodeExpression
{
public CodeExpressionCollection Initializers { get; } = new();
public CodeExpressionCollection Initializers { get; } = new();

public CodeArrayDimensionExpression(CodeExpression[] initializers)
{
Initializers.AddRange(initializers);
}
public CodeArrayDimensionExpression(CodeExpression[] initializers)
{
Initializers.AddRange(initializers);
}
}
}
25 changes: 12 additions & 13 deletions src/CodeDom/Common/CodeAssignExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace VarDump.CodeDom.Common
namespace VarDump.CodeDom.Common;

internal class CodeAssignExpression : CodeExpression
{
internal class CodeAssignExpression : CodeExpression
{
public CodeAssignExpression() { }
public CodeAssignExpression() { }

public CodeAssignExpression(CodeExpression left, CodeExpression right)
{
Left = left;
Right = right;
}
public CodeAssignExpression(CodeExpression left, CodeExpression right)
{
Left = left;
Right = right;
}

public CodeExpression Left { get; set; }
public CodeExpression Left { get; set; }

public CodeExpression Right { get; set; }
}
}
public CodeExpression Right { get; set; }
}
24 changes: 11 additions & 13 deletions src/CodeDom/Common/CodeAssignStatement.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace VarDump.CodeDom.Common;

namespace VarDump.CodeDom.Common
internal class CodeAssignStatement : CodeStatement
{
internal class CodeAssignStatement : CodeStatement
{
public CodeAssignStatement() { }
public CodeAssignStatement() { }

public CodeAssignStatement(CodeExpression left, CodeExpression right)
{
Left = left;
Right = right;
}
public CodeAssignStatement(CodeExpression left, CodeExpression right)
{
Left = left;
Right = right;
}

public CodeExpression Left { get; set; }
public CodeExpression Left { get; set; }

public CodeExpression Right { get; set; }
}
}
public CodeExpression Right { get; set; }
}
29 changes: 14 additions & 15 deletions src/CodeDom/Common/CodeBinaryOperatorExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace VarDump.CodeDom.Common
namespace VarDump.CodeDom.Common;

internal class CodeBinaryOperatorExpression : CodeExpression
{
internal class CodeBinaryOperatorExpression : CodeExpression
{
public CodeBinaryOperatorExpression() { }
public CodeBinaryOperatorExpression() { }

public CodeBinaryOperatorExpression(CodeExpression left, CodeBinaryOperatorType op, CodeExpression right)
{
Right = right;
Operator = op;
Left = left;
}
public CodeBinaryOperatorExpression(CodeExpression left, CodeBinaryOperatorType op, CodeExpression right)
{
Right = right;
Operator = op;
Left = left;
}

public CodeExpression Right { get; set; }
public CodeExpression Right { get; set; }

public CodeExpression Left { get; set; }
public CodeExpression Left { get; set; }

public CodeBinaryOperatorType Operator { get; set; }
}
}
public CodeBinaryOperatorType Operator { get; set; }
}
Loading

0 comments on commit 5da43d3

Please sign in to comment.