-
Notifications
You must be signed in to change notification settings - Fork 213
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
Яценко Ирина #198
base: master
Are you sure you want to change the base?
Яценко Ирина #198
Changes from 1 commit
871837c
767afb5
6432b58
0aae76d
d14c11a
3a465c2
4a2ac35
59e3400
b64fd61
5a18f9c
230d3cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,156 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
using System.Text; | ||
|
||
namespace ObjectPrinting | ||
{ | ||
public class PrintingConfig<TOwner> | ||
This comment was marked as resolved.
Sorry, something went wrong.
This comment was marked as resolved.
Sorry, something went wrong. |
||
{ | ||
private readonly List<MemberInfo> exludedProperties = new List<MemberInfo>(); | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
private readonly List<Type> excludedTypes = new List<Type>(); | ||
private readonly Dictionary<object, int> complexObjectLinks = new Dictionary<object, int>(); | ||
private int maxRecursion = 2; | ||
|
||
private readonly Type[] finalTypes = | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
{ | ||
typeof(int), typeof(double), typeof(float), typeof(string), typeof(DateTime), typeof(TimeSpan), typeof(Guid) | ||
}; | ||
|
||
private readonly Dictionary<Type, Func<object, string>> typeSerializesInfos = | ||
new Dictionary<Type, Func<object, string>>(); | ||
|
||
private readonly Dictionary<MemberInfo, Func<object, string>> membersSerializesInfos = | ||
new Dictionary<MemberInfo, Func<object, string>>(); | ||
|
||
|
||
public string PrintToString(TOwner obj) | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
{ | ||
return PrintToString(obj, 0); | ||
} | ||
|
||
public PrintingConfig<TOwner> WithMaxRecursion(int maxRecursion) | ||
{ | ||
if (maxRecursion < 1) | ||
throw new ArgumentException(); | ||
this.maxRecursion = maxRecursion; | ||
|
||
return this; | ||
} | ||
|
||
public PrintingConfig<TOwner> Exclude<TResult>(Expression<Func<TOwner, TResult>> exclude) | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
{ | ||
var memberInfo = GetMemberInfo(exclude); | ||
|
||
exludedProperties.Add(memberInfo); | ||
|
||
return this; | ||
} | ||
|
||
public PrintingConfig<TOwner> SetCulture<T>(CultureInfo culture) | ||
where T : IFormattable | ||
{ | ||
return SerializeWith<T>( | ||
p => p.ToString(null, culture)); | ||
} | ||
|
||
public PrintingConfig<TOwner> SerializeWith<T>(Func<T, string> serialize) | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
{ | ||
Func<object, string> func = p => serialize((T)p); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Можно выспользоваться делегатами и поздней привязкой, это позволит не использовать страшноватый p => serialize((T)p) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Это в сторону dynamic |
||
|
||
typeSerializesInfos[typeof(T)] = func; | ||
|
||
return this; | ||
} | ||
|
||
public PrintingConfig<TOwner> SerializeWith<T>( | ||
This comment was marked as resolved.
Sorry, something went wrong.
This comment was marked as resolved.
Sorry, something went wrong. |
||
Expression<Func<TOwner, T>> property, | ||
Func<T, string> serialize) | ||
{ | ||
var memberInfo = GetMemberInfo(property); | ||
Func<object, string> func = p => serialize((T)p); | ||
|
||
membersSerializesInfos[memberInfo] = func; | ||
|
||
return this; | ||
} | ||
|
||
private MemberInfo GetMemberInfo<T>(Expression<Func<TOwner, T>> expression) | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
{ | ||
var memberExpression = expression.Body is UnaryExpression unaryExpression | ||
? (MemberExpression)unaryExpression.Operand | ||
: (MemberExpression)expression.Body; | ||
|
||
return memberExpression.Member; | ||
} | ||
|
||
public PrintingConfig<TOwner> Trim(Expression<Func<TOwner, string>> property, int length) | ||
This comment was marked as resolved.
Sorry, something went wrong.
This comment was marked as resolved.
Sorry, something went wrong. |
||
{ | ||
Func<string, string> func = value => value.Length <= length | ||
? value | ||
: value.Substring(0, length); | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
|
||
return SerializeWith(property, func); | ||
} | ||
|
||
public PrintingConfig<TOwner> Exclude<T>() | ||
{ | ||
excludedTypes.Add(typeof(T)); | ||
|
||
return this; | ||
} | ||
|
||
private string PrintToString(object obj, int nestingLevel) | ||
This comment was marked as resolved.
Sorry, something went wrong.
This comment was marked as resolved.
Sorry, something went wrong. |
||
{ | ||
//TODO apply configurations | ||
if (obj == null) | ||
return "null" + Environment.NewLine; | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
|
||
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 (!complexObjectLinks.ContainsKey(obj)) | ||
This comment was marked as resolved.
Sorry, something went wrong.
This comment was marked as resolved.
Sorry, something went wrong. |
||
complexObjectLinks[obj] = 0; | ||
complexObjectLinks[obj]++; | ||
|
||
if (complexObjectLinks[obj] == maxRecursion) | ||
return "Maximum recursion has been reached" + Environment.NewLine; | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
|
||
var identation = new string('\t', nestingLevel + 1); | ||
This comment was marked as resolved.
Sorry, something went wrong. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Го заюзаем ради интереса string.Intern(), это позволит избежать лишних аллокаций Если хранить в ста или, что еще хуже, в миллионе строковых переменных одинаковое значение получится, что память для хранения значений строк будет выделяться снова и снова. Интернирование строки это способ обойти эту проблему |
||
|
||
var sb = new StringBuilder(); | ||
var type = obj.GetType(); | ||
sb.AppendLine(type.Name); | ||
|
||
foreach (var propertyInfo in type.GetProperties()) | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
{ | ||
if (exludedProperties.Any(m => m.Name == propertyInfo.Name) || | ||
excludedTypes.Contains(propertyInfo.PropertyType)) | ||
continue; | ||
|
||
if (membersSerializesInfos.TryGetValue(propertyInfo, out var serializeMember)) | ||
{ | ||
var t = serializeMember(propertyInfo.GetValue(obj)); | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
|
||
sb.Append(identation + propertyInfo.Name + " = " + t + Environment.NewLine); | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
continue; | ||
} | ||
|
||
if (typeSerializesInfos.TryGetValue(propertyInfo.PropertyType, out var serializeType)) | ||
{ | ||
var t = serializeType(propertyInfo.GetValue(obj)); | ||
|
||
sb.Append(identation + propertyInfo.Name + " = " + t + Environment.NewLine); | ||
continue; | ||
} | ||
|
||
sb.Append(identation + propertyInfo.Name + " = " + | ||
PrintToString(propertyInfo.GetValue(obj), | ||
nestingLevel + 1)); | ||
} | ||
|
||
return sb.ToString(); | ||
} | ||
} | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using System.Globalization; | ||
using FluentAssertions; | ||
using NUnit.Framework; | ||
using ObjectPrinting; | ||
using ObjectPrinting.Tests; | ||
|
||
namespace ObjectPrintingTests | ||
{ | ||
[TestFixture] | ||
public class ObjectPrinterAcceptanceTests | ||
This comment was marked as resolved.
Sorry, something went wrong.
This comment was marked as resolved.
Sorry, something went wrong. |
||
{ | ||
[Test] | ||
public void WhenExcludeType_ShouldReturnWithoutThisType() | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
{ | ||
var person = new Person { Name = "Alex", Age = 19 }; | ||
|
||
var printer = ObjectPrinter.For<Person>(); | ||
printer.Exclude(p => p.Age) | ||
.Exclude<double>(); | ||
|
||
//1. Исключить из сериализации свойства определенного типа | ||
//2. Указать альтернативный способ сериализации для определенного типа | ||
//3. Для числовых типов указать культуру | ||
//4. Настроить сериализацию конкретного свойства | ||
//5. Настроить обрезание строковых свойств (метод должен быть виден только для строковых свойств) | ||
//6. Исключить из сериализации конкретного свойства | ||
|
||
var s1 = printer.PrintToString(person); | ||
s1.Should().Be( | ||
"Person\r\n\tId = 00000000-0000-0000-0000-000000000000\r\n\tName = Alex\r\n\tSubPerson = null\r\n"); | ||
|
||
//7. Синтаксический сахар в виде метода расширения, сериализующего по-умолчанию | ||
//8. ...с конфигурированием | ||
} | ||
|
||
[Test] | ||
public void TrimString_ShouldReturnSubstring() | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
{ | ||
var person = new Person { Name = "Petr", Age = 20, Height = 180 }; | ||
var printer = ObjectPrinter.For<Person>(); | ||
|
||
var s1 = printer.Trim(p => p.Name, 1).PrintToString(person); | ||
|
||
s1.Should().Be( | ||
"Person\r\n\tId = 00000000-0000-0000-0000-000000000000\r\n\tName = P\r\n\tHeight = 180\r\n\tAge = 20\r\n\tSubPerson = null\r\n"); | ||
} | ||
|
||
[Test] | ||
public void ShouldSerializeMember_WithGivenFunc() | ||
{ | ||
var person = new Person { Name = "Petr", Age = 20, Height = 180 }; | ||
var printer = ObjectPrinter.For<Person>(); | ||
|
||
var s1 = printer.SerializeWith(person => person.Age, age => (age + 1000).ToString()).PrintToString(person); | ||
|
||
s1.Should().Be( | ||
"Person\r\n\tId = 00000000-0000-0000-0000-000000000000\r\n\tName = Petr\r\n\tHeight = 180\r\n\tAge = 1020\r\n\tSubPerson = null\r\n"); | ||
} | ||
|
||
[Test] | ||
public void SetCulture_ShouldAddedCultureInfo() | ||
{ | ||
var person = new Person { Name = "Petr", Age = 20, Height = 180.5 }; | ||
var printer = ObjectPrinter.For<Person>(); | ||
|
||
var s1 = printer.SetCulture<double>(CultureInfo.InvariantCulture).PrintToString(person); | ||
|
||
s1.Should().Be( | ||
"Person\r\n\tId = 00000000-0000-0000-0000-000000000000\r\n\tName = Petr\r\n\tHeight = 180.5\r\n\tAge = 20\r\n\tSubPerson = null\r\n"); | ||
} | ||
|
||
[Test] | ||
public void WhenCyclicLinksWasFound_ShouldPrintWithRecursionLimit() | ||
{ | ||
var person = new Person { Name = "Petr", Age = 20, Height = 180, SubPerson = new SubPerson() }; | ||
person.SubPerson.Age = 15; | ||
person.SubPerson.Person = person; | ||
var printer = ObjectPrinter.For<Person>(); | ||
|
||
var s1 = printer.PrintToString(person); | ||
|
||
s1.Should().Be( | ||
"Person\r\n\tId = 00000000-0000-0000-0000-000000000000\r\n\tName = Petr\r\n\tHeight = 180\r\n\tAge = 20\r\n\tSubPerson = SubPerson\r\n\t\tPerson = Maximum recursion has been reached\r\n\t\tAge = 15\r\n"); | ||
} | ||
|
||
[Test] | ||
public void WhenPassNull_ShouldReturnNullString() | ||
{ | ||
var printer = ObjectPrinter.For<Person>(); | ||
var s1 = printer.PrintToString(null); | ||
|
||
s1.Should().Be("null\r\n"); | ||
} | ||
|
||
[Test] | ||
public void WhenPassFinalType_ShouldReturnStringRepresentationOfThisType() | ||
{ | ||
var printer = ObjectPrinter.For<int>(); | ||
var s1 = printer.PrintToString(1); | ||
|
||
s1.Should().Be("1\r\n"); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="FluentAssertions" Version="6.12.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" /> | ||
<PackageReference Include="NUnit" Version="3.13.3" /> | ||
<PackageReference Include="NUnit3TestAdapter" Version="4.3.0" /> | ||
<PackageReference Include="NUnit.Analyzers" Version="3.5.0" /> | ||
<PackageReference Include="coverlet.collector" Version="3.1.2" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\ObjectPrinting\ObjectPrinting.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
|
||
namespace ObjectPrinting.Tests | ||
{ | ||
public class SubPerson | ||
{ | ||
public Person Person { get; set; } | ||
|
||
public int Age { get; set; } | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Все комменты обсуждаемы - можем обсуждать или в тредах, или в личке в телеге
За количество комментов не парься - это нормально, процесс обучения, повышения скилла, ревью именно для этого и нужно
На пофикшенные комментарии можно ставить реакции или писать пометочки, что и почему