-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
147 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
94 changes: 94 additions & 0 deletions
94
src/ExtendedXmlSerializer/ExtensionModel/Types/ImmutableListExtension.cs
This file contains 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,94 @@ | ||
using ExtendedXmlSerializer.ContentModel; | ||
using ExtendedXmlSerializer.ContentModel.Collections; | ||
using ExtendedXmlSerializer.ContentModel.Content; | ||
using ExtendedXmlSerializer.ContentModel.Format; | ||
using ExtendedXmlSerializer.ContentModel.Identification; | ||
using ExtendedXmlSerializer.ContentModel.Reflection; | ||
using ExtendedXmlSerializer.Core; | ||
using ExtendedXmlSerializer.Core.Specifications; | ||
using ExtendedXmlSerializer.ReflectionModel; | ||
using JetBrains.Annotations; | ||
using System.Collections.Immutable; | ||
using System.Collections.ObjectModel; | ||
using System.Reflection; | ||
|
||
namespace ExtendedXmlSerializer.ExtensionModel.Types | ||
{ | ||
sealed class ImmutableListExtension : ISerializerExtension | ||
{ | ||
public static ImmutableListExtension Default { get; } = new ImmutableListExtension(); | ||
|
||
ImmutableListExtension() : this(new IsAssignableGenericSpecification(typeof(ImmutableList<>))) {} | ||
|
||
readonly ISpecification<TypeInfo> _specification; | ||
|
||
public ImmutableListExtension(ISpecification<TypeInfo> specification) => _specification = specification; | ||
|
||
public IServiceRepository Get(IServiceRepository parameter) | ||
=> parameter.DecorateContentsWith<ImmutableLists>() | ||
.When(_specification) | ||
.Decorate<IGenericTypes, GenericTypes>(); | ||
|
||
void ICommand<IServices>.Execute(IServices parameter) {} | ||
|
||
sealed class GenericTypes : IGenericTypes | ||
{ | ||
readonly static TypeInfo Check = typeof(ImmutableList).GetTypeInfo(); | ||
readonly static ImmutableArray<TypeInfo> Type = typeof(ImmutableList<>).GetTypeInfo() | ||
.Yield() | ||
.ToImmutableArray(); | ||
|
||
readonly IGenericTypes _types; | ||
|
||
public GenericTypes(IGenericTypes types) => _types = types; | ||
|
||
public ImmutableArray<TypeInfo> Get(IIdentity parameter) | ||
{ | ||
var type = _types.Get(parameter); | ||
var result = Equals(type.Only(), Check) ? Type : type; | ||
return result; | ||
} | ||
} | ||
|
||
sealed class ImmutableLists : Collections | ||
{ | ||
public ImmutableLists(RuntimeSerializers serializers, Contents contents) : base(serializers, contents) {} | ||
} | ||
|
||
sealed class Contents : ICollectionContents | ||
{ | ||
readonly IInnerContentServices _contents; | ||
readonly IEnumerators _enumerators; | ||
|
||
public Contents(IInnerContentServices contents, IEnumerators enumerators) | ||
{ | ||
_contents = contents; | ||
_enumerators = enumerators; | ||
} | ||
|
||
public ISerializer Get(CollectionContentInput parameter) | ||
=> new Serializer(Readers.Instance.Get(parameter.ItemType)(_contents, parameter.Item), | ||
new EnumerableWriter(_enumerators, parameter.Item).Adapt()); | ||
|
||
sealed class Readers : Generic<IInnerContentServices, IReader, IReader> | ||
{ | ||
public static Readers Instance { get; } = new Readers(); | ||
|
||
Readers() : base(typeof(Reader<>)) {} | ||
} | ||
|
||
sealed class Reader<T> : IReader | ||
{ | ||
readonly IReader<Collection<T>> _reader; | ||
|
||
[UsedImplicitly] | ||
public Reader(IInnerContentServices services, IReader item) | ||
: this(services.CreateContents<Collection<T>>(new CollectionInnerContentHandler(item, services))) {} | ||
|
||
Reader(IReader<Collection<T>> reader) => _reader = reader; | ||
|
||
public object Get(IFormatReader parameter) => _reader.Get(parameter).ToImmutableList(); | ||
} | ||
} | ||
} | ||
} |
This file contains 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
19 changes: 19 additions & 0 deletions
19
test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue483Tests.cs
This file contains 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,19 @@ | ||
using ExtendedXmlSerializer.Configuration; | ||
using ExtendedXmlSerializer.ExtensionModel.Xml; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace ExtendedXmlSerializer.Tests.ReportedIssues | ||
{ | ||
public sealed class Issue483Tests | ||
{ | ||
[Fact] | ||
public void Verify() | ||
{ | ||
new ConfigurationContainer().Type<Issue483Tests>().Root.With<MigrationsExtension>().Should().NotBeNull(); | ||
|
||
} | ||
|
||
|
||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue485Tests.cs
This file contains 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,25 @@ | ||
using ExtendedXmlSerializer.Configuration; | ||
using ExtendedXmlSerializer.Tests.ReportedIssues.Support; | ||
using FluentAssertions; | ||
using System.Collections.Immutable; | ||
using Xunit; | ||
|
||
namespace ExtendedXmlSerializer.Tests.ReportedIssues | ||
{ | ||
public sealed class Issue485Tests | ||
{ | ||
[Fact] | ||
public void Verify() | ||
{ | ||
var instance = new[] { 1, 2, 3, 4 }.ToImmutableList(); | ||
|
||
var serializer = new ConfigurationContainer().UseAutoFormatting() | ||
.UseOptimizedNamespaces() | ||
.EnableParameterizedContentWithPropertyAssignments() | ||
.Create() | ||
.ForTesting(); | ||
|
||
serializer.Cycle(instance).Should().BeEquivalentTo(instance); | ||
} | ||
} | ||
} |