Skip to content

Commit

Permalink
Refactor printing config and object printer
Browse files Browse the repository at this point in the history
  • Loading branch information
gODealOAple committed Nov 28, 2021
1 parent 072b85d commit 445311c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
16 changes: 16 additions & 0 deletions ObjectPrinting/BasePrintingConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;

namespace ObjectPrinting
{
public class BasePrintingConfig
{
protected static readonly Type[] FinalTypes = {
typeof(int),
typeof(double),
typeof(float),
typeof(string),
typeof(DateTime),
typeof(TimeSpan)
};
}
}
7 changes: 2 additions & 5 deletions ObjectPrinting/ObjectPrinter.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
namespace ObjectPrinting
{
public class ObjectPrinter
public static class ObjectPrinter
{
public static PrintingConfig<T> For<T>()
{
return new PrintingConfig<T>();
}
public static PrintingConfig<T> For<T>() => new PrintingConfig<T>();
}
}
19 changes: 5 additions & 14 deletions ObjectPrinting/PrintingConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,17 @@

namespace ObjectPrinting
{
public class PrintingConfig<TOwner>
public class PrintingConfig<TOwner> : BasePrintingConfig
{
public string PrintToString(TOwner obj)
{
return PrintToString(obj, 0);
}
public string PrintToString(TOwner obj) => PrintToString(obj, 0);

private string PrintToString(object obj, int nestingLevel)
{
//TODO apply configurations
if (obj == null)
return "null" + Environment.NewLine;
return $"null{Environment.NewLine}";

var finalTypes = new[]
{
typeof(int), typeof(double), typeof(float), typeof(string),
typeof(DateTime), typeof(TimeSpan)
};
if (finalTypes.Contains(obj.GetType()))
return obj + Environment.NewLine;
if (FinalTypes.Contains(obj.GetType()))
return $"{obj}{Environment.NewLine}";

var identation = new string('\t', nestingLevel + 1);
var sb = new StringBuilder();
Expand Down

0 comments on commit 445311c

Please sign in to comment.