-
Notifications
You must be signed in to change notification settings - Fork 264
Копытов Михаил #253
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
Mihail3141
wants to merge
8
commits into
kontur-courses:master
Choose a base branch
from
Mihail3141: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
Копытов Михаил #253
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cda7b93
Удалил ненужные файлы и добавил нужные файлы
Mihail3141 bdaa68c
Выбрал семантику
Mihail3141 33fac7a
Разработал PrintingConfig
Mihail3141 078336d
Сделал Serializer
Mihail3141 f64f7af
Написал тесты
Mihail3141 49436e3
Исправил тесты и добавил новые
Mihail3141 dfc6ea8
Добавил Trimm для всех объектов
Mihail3141 a1d75a9
Отрефакторил Serializer
Mihail3141 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 was deleted.
Oops, something went wrong.
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,43 @@ | ||
| using System; | ||
| using System.Globalization; | ||
| using System.Reflection; | ||
|
|
||
| namespace ObjectPrinting.ObjectPrinter; | ||
|
|
||
| public class MemberPrintingConfig<TOwner, TMemberType>( | ||
| PrintingConfig<TOwner> printingConfig, | ||
| MemberInfo? memberInfo = null) | ||
| { | ||
| protected readonly PrintingConfig<TOwner> PrintingConfig = printingConfig; | ||
| private readonly MemberInfo? MemberInfo = memberInfo; | ||
|
|
||
| public PrintingConfig<TOwner> Using(Func<TMemberType, string> printingMethod) | ||
| { | ||
| if (MemberInfo is null) | ||
| PrintingConfig.CustomTypeSerializers[typeof(TMemberType)] = Wrapper; | ||
| else | ||
| PrintingConfig.CustomMemberSerializers[MemberInfo] = Wrapper; | ||
|
|
||
| return PrintingConfig; | ||
|
|
||
| string Wrapper(object value) => printingMethod((TMemberType)value); | ||
| } | ||
|
|
||
| public PrintingConfig<TOwner> Using(CultureInfo culture) | ||
| { | ||
| PrintingConfig.CulturesForTypes[typeof(TMemberType)] = culture; | ||
| return PrintingConfig; | ||
| } | ||
|
|
||
| public PrintingConfig<TOwner> TrimmedToLength(int length) | ||
| { | ||
| ArgumentOutOfRangeException.ThrowIfNegative(length); | ||
|
|
||
| if (MemberInfo is null) | ||
| PrintingConfig.TrimmedTypes[typeof(TMemberType)] = length; | ||
| else | ||
| PrintingConfig.TrimmedMembers[MemberInfo] = length; | ||
|
|
||
| return PrintingConfig; | ||
| } | ||
| } |
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 ObjectPrinting.ObjectPrinter; | ||
|
|
||
| public static 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,12 @@ | ||
| using System; | ||
|
|
||
| namespace ObjectPrinting.ObjectPrinter; | ||
|
|
||
| public static class ObjectPrintingExtensions | ||
| { | ||
| public static string PrintToString<T>(this T obj) | ||
| => ObjectPrinter.For<T>().PrintToString(obj); | ||
|
|
||
| public static string PrintToString<T>(this T obj, Func<PrintingConfig<T>, PrintingConfig<T>> config) | ||
| => config(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,53 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Globalization; | ||
| using System.Linq.Expressions; | ||
| using System.Reflection; | ||
|
|
||
| namespace ObjectPrinting.ObjectPrinter; | ||
|
|
||
| public class PrintingConfig<TOwner> | ||
| { | ||
| public readonly HashSet<Type> ExcludedTypes = []; | ||
| public readonly HashSet<MemberInfo> ExcludedMember = []; | ||
| public readonly Dictionary<MemberInfo, int> TrimmedMembers = new(); | ||
| public readonly Dictionary<Type, CultureInfo> CulturesForTypes = new(); | ||
| internal readonly Dictionary<Type, Func<object, string>> CustomTypeSerializers = new(); | ||
| internal readonly Dictionary<MemberInfo, Func<object, string>> CustomMemberSerializers = new(); | ||
| public readonly Dictionary<Type, int> TrimmedTypes = new(); | ||
|
|
||
| public int MaxNestingLevel { get; init; } = 5; | ||
|
|
||
| public PrintingConfig<TOwner> Excluding<TMember>() | ||
| { | ||
| ExcludedTypes.Add(typeof(TMember)); | ||
| return this; | ||
| } | ||
|
|
||
| public PrintingConfig<TOwner> Excluding<TMember>(Expression<Func<TOwner, TMember>> memberSelector) | ||
| { | ||
| ExcludedMember.Add(GetMember(memberSelector)); | ||
| return this; | ||
| } | ||
|
|
||
| public MemberPrintingConfig<TOwner, TMember> Printing<TMember>() | ||
| => new(this); | ||
|
|
||
| public StringPrintConfig<TOwner> Printing(Expression<Func<TOwner, string>> stringMemberSelector) | ||
| => new(this, GetMember(stringMemberSelector)); | ||
|
|
||
| public MemberPrintingConfig<TOwner, TMember> Printing<TMember>(Expression<Func<TOwner, TMember>> memberSelector) | ||
| => new(this, GetMember(memberSelector)); | ||
|
|
||
|
|
||
| public string PrintToString(TOwner? obj) | ||
| => Serializer.SerializeObject(obj, this); | ||
|
|
||
|
|
||
| private static MemberInfo GetMember<TMember>(Expression<Func<TOwner, TMember>> selector) | ||
| { | ||
| if (selector.Body is MemberExpression m) | ||
| return m.Member; | ||
| throw new ArgumentException("Member selector must be a simple member access.", nameof(selector)); | ||
| } | ||
| } | ||
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.Collections.Generic; | ||
| using System.Text; | ||
|
|
||
| namespace ObjectPrinting.ObjectPrinter; | ||
|
|
||
| internal class SerializationContext<TOwner>(PrintingConfig<TOwner> config, StringBuilder builder) | ||
| { | ||
| public readonly PrintingConfig<TOwner> Config = config; | ||
| public readonly HashSet<object> Visited = new(ReferenceEqualityComparer.Instance); | ||
| public readonly StringBuilder Builder = builder; | ||
|
|
||
| public void Indent(int level) => Builder.Append(new string('\t', level)); | ||
| } | ||
|
|
||
|
|
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.
А давай везде file scoped namespace-ы сделаем, и namespace-ы приведем к виду, чтобы они отражали структуру папок. Тут например будет ObjectPrinting.ObjectPrinter. Название папки тоже можно поменять, либо вообще из неё вытащить