Skip to content

Commit

Permalink
Added non-generic equivalent for AddMigration (#484)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike-E-angelo authored Dec 7, 2020
1 parent 087c5b9 commit 5e5c10c
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 2 deletions.
37 changes: 37 additions & 0 deletions src/ExtendedXmlSerializer/ExtensionMethodsForXml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,43 @@ public static ITypeConfiguration<T> AddMigration<T>(this ITypeConfiguration<T> @
.Apply(@this.Get(), migrations.Fixed())
.Return(@this);

/// <summary>
/// Adds a migration command to the configured type. A migration allows older persisted XML to migrate to an object
/// model schema that has changed since the XML was persisted. The provided command specifies how to manipulate the
/// element that represents the type so that it can (hopefully 😇) be deserialized without error.
/// </summary>
/// <param name="this">The type configuration to configure.</param>
/// <param name="migration">The command that specifies how to migrate an Xml element that represents an older schema.</param>
/// <returns>The configured type configuration.</returns>
public static ITypeConfiguration AddMigration(this ITypeConfiguration @this, ICommand<XElement> migration)
=> @this.AddMigration(migration.Execute);

/// <summary>
/// Adds a migration delegate to the configured type. A migration allows older persisted XML to migrate to an object
/// model schema that has changed since the XML was persisted. The provided command specifies how to manipulate the
/// element that represents the type so that it can (hopefully 😇) be deserialized without error.
/// </summary>
/// <param name="this">The type configuration to configure.</param>
/// <param name="migration">The delegate that specifies how to migrate an Xml element that represents an older schema.
/// </param>
/// <returns>The configured type configuration.</returns>
public static ITypeConfiguration AddMigration(this ITypeConfiguration @this, Action<XElement> migration)
=> @this.AddMigration(migration.Yield());

/// <summary>
/// Adds a set of migration delegates to the configured type. A migration allows older persisted XML to migrate to an
/// object model schema that has changed since the XML was persisted. The provided command specifies how to
/// manipulate the element that represents the type so that it can (hopefully 😇) be deserialized without error.
/// </summary>
/// <param name="this">The type configuration to configure.</param>
/// <param name="migrations">The delegates that specify how to migrate an Xml element that represents an older schema.
/// </param>
/// <returns>The configured type configuration.</returns>
public static ITypeConfiguration AddMigration(this ITypeConfiguration @this, IEnumerable<Action<XElement>> migrations)
=> @this.Root.With<MigrationsExtension>()
.Apply(@this.Get(), migrations.Fixed())
.Return(@this);

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using ExtendedXmlSerializer.Core;
using ExtendedXmlSerializer.Core.Sources;
using ExtendedXmlSerializer.ReflectionModel;
using JetBrains.Annotations;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
Expand All @@ -21,7 +20,6 @@ namespace ExtendedXmlSerializer.ExtensionModel.Xml
{
sealed class MigrationsExtension : TypedTable<IEnumerable<Action<XElement>>>, ISerializerExtension
{
[UsedImplicitly]
public MigrationsExtension() : this(new Dictionary<TypeInfo, IEnumerable<Action<XElement>>>()) {}

public MigrationsExtension(IDictionary<TypeInfo, IEnumerable<Action<XElement>>> store) : base(store) {}
Expand Down
93 changes: 93 additions & 0 deletions test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue483Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using ExtendedXmlSerializer.Configuration;
using FluentAssertions;
using System.Linq;
using Xunit;

namespace ExtendedXmlSerializer.Tests.ReportedIssues
{
public sealed class Issue483Tests
{
[Fact]
public void Verify()
{
//// Write legacy XML
//var rootInitialVersion = new Root { Identifier = Guid.NewGuid() };
//var someClass1 = new SomeClass { Id = 1, SomeString = "String1" };
//var someClass2 = new SomeClass { Id = 2, SomeString = "String2" };
//var children = new List<Node>();
//for (var i = 1; i < 10; i++)
// children.Add(new Node
// {
// Id = i,
// Blubb = i % 2 == 0 ? someClass1 : someClass2
// });
//rootInitialVersion.Children = children;

//var serializer = new ConfigurationContainer()
// .ConfigureType<SomeClass>()
// .EnableReferences(s => s.Id)
// .Create();
//var initialXml = serializer.Serialize(new XmlWriterSettings { Indent = true }, rootInitialVersion);

var initialXml = @"<?xml version='1.0' encoding='utf-8'?>
<Root xmlns='clr-namespace:ExtendedXmlSerializer.Tests.ReportedIssues;assembly=ExtendedXmlSerializer.Tests.ReportedIssues'>
<Identifier>391047bb-21a7-46cb-b49c-6b9353305d09</Identifier>
<Children>
<Capacity>16</Capacity>
<Node>
<Id>1</Id>
<Blubb Id='2'>
<SomeString>String2</SomeString>
</Blubb>
</Node>
<Node>
<Id>2</Id>
<Blubb Id='1'>
<SomeString>String1</SomeString>
</Blubb>
</Node>
<Node>
<Id>3</Id>
<Blubb xmlns:exs='https://extendedxmlserializer.github.io/v2' exs:entity='2' />
</Node>
<Node>
<Id>4</Id>
<Blubb xmlns:exs='https://extendedxmlserializer.github.io/v2' exs:entity='1' />
</Node>
<Node>
<Id>5</Id>
<Blubb xmlns:exs='https://extendedxmlserializer.github.io/v2' exs:entity='2' />
</Node>
<Node>
<Id>6</Id>
<Blubb xmlns:exs='https://extendedxmlserializer.github.io/v2' exs:entity='1' />
</Node>
<Node>
<Id>7</Id>
<Blubb xmlns:exs='https://extendedxmlserializer.github.io/v2' exs:entity='2' />
</Node>
<Node>
<Id>8</Id>
<Blubb xmlns:exs='https://extendedxmlserializer.github.io/v2' exs:entity='1' />
</Node>
<Node>
<Id>9</Id>
<Blubb xmlns:exs='https://extendedxmlserializer.github.io/v2' exs:entity='2' />
</Node>
</Children>
</Root>";

new ConfigurationContainer().Type<SomeClass>()
.EnableReferences(s => s.Id)
.As<ITypeConfiguration>()
.AddMigration(new SomeClassMigrations())
.Create()
.Deserialize<Root>(initialXml)
.Children
.Select(x => x.Blubb.RenamedProperty)
.Take(4)
.Should()
.Equal("String2", "String1", "String2", "String1");
}
}
}

0 comments on commit 5e5c10c

Please sign in to comment.