-
Notifications
You must be signed in to change notification settings - Fork 263
Русинов Матвей #245
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
base: master
Are you sure you want to change the base?
Русинов Матвей #245
Changes from all commits
c742582
297db5e
09b3d1d
c86c148
7f0c50e
0c568c7
b8d005c
042fc39
35e6930
9cd1c4e
5b436bc
aef7098
beaa56e
83d7e76
56c3baf
241850b
6b387fe
1a951d2
e478c2d
80f668b
045fe7f
1685801
1b87d86
0c8bb3b
3c88501
306a139
fab41c7
db4f53a
e60e250
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 |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| using System; | ||
| using ObjectPrinting.Configs.Interfaces; | ||
|
|
||
| namespace ObjectPrinting.Configs.Extensions; | ||
|
|
||
| public static class PropertyPrintingConfigExtensions | ||
| { | ||
| public static PrintingConfig<TOwner> Trim<TOwner>( | ||
| this PropertyPrintingConfig<TOwner, string> config, | ||
| int maxLen) | ||
| { | ||
| ArgumentOutOfRangeException.ThrowIfNegative(maxLen); | ||
|
|
||
| var parent = ((IChildPrintingConfig<TOwner, string>)config).ParentConfig; | ||
|
|
||
| var path = config.PropertyPath; | ||
|
|
||
| parent.Settings.StringTrimLengths[path] = maxLen; | ||
|
|
||
| return parent; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| using System; | ||
| using System.Numerics; | ||
| using ObjectPrinting.Configs.Interfaces; | ||
|
|
||
| namespace ObjectPrinting.Configs.Extensions; | ||
|
|
||
| public static class TypePrintingConfigExtensions | ||
| { | ||
| public static PrintingConfig<TOwner> Trim<TOwner>( | ||
| this TypePrintingConfig<TOwner, string> config, | ||
| int maxLen) | ||
| { | ||
| ArgumentOutOfRangeException.ThrowIfNegative(maxLen); | ||
|
|
||
| var parent = ((IChildPrintingConfig<TOwner, string>)config).ParentConfig; | ||
|
|
||
| parent.Settings.GlobalStringTrimLength = maxLen; | ||
|
|
||
| return parent; | ||
| } | ||
|
|
||
| public static PrintingConfig<TOwner> Use<TOwner, TPropType>( | ||
| this TypePrintingConfig<TOwner, TPropType> config, | ||
| IFormatProvider provider) where TPropType : IFormattable | ||
| { | ||
| ArgumentNullException.ThrowIfNull(provider); | ||
|
|
||
| var parent = ((IChildPrintingConfig<TOwner, TPropType>)config).ParentConfig; | ||
|
|
||
| parent.Settings.TypeCultures[typeof(TPropType)] = provider; | ||
|
|
||
| return parent; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| namespace ObjectPrinting.Configs.Interfaces; | ||
|
|
||
| public interface IChildPrintingConfig<TOwner, TPropType> | ||
| { | ||
| PrintingConfig<TOwner> ParentConfig { get; } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| using System; | ||
| using System.Linq.Expressions; | ||
| using ObjectPrinting.PrintingHandlers; | ||
|
|
||
| namespace ObjectPrinting.Configs; | ||
|
|
||
| public class PrintingConfig<TOwner>(PrintingSettings settings) | ||
| { | ||
| public readonly PrintingSettings Settings = settings; | ||
|
|
||
| public TypePrintingConfig<TOwner, TPropType> For<TPropType>() | ||
| { | ||
| return new TypePrintingConfig<TOwner, TPropType>(this); | ||
| } | ||
|
|
||
| public PropertyPrintingConfig<TOwner, TPropType> For<TPropType>(Expression<Func<TOwner, TPropType>> selector) | ||
| { | ||
| return new PropertyPrintingConfig<TOwner, TPropType>(this, selector); | ||
| } | ||
|
|
||
| public string PrintToString(TOwner obj) | ||
| { | ||
| var engine = new PrintingEngine(Settings); | ||
| return engine.Print(obj); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| using System; | ||
| using System.Linq.Expressions; | ||
| using ObjectPrinting.Configs.Interfaces; | ||
|
|
||
| namespace ObjectPrinting.Configs; | ||
|
|
||
| public class PropertyPrintingConfig<TOwner, TPropType> : IChildPrintingConfig<TOwner, TPropType> | ||
| { | ||
| private readonly PrintingConfig<TOwner> printingConfig; | ||
| public readonly string PropertyPath; | ||
|
|
||
| PrintingConfig<TOwner> IChildPrintingConfig<TOwner, TPropType>.ParentConfig => printingConfig; | ||
|
|
||
| internal PropertyPrintingConfig(PrintingConfig<TOwner> parentConfig, Expression<Func<TOwner, TPropType>> selector) | ||
| { | ||
| printingConfig = parentConfig ?? throw new ArgumentNullException(nameof(parentConfig)); | ||
|
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. Прям везде проверять на |
||
| PropertyPath = BuildMemberPath(selector); | ||
| } | ||
|
|
||
| public PrintingConfig<TOwner> Use(Func<TPropType, string> serializer) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(serializer); | ||
|
|
||
| printingConfig.Settings.PropertySerializers[PropertyPath] = obj => serializer((TPropType)obj!); | ||
| return printingConfig; | ||
| } | ||
|
|
||
| public PrintingConfig<TOwner> Exclude() | ||
| { | ||
| printingConfig.Settings.ExcludedProperties.Add(PropertyPath); | ||
| return printingConfig; | ||
| } | ||
|
|
||
| private static string BuildMemberPath(Expression<Func<TOwner, TPropType>> selector) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(selector); | ||
|
|
||
| var expression = selector.Body; | ||
| var parts = new System.Collections.Generic.List<string>(); | ||
| while (expression is MemberExpression m) | ||
| { | ||
| parts.Add(m.Member.Name); | ||
| expression = m.Expression; | ||
| } | ||
|
|
||
| parts.Reverse(); | ||
| var ownerName = typeof(TOwner).Name; | ||
| return ownerName + "." + string.Join('.', parts); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| using System; | ||
| using ObjectPrinting.Configs.Interfaces; | ||
|
|
||
| namespace ObjectPrinting.Configs; | ||
|
|
||
| public class TypePrintingConfig<TOwner, TPropType> : IChildPrintingConfig<TOwner, TPropType> | ||
| { | ||
| private readonly PrintingConfig<TOwner> printingConfig; | ||
|
|
||
| PrintingConfig<TOwner> IChildPrintingConfig<TOwner, TPropType>.ParentConfig => printingConfig; | ||
|
|
||
| internal TypePrintingConfig(PrintingConfig<TOwner> parentConfig) | ||
| { | ||
| printingConfig = parentConfig ?? throw new ArgumentNullException(nameof(parentConfig)); | ||
| } | ||
|
|
||
| public PrintingConfig<TOwner> Use(Func<TPropType, string> serializer) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(serializer); | ||
| printingConfig.Settings.TypeSerializers[typeof(TPropType)] = obj => serializer((TPropType)obj!); | ||
| return printingConfig; | ||
| } | ||
|
|
||
| public PrintingConfig<TOwner> Exclude() | ||
| { | ||
| printingConfig.Settings.ExcludedTypes.Add(typeof(TPropType)); | ||
| return printingConfig; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,11 @@ | ||
| namespace ObjectPrinting | ||
| using ObjectPrinting.Configs; | ||
|
|
||
| namespace ObjectPrinting; | ||
|
|
||
| public class ObjectPrinter | ||
| { | ||
| public class ObjectPrinter | ||
| public static PrintingConfig<T> InClass<T>() | ||
| { | ||
| public static PrintingConfig<T> For<T>() | ||
| { | ||
| return new PrintingConfig<T>(); | ||
| } | ||
| return new PrintingConfig<T>(new PrintingSettings()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| using System; | ||
| using ObjectPrinting.Configs; | ||
|
|
||
| namespace ObjectPrinting; | ||
|
|
||
| public static class ObjectPrinterExtensions | ||
| { | ||
| public static string PrintToString<T>(this T obj) | ||
| { | ||
| return ObjectPrinter.InClass<T>().PrintToString(obj); | ||
| } | ||
|
|
||
| public static string PrintToString<T>(this T obj, Func<PrintingConfig<T>, PrintingConfig<T>> config) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(config); | ||
| var printer = config(ObjectPrinter.InClass<T>()); | ||
| return printer.PrintToString(obj); | ||
| } | ||
| } |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| namespace ObjectPrinting.PrintingHandlers.ApplyingSettings; | ||
|
|
||
| public readonly struct ApplierResult | ||
| { | ||
| public bool Applied { get; } | ||
| public bool Exclude { get; } | ||
| public string? Serialized { get; } | ||
| public object? NewValue { get; } | ||
|
|
||
| private ApplierResult(bool applied, bool exclude, string? serialized, object? newValue) | ||
| { | ||
| Applied = applied; | ||
| Exclude = exclude; | ||
| Serialized = serialized; | ||
| NewValue = newValue; | ||
| } | ||
|
|
||
| public static ApplierResult NotApplied => new(false, false, null, null); | ||
|
|
||
| public static ApplierResult Excluded() => new(true, true, null, null); | ||
|
|
||
| public static ApplierResult SerializedValue(string value) => | ||
| new(true, false, value, null); | ||
|
|
||
| public static ApplierResult Modified(object? newValue) => | ||
| new(true, false, null, newValue); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| using System; | ||
| using ObjectPrinting.PrintingHandlers.ApplyingSettings.Interfaces; | ||
|
|
||
| namespace ObjectPrinting.PrintingHandlers.ApplyingSettings.Appliers; | ||
|
|
||
| internal class CultureSettingsApplier : ISettingsApplier | ||
| { | ||
| public ApplierResult Apply(ValueContext context) | ||
| { | ||
| if (context.Type == null || | ||
| !context.Settings.TypeCultures.TryGetValue(context.Type, out var culture)) | ||
| return ApplierResult.NotApplied; | ||
|
|
||
| var str = ((IFormattable)context.Value).ToString(null, culture); | ||
| return ApplierResult.SerializedValue(str); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| using ObjectPrinting.PrintingHandlers.ApplyingSettings.Interfaces; | ||
|
|
||
| namespace ObjectPrinting.PrintingHandlers.ApplyingSettings.Appliers; | ||
|
|
||
| internal class ExcludeSettingsApplier : ISettingsApplier | ||
| { | ||
| public ApplierResult Apply(ValueContext context) | ||
| { | ||
| if (context.Settings.ExcludedProperties.Contains(context.Path) || | ||
| (context.Type != null && context.Settings.ExcludedTypes.Contains(context.Type))) | ||
| return ApplierResult.Excluded(); | ||
|
|
||
| return ApplierResult.NotApplied; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| using ObjectPrinting.PrintingHandlers.ApplyingSettings.Interfaces; | ||
|
|
||
| namespace ObjectPrinting.PrintingHandlers.ApplyingSettings.Appliers; | ||
|
|
||
| internal class SerializerSettingsApplier : ISettingsApplier | ||
| { | ||
| public ApplierResult Apply(ValueContext context) | ||
| { | ||
| if (context.Settings.PropertySerializers.TryGetValue(context.Path, out var propertySerializer)) | ||
| return ApplierResult.SerializedValue(propertySerializer(context.Value)); | ||
|
|
||
| if (context.Type != null && | ||
| context.Settings.TypeSerializers.TryGetValue(context.Type, out var typeSerializer)) | ||
| return ApplierResult.SerializedValue(typeSerializer(context.Value)); | ||
|
|
||
| return ApplierResult.NotApplied; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| using System; | ||
| using ObjectPrinting.PrintingHandlers.ApplyingSettings.Interfaces; | ||
|
|
||
| namespace ObjectPrinting.PrintingHandlers.ApplyingSettings.Appliers; | ||
|
|
||
| internal class TrimStringSettingsApplier : ISettingsApplier | ||
| { | ||
| public ApplierResult Apply(ValueContext context) | ||
| { | ||
| if (context.Type != typeof(string) || context.Value is not string s) | ||
| return ApplierResult.NotApplied; | ||
|
|
||
| var settings = context.Settings; | ||
|
|
||
| if (settings.StringTrimLengths.TryGetValue(context.Path, out var propertyMax)) | ||
| { | ||
| return ApplierResult.Modified(s.Length <= propertyMax ? s : s.AsSpan(0, propertyMax).ToString()); | ||
| } | ||
|
|
||
| if (settings.GlobalStringTrimLength <= 0) return ApplierResult.NotApplied; | ||
|
|
||
| var max = settings.GlobalStringTrimLength; | ||
| return ApplierResult.Modified(s.Length <= max ? s : s.AsSpan(0, max).ToString()); | ||
| } | ||
| } |
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.
Почему нельзя обрезать все строки, а только конкретные пропы?