-
Notifications
You must be signed in to change notification settings - Fork 263
Д/з ObjectPrinter #246
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
Open
Azkraft
wants to merge
5
commits into
kontur-courses:master
Choose a base branch
from
Azkraft:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Д/з ObjectPrinter #246
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
|
|
||
| <OutputType>Library</OutputType> | ||
| </PropertyGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| using System.Globalization; | ||
| using System.Reflection; | ||
|
|
||
| namespace Homework; | ||
|
|
||
| public interface IPrintingConfig | ||
| { | ||
| HashSet<Type> ExcludeTypes { get; } | ||
| HashSet<MemberInfo> ExcludeMembers { get; } | ||
| Dictionary<Type, Func<object, string>> AlternativeTypesSerialization { get; } | ||
| Dictionary<MemberInfo, Func<object, string>> AlternativeMembersSerialization { get; } | ||
| Dictionary<Type, CultureInfo> TypesCultureInfo { get; } | ||
| Dictionary<MemberInfo, int> StringsTrim { get; } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| using System.Reflection; | ||
|
|
||
| namespace Homework; | ||
|
|
||
| public interface IPropertyPrintingConfig<TOwner, TPropType> | ||
| { | ||
| PrintingConfig<TOwner> ParentConfig { get; } | ||
| MemberInfo? MemberInfo { get; } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| using System.Linq.Expressions; | ||
|
|
||
| namespace Homework; | ||
|
|
||
| public class LastMemberVisitor : ExpressionVisitor | ||
| { | ||
| public MemberExpression? LastMemberExpression { get; private set; } | ||
|
|
||
| protected override Expression VisitMember(MemberExpression node) | ||
| { | ||
| LastMemberExpression = node; | ||
|
|
||
| return base.VisitMember(node); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| namespace Homework; | ||
|
|
||
| public static class ObjectExtensions | ||
| { | ||
| public static string PrintToString<T>(this T obj) | ||
| { | ||
| return ObjectPrinter.For<T>().PrintToString(obj); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| namespace Homework; | ||
|
|
||
| public class ObjectPrinter | ||
| { | ||
| public static PrintingConfig<T> For<T>() | ||
| { | ||
| return new PrintingConfig<T>(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| using System.Diagnostics.CodeAnalysis; | ||
|
|
||
| namespace Homework; | ||
|
|
||
| public class ObjectReferenceEqualityComparer : IEqualityComparer<object> | ||
| { | ||
| public new bool Equals(object? x, object? y) | ||
| { | ||
| return ReferenceEquals(x, y); | ||
| } | ||
|
|
||
| public int GetHashCode([DisallowNull] object obj) | ||
| { | ||
| return obj.GetHashCode(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| using System.Collections; | ||
| using System.Globalization; | ||
| using System.Linq.Expressions; | ||
| using System.Reflection; | ||
| using System.Text; | ||
|
|
||
| namespace Homework; | ||
|
|
||
| public class PrintingConfig<TOwner> : IPrintingConfig | ||
| { | ||
| public PropertyPrintingConfig<TOwner, TPropType> Printing<TPropType>() | ||
| { | ||
| return new PropertyPrintingConfig<TOwner, TPropType>(this, null); | ||
| } | ||
|
|
||
| public PropertyPrintingConfig<TOwner, TPropType> Printing<TPropType>(Expression<Func<TOwner, TPropType>> memberSelector) | ||
| { | ||
| var lambda = memberSelector as LambdaExpression; | ||
| var visitor = new LastMemberVisitor(); | ||
| visitor.Visit(lambda.Body); | ||
| var memberInfo = visitor.LastMemberExpression?.Member ?? throw new ArgumentException(); | ||
|
|
||
| return new PropertyPrintingConfig<TOwner, TPropType>(this, memberInfo); | ||
| } | ||
|
|
||
| public PrintingConfig<TOwner> Excluding<TPropType>(Expression<Func<TOwner, TPropType>> memberSelector) | ||
| { | ||
| var lambda = memberSelector as LambdaExpression; | ||
| var visitor = new LastMemberVisitor(); | ||
| visitor.Visit(lambda.Body); | ||
| var memberInfo = visitor.LastMemberExpression?.Member ?? throw new ArgumentException(); | ||
|
|
||
| ((IPrintingConfig)this).ExcludeMembers.Add(memberInfo); | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| public PrintingConfig<TOwner> Excluding<TPropType>() | ||
| { | ||
| ((IPrintingConfig)this).ExcludeTypes.Add(typeof(TPropType)); | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| public string PrintToString(TOwner obj) | ||
| { | ||
| return PrintToString(obj, null, 0, new(new ObjectReferenceEqualityComparer())); | ||
| } | ||
|
|
||
| private string PrintToString(object? obj, MemberInfo? memberInfo, int nestingLevel, Dictionary<object, int> printedObjects) | ||
| { | ||
| if (obj == null) | ||
| return "null" + Environment.NewLine; | ||
|
|
||
| var type = obj.GetType(); | ||
| if (printedObjects.TryGetValue(obj, out var level)) | ||
| return $"(cycle with object {type.Name} at level {level}){Environment.NewLine}"; | ||
|
|
||
| if (DoesTypeOverrideToString(type)) | ||
| return Serialize(obj, memberInfo) + Environment.NewLine; | ||
|
|
||
| if (type.IsClass) | ||
| printedObjects.Add(obj, nestingLevel); | ||
|
|
||
| var result = obj is ICollection collection | ||
| ? PrintCollectionToString(collection, nestingLevel, printedObjects) | ||
| : PrintComplexObjectToString(obj, nestingLevel, printedObjects); | ||
|
|
||
| if (type.IsClass) | ||
| printedObjects.Remove(obj); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| private string PrintCollectionToString(ICollection collection, int nestingLevel, Dictionary<object, int> printedObjects) | ||
| { | ||
| var identation = new string('\t', nestingLevel); | ||
| var sb = new StringBuilder(); | ||
| sb.Append((nestingLevel > 0 ? Environment.NewLine + identation : "") + '[' + Environment.NewLine); | ||
| foreach (var item in collection) | ||
| sb.Append(identation + '\t' + | ||
| PrintToString( | ||
| item, | ||
| null, | ||
| nestingLevel + 1, | ||
| printedObjects)); | ||
|
|
||
| sb.Append(identation + ']' + Environment.NewLine); | ||
|
|
||
| return sb.ToString(); | ||
| } | ||
|
|
||
| private string PrintComplexObjectToString(object obj, int nestingLevel, Dictionary<object, int> printedObjects) | ||
| { | ||
| var type = obj.GetType(); | ||
| var identation = new string('\t', nestingLevel + 1); | ||
| var sb = new StringBuilder(); | ||
| sb.AppendLine(type.Name); | ||
| foreach (var nestedMemberInfo in | ||
| GetPublicPropertiesAndFields(type).OrderBy(t => t.Name)) | ||
| { | ||
| if (((IPrintingConfig)this).ExcludeMembers.Contains(nestedMemberInfo)) | ||
| continue; | ||
|
|
||
| var newObj = GetValueFromPropertyOrField(obj, nestedMemberInfo); | ||
|
|
||
| if (newObj is not null | ||
| && ((IPrintingConfig)this).ExcludeTypes.Contains(newObj.GetType())) | ||
| continue; | ||
|
|
||
| sb.Append(identation + nestedMemberInfo.Name + " = " + | ||
| PrintToString( | ||
| newObj, | ||
| nestedMemberInfo, | ||
| nestingLevel + 1, | ||
| printedObjects)); | ||
| } | ||
|
|
||
| return sb.ToString(); | ||
| } | ||
|
|
||
| private IEnumerable<MemberInfo> GetPublicPropertiesAndFields(Type type) | ||
| => type | ||
| .GetProperties() | ||
| .Select(t => (MemberInfo)t) | ||
| .Concat(type.GetFields()); | ||
|
|
||
| private object? GetValueFromPropertyOrField(object obj, MemberInfo memberInfo) | ||
| { | ||
| if (memberInfo is FieldInfo fieldInfo) | ||
| return fieldInfo.GetValue(obj); | ||
|
|
||
| return ((PropertyInfo)memberInfo).GetValue(obj); | ||
| } | ||
|
|
||
| private string Serialize(object obj, MemberInfo? memberInfo) | ||
| { | ||
| if (memberInfo is not null | ||
| && ((IPrintingConfig)this).AlternativeMembersSerialization.TryGetValue(memberInfo, out var serializeMember)) | ||
| return serializeMember(obj); | ||
|
|
||
| if (((IPrintingConfig)this).AlternativeTypesSerialization.TryGetValue(obj.GetType(), out var serializeType)) | ||
| return serializeType(obj); | ||
|
|
||
| string? str; | ||
| if (((IPrintingConfig)this).TypesCultureInfo.TryGetValue(obj.GetType(), out var culture)) | ||
| str = (string)obj.GetType().GetMethod("ToString", [typeof(IFormatProvider)]).Invoke(obj, [culture]); | ||
| else | ||
| str = obj.ToString(); | ||
|
|
||
| if (memberInfo is not null | ||
| && ((IPrintingConfig)this).StringsTrim.TryGetValue(memberInfo, out var maxLength) | ||
| && str?.Length > maxLength) | ||
| str = str[..maxLength]; | ||
|
|
||
| return str ?? "null"; | ||
| } | ||
|
|
||
| private bool DoesTypeOverrideToString(Type type) | ||
| { | ||
| var toStringMethod = type.GetMethod("ToString", Type.EmptyTypes); | ||
| return toStringMethod?.DeclaringType != typeof(object) && | ||
| toStringMethod?.DeclaringType == type; | ||
| } | ||
|
|
||
| HashSet<Type> IPrintingConfig.ExcludeTypes { get; } = []; | ||
|
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. Можешь рассказать почему выбрал такое решение (с интерфейсом)? |
||
| HashSet<MemberInfo> IPrintingConfig.ExcludeMembers { get; } = []; | ||
| Dictionary<Type, Func<object, string>> IPrintingConfig.AlternativeTypesSerialization { get; } = []; | ||
| Dictionary<MemberInfo, Func<object, string>> IPrintingConfig.AlternativeMembersSerialization { get; } = []; | ||
| Dictionary<Type, CultureInfo> IPrintingConfig.TypesCultureInfo { get; } = []; | ||
| Dictionary<MemberInfo, int> IPrintingConfig.StringsTrim { get; } = []; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| using System.Reflection; | ||
|
|
||
| namespace Homework; | ||
|
|
||
| public class PropertyPrintingConfig<TOwner, TPropType>(PrintingConfig<TOwner> printingConfig, MemberInfo? memberInfo) | ||
| : IPropertyPrintingConfig<TOwner, TPropType> | ||
| { | ||
| public PrintingConfig<TOwner> Using(Func<TPropType, string> print) | ||
| { | ||
| if (memberInfo is not null) | ||
| { | ||
| ((IPrintingConfig)printingConfig).AlternativeMembersSerialization[memberInfo] = obj => print((TPropType)obj); | ||
| } | ||
| else | ||
| { | ||
| ((IPrintingConfig)printingConfig).AlternativeTypesSerialization[typeof(TPropType)] = obj => print((TPropType)obj); | ||
| } | ||
|
|
||
| return printingConfig; | ||
| } | ||
|
|
||
| PrintingConfig<TOwner> IPropertyPrintingConfig<TOwner, TPropType>.ParentConfig => printingConfig; | ||
| MemberInfo? IPropertyPrintingConfig<TOwner, TPropType>.MemberInfo => memberInfo; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| using System.Globalization; | ||
|
|
||
| namespace Homework; | ||
|
|
||
| public static class PropertyPrintingConfigExtensions | ||
| { | ||
| public static string PrintToString<T>(this T obj, Func<PrintingConfig<T>, PrintingConfig<T>> config) | ||
| { | ||
| return config(ObjectPrinter.For<T>()).PrintToString(obj); | ||
| } | ||
|
|
||
| public static PrintingConfig<TOwner> TrimmedToLength<TOwner>(this PropertyPrintingConfig<TOwner, string?> propConfig, int maxLen) | ||
| { | ||
| var configIface = (IPropertyPrintingConfig<TOwner, string>)propConfig; | ||
| if (configIface.MemberInfo is not null) | ||
| ((IPrintingConfig)configIface.ParentConfig).StringsTrim[configIface.MemberInfo] = maxLen; | ||
|
|
||
| return configIface.ParentConfig; | ||
| } | ||
|
|
||
| public static PrintingConfig<TOwner> Using<TOwner, TPropType>( | ||
| this PropertyPrintingConfig<TOwner, TPropType> propConfig, | ||
| CultureInfo culture) | ||
| where TPropType : IFormattable | ||
| { | ||
| var configIface = (IPropertyPrintingConfig<TOwner, TPropType>)propConfig; | ||
| ((IPrintingConfig)configIface.ParentConfig).TypesCultureInfo[typeof(TPropType)] = culture; | ||
|
|
||
| return configIface.ParentConfig; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
|
|
||
| <IsPackable>false</IsPackable> | ||
| <IsTestProject>true</IsTestProject> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="coverlet.collector" Version="6.0.0" /> | ||
| <PackageReference Include="FluentAssertions" Version="8.8.0" /> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> | ||
| <PackageReference Include="NUnit" Version="3.14.0" /> | ||
| <PackageReference Include="NUnit.Analyzers" Version="3.9.0" /> | ||
| <PackageReference Include="NUnit3TestAdapter" Version="4.5.0" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\Homework\Homework.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Using Include="NUnit.Framework" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Интересно, зачем поход в Visitor?