| | | 1 | | using System; |
| | | 2 | | using System.Collections.Concurrent; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Reflection; |
| | | 5 | | using System.Threading; |
| | | 6 | | using Newtonsoft.Json; |
| | | 7 | | using Newtonsoft.Json.Linq; |
| | | 8 | | |
| | | 9 | | namespace FastMigrations.Runtime |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Variant of handling missing "Migrate_<see cref="MigratableAttribute.Version"/>(JObject data)" method |
| | | 13 | | /// </summary> |
| | | 14 | | /// <seealso cref="MigratorMissingMethodHandling"/> |
| | | 15 | | public enum MigratorMissingMethodHandling |
| | | 16 | | { |
| | | 17 | | /// <summary>Throws <see cref="MigrationException"/> if "Migrate_<see cref="MigratableAttribute.Version"/>(JObje |
| | | 18 | | ThrowException, |
| | | 19 | | /// <summary>Skips migration if "Migrate_<see cref="MigratableAttribute.Version"/>(JObject data)" method doesn't |
| | | 20 | | Ignore |
| | | 21 | | } |
| | | 22 | | |
| | | 23 | | internal delegate JObject MigrateMethod(JObject data); |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Thread safe JsonConverter who calls "Migrate_<see cref="MigratableAttribute.Version"/>(JObject data)" methods on |
| | | 27 | | /// |
| | | 28 | | /// All methods must have signature "private/protected static JObject Migrate_<see cref="MigratableAttribute.Version |
| | | 29 | | /// |
| | | 30 | | /// All methods will be called from current version to target version (inclusive). |
| | | 31 | | /// </summary> |
| | | 32 | | /// |
| | | 33 | | /// <remarks> |
| | | 34 | | /// By default all classes have <see cref="MigratableAttribute.Version"/> 0. |
| | | 35 | | /// You can mark all potentially migratable objects with <see cref="MigratableAttribute"/>. |
| | | 36 | | /// </remarks> |
| | | 37 | | /// |
| | | 38 | | /// <example> |
| | | 39 | | /// Methods you have to implement in your class: |
| | | 40 | | /// <code> |
| | | 41 | | /// [Migratable(1)] |
| | | 42 | | /// public class YouObjectType |
| | | 43 | | /// { |
| | | 44 | | /// private static JObject Migrate_1(JObject data) |
| | | 45 | | /// // OR |
| | | 46 | | /// protected static JObject Migrate_1(JObject data) |
| | | 47 | | /// // !public modifier is not allowed! |
| | | 48 | | /// } |
| | | 49 | | /// </code> |
| | | 50 | | /// How to add migrator to JsonConverter: |
| | | 51 | | /// <code> |
| | | 52 | | /// var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException); |
| | | 53 | | /// var person = JsonConvert.DeserializeObject<YouObjectType>(json, migrator); |
| | | 54 | | /// // OR |
| | | 55 | | /// JsonConvert.DefaultSettings = () => new JsonSerializerSettings |
| | | 56 | | /// { |
| | | 57 | | /// Converters = new List<JsonConverter> { new FastMigrationsConverter(MigratorMissingMethodHandling.Throw |
| | | 58 | | /// }; |
| | | 59 | | /// </code> |
| | | 60 | | /// </example> |
| | | 61 | | /// |
| | | 62 | | /// <exception cref="MigrationException">"Migrate_<see cref="MigratableAttribute.Version"/>(JObject data)" method do |
| | | 63 | | public class FastMigrationsConverter : JsonConverter |
| | | 64 | | { |
| | 32 | 65 | | public override bool CanRead => true; |
| | 2 | 66 | | public override bool CanWrite => true; |
| | | 67 | | |
| | | 68 | | private readonly MigratorMissingMethodHandling _methodHandling; |
| | | 69 | | |
| | | 70 | | private readonly ThreadLocal<HashSet<Type>> _migrationInProgress; |
| | | 71 | | private readonly IDictionary<Type, MigratableAttribute> _attributeByTypeCache; |
| | | 72 | | private readonly IDictionary<Type, IDictionary<int, MigrateMethod>> _migrateMethodsByType; |
| | | 73 | | |
| | | 74 | | /// <param name="methodHandling">Variant of handling missing "Migrate_<see cref="MigratableAttribute.Version"/>( |
| | | 75 | | /// <seealso cref="MigratorMissingMethodHandling"/> |
| | 23 | 76 | | public FastMigrationsConverter(MigratorMissingMethodHandling methodHandling) |
| | 23 | 77 | | { |
| | 43 | 78 | | _migrationInProgress = new ThreadLocal<HashSet<Type>>(() => new HashSet<Type>()); |
| | 23 | 79 | | _attributeByTypeCache = new ConcurrentDictionary<Type, MigratableAttribute>(); |
| | 23 | 80 | | _migrateMethodsByType = new ConcurrentDictionary<Type, IDictionary<int, MigrateMethod>>(); |
| | | 81 | | |
| | 23 | 82 | | _methodHandling = methodHandling; |
| | 23 | 83 | | } |
| | | 84 | | |
| | | 85 | | public override bool CanConvert(Type objectType) |
| | 143 | 86 | | { |
| | 143 | 87 | | MigratableAttribute attribute = GetMigratableAttribute(objectType, _attributeByTypeCache); |
| | | 88 | | |
| | 143 | 89 | | if (attribute == null) |
| | 76 | 90 | | return false; |
| | | 91 | | |
| | 67 | 92 | | if (attribute.Version == MigratorConstants.DefaultVersion) |
| | 4 | 93 | | return false; |
| | | 94 | | |
| | 63 | 95 | | return !_migrationInProgress.Value.Contains(objectType); |
| | 143 | 96 | | } |
| | | 97 | | |
| | | 98 | | public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) |
| | 2 | 99 | | { |
| | 2 | 100 | | Type valueType = value.GetType(); |
| | | 101 | | |
| | | 102 | | try |
| | 2 | 103 | | { |
| | 2 | 104 | | if (_migrationInProgress.Value.Contains(valueType)) |
| | 0 | 105 | | return; |
| | | 106 | | |
| | 2 | 107 | | _migrationInProgress.Value.Add(valueType); |
| | | 108 | | |
| | 2 | 109 | | var jObject = JObject.FromObject(value, serializer); |
| | 2 | 110 | | var migratableAttribute = GetMigratableAttribute(valueType, _attributeByTypeCache); |
| | 2 | 111 | | jObject.Add(MigratorConstants.VersionJsonFieldName, migratableAttribute.Version); |
| | 2 | 112 | | jObject.WriteTo(writer); |
| | 2 | 113 | | } |
| | | 114 | | finally |
| | 2 | 115 | | { |
| | 2 | 116 | | _migrationInProgress.Value.Remove(valueType); |
| | 2 | 117 | | } |
| | 2 | 118 | | } |
| | | 119 | | |
| | | 120 | | public override object ReadJson(JsonReader reader, Type objectType, object existingValue, |
| | | 121 | | JsonSerializer serializer) |
| | 32 | 122 | | { |
| | | 123 | | try |
| | 32 | 124 | | { |
| | 32 | 125 | | if (_migrationInProgress.Value.Contains(objectType)) |
| | 0 | 126 | | return existingValue; |
| | | 127 | | |
| | 32 | 128 | | _migrationInProgress.Value.Add(objectType); |
| | | 129 | | |
| | 32 | 130 | | var jObject = JObject.Load(reader); |
| | | 131 | | |
| | | 132 | | //don't try and repeat migration for objects serialized as refs to previous |
| | 32 | 133 | | if (jObject["$ref"] != null) |
| | 4 | 134 | | return serializer.ReferenceResolver?.ResolveReference(serializer, (string)jObject["$ref"]); |
| | | 135 | | |
| | 28 | 136 | | int fromVersion = MigratorConstants.DefaultVersion; |
| | | 137 | | |
| | 28 | 138 | | if (jObject.ContainsKey(MigratorConstants.VersionJsonFieldName)) |
| | 2 | 139 | | fromVersion = jObject[MigratorConstants.VersionJsonFieldName].ToObject<int>(); |
| | | 140 | | |
| | 28 | 141 | | var migratableAttribute = GetMigratableAttribute(objectType, _attributeByTypeCache); |
| | 28 | 142 | | uint toVersion = migratableAttribute.Version; |
| | | 143 | | |
| | 28 | 144 | | if (toVersion + fromVersion != 0 && fromVersion != toVersion) |
| | 27 | 145 | | jObject = RunMigrations(jObject, objectType, fromVersion, toVersion, |
| | | 146 | | _methodHandling); |
| | | 147 | | |
| | 27 | 148 | | if (existingValue != null && serializer.ObjectCreationHandling != ObjectCreationHandling.Replace) |
| | 1 | 149 | | { |
| | 1 | 150 | | using (JsonReader jObjReader = jObject.CreateReader()) |
| | 1 | 151 | | { |
| | 1 | 152 | | serializer.Populate(jObjReader, existingValue); |
| | 1 | 153 | | return existingValue; |
| | | 154 | | } |
| | | 155 | | } |
| | | 156 | | |
| | 26 | 157 | | return jObject.ToObject(objectType, serializer); |
| | | 158 | | } |
| | | 159 | | finally |
| | 32 | 160 | | { |
| | 32 | 161 | | _migrationInProgress.Value.Remove(objectType); |
| | 32 | 162 | | } |
| | 31 | 163 | | } |
| | | 164 | | |
| | | 165 | | private JObject RunMigrations(JObject jObject, Type objectType, int fromVersion, |
| | | 166 | | uint toVersion, MigratorMissingMethodHandling methodHandling) |
| | 27 | 167 | | { |
| | 27 | 168 | | fromVersion += MigratorConstants.MinVersionToStartMigration; |
| | | 169 | | |
| | 126 | 170 | | for (int currVersion = fromVersion; currVersion <= toVersion; ++currVersion) |
| | 37 | 171 | | { |
| | 37 | 172 | | var migrationMethod = GetMigrateMethod(objectType, currVersion, _migrateMethodsByType); |
| | | 173 | | |
| | 37 | 174 | | if (migrationMethod == null) |
| | 9 | 175 | | { |
| | 9 | 176 | | switch (methodHandling) |
| | | 177 | | { |
| | | 178 | | case MigratorMissingMethodHandling.ThrowException: |
| | 1 | 179 | | { |
| | 1 | 180 | | var methodName = string.Format(MigratorConstants.MigrateMethodFormat, currVersion); |
| | 1 | 181 | | throw new MigrationException($"Migration method {methodName} not found in {objectType.Name}" |
| | | 182 | | } |
| | | 183 | | case MigratorMissingMethodHandling.Ignore: |
| | 8 | 184 | | { |
| | 8 | 185 | | continue; |
| | | 186 | | } |
| | | 187 | | } |
| | 0 | 188 | | } |
| | | 189 | | |
| | 28 | 190 | | jObject = migrationMethod(jObject); |
| | 28 | 191 | | } |
| | 26 | 192 | | return jObject; |
| | 26 | 193 | | } |
| | | 194 | | |
| | | 195 | | private static MigratableAttribute GetMigratableAttribute(Type objectType, IDictionary<Type, MigratableAttribute |
| | 173 | 196 | | { |
| | 173 | 197 | | if (cache.TryGetValue(objectType, out MigratableAttribute attribute)) |
| | 97 | 198 | | return attribute; |
| | | 199 | | |
| | 76 | 200 | | attribute = (MigratableAttribute)objectType.GetCustomAttribute(typeof(MigratableAttribute), false); |
| | 76 | 201 | | cache[objectType] = attribute; |
| | 76 | 202 | | return attribute; |
| | 173 | 203 | | } |
| | | 204 | | |
| | | 205 | | private static MigrateMethod GetMigrateMethod(Type objectType, int version, IDictionary<Type, IDictionary<int, M |
| | 37 | 206 | | { |
| | 37 | 207 | | if (!cache.TryGetValue(objectType, out IDictionary<int, MigrateMethod> methodsByVersion)) |
| | 21 | 208 | | { |
| | 21 | 209 | | methodsByVersion = new ConcurrentDictionary<int, MigrateMethod>(); |
| | 21 | 210 | | cache[objectType] = methodsByVersion; |
| | 21 | 211 | | } |
| | | 212 | | |
| | 37 | 213 | | if (methodsByVersion.TryGetValue(version, out MigrateMethod method)) |
| | 6 | 214 | | return method; |
| | | 215 | | |
| | 31 | 216 | | var methodName = string.Format(MigratorConstants.MigrateMethodFormat, version); |
| | 31 | 217 | | var methodInfo = objectType.GetMethod(methodName, BindingFlags.Static | BindingFlags.NonPublic); |
| | | 218 | | |
| | 31 | 219 | | if (methodInfo == null) |
| | 9 | 220 | | return null; |
| | | 221 | | |
| | 22 | 222 | | MigrateMethod newMethodDelegate = (MigrateMethod)methodInfo.CreateDelegate(typeof(MigrateMethod)); |
| | 22 | 223 | | methodsByVersion[version] = newMethodDelegate; |
| | 22 | 224 | | return newMethodDelegate; |
| | 37 | 225 | | } |
| | | 226 | | } |
| | | 227 | | } |