mirror of
https://github.com/Readarr/Readarr.git
synced 2026-04-15 21:06:20 -04:00
New: Lidarr to Readarr
This commit is contained in:
54
src/Readarr.Http/Mapping/MappingValidation.cs
Normal file
54
src/Readarr.Http/Mapping/MappingValidation.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using NzbDrone.Common.Reflection;
|
||||
using Readarr.Http.REST;
|
||||
|
||||
namespace Readarr.Http.Mapping
|
||||
{
|
||||
public static class MappingValidation
|
||||
{
|
||||
public static void ValidateMapping(Type modelType, Type resourceType)
|
||||
{
|
||||
var errors = modelType.GetSimpleProperties().Where(c => !c.GetGetMethod().IsStatic).Select(p => GetError(resourceType, p)).Where(c => c != null).ToList();
|
||||
|
||||
if (errors.Any())
|
||||
{
|
||||
throw new ResourceMappingException(errors);
|
||||
}
|
||||
|
||||
PrintExtraProperties(modelType, resourceType);
|
||||
}
|
||||
|
||||
private static void PrintExtraProperties(Type modelType, Type resourceType)
|
||||
{
|
||||
var resourceBaseProperties = typeof(RestResource).GetProperties().Select(c => c.Name);
|
||||
var resourceProperties = resourceType.GetProperties().Select(c => c.Name).Except(resourceBaseProperties);
|
||||
var modelProperties = modelType.GetProperties().Select(c => c.Name);
|
||||
|
||||
var extra = resourceProperties.Except(modelProperties);
|
||||
|
||||
foreach (var extraProp in extra)
|
||||
{
|
||||
Console.WriteLine("Extra: [{0}]", extraProp);
|
||||
}
|
||||
}
|
||||
|
||||
private static string GetError(Type resourceType, PropertyInfo modelProperty)
|
||||
{
|
||||
var resourceProperty = resourceType.GetProperties().FirstOrDefault(c => c.Name == modelProperty.Name);
|
||||
|
||||
if (resourceProperty == null)
|
||||
{
|
||||
return string.Format("public {0} {1} {{ get; set; }}", modelProperty.PropertyType.Name, modelProperty.Name);
|
||||
}
|
||||
|
||||
if (resourceProperty.PropertyType != modelProperty.PropertyType && !typeof(RestResource).IsAssignableFrom(resourceProperty.PropertyType))
|
||||
{
|
||||
return string.Format("Expected {0}.{1} to have type of {2} but found {3}", resourceType.Name, resourceProperty.Name, modelProperty.PropertyType, resourceProperty.PropertyType);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
14
src/Readarr.Http/Mapping/ResourceMappingException.cs
Normal file
14
src/Readarr.Http/Mapping/ResourceMappingException.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Readarr.Http.Mapping
|
||||
{
|
||||
public class ResourceMappingException : ApplicationException
|
||||
{
|
||||
public ResourceMappingException(IEnumerable<string> error)
|
||||
: base(Environment.NewLine + string.Join(Environment.NewLine, error.OrderBy(c => c)))
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user