Skip to content
Open
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
7 changes: 7 additions & 0 deletions ObjectPrinting/Actions/ActionResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace ObjectPrinting.Actions;

public class ActionResult
{
public string Value { get; set; }
public bool IsSkipped { get; set; }
}
34 changes: 34 additions & 0 deletions ObjectPrinting/Actions/ExcludeAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Reflection;

namespace ObjectPrinting.Actions;

public class ExcludeAction : IAction
{
private readonly Type? typeToExclude = null;
private readonly PropertyInfo? propertyToExclude = null;

public ExcludeAction(Type typeToExclude)
{
this.typeToExclude = typeToExclude;
}

public ExcludeAction(PropertyInfo propertyToExclude)
{
this.propertyToExclude = propertyToExclude;
}

public bool CanHandle(PropertyInfo property)
{
return propertyToExclude != null && propertyToExclude == property ||
typeToExclude != null && typeToExclude.IsAssignableFrom(property.PropertyType);
}

public ActionResult Handle(object value)
{
return new ActionResult
{
IsSkipped = true
};
}
}
9 changes: 9 additions & 0 deletions ObjectPrinting/Actions/IAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Reflection;

namespace ObjectPrinting.Actions;

public interface IAction
{
bool CanHandle(PropertyInfo property);
ActionResult Handle(object value);
}
39 changes: 39 additions & 0 deletions ObjectPrinting/Actions/SerializeAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Reflection;

namespace ObjectPrinting.Actions;

public class SerializeAction : IAction
{
private readonly Func<object, string> serializer;

private readonly Type? typeToSerialize = null;
private readonly PropertyInfo? propertyToSerialize = null;

public SerializeAction(Type typeToSerialize, Func<object, string> serializer)
{
this.typeToSerialize = typeToSerialize;
this.serializer = serializer;
}

public SerializeAction(PropertyInfo propertyToSerialize, Func<object, string> serializer)
{
this.propertyToSerialize = propertyToSerialize;
this.serializer = serializer;
}

public bool CanHandle(PropertyInfo property)
{
return propertyToSerialize != null && propertyToSerialize == property ||
typeToSerialize != null && typeToSerialize.IsAssignableFrom(property.PropertyType);
}

public ActionResult Handle(object value)
{
return new ActionResult
{
IsSkipped = false,
Value = serializer(value)
};
}
}
41 changes: 41 additions & 0 deletions ObjectPrinting/Actions/SpecifyCultureAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Globalization;
using System.Reflection;

namespace ObjectPrinting.Actions;

public class SpecifyCultureAction : IAction
{
private readonly Type? typeToFormat = null;
private readonly PropertyInfo? propertyToFormat = null;
private readonly CultureInfo cultureInfo;

public SpecifyCultureAction(Type typeToFormat, CultureInfo cultureInfo)
{
this.typeToFormat = typeToFormat;
this.cultureInfo = cultureInfo;
}

public SpecifyCultureAction(PropertyInfo propertyToFormat, CultureInfo cultureInfo)
{
this.propertyToFormat = propertyToFormat;
this.cultureInfo = cultureInfo;
}


public bool CanHandle(PropertyInfo property)
{
return propertyToFormat != null && propertyToFormat == property &&
typeof(IFormattable).IsAssignableFrom(property.PropertyType) ||
typeToFormat != null && typeToFormat.IsAssignableFrom(property.PropertyType);
}

public ActionResult Handle(object value)
{
return new ActionResult
{
IsSkipped = false,
Value = ((IFormattable)value).ToString(null, cultureInfo)
};
}
}
32 changes: 32 additions & 0 deletions ObjectPrinting/Actions/TrimAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Reflection;

namespace ObjectPrinting.Actions;

public class TrimAction : IAction
{
private readonly int maxLength;
private readonly PropertyInfo propertyToTrim;

public TrimAction(PropertyInfo propertyToTrim, int maxLength)
{
ArgumentOutOfRangeException.ThrowIfNegative(maxLength);

this.propertyToTrim = propertyToTrim;
this.maxLength = maxLength;
Comment thread
sheptikhinv marked this conversation as resolved.
}

public bool CanHandle(PropertyInfo property)
{
return propertyToTrim == property && property.PropertyType == typeof(string);
}

public ActionResult Handle(object value)
{
return new ActionResult
{
IsSkipped = false,
Value = ((string)value)[..maxLength]
};
}
}
11 changes: 11 additions & 0 deletions ObjectPrinting/Expected/ObjectPrinter_CustomPropertySerializer.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Person
Id = 00000000-0000-0000-0000-000000000000
Name = Alex
Surname = null
Height = 0
Age = 19 years old
LastTimeOnline = 01.01.0001 0:00:00
Father = null
Child = null
Tags = null
CustomProperties = null
11 changes: 11 additions & 0 deletions ObjectPrinting/Expected/ObjectPrinter_CustomTypeSerializer.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Person
Id = 00000000-0000-0000-0000-000000000000
Name = Alex - string
Surname = Brown - string
Height = 2005
Age = 19
LastTimeOnline = 01.01.0001 0:00:00
Father = null
Child = null
Tags = null
CustomProperties = null
10 changes: 10 additions & 0 deletions ObjectPrinting/Expected/ObjectPrinter_ExcludeProperty.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Person
Id = 00000000-0000-0000-0000-000000000000
Name = Alex
Surname = null
Height = 0
LastTimeOnline = 01.01.0001 0:00:00
Father = null
Child = null
Tags = null
CustomProperties = null
10 changes: 10 additions & 0 deletions ObjectPrinting/Expected/ObjectPrinter_ExcludeType.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Person
Name = Alex
Surname = null
Height = 0
Age = 19
LastTimeOnline = 01.01.0001 0:00:00
Father = null
Child = null
Tags = null
CustomProperties = null
14 changes: 14 additions & 0 deletions ObjectPrinting/Expected/ObjectPrinter_ICollection.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Person
Id = 00000000-0000-0000-0000-000000000000
Name = Alex
Surname = null
Height = 0
Age = 19
LastTimeOnline = 01.01.0001 0:00:00
Father = null
Child = null
Tags = [
[0] = tag1
[1] = tag2
]
CustomProperties = null
14 changes: 14 additions & 0 deletions ObjectPrinting/Expected/ObjectPrinter_IDictionary.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Person
Id = 00000000-0000-0000-0000-000000000000
Name = Alex
Surname = null
Height = 0
Age = 19
LastTimeOnline = 01.01.0001 0:00:00
Father = null
Child = null
Tags = null
CustomProperties = {
PhoneNumber = 88005553535
HomeAddress = Novokoltsovo
}
21 changes: 21 additions & 0 deletions ObjectPrinting/Expected/ObjectPrinter_NoStackOverflow.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Person
Id = 00000000-0000-0000-0000-000000000000
Name = Alex
Surname = null
Height = 0
Age = 19
LastTimeOnline = 01.01.0001 0:00:00
Father = Person
Id = 00000000-0000-0000-0000-000000000000
Name = John
Surname = null
Height = 0
Age = 42
LastTimeOnline = 01.01.0001 0:00:00
Father = null
Child = cyclic reference
Tags = null
CustomProperties = null
Child = null
Tags = null
CustomProperties = null
11 changes: 11 additions & 0 deletions ObjectPrinting/Expected/ObjectPrinter_SpecifyPropertyCulture.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Person
Id = 00000000-0000-0000-0000-000000000000
Name = Alex
Surname = null
Height = 185.12
Age = 19
LastTimeOnline = 01.01.0001 0:00:00
Father = null
Child = null
Tags = null
CustomProperties = null
11 changes: 11 additions & 0 deletions ObjectPrinting/Expected/ObjectPrinter_SpecifyTypeCulture.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Person
Id = 00000000-0000-0000-0000-000000000000
Name = Alex
Surname = null
Height = 0
Age = 19
LastTimeOnline = 1/1/2021 12:00:00 PM
Father = null
Child = null
Tags = null
CustomProperties = null
11 changes: 11 additions & 0 deletions ObjectPrinting/Expected/ObjectPrinter_TrimProperty.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Person
Id = 00000000-0000-0000-0000-000000000000
Name = Alex
Surname = VeryLongSu
Height = 0
Age = 19
LastTimeOnline = 01.01.0001 0:00:00
Father = null
Child = null
Tags = null
CustomProperties = null
Loading