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

Add constructor resolution to datacontractresolver #48

Merged
merged 3 commits into from
Nov 26, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
59 changes: 48 additions & 11 deletions src/Chr.Avro.Binary/BinaryDeserializerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,26 +372,63 @@ public override Delegate BuildDelegate(TypeResolution resolution, Schema schema,
ExceptionDispatchInfo.Capture(indirect.InnerException).Throw();
}

var convert = typeof(Enumerable).GetMethods()
.Where(m => m.Name == (target.IsArray
? nameof(Enumerable.ToArray)
: nameof(Enumerable.ToList)
))
.Single()
.MakeGenericMethod(item);

if (!target.IsAssignableFrom(convert.ReturnType))
var constructor = FindEnumerableConstructor(arrayResolution, item);

if (constructor != null)
{
throw new UnsupportedTypeException(target, $"An array deserializer cannot be built for type {target.FullName}.");
var value = Expression.Parameter(target);
result = Expression.Block(
new[] { value },
Expression.Assign(value, Expression.New(constructor.Constructor, new[] { result })),
value
);
}
else
{
var convert = typeof(Enumerable).GetMethods()
.Where(m => m.Name == (target.IsArray
? nameof(Enumerable.ToArray)
: nameof(Enumerable.ToList)
))
.Single()
.MakeGenericMethod(item);

result = Expression.ConvertChecked(Expression.Call(null, convert, result), target);
if (!target.IsAssignableFrom(convert.ReturnType))
{
throw new UnsupportedTypeException(target, $"An array deserializer cannot be built for type {target.FullName}.");
}

result = Expression.ConvertChecked(Expression.Call(null, convert, result), target);
}

var lambda = Expression.Lambda(result, "array deserializer", new[] { stream });
var compiled = lambda.Compile();

return cache.GetOrAdd((target, schema), compiled);
}

private ConstructorResolution FindEnumerableConstructor(ArrayResolution arrayResolution, Type type)
{
ConstructorResolution match = null;
foreach (var constructor in arrayResolution.Constructors)
{
if (constructor.Parameters.Count == 1)
{
var parameterType = constructor.Parameters.First().Type;
if (parameterType.IsGenericType && parameterType.GetGenericTypeDefinition() == typeof(IEnumerable<>))
{
var arguments = parameterType.GetGenericArguments();
if (arguments.Count() == 1 && arguments[0] == type)
{
match = constructor;
dstelljes marked this conversation as resolved.
Show resolved Hide resolved
break;
}
}
}
}

return match;
}
}

/// <summary>
Expand Down
10 changes: 8 additions & 2 deletions src/Chr.Avro/Resolution/DataContractResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public class DataContractResolver : TypeResolver
resolver => new DictionaryResolverCase(),

// enumerables:
resolver => new EnumerableResolverCase(),
resolver => new EnumerableResolverCase(memberVisibility),

// built-ins:
resolver => new DateTimeResolverCase(),
Expand Down Expand Up @@ -278,7 +278,13 @@ public override TypeResolution Resolve(Type type)
.Select(m => new FieldResolution(m.MemberInfo, m.Type, m.Name))
.ToList();

return new RecordResolution(type, name, @namespace, fields);
var constructors = GetConstructors(type, MemberVisibility)
.Select(c => new ConstructorResolution(
c.ConstructorInfo,
c.Parameters.Select(p => new ParameterResolution(p, p.ParameterType, new IdentifierResolution(p.Name))).ToList()
)).ToList();

return new RecordResolution(type, name, @namespace, fields, constructors);
}
}
}
38 changes: 36 additions & 2 deletions src/Chr.Avro/Resolution/ReflectionResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public class ReflectionResolver : TypeResolver
resolver => new DictionaryResolverCase(),

// enumerables:
resolver => new EnumerableResolverCase(),
resolver => new EnumerableResolverCase(memberVisibility),

// built-ins:
resolver => new DateTimeResolverCase(),
Expand Down Expand Up @@ -347,6 +347,13 @@ public override TypeResolution Resolve(Type type)
/// </summary>
public class EnumResolverCase : ReflectionResolverCase
{
/// <summary>
Copy link
Member

Choose a reason for hiding this comment

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

not needed on EnumResolverCase?

/// The binding flags that will be used to select fields and properties.
/// </summary>
public BindingFlags MemberVisibility { get; }



/// <summary>
/// Resolves enum type information.
/// </summary>
Expand Down Expand Up @@ -445,6 +452,22 @@ public override TypeResolution Resolve(Type type)
/// </summary>
public class EnumerableResolverCase : ReflectionResolverCase
{
/// <summary>
/// The binding flags that will be used to select fields and properties.
Copy link
Member

@dstelljes dstelljes Nov 26, 2019

Choose a reason for hiding this comment

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

constructors in this case, not fields and properties

/// </summary>
public BindingFlags MemberVisibility { get; }

/// <summary>
/// Creates a new object resolver case.
/// </summary>
/// <param name="memberVisibility">
/// The binding flags that will be used to select fields and properties.
Copy link
Member

@dstelljes dstelljes Nov 26, 2019

Choose a reason for hiding this comment

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

constructors in this case, not fields and properties

/// </param>
public EnumerableResolverCase(BindingFlags memberVisibility)
{
MemberVisibility = memberVisibility;
}

/// <summary>
/// Resolves enumerable type information.
/// </summary>
Expand All @@ -466,7 +489,18 @@ public override TypeResolution Resolve(Type type)
throw new UnsupportedTypeException(type);
}

return new ArrayResolution(type, itemType);
ICollection<ConstructorResolution> constructors = null;
Sikta marked this conversation as resolved.
Show resolved Hide resolved

if (!type.IsArray)
{
constructors = GetConstructors(type, MemberVisibility)
.Select(c => new ConstructorResolution(
c.ConstructorInfo,
c.Parameters.Select(p => new ParameterResolution(p, p.ParameterType, new IdentifierResolution(p.Name))).ToList()
)).ToList();
}

return new ArrayResolution(type, itemType, constructors);
}
}

Expand Down
20 changes: 19 additions & 1 deletion src/Chr.Avro/Resolution/TypeResolution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,25 @@ public TypeResolution(Type type, bool isNullable = false)
/// </summary>
public class ArrayResolution : TypeResolution
{
private ICollection<ConstructorResolution> constructors;

private Type itemType;

/// <summary>
/// The record constructors.
/// </summary>
public virtual ICollection<ConstructorResolution> Constructors
{
get
{
return constructors ?? throw new InvalidOperationException();
}
set
{
constructors = value ?? throw new ArgumentNullException(nameof(value), "Record constructor collection cannot be null.");
}
}

/// <summary>
/// The array item type.
/// </summary>
Expand Down Expand Up @@ -97,8 +114,9 @@ public virtual Type ItemType
/// <exception cref="ArgumentNullException">
/// Thrown when the array type or the item type is null.
/// </exception>
public ArrayResolution(Type type, Type itemType, bool isNullable = false) : base(type, isNullable)
public ArrayResolution(Type type, Type itemType, ICollection<ConstructorResolution> constructors, bool isNullable = false) : base(type, isNullable)
{
Constructors = constructors ?? new ConstructorResolution[0];
ItemType = itemType;
}
}
Expand Down
11 changes: 11 additions & 0 deletions tests/Chr.Avro.Binary.Tests/ArraySerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ public void ListValues(int[] value)
Assert.Equal(value, deserializer.Deserialize(serializer.Serialize(value.ToList())));
}

[Theory]
[MemberData(nameof(ISetData))]
public void HashSetValues(HashSet<string> value)
{
var schema = new ArraySchema(new StringSchema());
var deserializer = DeserializerBuilder.BuildDeserializer<HashSet<string>>(schema);
var serializer = SerializerBuilder.BuildSerializer<HashSet<string>>(schema);

Assert.Equal(value, deserializer.Deserialize(serializer.Serialize(value)));
}

public static IEnumerable<object[]> ArrayData => new List<object[]>
{
new object[] { new int[] { } },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@ public class RecordConstructorDeserializationTests
{
protected readonly IBinaryDeserializerBuilder DeserializerBuilder;

protected readonly ITypeResolver TypeResolver;

protected readonly ISchemaBuilder SchemaBuilder;

protected readonly IBinarySerializerBuilder SerializerBuilder;

public RecordConstructorDeserializationTests()
{
TypeResolver = new ReflectionResolver(resolveReferenceTypesAsNullable: true);
DeserializerBuilder = new BinaryDeserializerBuilder(resolver: TypeResolver);
SchemaBuilder = new SchemaBuilder(typeResolver: TypeResolver);
DeserializerBuilder = new BinaryDeserializerBuilder();
SchemaBuilder = new SchemaBuilder();
SerializerBuilder = new BinarySerializerBuilder();
}

Expand Down