forked from Jumoo/uSyncMigrations
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyncPropertyMigratorBase.cs
More file actions
68 lines (52 loc) · 2.55 KB
/
SyncPropertyMigratorBase.cs
File metadata and controls
68 lines (52 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using Umbraco.Extensions;
using uSync.Migrations.Extensions;
using uSync.Migrations.Migrators.Models;
using uSync.Migrations.Models;
namespace uSync.Migrations.Migrators;
public abstract class SyncPropertyMigratorBase : ISyncPropertyMigrator
{
private readonly Type? configurationType;
protected readonly string _defaultAlias = string.Empty;
public virtual string[] Editors { get; private set; }
protected SyncPropertyMigratorBase()
{
// read the attribute
var attributes = this.GetType().GetCustomAttributes<SyncMigratorAttribute>(false);
if (attributes != null && attributes.Any())
{
Editors = attributes.Select(x => x.EditorAlias).ToArray();
if (attributes.Count(x => x.ConfigurationType != null) > 1)
{
throw new InvalidOperationException("Cannot have multiple configuration types via SyncMigratorAttribute");
}
configurationType = attributes.FirstOrDefault(x => x.ConfigurationType != null)?.ConfigurationType ?? null;
if (attributes.Count(x => x.IsDefaultAlias) > 1)
{
throw new InvalidOperationException("Cannot have multiple default editor attributes");
}
var defaultAttribute = attributes.FirstOrDefault(x => x.IsDefaultAlias);
if (defaultAttribute != null)
{
_defaultAlias = defaultAttribute.EditorAlias;
}
}
else
{
throw new InvalidOperationException($"Migrators inheriting from {nameof(SyncPropertyMigratorBase)} must contain at least one ${nameof(SyncMigratorAttribute)}");
}
}
public virtual string GetDatabaseType(SyncMigrationDataTypeProperty dataTypeProperty, SyncMigrationContext context)
=> dataTypeProperty.DatabaseType;
public virtual string GetEditorAlias(SyncMigrationDataTypeProperty dataTypeProperty, SyncMigrationContext context)
=> string.IsNullOrWhiteSpace(_defaultAlias) ? dataTypeProperty.EditorAlias : _defaultAlias;
public virtual object GetConfigValues(SyncMigrationDataTypeProperty dataTypeProperty, SyncMigrationContext context)
{
if (configurationType is not null)
{
return Activator.CreateInstance(configurationType).MapPreValues(dataTypeProperty.PreValues);
}
return dataTypeProperty.PreValues.ConvertPreValuesToJson(false);
}
public virtual string GetContentValue(SyncMigrationContentProperty contentProperty, SyncMigrationContext context)
=> contentProperty.Value;
}