Skip to content
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

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions ObjectPrinting/DataMember.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Reflection;

namespace ObjectPrinting
{
public class DataMember
{
public DataMember(FieldInfo fieldInfo)
{
Name = fieldInfo.Name;
GetValue = fieldInfo.GetValue;
Type = fieldInfo.FieldType;
MemberInfo = fieldInfo;
}

public DataMember(PropertyInfo property)
{
Name = property.Name;
GetValue = property.GetValue;
Type = property.PropertyType;
MemberInfo = property;
}

public string Name { get; }
public Type Type { get; }
public Func<object, object> GetValue { get; }
public MemberInfo MemberInfo { get; }
}
}
2 changes: 1 addition & 1 deletion ObjectPrinting/ObjectPrinter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace ObjectPrinting
{
public class ObjectPrinter
public static class ObjectPrinter
{
public static PrintingConfig<T> For<T>()
{
Expand Down
10 changes: 10 additions & 0 deletions ObjectPrinting/PrinterExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace ObjectPrinting
{
public static class PrinterExtensions
{
public static PrintingConfig<T> CreatePrinter<T>(this T instance)
{
return ObjectPrinter.For<T>();
}
}
}
107 changes: 82 additions & 25 deletions ObjectPrinting/PrintingConfig.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,98 @@
using System;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Все комменты обсуждаемы - можем обсуждать или в тредах, или в личке в телеге
За количество комментов не парься - это нормально, процесс обучения, повышения скилла, ревью именно для этого и нужно

На пофикшенные комментарии можно ставить реакции или писать пометочки, что и почему

using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using ObjectPrinting.Serialization;

namespace ObjectPrinting
{
public class PrintingConfig<TOwner>

This comment was marked as resolved.

This comment was marked as resolved.

{
private readonly HashSet<MemberInfo> excludedMembers = new HashSet<MemberInfo>();
private readonly HashSet<Type> excludedTypes = new HashSet<Type>();

private readonly Dictionary<MemberInfo, IHasSerializationFunc> membersSerializesInfos =
new Dictionary<MemberInfo, IHasSerializationFunc>();

private readonly Dictionary<Type, IHasSerializationFunc> typeSerializesInfos =
new Dictionary<Type, IHasSerializationFunc>();

private Func<object, string> handleMaxRecursion;

private readonly int maxRecursion = 1;

public string PrintToString(TOwner obj)

This comment was marked as resolved.

{
return PrintToString(obj, 0);
}

public PrintingConfig<TOwner> OnMaxRecursion(Func<object,string> func)
{
handleMaxRecursion = func;

return this;
}

public PrintingConfig<TOwner> Exclude<TPropType>(Expression<Func<TOwner, TPropType>> exclude)
{
if (exclude == null)
throw new ArgumentNullException();

var memberInfo = GetMemberInfo(exclude);
excludedMembers.Add(memberInfo);

return this;
}

private static MemberInfo GetMemberInfo<TPropType>(Expression<Func<TOwner, TPropType>> expression)
{
var memberExpression = expression.Body is UnaryExpression unaryExpression
? (MemberExpression)unaryExpression.Operand
: (MemberExpression)expression.Body;

return memberExpression.Member;
}


public PrintingConfig<TOwner> Exclude<TPropType>()
{
excludedTypes.Add(typeof(TPropType));

return this;
}

private string PrintToString(object obj, int nestingLevel)

This comment was marked as resolved.

This comment was marked as resolved.

{
//TODO apply configurations
if (obj == null)
return "null" + Environment.NewLine;

var finalTypes = new[]
{
typeof(int), typeof(double), typeof(float), typeof(string),
typeof(DateTime), typeof(TimeSpan)
};
if (finalTypes.Contains(obj.GetType()))
return obj + Environment.NewLine;

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())
{
sb.Append(identation + propertyInfo.Name + " = " +
PrintToString(propertyInfo.GetValue(obj),
nestingLevel + 1));
}
return sb.ToString();
var serializer = new Serializer(
excludedMembers,
excludedTypes,
membersSerializesInfos,
typeSerializesInfos,
maxRecursion,
handleMaxRecursion);

return serializer.Serialize(obj, nestingLevel);
}

public IUsing<TOwner, T> Printing<T>()
{
var config = new SerializationConfig<TOwner, T>(this);
typeSerializesInfos[typeof(T)] = config;

return config;
}

public IUsing<TOwner, T> Printing<T>(Expression<Func<TOwner, T>> property)
{
if (property == null)
throw new ArgumentNullException();

var memberInfo = GetMemberInfo(property);

var config = new SerializationConfig<TOwner, T>(this);
membersSerializesInfos[memberInfo] = config;

return config;
}
}
}
9 changes: 9 additions & 0 deletions ObjectPrinting/Serialization/IHasSerializationFunc.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace ObjectPrinting.Serialization
{
public interface IHasSerializationFunc
{
Func<object, string> SerializationFunc { get; }
}
}
9 changes: 9 additions & 0 deletions ObjectPrinting/Serialization/ISerializationConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace ObjectPrinting.Serialization
{
public interface ISerializationConfig<TOwner, TSerialization> :
IWrap<TOwner>,
IUsing<TOwner, TSerialization>,
IHasSerializationFunc
{
}
}
9 changes: 9 additions & 0 deletions ObjectPrinting/Serialization/IUsing.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace ObjectPrinting.Serialization
{
public interface IUsing<TOwner, TSerialization>
{
public IWrap<TOwner> Using(Func<TSerialization, string> serialize);
}
}
11 changes: 11 additions & 0 deletions ObjectPrinting/Serialization/IWrap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace ObjectPrinting.Serialization
{
public interface IWrap<TOwner>
{
PrintingConfig<TOwner> And { get; }

IWrap<TOwner> Wrap(Func<string, string> modify);
}
}
38 changes: 38 additions & 0 deletions ObjectPrinting/Serialization/SerializationConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;

namespace ObjectPrinting.Serialization
{
public class SerializationConfig<TOwner, TSerialization> : ISerializationConfig<TOwner, TSerialization>
{
private Func<TSerialization, string> serialize;

public SerializationConfig(PrintingConfig<TOwner> printingConfig)
{
And = printingConfig;
}

public PrintingConfig<TOwner> And { get; }

public IWrap<TOwner> Using(Func<TSerialization, string> serialize)
{
if (serialize == null)
throw new ArgumentNullException();

this.serialize = serialize;
return this;
}

public IWrap<TOwner> Wrap(Func<string, string> modify)
{
if (modify == null)
throw new ArgumentNullException();

var currentFunc = serialize;
serialize = value => modify(currentFunc(value));
return this;
}

public Func<object, string> SerializationFunc =>
p => serialize((TSerialization)p);
}
}
Loading