-
Notifications
You must be signed in to change notification settings - Fork 214
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
Овечкин Илья #190
base: master
Are you sure you want to change the base?
Овечкин Илья #190
Changes from all commits
f674a77
3e67dd3
126567b
19c1d99
58d8da8
7176de7
c9aea10
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,4 +1,4 @@ | ||
namespace ObjectPrinting.Solved | ||
namespace ObjectPrinting | ||
{ | ||
public static class ObjectExtensions | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,131 @@ | ||
using NUnit.Framework; | ||
using System; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using System.Globalization; | ||
using System.Linq.Expressions; | ||
using System.Collections; | ||
|
||
namespace ObjectPrinting | ||
{ | ||
public class PrintingConfig<TOwner> | ||
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. |
||
{ | ||
private readonly SerializerConfig configuration; | ||
private readonly Type[] finalTypes = new[] | ||
{ | ||
typeof(int), typeof(double), typeof(float), typeof(string), | ||
typeof(DateTime), typeof(TimeSpan), typeof(Guid) | ||
}; | ||
|
||
public PrintingConfig() | ||
{ | ||
configuration = new SerializerConfig(); | ||
} | ||
|
||
public PrintingConfig(SerializerConfig configuration) | ||
{ | ||
this.configuration = new SerializerConfig(configuration); | ||
} | ||
|
||
public PrintingConfig<TOwner> Excluding<Type>() | ||
{ | ||
var newConfig = new PrintingConfig<TOwner>(configuration); | ||
newConfig.configuration.excludedTypes.Add(typeof(Type)); | ||
return newConfig; | ||
} | ||
|
||
public PrintingConfig<TOwner> Excluding<Property>(Expression<Func<TOwner, Property>> memberSelector) | ||
{ | ||
var propInfo = ((MemberExpression)memberSelector.Body).Member as PropertyInfo; | ||
var newConfig = new PrintingConfig<TOwner>(configuration); | ||
newConfig.configuration.excludedProperties.Add(propInfo); | ||
return newConfig; | ||
} | ||
|
||
public PropertyPrintingConfig<TOwner, TPropType> Printing<TPropType>() | ||
{ | ||
var newConfig = new PrintingConfig<TOwner>(configuration); | ||
return new PropertyPrintingConfig<TOwner, TPropType>(newConfig, newConfig.configuration); | ||
} | ||
|
||
public PropertyPrintingConfig<TOwner, TPropType> Printing<TPropType>(Expression<Func<TOwner, TPropType>> memberSelector) | ||
{ | ||
var propInfo = ((MemberExpression)memberSelector.Body).Member as PropertyInfo; | ||
var newConfig = new PrintingConfig<TOwner>(configuration); | ||
return new PropertyPrintingConfig<TOwner, TPropType>(newConfig, newConfig.configuration, propInfo); | ||
} | ||
|
||
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[] | ||
var type = obj.GetType(); | ||
|
||
if (finalTypes.Contains(type)) | ||
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. В таком случае, получается, что если я принтер настрою на специфический формат инта, а вызову printer.PrintToString(123), то он просто напечатает 123, а не мой специфический формат. |
||
{ | ||
typeof(int), typeof(double), typeof(float), typeof(string), | ||
typeof(DateTime), typeof(TimeSpan) | ||
}; | ||
if (finalTypes.Contains(obj.GetType())) | ||
if(configuration.typeSerialization.TryGetValue(type, out var serializer)) | ||
return serializer.DynamicInvoke(obj) + Environment.NewLine; | ||
return obj + Environment.NewLine; | ||
} | ||
|
||
if (obj is ICollection collection) | ||
return SerializeCollection(collection, nestingLevel); | ||
|
||
configuration.serializedObjects.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 sb = new StringBuilder().Append(type.Name + Environment.NewLine); | ||
var properties = type.GetProperties().Where(propInfo => | ||
!configuration.excludedProperties.Contains(propInfo) && | ||
!configuration.excludedTypes.Contains(propInfo.PropertyType)); | ||
|
||
foreach (var propertyInfo in properties) | ||
{ | ||
sb.Append(identation + propertyInfo.Name + " = " + | ||
PrintToString(propertyInfo.GetValue(obj), | ||
nestingLevel + 1)); | ||
var propertyValue = propertyInfo.GetValue(obj); | ||
if (configuration.serializedObjects.Contains(propertyValue)) | ||
continue; | ||
if (configuration.propertiesSerialization.TryGetValue(propertyInfo, out var propSerializer)) | ||
sb.Append(identation + propertyInfo.Name + " = " + propSerializer.DynamicInvoke(propertyValue) + Environment.NewLine); | ||
else | ||
sb.Append(identation + propertyInfo.Name + " = " + PrintToString(propertyValue, nestingLevel + 1)); | ||
Comment on lines
+94
to
+97
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. Здесь была речь о том, что мы, по логике, должны уметь применять к полю обе сериализации, как типа, так и самого поля. |
||
} | ||
return sb.ToString(); | ||
} | ||
|
||
private string SerializeCollection(ICollection collection, int nestingLevel) | ||
{ | ||
if (collection is IDictionary dictionary) | ||
return SerializeDictionary(dictionary, nestingLevel); | ||
|
||
var sb = new StringBuilder(); | ||
foreach (var item in collection) | ||
sb.Append(PrintToString(item, nestingLevel + 1).Trim() + " "); | ||
|
||
return $"[ {sb}]" + Environment.NewLine; | ||
} | ||
|
||
private string SerializeDictionary(IDictionary dictionary, int nestingLevel) | ||
{ | ||
var sb = new StringBuilder(); | ||
var identation = new string('\t', nestingLevel); | ||
sb.Append(identation + "{" + Environment.NewLine); | ||
foreach (DictionaryEntry keyValuePair in dictionary) | ||
{ | ||
identation = new string('\t', nestingLevel + 1); | ||
sb.Append(identation + "[" + PrintToString(keyValuePair.Key, nestingLevel + 1).Trim() + " - "); | ||
sb.Append(PrintToString(keyValuePair.Value, 0).Trim()); | ||
sb.Append("],"); | ||
sb.Append(Environment.NewLine); | ||
} | ||
|
||
return sb + "}" + Environment.NewLine; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using NUnit.Framework; | ||
using System; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using System.Globalization; | ||
using System.Linq.Expressions; | ||
using Newtonsoft.Json.Linq; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace ObjectPrinting | ||
{ | ||
public class PropertyPrintingConfig<TOwner, TPropType> | ||
{ | ||
private readonly PrintingConfig<TOwner> printingConfig; | ||
private readonly SerializerConfig configuration; | ||
private readonly PropertyInfo propertyInfo; | ||
|
||
|
||
public PropertyPrintingConfig(PrintingConfig<TOwner> printingConfig, SerializerConfig configuration, PropertyInfo propertyInfo = null) | ||
{ | ||
this.printingConfig = printingConfig; | ||
this.configuration = configuration; | ||
this.propertyInfo = propertyInfo; | ||
} | ||
|
||
public PrintingConfig<TOwner> Using(Func<TPropType, string> print) | ||
{ | ||
if (propertyInfo == null) | ||
{ | ||
if (!configuration.typeSerialization.ContainsKey(typeof(TPropType))) | ||
configuration.typeSerialization.Add(typeof(TPropType), print); | ||
else | ||
configuration.typeSerialization[typeof(TPropType)] = print; | ||
} | ||
else | ||
{ | ||
if (!configuration.propertiesSerialization.ContainsKey(propertyInfo)) | ||
configuration.propertiesSerialization.Add(propertyInfo, print); | ||
else | ||
configuration.propertiesSerialization[propertyInfo] = print; | ||
} | ||
return printingConfig; | ||
} | ||
|
||
public PrintingConfig<TOwner> ParentConfig => printingConfig; | ||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using NUnit.Framework; | ||
using System; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using System.Globalization; | ||
using System.Linq.Expressions; | ||
using Newtonsoft.Json.Linq; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace ObjectPrinting | ||
{ | ||
public static class PropertyPrintingConfigExtensions | ||
{ | ||
public static PrintingConfig<TOwner> TrimmedToLength<TOwner>(this PropertyPrintingConfig<TOwner, string> propConfig, int maxLen) | ||
{ | ||
propConfig.Using(property => property.Substring(0,Math.Min(maxLen, property.Length)).ToString()); | ||
return propConfig.ParentConfig; | ||
} | ||
|
||
public static PrintingConfig<TOwner> Using<TOwner>(this PropertyPrintingConfig<TOwner, DateTime> propConfig, CultureInfo culture) | ||
{ | ||
return propConfig.Using(x => x.ToString(culture)); | ||
} | ||
|
||
public static PrintingConfig<TOwner> Using<TOwner>(this PropertyPrintingConfig<TOwner, double> propConfig, CultureInfo culture) | ||
{ | ||
return propConfig.Using(x => x.ToString(culture)); | ||
} | ||
|
||
public static PrintingConfig<TOwner> Using<TOwner>(this PropertyPrintingConfig<TOwner, float> propConfig, CultureInfo culture) | ||
{ | ||
return propConfig.Using(x => x.ToString(culture)); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Reflection; | ||
using System; | ||
|
||
namespace ObjectPrinting | ||
{ | ||
public class SerializerConfig | ||
{ | ||
public HashSet<object> serializedObjects; | ||
public HashSet<Type> excludedTypes; | ||
public HashSet<PropertyInfo> excludedProperties; | ||
public Dictionary<Type, Delegate> typeSerialization; | ||
public Dictionary<PropertyInfo, Delegate> propertiesSerialization; | ||
|
||
public SerializerConfig() | ||
{ | ||
serializedObjects = new HashSet<object>(); | ||
excludedTypes = new HashSet<Type>(); | ||
excludedProperties = new HashSet<PropertyInfo>(); | ||
typeSerialization = new Dictionary<Type, Delegate>(); | ||
propertiesSerialization = new Dictionary<PropertyInfo, Delegate>(); | ||
|
||
} | ||
|
||
public SerializerConfig(SerializerConfig configuration) | ||
{ | ||
serializedObjects = new HashSet<object>(configuration.serializedObjects); | ||
excludedTypes = new HashSet<Type>(configuration.excludedTypes); | ||
excludedProperties = new HashSet<PropertyInfo>(configuration.excludedProperties); | ||
typeSerialization = new Dictionary<Type, Delegate>(configuration.typeSerialization); | ||
propertiesSerialization = new Dictionary<PropertyInfo, Delegate>(configuration.propertiesSerialization); | ||
} | ||
|
||
} | ||
} |
This file was deleted.
This file was deleted.
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.
Не хватает иммутабельности.