Skip to content

Commit

Permalink
Add support to MessageData on nested ICollection
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio Torrella authored and phatboyg committed Apr 6, 2023
1 parent 555b6c2 commit fff6f20
Showing 1 changed file with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public class ListPropertyConverter<TElement> :
IPropertyConverter<List<TElement>, IEnumerable<TElement>>,
IPropertyConverter<IList<TElement>, IEnumerable<TElement>>,
IPropertyConverter<IReadOnlyList<TElement>, IEnumerable<TElement>>,
IPropertyConverter<IEnumerable<TElement>, IEnumerable<TElement>>
IPropertyConverter<IEnumerable<TElement>, IEnumerable<TElement>>,
IPropertyConverter<ICollection<TElement>, IEnumerable<TElement>>
{
Task<IEnumerable<TElement>> IPropertyConverter<IEnumerable<TElement>, IEnumerable<TElement>>.Convert<T>(InitializeContext<T> context,
IEnumerable<TElement> input)
Expand Down Expand Up @@ -64,14 +65,28 @@ public Task<List<TElement>> Convert<TMessage>(InitializeContext<TMessage> contex
return Task.FromResult(input.ToList());
}
}

Task<ICollection<TElement>> IPropertyConverter<ICollection<TElement>, IEnumerable<TElement>>.Convert<T>(InitializeContext<T> context, IEnumerable<TElement> input)
{
switch (input)
{
case null:
return TaskUtil.Default<ICollection<TElement>>();
case ICollection<TElement> list:
return Task.FromResult(list);
default:
return Task.FromResult<ICollection<TElement>>(input.ToList());
}
}
}


public class ListPropertyConverter<TElement, TInputElement> :
IPropertyConverter<List<TElement>, IEnumerable<TInputElement>>,
IPropertyConverter<IList<TElement>, IEnumerable<TInputElement>>,
IPropertyConverter<IReadOnlyList<TElement>, IEnumerable<TInputElement>>,
IPropertyConverter<IEnumerable<TElement>, IEnumerable<TInputElement>>
IPropertyConverter<IEnumerable<TElement>, IEnumerable<TInputElement>>,
IPropertyConverter<ICollection<TElement>, IEnumerable<TInputElement>>
{
readonly IPropertyConverter<TElement, TInputElement> _converter;

Expand Down Expand Up @@ -125,6 +140,21 @@ async Task<IReadOnlyList<TElement>> ConvertAsync()
return ConvertAsync();
}

public Task<ICollection<TElement>> Convert<T>(InitializeContext<T> context, IEnumerable<TInputElement> elements)
where T : class
{
Task<List<TElement>> resultTask = ConvertSync(context, elements);
if (resultTask.Status == TaskStatus.RanToCompletion)
return Task.FromResult<ICollection<TElement>>(resultTask.Result);

async Task<ICollection<TElement>> ConvertAsync()
{
return await resultTask.ConfigureAwait(false);
}

return ConvertAsync();
}

Task<List<TElement>> IPropertyConverter<List<TElement>, IEnumerable<TInputElement>>.Convert<T>(InitializeContext<T> context,
IEnumerable<TInputElement> elements)
{
Expand Down

0 comments on commit fff6f20

Please sign in to comment.