mirror of https://github.com/Radarr/Radarr
embedded type convector is now registered for all embedded types automatically.
This commit is contained in:
parent
bdcdc7c0f6
commit
c1fba9093d
|
@ -38,7 +38,7 @@ namespace Marr.Data
|
||||||
internal Dictionary<Type, string> Tables { get; set; }
|
internal Dictionary<Type, string> Tables { get; set; }
|
||||||
internal Dictionary<Type, ColumnMapCollection> Columns { get; set; }
|
internal Dictionary<Type, ColumnMapCollection> Columns { get; set; }
|
||||||
internal Dictionary<Type, RelationshipCollection> Relationships { get; set; }
|
internal Dictionary<Type, RelationshipCollection> Relationships { get; set; }
|
||||||
internal Dictionary<Type, IConverter> TypeConverters { get; set; }
|
public Dictionary<Type, IConverter> TypeConverters { get; private set; }
|
||||||
|
|
||||||
// Explicit static constructor to tell C# compiler
|
// Explicit static constructor to tell C# compiler
|
||||||
// not to mark type as beforefieldinit
|
// not to mark type as beforefieldinit
|
||||||
|
@ -202,16 +202,9 @@ namespace Marr.Data
|
||||||
/// <param name="type">The CLR data type that will be converted.</param>
|
/// <param name="type">The CLR data type that will be converted.</param>
|
||||||
/// <param name="converter">An IConverter object that will handle the data conversion.</param>
|
/// <param name="converter">An IConverter object that will handle the data conversion.</param>
|
||||||
public void RegisterTypeConverter(Type type, IConverter converter)
|
public void RegisterTypeConverter(Type type, IConverter converter)
|
||||||
{
|
|
||||||
if (TypeConverters.ContainsKey(type))
|
|
||||||
{
|
{
|
||||||
TypeConverters[type] = converter;
|
TypeConverters[type] = converter;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
TypeConverters.Add(type, converter);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Checks for a type converter (if one exists).
|
/// Checks for a type converter (if one exists).
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
|
using Marr.Data;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
using NzbDrone.Core.Datastore;
|
using NzbDrone.Core.Datastore;
|
||||||
|
using NzbDrone.Core.Datastore.Converters;
|
||||||
using NzbDrone.Core.Tv;
|
using NzbDrone.Core.Tv;
|
||||||
|
|
||||||
namespace NzbDrone.Core.Test.Datastore
|
namespace NzbDrone.Core.Test.Datastore
|
||||||
|
@ -35,6 +37,15 @@ namespace NzbDrone.Core.Test.Datastore
|
||||||
public int WriteOnly { private get; set; }
|
public int WriteOnly { private get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void Setup()
|
||||||
|
{
|
||||||
|
MapRepository.Instance.RegisterTypeConverter(typeof(List<EmbeddedType>), new EmbeddedDocumentConverter());
|
||||||
|
MapRepository.Instance.RegisterTypeConverter(typeof(EmbeddedType), new EmbeddedDocumentConverter());
|
||||||
|
MapRepository.Instance.RegisterTypeConverter(typeof(Int32), new Int32Converter());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void test_mappable_types()
|
public void test_mappable_types()
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using Marr.Data;
|
||||||
using Marr.Data.Mapping;
|
using Marr.Data.Mapping;
|
||||||
|
|
||||||
namespace NzbDrone.Core.Datastore
|
namespace NzbDrone.Core.Datastore
|
||||||
|
@ -25,19 +26,18 @@ namespace NzbDrone.Core.Datastore
|
||||||
|
|
||||||
if (propertyInfo == null) return false;
|
if (propertyInfo == null) return false;
|
||||||
|
|
||||||
if (propertyInfo.PropertyType.GetInterfaces().Any(i => i == typeof(IEmbeddedDocument)))
|
|
||||||
|
if (!propertyInfo.IsReadable() || !propertyInfo.IsWritable())
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (IsSimpleType(propertyInfo.PropertyType) || MapRepository.Instance.TypeConverters.ContainsKey(propertyInfo.PropertyType))
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof(IEnumerable<IEmbeddedDocument>).IsAssignableFrom(propertyInfo.PropertyType))
|
return false;
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
var result = propertyInfo.IsReadable() && propertyInfo.IsWritable() && IsSimpleType(propertyInfo.PropertyType);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool IsSimpleType(Type type)
|
public static bool IsSimpleType(Type type)
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq.Expressions;
|
using System.Linq;
|
||||||
using System.Reflection;
|
|
||||||
using Marr.Data;
|
using Marr.Data;
|
||||||
using Marr.Data.Mapping;
|
using Marr.Data.Mapping;
|
||||||
using NzbDrone.Core.Configuration;
|
using NzbDrone.Core.Configuration;
|
||||||
|
@ -16,7 +15,6 @@ using NzbDrone.Core.ReferenceData;
|
||||||
using NzbDrone.Core.RootFolders;
|
using NzbDrone.Core.RootFolders;
|
||||||
using NzbDrone.Core.Tv;
|
using NzbDrone.Core.Tv;
|
||||||
using BooleanIntConverter = NzbDrone.Core.Datastore.Converters.BooleanIntConverter;
|
using BooleanIntConverter = NzbDrone.Core.Datastore.Converters.BooleanIntConverter;
|
||||||
using System.Linq;
|
|
||||||
|
|
||||||
namespace NzbDrone.Core.Datastore
|
namespace NzbDrone.Core.Datastore
|
||||||
{
|
{
|
||||||
|
@ -56,12 +54,28 @@ namespace NzbDrone.Core.Datastore
|
||||||
|
|
||||||
private static void RegisterMappers()
|
private static void RegisterMappers()
|
||||||
{
|
{
|
||||||
|
RegisterEmbeddedConverter();
|
||||||
|
|
||||||
MapRepository.Instance.RegisterTypeConverter(typeof(Int32), new Int32Converter());
|
MapRepository.Instance.RegisterTypeConverter(typeof(Int32), new Int32Converter());
|
||||||
MapRepository.Instance.RegisterTypeConverter(typeof(Boolean), new BooleanIntConverter());
|
MapRepository.Instance.RegisterTypeConverter(typeof(Boolean), new BooleanIntConverter());
|
||||||
MapRepository.Instance.RegisterTypeConverter(typeof(Enum), new EnumIntConverter());
|
MapRepository.Instance.RegisterTypeConverter(typeof(Enum), new EnumIntConverter());
|
||||||
MapRepository.Instance.RegisterTypeConverter(typeof(QualityModel), new EmbeddedDocumentConverter());
|
|
||||||
MapRepository.Instance.RegisterTypeConverter(typeof(List<MediaCover.MediaCover>), new EmbeddedDocumentConverter());
|
|
||||||
MapRepository.Instance.RegisterTypeConverter(typeof(Quality), new QualityIntConverter());
|
MapRepository.Instance.RegisterTypeConverter(typeof(Quality), new QualityIntConverter());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void RegisterEmbeddedConverter()
|
||||||
|
{
|
||||||
|
var embeddedTypes = typeof(IEmbeddedDocument).Assembly.GetTypes()
|
||||||
|
.Where(c => c.GetInterfaces().Any(i => i == typeof(IEmbeddedDocument)));
|
||||||
|
|
||||||
|
|
||||||
|
var embeddedConvertor = new EmbeddedDocumentConverter();
|
||||||
|
var genericListDefinition = typeof(List<>).GetGenericTypeDefinition();
|
||||||
|
foreach (var embeddedType in embeddedTypes)
|
||||||
|
{
|
||||||
|
var embeddedListType = genericListDefinition.MakeGenericType(embeddedType);
|
||||||
|
MapRepository.Instance.RegisterTypeConverter(embeddedType, embeddedConvertor);
|
||||||
|
MapRepository.Instance.RegisterTypeConverter(embeddedListType, embeddedConvertor);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -5,7 +5,7 @@ using NzbDrone.Core.Datastore;
|
||||||
|
|
||||||
namespace NzbDrone.Core.Qualities
|
namespace NzbDrone.Core.Qualities
|
||||||
{
|
{
|
||||||
public class Quality : IComparable<Quality>, IEmbeddedDocument
|
public class Quality : IComparable<Quality>
|
||||||
{
|
{
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
|
|
Loading…
Reference in New Issue