Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/23/18 14:38:14 (6 years ago)
Author:
jkarder
Message:

#2520: worked on conversions

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Transformers/Transformers.cs

    r15529 r15857  
    848848        typeVersions.Add(Tuple.Create(Guid.Parse(mapper.GetString(parentTypeVersions[i])), parentTypeVersions[i + 1]));
    849849
    850       var originalType = type;
    851       var originalTypeInfo = typeInfo;
    852 
    853       foreach (var entry in typeVersions) {
    854         var guid = entry.Item1;
    855         var version = entry.Item2;
    856 
    857         var conversionMethods = (from methodInfo in Mapper.StaticCache.GetStorableConversions(guid)
    858                                  let attrib = StorableConversionAttribute.GetStorableConversionAttribute(methodInfo)
    859                                  where attrib.Version >= version
    860                                  orderby attrib.Version
    861                                  select methodInfo).ToList();
    862 
    863         uint targetVersion;
    864 
    865         try {
    866           type = Mapper.StaticCache.GetType(guid);
    867           typeInfo = Mapper.StaticCache.GetTypeInfo(type);
    868           targetVersion = typeInfo.StorableTypeAttribute.Version;
    869         } catch (KeyNotFoundException) {
    870           targetVersion = 0;
    871         }
    872 
    873         Dictionary<string, object> lastDict = new Dictionary<string, object>();
    874         foreach (var conversionMethod in conversionMethods) {
    875           if (StorableConversionAttribute.GetVersion(conversionMethod) != version)
    876             throw new PersistenceException(string.Format("No conversion method defined for type {0} version {1}", guid, version));
    877 
    878           lastDict = (Dictionary<string, object>)conversionMethod.Invoke(null, new object[] { dict });
    879           foreach (var kvp in lastDict) dict[kvp.Key] = kvp.Value;
    880 
    881           version++;
    882         }
    883 
    884         if (version < targetVersion)
    885           throw new PersistenceException(string.Format("Missing one or more conversion methods for type {0} version {1}", guid, version));
     850      while (true) {
     851        var applicableConversion = Mapper.StaticCache.GetStorableConversions(typeVersions).FirstOrDefault();
     852        if (applicableConversion == null) break;
     853
     854        Dictionary<string, object> newDict;
     855        List<Tuple<string, uint>> typeChain = null;
     856
     857        var conversionParameters = applicableConversion.GetParameters();
     858        if (conversionParameters.Length == 1) {
     859          newDict = (Dictionary<string, object>)applicableConversion.Invoke(null, new object[] { dict });
     860        } else if (conversionParameters.Length == 2) {
     861          var parameters = new object[] { dict, typeChain };
     862          newDict = (Dictionary<string, object>)applicableConversion.Invoke(null, parameters);
     863          typeChain = (List<Tuple<string, uint>>)parameters[1];
     864        } else throw new PersistenceException("Conversion method has an invalid signature.");
     865
     866        foreach (var kvp in newDict) dict[kvp.Key] = kvp.Value;
     867
     868        var convertedType = StorableConversionAttribute.GetGuid(applicableConversion);
     869        var convertedVersion = StorableConversionAttribute.GetVersion(applicableConversion);
     870
     871        var index = typeVersions.FindIndex(x => x.Item1 == convertedType);
     872        typeVersions.RemoveAt(index);
     873        typeVersions.Insert(index, Tuple.Create(convertedType, convertedVersion + 1));
     874
     875        if (typeChain != null && typeChain.Any()) {
     876          typeVersions.RemoveRange(index + 1, typeVersions.Count - (index + 1));
     877          typeVersions.AddRange(typeChain.Select(x => Tuple.Create(Guid.Parse(x.Item1), x.Item2)));
     878        }
    886879      }
    887880
    888881      var typeStack = new Stack<Tuple<Type, TypeInfo>>();
    889       type = originalType;
    890       typeInfo = originalTypeInfo;
    891882
    892883      while (StorableTypeAttribute.IsStorableType(type)) {
Note: See TracChangeset for help on using the changeset viewer.