Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Шмаков Данил #186

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
37 changes: 37 additions & 0 deletions ObjectPrinting/Helper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Linq.Expressions;
using System.Reflection;

namespace ObjectPrinting
{
public static class Helper
{
public static PropertyInfo GetPropertyInfo<TSource, TProperty>(Expression<Func<TSource, TProperty>> propertyLambda)
{
if (!(propertyLambda.Body is MemberExpression member))
{
Reltig marked this conversation as resolved.
Show resolved Hide resolved
throw new ArgumentException(string.Format(
"Expression '{0}' refers to a method, not a property.",
propertyLambda.ToString()));
Reltig marked this conversation as resolved.
Show resolved Hide resolved
}

if (!(member.Member is PropertyInfo propInfo))
{
throw new ArgumentException(string.Format(
"Expression '{0}' refers to a field, not a property.",
propertyLambda.ToString()));
}

Type type = typeof(TSource);
if (propInfo.ReflectedType != null && type != propInfo.ReflectedType && !type.IsSubclassOf(propInfo.ReflectedType))
Reltig marked this conversation as resolved.
Show resolved Hide resolved
{
throw new ArgumentException(string.Format(
"Expression '{0}' refers to a property that is not from type {1}.",
propertyLambda.ToString(),
type));
}

return propInfo;
}
}
}
14 changes: 14 additions & 0 deletions ObjectPrinting/IBaseConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Linq.Expressions;

namespace ObjectPrinting
{
public interface IBaseConfig<TOwner>
{
string PrintToString(TOwner obj);
IBaseConfig<TOwner> Exclude<T>();
IBaseConfig<TOwner> Exclude<TArg>(Expression<Func<TOwner, TArg>> f);
TypeConfig<TOwner, TArg> Printing<TArg>();
PropertyConfig<TOwner, TProperty> Printing<TProperty>(Expression<Func<TOwner, TProperty>> f);
}
}
2 changes: 1 addition & 1 deletion ObjectPrinting/ObjectPrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace ObjectPrinting
{
public class ObjectPrinter
{
public static PrintingConfig<T> For<T>()
public static IBaseConfig<T> For<T>()
{
return new PrintingConfig<T>();
}
Expand Down
5 changes: 5 additions & 0 deletions ObjectPrinting/ObjectPrinting.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="7.0.0-alpha.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="NUnit" Version="3.12.0" />
</ItemGroup>

<ItemGroup>
<Folder Include="Tests\" />
</ItemGroup>

Reltig marked this conversation as resolved.
Show resolved Hide resolved
</Project>
201 changes: 167 additions & 34 deletions ObjectPrinting/PrintingConfig.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,174 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using NUnit.Framework;

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

private string PrintToString(object obj, int nestingLevel)
{
//TODO apply configurations
if (obj == null)
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;

var identation = new string('\t', nestingLevel + 1);
var sb = new StringBuilder();
var type = obj.GetType();
sb.AppendLine(type.Name);
foreach (var propertyInfo in type.GetProperties())
{
sb.Append(identation + propertyInfo.Name + " = " +
PrintToString(propertyInfo.GetValue(obj),
nestingLevel + 1));
}
return sb.ToString();
}
}
public class PrintingConfig<TOwner> :
IBaseConfig<TOwner>
{
private readonly Type[] finalTypes = new[]
{
typeof(int), typeof(double), typeof(float), typeof(string),
typeof(DateTime), typeof(TimeSpan), typeof(Guid)
};

private readonly HashSet<object> objects;

private List<Type> excludedTypes;
private List<PropertyInfo> excludedProperties;

private Dictionary<Type, Func<object, string>> serializedByType;
private Dictionary<PropertyInfo, Func<object, string>> serializedByPropertyInfo;

Reltig marked this conversation as resolved.
Show resolved Hide resolved
public PrintingConfig()
{
objects = new HashSet<object>();
excludedTypes = new List<Type>();
excludedProperties = new List<PropertyInfo>();
serializedByType = new Dictionary<Type, Func<object, string>>();
serializedByPropertyInfo = new Dictionary<PropertyInfo, Func<object, string>>();
}

public string PrintToString(TOwner obj)
{
return PrintToString(obj, 0);
}

private string PrintToString(object obj, int nestingLevel)
{
//TODO apply configurations
if (obj == null)
Reltig marked this conversation as resolved.
Show resolved Hide resolved
return "null" + Environment.NewLine;

if (finalTypes.Contains(obj.GetType()))
return obj + Environment.NewLine;

if (objects.Contains(obj))
return "cycled" + Environment.NewLine;
objects.Add(obj);

var identation = new string('\t', nestingLevel + 1);
var sb = new StringBuilder();
var type = obj.GetType();
sb.AppendLine(type.Name);
foreach (var propertyInfo in type.GetProperties())
{
var propType = propertyInfo.PropertyType;

if (excludedProperties.Contains(propertyInfo) || excludedTypes.Contains(propType))
continue;

if (serializedByPropertyInfo.ContainsKey(propertyInfo))
{
Reltig marked this conversation as resolved.
Show resolved Hide resolved
var serializedValue = serializedByPropertyInfo[propertyInfo](propertyInfo.GetValue(obj));
sb.Append(identation + propertyInfo.Name + " = " + serializedValue);
continue;
}

if (serializedByType.ContainsKey(propType))
{
var serializedValue = serializedByType[propType](propertyInfo.GetValue(obj));
sb.Append(identation + propertyInfo.Name + " = " + serializedValue);
continue;
}

if (propType.BaseType == typeof(Array) || propType.GetInterfaces().Contains(typeof(IList)))
{
sb.Append(identation + propertyInfo.Name + ": ");
sb.Append(SerializeListElements(propertyInfo.GetValue(obj), nestingLevel + 1));
continue;
}
Reltig marked this conversation as resolved.
Show resolved Hide resolved

if (propType.GetInterfaces().Contains(typeof(IDictionary)))
{
sb.Append(identation + propertyInfo.Name + ": ");
sb.Append(SerializeDictionaryElements(propertyInfo.GetValue(obj), nestingLevel + 1));
continue;
}

sb.Append(identation + propertyInfo.Name + " = " +
PrintToString(propertyInfo.GetValue(obj),
nestingLevel + 1));
}

return sb.ToString();
}

private string SerializeDictionaryElements(object obj, int nesting)
{
var sb = new StringBuilder();
var dictionary = obj as IDictionary;
var identation = new string('\t', nesting + 1);
if (dictionary == null || dictionary.Keys.Count == 0)
return "<empty>" + Environment.NewLine;
sb.Append(Environment.NewLine);
var index = 1;
foreach (var element in dictionary.Keys)
{
sb.AppendLine(identation + index++ + " element:");
sb.Append(identation + "\tKey: " + PrintToString(element, nesting + 2));
sb.Append(identation + "\tValue: " + PrintToString(dictionary[element], nesting + 2));
}

return sb.ToString();
}

private string SerializeListElements(object obj, int nesting)
{
var sb = new StringBuilder();
var list = obj as IEnumerable;
var identation = new string('\t', nesting + 1);
if (list == null || !list.Cast<object>().Any())
return "<empty>" + Environment.NewLine;
Reltig marked this conversation as resolved.
Show resolved Hide resolved
sb.Append(Environment.NewLine);
var index = 1;
Reltig marked this conversation as resolved.
Show resolved Hide resolved
foreach (var element in list)
{
sb.Append(identation + index++ + ": " + PrintToString(element, nesting + 1));
}

return sb.ToString();
}

public IBaseConfig<TOwner> Exclude<TArg>(Expression<Func<TOwner, TArg>> f)
{
var configuratedProperty = Helper.GetPropertyInfo(f);
excludedProperties.Add(configuratedProperty);
return this;
}

public IBaseConfig<TOwner> Exclude<T>()
{
excludedTypes.Add(typeof(T));
return this;
}

TypeConfig<TOwner, TArg> IBaseConfig<TOwner>.Printing<TArg>()
{
var configuratedType = typeof(TArg);
return new TypeConfig<TOwner, TArg>(this, configuratedType);
}
Reltig marked this conversation as resolved.
Show resolved Hide resolved

public PropertyConfig<TOwner, TArg> Printing<TArg>(Expression<Func<TOwner, TArg>> f)
{
var configuratedProperty = Helper.GetPropertyInfo(f);
return new PropertyConfig<TOwner, TArg>(this, configuratedProperty);
}

public void AddPropertySerialization<TProperty>(Func<TProperty, string> f, PropertyInfo configuratedProperty)
{
serializedByPropertyInfo.Add(configuratedProperty, obj => f((TProperty)obj));
}

public void AddTypeSerialization<TProperty>(Func<TProperty, string> f, Type configuratedType)
{
serializedByType.Add(configuratedType, obj => f((TProperty)obj));
}
}
}
28 changes: 28 additions & 0 deletions ObjectPrinting/PrintingExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Globalization;

namespace ObjectPrinting
{
public static class PrintingExtension
{
public static PrintingConfig<TOwner> Truncate<TOwner>(this PropertyConfig<TOwner, string> config, int startPos, int length)
{
return config.SerializeAs(s => s.Substring(startPos, length));
}

public static PrintingConfig<TOwner> SetCulture<TOwner>(this TypeConfig<TOwner, double> config, CultureInfo info)
{
return config.SerializeAs(d => d.ToString(info));
}

public static PrintingConfig<TOwner> SetCulture<TOwner>(this PropertyConfig<TOwner, double> config, CultureInfo info)
{
return config.SerializeAs(d => d.ToString(info));
}

public static PrintingConfig<TOwner> SetCulture<TOwner>(this TypeConfig<TOwner, DateTime> config, CultureInfo info)
{
return config.SerializeAs(date => date.ToString(info));
}
Reltig marked this conversation as resolved.
Show resolved Hide resolved
}
}
23 changes: 23 additions & 0 deletions ObjectPrinting/PropertyConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Reflection;

namespace ObjectPrinting
{
public class PropertyConfig<TOwner, TProperty>
{
private readonly PrintingConfig<TOwner> config;
private readonly PropertyInfo configuratedProperty;

public PropertyConfig(PrintingConfig<TOwner> config, PropertyInfo configuratedProperty)
{
this.config = config;
this.configuratedProperty = configuratedProperty;
}

public PrintingConfig<TOwner> SerializeAs(Func<TProperty, string> f)
{
config.AddPropertySerialization(f, configuratedProperty);
return config;
}
}
}
27 changes: 0 additions & 27 deletions ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs

This file was deleted.

22 changes: 22 additions & 0 deletions ObjectPrinting/TypeConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;

namespace ObjectPrinting
{
public class TypeConfig<TOwner, TProperty>
{
private readonly PrintingConfig<TOwner> config;
private readonly Type configuratedType;

public TypeConfig(PrintingConfig<TOwner> config, Type configuratedType)
{
this.config = config;
this.configuratedType = configuratedType;
}

public PrintingConfig<TOwner> SerializeAs(Func<TProperty, string> f)
{
config.AddTypeSerialization(f, configuratedType);
return config;
}
}
}
1 change: 1 addition & 0 deletions ObjectPrintingTest/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using NUnit.Framework;
Loading