Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/16/17 11:32:56 (7 years ago)
Author:
gkronber
Message:

#2520: improved conversions

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/PersistenceReintegration/HeuristicLab.Tests/HeuristicLab.Persistence-3.3/UseCasesPersistenceNew.cs

    r15020 r15034  
    21662166    }
    21672167    [StorableType("00000000-0000-0000-0000-BADCAFFEE000", 2)] // for testing (version 2)
    2168     private partial class V2 : NewBaseType {
     2168    private class V2 : NewBaseType {
    21692169      [Storable]
    21702170      public int a;
     
    21862186    }
    21872187
    2188     // conversion part
    2189     private partial class V2 : NewBaseType {
    2190       [StorableConversion(srcVersion: 1)]
     2188    [StorableType("00000000-0000-0000-0000-BADCAFFEE200", 3)] // for testing (version 3)
     2189    private class V3 : NewBaseType {
     2190      [Storable]
     2191      public int a;
     2192
     2193      [Storable]
     2194      public int[] val;
     2195
     2196      [Storable]
     2197      public V3 mySelf;
     2198
     2199      [Storable]
     2200      public Tuple<int, int> tup;
     2201
     2202      [Storable]
     2203      public Point coords;
     2204    }
     2205
     2206    private static class Conversions {
     2207      [StorableConversion("D211A828-6440-4E72-A8C7-AA4F9B4FFA75", 1)]
    21912208      private static Dictionary<string, object> ConvertV1(Dictionary<string, object> values) {
    21922209        var newValues = new Dictionary<string, object>();
     
    22082225        return newValues;
    22092226      }
    2210     }
    2211 
    2212     [StorableType("00000000-0000-0000-0000-BADCAFFEE200", 3)] // for testing (version 3)
    2213     private partial class V3 : NewBaseType {
    2214       [Storable]
    2215       public int a;
    2216 
    2217       [Storable]
    2218       public int[] val;
    2219 
    2220       [Storable]
    2221       public V3 mySelf;
    2222 
    2223       [Storable]
    2224       public Tuple<int, int> tup;
    2225 
    2226       [Storable]
    2227       public Point coords;
    2228     }
    2229 
    2230     // conversion part
    2231     private partial class V3 {
    2232       [StorableConversion(srcVersion: 1)]
    2233       private static Dictionary<string, object> ConvertV1(Dictionary<string, object> values) {
    2234         var newValues = new Dictionary<string, object>();
    2235         var items = (ItemCollection<IItem>)values["OldBaseType.items"];
    2236         newValues["NewBaseType.items"] = items.Select(iv => new DoubleValue((double)(((dynamic)iv).Value))).ToArray();
    2237         newValues["V2.a"] = ((IntValue)values["V1.a"]).Value;
    2238         newValues["V2.val"] = ((ItemList<IntValue>)values["V1.vals"]).Select(iv => iv.Value).ToArray();
    2239 
    2240         newValues["V2.mySelf"] = values["V1.mySelf"]; // myself type will be mapped correctly
    2241 
    2242         var tup = (Tuple<int, int>)values["V1.tup"];
    2243         if (tup != null) {
    2244           newValues["V2.TupItem1"] = tup.Item1;
    2245           newValues["V2.TupItem2"] = tup.Item2;
    2246         }
    2247 
    2248         newValues["V2.coords"] = new Point((int)values["V1.x"], (int)values["V1.y"]);
    2249 
    2250         return newValues;
    2251       }
    2252 
    2253       [StorableConversion(srcVersion: 2)]
     2227
     2228      [StorableConversion("D211A828-6440-4E72-A8C7-AA4F9B4FFA75", 2)]
    22542229      private static Dictionary<string, object> ConvertV2(Dictionary<string, object> values) {
    22552230        var newValues = new Dictionary<string, object>();
     
    22862261      Mapper.StaticCache.DeregisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(V1)).Guid);
    22872262      Mapper.StaticCache.DeregisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(V2)).Guid);
    2288 
    2289       Mapper.StaticCache.RegisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(V1)).Guid, typeof(V2));
    2290 
    2291       object o = serializer.Deserialize(tempFile);
    2292       var restored = (V2)o;
     2263      Mapper.StaticCache.DeregisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(V3)).Guid);
     2264
     2265      Mapper.StaticCache.RegisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(V1)).Guid, typeof(V3));
     2266      var pi = typeof(StorableTypeAttribute).GetProperty("Guid");
     2267      pi.SetValue(
     2268        Mapper.StaticCache.GetTypeInfo(typeof(V3)).StorableTypeAttribute,
     2269        StorableTypeAttribute.GetStorableTypeAttribute(typeof(V1)).Guid,
     2270        null);
     2271
     2272      object o = serializer.Deserialize(tempFile);
     2273      var restored = (V3)o;
    22932274      Assert.AreEqual(restored.a, old.a.Value);
    2294       Assert.IsTrue(restored.val.SequenceEqual(old.vals.Select(iv => iv.Value)));
    2295       Assert.IsTrue(restored.items.Select(item => item.Value).SequenceEqual(old.items.Select(iv => (double)((dynamic)iv).Value)));
    2296       // Assert.AreSame(restored.items[0], restored.items[1]);
    2297       Assert.AreSame(restored, restored.mySelf);
    2298 
    2299       //string msg = Profile(test);
    2300       //Console.WriteLine(msg);
    2301     }
    2302 
    2303     [TestMethod]
    2304     [TestCategory("Persistence4")]
    2305     [TestProperty("Time", "short")]
    2306     public void TestConversion2() {
    2307       var test = new Func<V2>(() => {
    2308         var p = new V2();
    2309         p.a = 1;
    2310         p.mySelf = p;
    2311         p.val = new int[] { 2, 3, 4 };
    2312         p.TupItem1 = 17;
    2313         p.TupItem2 = 4;
    2314         p.items = new DoubleValue[] { new DoubleValue(1.0), new DoubleValue(2.0) };
    2315         return p;
    2316       });
    2317 
    2318       ProtoBufSerializer serializer = new ProtoBufSerializer();
    2319       var old = test();
    2320       serializer.Serialize(old, tempFile);
    2321       Mapper.StaticCache.DeregisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(V1)).Guid);
    2322       Mapper.StaticCache.DeregisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(V2)).Guid);
    2323       Mapper.StaticCache.DeregisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(V3)).Guid);
    2324 
    2325       Mapper.StaticCache.RegisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(V2)).Guid, typeof(V3));
    2326 
    2327       object o = serializer.Deserialize(tempFile);
    2328       var restored = (V3)o;
    2329       Assert.AreEqual(restored.a, old.a);
    2330       Assert.IsTrue(restored.val.SequenceEqual(old.val));
     2275      Assert.IsTrue(restored.val.SequenceEqual(old.vals.Select(iv=>iv.Value)));
    23312276      Assert.IsTrue(restored.items.Select(item => item.Value).SequenceEqual(old.items.Select(iv => (double)((dynamic)iv).Value)));
    23322277      // Assert.AreSame(restored.items[0], restored.items[1]);
Note: See TracChangeset for help on using the changeset viewer.