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 2 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
33 changes: 33 additions & 0 deletions ObjectPrinting/Helper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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 not MemberExpression member)
{
throw new ArgumentException($"Expression '{propertyLambda}' refers to a method, not a property.");
}

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

var type = typeof(TSource);
if (propInfo.ReflectedType != null && type != propInfo.ReflectedType &&
!type.IsSubclassOf(propInfo.ReflectedType))
{
throw new ArgumentException(
$"Expression '{propertyLambda}' refers to a property that is not from type {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
3 changes: 2 additions & 1 deletion ObjectPrinting/ObjectPrinting.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<LangVersion>8</LangVersion>
<LangVersion>latest</LangVersion>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
</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>
Expand Down
146 changes: 139 additions & 7 deletions ObjectPrinting/PrintingConfig.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,173 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;

namespace ObjectPrinting
{
public class PrintingConfig<TOwner>
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 readonly List<Type> excludedTypes;
private readonly List<PropertyInfo> excludedProperties;

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

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)
{
objects.Clear();
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;

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.TryGetValue(propertyInfo, out var propertySerialization))
{
var serializedValue = propertySerialization(propertyInfo.GetValue(obj));
sb.Append(identation + propertyInfo.Name + " = " + serializedValue);
continue;
}

if (serializedByType.TryGetValue(propType, out var typeSerialization))
{
var serializedValue = typeSerialization(propertyInfo.GetValue(obj));
sb.Append(identation + propertyInfo.Name + " = " + serializedValue);
continue;
}

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

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 = 0;
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;
sb.Append(Environment.NewLine);
var index = 0;
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;
}

public TypeConfig<TOwner, TArg> Printing<TArg>()
{
var configuratedType = typeof(TArg);
return new TypeConfig<TOwner, TArg>(this, configuratedType);
}

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));
}
}
}
61 changes: 61 additions & 0 deletions ObjectPrinting/PrintingExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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));
}

public static PrintingConfig<TOwner> SetCulture<TOwner>(this PropertyConfig<TOwner, DateTime> config,
CultureInfo info)
{
return config.SerializeAs(date => date.ToString(info));
}
Reltig marked this conversation as resolved.
Show resolved Hide resolved

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

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

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

public static PrintingConfig<TOwner> SetCulture<TOwner>(this PropertyConfig<TOwner, int> config,
CultureInfo info)
{
return config.SerializeAs(date => date.ToString(info));
}
}
}
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;
}
}
}
Loading