|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics.CodeAnalysis; |
| 4 | +using System.Linq; |
| 5 | +using Umbraco.Cms.Core; |
| 6 | +using Umbraco.Cms.Core.PropertyEditors; |
| 7 | +using Umbraco.Cms.Core.Serialization; |
| 8 | +using Umbraco.Deploy.Infrastructure.Migrators; |
| 9 | +using Umbraco.Extensions; |
| 10 | + |
| 11 | +namespace Umbraco.Deploy.Contrib.Migrators.Legacy; |
| 12 | + |
| 13 | +/// <inheritdoc /> |
| 14 | +public abstract class LegacyReplaceDataTypeArtifactMigratorBase : ReplaceDataTypeArtifactMigratorBase |
| 15 | +{ |
| 16 | + private readonly IConfigurationEditorJsonSerializer _configurationEditorJsonSerializer; |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// Initializes a new instance of the <see cref="LegacyReplaceDataTypeArtifactMigratorBase" /> class. |
| 20 | + /// </summary> |
| 21 | + /// <param name="fromEditorAlias">The editor alias to migrate from.</param> |
| 22 | + /// <param name="toEditorAlias">The editor alias to migrate to.</param> |
| 23 | + /// <param name="toEditorUiAlias">The editor UI alias to migrate to.</param> |
| 24 | + /// <param name="propertyEditors">The property editors.</param> |
| 25 | + /// <param name="configurationEditorJsonSerializer">The configuration editor JSON serializer.</param> |
| 26 | + protected LegacyReplaceDataTypeArtifactMigratorBase(string fromEditorAlias, string toEditorAlias, string toEditorUiAlias, PropertyEditorCollection propertyEditors, IConfigurationEditorJsonSerializer configurationEditorJsonSerializer) |
| 27 | + : base(fromEditorAlias, toEditorAlias, toEditorUiAlias, propertyEditors, configurationEditorJsonSerializer) |
| 28 | + => _configurationEditorJsonSerializer = configurationEditorJsonSerializer; |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Replaces the old key with a new key. |
| 32 | + /// </summary> |
| 33 | + /// <param name="configuration">The configuration.</param> |
| 34 | + /// <param name="oldKey">The old key.</param> |
| 35 | + /// <param name="newKey">The new key.</param> |
| 36 | + protected static void ReplaceKey(ref IDictionary<string, object> configuration, string oldKey, string newKey) |
| 37 | + { |
| 38 | + if (configuration.Remove(oldKey, out var value)) |
| 39 | + { |
| 40 | + configuration[newKey] = value; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Replaces the UDI value with the GUID. |
| 46 | + /// </summary> |
| 47 | + /// <param name="configuration">The configuration.</param> |
| 48 | + /// <param name="key">The key.</param> |
| 49 | + /// <param name="keepInvalid">If set to <c>true</c> keeps the invalid value.</param> |
| 50 | + protected static void ReplaceUdiWithGuid(ref IDictionary<string, object> configuration, string key, bool keepInvalid = false) |
| 51 | + { |
| 52 | + if (configuration.TryGetValue(key, out var value)) |
| 53 | + { |
| 54 | + if (value is string udi && UdiParser.TryParse(udi, out GuidUdi? guidUdi)) |
| 55 | + { |
| 56 | + configuration[key] = guidUdi.Guid; |
| 57 | + } |
| 58 | + else if (keepInvalid is false && (value is not string guid || Guid.TryParse(guid, out _) is false)) |
| 59 | + { |
| 60 | + configuration.Remove(key); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Replaces the integer value with a boolean (defaults to false). |
| 67 | + /// </summary> |
| 68 | + /// <param name="configuration">The configuration.</param> |
| 69 | + /// <param name="key">The key.</param> |
| 70 | + protected static void ReplaceIntegerWithBoolean(ref IDictionary<string, object> configuration, string key) |
| 71 | + { |
| 72 | + if (configuration.TryGetValue(key, out var value) && |
| 73 | + value is not bool) |
| 74 | + { |
| 75 | + configuration[key] = value?.ToString()?.ToLowerInvariant() switch |
| 76 | + { |
| 77 | + "1" or "true" => true, |
| 78 | + _ => false, |
| 79 | + }; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Replaces the string value with an integer. |
| 85 | + /// </summary> |
| 86 | + /// <param name="configuration">The configuration.</param> |
| 87 | + /// <param name="key">The key.</param> |
| 88 | + /// <param name="keepInvalid">If set to <c>true</c> keeps the invalid value.</param> |
| 89 | + protected static void ReplaceStringWithInteger(ref IDictionary<string, object> configuration, string key, bool keepInvalid = false) |
| 90 | + { |
| 91 | + if (configuration.TryGetValue(key, out var value) && |
| 92 | + value is not int) |
| 93 | + { |
| 94 | + if (value is string stringValue && |
| 95 | + int.TryParse(stringValue, out int intValue)) |
| 96 | + { |
| 97 | + configuration[key] = intValue; |
| 98 | + } |
| 99 | + else if (keepInvalid is false) |
| 100 | + { |
| 101 | + configuration.Remove(key); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /// <summary> |
| 107 | + /// Replaces the value list array with a string array. |
| 108 | + /// </summary> |
| 109 | + /// <param name="configuration">The configuration.</param> |
| 110 | + /// <param name="key">The key.</param> |
| 111 | + protected void ReplaceValueListArrayWithStringArray(ref IDictionary<string, object> configuration, string key) |
| 112 | + { |
| 113 | + if (TryDeserialize(ref configuration, key, out IEnumerable<LegacyValueListItem>? items)) |
| 114 | + { |
| 115 | + configuration[key] = items.Select(x => x.Value).ToArray(); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + /// <summary> |
| 120 | + /// Replaces the UDI in the tree source ID value with the GUID. |
| 121 | + /// </summary> |
| 122 | + /// <param name="configuration">The configuration.</param> |
| 123 | + /// <param name="key">The key.</param> |
| 124 | + /// <param name="treeSourceType">The entity type of the tree source.</param> |
| 125 | + protected void ReplaceTreeSourceIdUdiWithGuid(ref IDictionary<string, object> configuration, string key, out string? treeSourceType) |
| 126 | + { |
| 127 | + if (TryDeserialize(ref configuration, key, out LegacyTreeSource? treeSource)) |
| 128 | + { |
| 129 | + configuration[key] = new TreeSource() |
| 130 | + { |
| 131 | + Type = treeSource.Type, |
| 132 | + Id = treeSource.Id?.Guid, |
| 133 | + DynamicRoot = treeSource.DynamicRoot, |
| 134 | + }; |
| 135 | + } |
| 136 | + |
| 137 | + treeSourceType = treeSource?.Type; |
| 138 | + } |
| 139 | + |
| 140 | + /// <summary> |
| 141 | + /// Replaces the aliases with keys. |
| 142 | + /// </summary> |
| 143 | + /// <param name="configuration">The configuration.</param> |
| 144 | + /// <param name="key">The key.</param> |
| 145 | + /// <param name="getKeyByAlias">The delegate to get the key by alias.</param> |
| 146 | + protected static void ReplaceAliasesWithKeys(ref IDictionary<string, object> configuration, string key, Func<string, Guid?> getKeyByAlias) |
| 147 | + { |
| 148 | + if (configuration.TryGetValue(key, out var value) && |
| 149 | + value is string stringValue) |
| 150 | + { |
| 151 | + configuration[key] = string.Join(',', stringValue.Split(Constants.CharArrays.Comma).Select(getKeyByAlias).OfType<Guid>()); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + /// <summary> |
| 156 | + /// Attempts to JSON deserialize the value. |
| 157 | + /// </summary> |
| 158 | + /// <typeparam name="T">The type to deserialize to.</typeparam> |
| 159 | + /// <param name="configuration">The configuration.</param> |
| 160 | + /// <param name="key">The key.</param> |
| 161 | + /// <param name="value">The value.</param> |
| 162 | + /// <returns> |
| 163 | + /// <c>true</c> if the value was deserialized; otherwise, <c>false</c>. |
| 164 | + /// </returns> |
| 165 | + protected bool TryDeserialize<T>(ref IDictionary<string, object> configuration, string key, [NotNullWhen(true)] out T? value) |
| 166 | + { |
| 167 | + if (configuration.TryGetValue(key, out var configurationValue) && |
| 168 | + configurationValue?.ToString() is string stringValue && |
| 169 | + stringValue.DetectIsJson()) |
| 170 | + { |
| 171 | + value = _configurationEditorJsonSerializer.Deserialize<T>(stringValue); |
| 172 | + } |
| 173 | + else |
| 174 | + { |
| 175 | + value = default; |
| 176 | + } |
| 177 | + |
| 178 | + return value is not null; |
| 179 | + } |
| 180 | + |
| 181 | + private sealed class LegacyValueListItem |
| 182 | + { |
| 183 | + public required string Value { get; set; } |
| 184 | + } |
| 185 | + |
| 186 | + private sealed class LegacyTreeSource |
| 187 | + { |
| 188 | + public required string Type { get; set; } |
| 189 | + public GuidUdi? Id { get; set; } |
| 190 | + public object? DynamicRoot { get; set; } |
| 191 | + } |
| 192 | + |
| 193 | + private sealed class TreeSource |
| 194 | + { |
| 195 | + public required string Type { get; set; } |
| 196 | + public Guid? Id { get; set; } |
| 197 | + public object? DynamicRoot { get; set; } |
| 198 | + } |
| 199 | +} |
0 commit comments