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

Козлов Данил #185

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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/IPrintingConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace ObjectPrinting
{
public interface IPrintingConfig<TOwner>
{
SerializationSettings SerializationSettings { get; }
}
}
11 changes: 11 additions & 0 deletions ObjectPrinting/IPropertyPrintingConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Reflection;

namespace ObjectPrinting
{
public interface IPropertyPrintingConfig<TOwner, TPropType>
{
PrintingConfig<TOwner> ParentConfig { get; }

PropertyInfo PropertyInfo { get; }
}
}
20 changes: 11 additions & 9 deletions ObjectPrinting/ObjectPrinting.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<LangVersion>8</LangVersion>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
</PropertyGroup>
<PropertyGroup>
<LangVersion>8</LangVersion>
<TargetFramework>netcoreapp3.1</TargetFramework>
SenyaPevko marked this conversation as resolved.
Show resolved Hide resolved
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="NUnit" Version="3.12.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1"/>
<PackageReference Include="NUnit" Version="3.12.0"/>
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0"/>
</ItemGroup>

</Project>
122 changes: 92 additions & 30 deletions ObjectPrinting/PrintingConfig.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,103 @@
using System;
using System.Linq;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;

namespace ObjectPrinting
{
public class PrintingConfig<TOwner>
public class PrintingConfig<TOwner> : IPrintingConfig<TOwner>
{
private readonly SerializationSettings _serializationSettings;

This comment was marked as resolved.


public PrintingConfig()
{
_serializationSettings = new SerializationSettings();

This comment was marked as resolved.

}

SerializationSettings IPrintingConfig<TOwner>.SerializationSettings => _serializationSettings;

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();
return PrintObjectToString(obj, 0);
}

public string PrintToString(IList<TOwner> lsit)

This comment was marked as resolved.

This comment was marked as resolved.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

исправил

{
return PrintEnumerableToString(lsit);
}

public string PrintToString(TOwner[] array)
{
return PrintEnumerableToString(array);
}

public string PrintToString<TValue>(Dictionary<TOwner, TValue> dict)
{
return PrintDictionaryToString(dict);
}

public string PrintToString<TKey>(Dictionary<TKey, TOwner> dict)
{
return PrintDictionaryToString(dict);
}

public PrintingConfig<TOwner> Excluding<TPropType>()
{
_serializationSettings.AddTypeToExclude(typeof(TPropType));

return this;
}

public PrintingConfig<TOwner> Excluding<TPropType>(Expression<Func<TOwner, TPropType>> property)
{
var memberBody = (MemberExpression)property.Body;
var propertyInfo = memberBody.Member as PropertyInfo;
_serializationSettings.AddPropertyToExclude(propertyInfo);

return this;
}

public PropertyPrintingConfig<TOwner, TPropType> Printing<TPropType>()
{
return new PropertyPrintingConfig<TOwner, TPropType>(this);
}

public PropertyPrintingConfig<TOwner, TPropType> Printing<TPropType>(
Expression<Func<TOwner, TPropType>> printProperty)
{
var memberBody = (MemberExpression)printProperty.Body;
var propertyInfo = memberBody.Member as PropertyInfo;

return new PropertyPrintingConfig<TOwner, TPropType>(this, propertyInfo);
}

private string PrintObjectToString(object obj, int nestingLevel)
{
_serializationSettings.GetSerializedObjects().Clear();
var serializer = new Serializer(_serializationSettings);

return serializer.SerializeObject(obj, nestingLevel);
}

private string PrintEnumerableToString(IEnumerable<TOwner> enumerable)

This comment was marked as resolved.

{
var enumerableInString = new StringBuilder();

foreach (var item in enumerable)
enumerableInString.Append(PrintObjectToString(item, 0));

return enumerableInString.ToString();
}

private string PrintDictionaryToString<TKey, TValue>(IDictionary<TKey, TValue> dictionary)
{
var dictInString = new StringBuilder();

This comment was marked as resolved.


foreach (var pair in dictionary)
dictInString.AppendLine(PrintObjectToString(pair.Key, 0) + " : " + PrintObjectToString(pair.Value, 0));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Заюзал стрингбилдер и тут же используешь конкатенацию, почему бы не интерполировать строки?


return dictInString.ToString();
}
}
}
46 changes: 46 additions & 0 deletions ObjectPrinting/PropertyPrintingConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Globalization;
using System.Reflection;

namespace ObjectPrinting
{
public class PropertyPrintingConfig<TOwner, TPropType> : IPropertyPrintingConfig<TOwner, TPropType>

This comment was marked as resolved.

{
private readonly PrintingConfig<TOwner> printingConfig;
private readonly PropertyInfo propertyInfo;

public PropertyPrintingConfig(PrintingConfig<TOwner> printingConfig)
{
this.printingConfig = printingConfig;
}

public PropertyPrintingConfig(PrintingConfig<TOwner> printingConfig, PropertyInfo propertyInfo)
{
this.printingConfig = printingConfig;
this.propertyInfo = propertyInfo;
}

PropertyInfo IPropertyPrintingConfig<TOwner, TPropType>.PropertyInfo => propertyInfo;

This comment was marked as resolved.


PrintingConfig<TOwner> IPropertyPrintingConfig<TOwner, TPropType>.ParentConfig => printingConfig;

public PrintingConfig<TOwner> Using<IFormatable>(CultureInfo cultureInfo)

This comment was marked as resolved.

{
((IPrintingConfig<TOwner>)printingConfig).SerializationSettings.AddCultureForType(typeof(TPropType),
cultureInfo);

return printingConfig;
}

public PrintingConfig<TOwner> Using(Func<TPropType, string> printProperty)
{
if (propertyInfo == null)

This comment was marked as resolved.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

вынес работу со свойствами и полями в отдельный класс

((IPrintingConfig<TOwner>)printingConfig).SerializationSettings.AddTypeSerialization(printProperty);
else
((IPrintingConfig<TOwner>)printingConfig).SerializationSettings.AddPropertySerialization(propertyInfo,
printProperty);

return printingConfig;
}
}
}
16 changes: 16 additions & 0 deletions ObjectPrinting/PropertyPrintingConfigExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace ObjectPrinting
{
public static class PropertyPrintingConfigExtension
{
public static PrintingConfig<TOwner> TrimmedToLength<TOwner>(
this PropertyPrintingConfig<TOwner, string> propConfig, int length)

This comment was marked as resolved.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

сделал выбрасывание ошибки на такой случай

{
var printingConfig = ((IPropertyPrintingConfig<TOwner, string>)propConfig).ParentConfig;
var property = ((IPropertyPrintingConfig<TOwner, string>)propConfig).PropertyInfo;

((IPrintingConfig<TOwner>)printingConfig).SerializationSettings.AddPropertyToTrim(property, length);

return printingConfig;
}
}
}
115 changes: 115 additions & 0 deletions ObjectPrinting/SerializationSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;

namespace ObjectPrinting
{
public class SerializationSettings
{
private static readonly HashSet<Type> _finalTypes = new HashSet<Type>
{
typeof(int), typeof(uint), typeof(double), typeof(float), typeof(decimal),
typeof(long), typeof(ulong), typeof(short), typeof(ushort),
typeof(string), typeof(bool),
typeof(DateTime), typeof(TimeSpan)
};

private readonly Dictionary<Type, CultureInfo> _cultureForType;

This comment was marked as resolved.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

исправил

private readonly HashSet<PropertyInfo> _propertiesToExclude;
private readonly Dictionary<PropertyInfo, int> _propertiesToTrim;
private readonly Dictionary<PropertyInfo, Func<object, string>> _serializationForProperty;

This comment was marked as resolved.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

пытался понять как это сделать, но не успел к дедлайну

private readonly Dictionary<Type, Func<object, string>> _serializationForType;
private readonly HashSet<object> _serializedObjects;
private readonly HashSet<Type> _typesToExclude;

public SerializationSettings()
{
_typesToExclude = new HashSet<Type>();
_propertiesToExclude = new HashSet<PropertyInfo>();
_cultureForType = new Dictionary<Type, CultureInfo>();
_serializationForType = new Dictionary<Type, Func<object, string>>();
_serializationForProperty = new Dictionary<PropertyInfo, Func<object, string>>();
_propertiesToTrim = new Dictionary<PropertyInfo, int>();
_serializedObjects = new HashSet<object>();
}

public void AddTypeToExclude(params Type[] types)
{
foreach (var type in types)
_typesToExclude.Add(type);
}

public void AddPropertyToExclude(params PropertyInfo[] properties)
{
foreach (var property in properties)
_propertiesToExclude.Add(property);
}

public void AddCultureForType(Type type, CultureInfo culture)

This comment was marked as resolved.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

исправил

{
_cultureForType.TryAdd(type, culture);
}

public void AddTypeSerialization<TPropType>(Func<TPropType, string> typeSerialization)
{
_serializationForType.TryAdd(typeof(TPropType), type => typeSerialization((TPropType)type));
}

public void AddPropertySerialization<TPropType>(PropertyInfo property,
Func<TPropType, string> propertySerialization)
{
_serializationForProperty.TryAdd(property, type => propertySerialization((TPropType)type));
}

public void AddPropertyToTrim(PropertyInfo property, int length)
{
_propertiesToTrim.TryAdd(property, length);
}

public void AddSerializedObject(object obj)
{
_serializedObjects.Add(obj);
}

public HashSet<Type> GetExcludingTypes()

This comment was marked as resolved.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

убрал доступ к приватным полям

{
return _typesToExclude;
}

public HashSet<PropertyInfo> GetExcludingProperties()
{
return _propertiesToExclude;
}

public Dictionary<Type, CultureInfo> GetTypesWithCulture()
{
return _cultureForType;
}

public Dictionary<Type, Func<object, string>> GetTypesSerializations()
{
return _serializationForType;
}

public Dictionary<PropertyInfo, Func<object, string>> GetPropertiesSerializations()
{
return _serializationForProperty;
}

public Dictionary<PropertyInfo, int> GetPropertiesToTrim()
{
return _propertiesToTrim;
}

public HashSet<Type> GetFinalTypes()
{
return _finalTypes;

This comment was marked as resolved.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

убрал доступ к публичным полям

}

public HashSet<object> GetSerializedObjects()
{
return _serializedObjects;
}
}
}
Loading