mirror of
https://github.com/lidarr/lidarr.git
synced 2025-08-21 14:03:29 -07:00
release endpoint now returns fully parsed rss info with decisions.
This commit is contained in:
parent
7e473ca78d
commit
ca8eba9cf1
43 changed files with 458 additions and 336 deletions
71
NzbDrone.Api/Mapping/CloneInjection.cs
Normal file
71
NzbDrone.Api/Mapping/CloneInjection.cs
Normal file
|
@ -0,0 +1,71 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Omu.ValueInjecter;
|
||||
|
||||
namespace NzbDrone.Api.Mapping
|
||||
{
|
||||
public class CloneInjection : ConventionInjection
|
||||
{
|
||||
protected override bool Match(ConventionInfo conventionInfo)
|
||||
{
|
||||
return conventionInfo.SourceProp.Name == conventionInfo.TargetProp.Name &&
|
||||
conventionInfo.SourceProp.Value != null;
|
||||
}
|
||||
|
||||
protected override object SetValue(ConventionInfo conventionInfo)
|
||||
{
|
||||
//for value types and string just return the value as is
|
||||
if (conventionInfo.SourceProp.Type.IsValueType || conventionInfo.SourceProp.Type == typeof(string))
|
||||
return conventionInfo.SourceProp.Value;
|
||||
|
||||
//handle arrays
|
||||
if (conventionInfo.SourceProp.Type.IsArray)
|
||||
{
|
||||
var array = (Array)conventionInfo.SourceProp.Value;
|
||||
var clone = (Array)array.Clone();
|
||||
|
||||
for (var index = 0; index < array.Length; index++)
|
||||
{
|
||||
var item = array.GetValue(index);
|
||||
if (!item.GetType().IsValueType && !(item is string))
|
||||
{
|
||||
clone.SetValue(Activator.CreateInstance(item.GetType()).InjectFrom<CloneInjection>(item), index);
|
||||
}
|
||||
}
|
||||
|
||||
return clone;
|
||||
}
|
||||
|
||||
|
||||
if (conventionInfo.SourceProp.Type.IsGenericType)
|
||||
{
|
||||
//handle IEnumerable<> also ICollection<> IList<> List<>
|
||||
if (conventionInfo.SourceProp.Type.GetGenericTypeDefinition().GetInterfaces().Any(d => d == typeof(IEnumerable)))
|
||||
{
|
||||
var t = conventionInfo.SourceProp.Type.GetGenericArguments()[0];
|
||||
if (t.IsValueType || t == typeof(string)) return conventionInfo.SourceProp.Value;
|
||||
|
||||
var tlist = typeof(List<>).MakeGenericType(t);
|
||||
var list = Activator.CreateInstance(tlist);
|
||||
|
||||
var addMethod = tlist.GetMethod("Add");
|
||||
foreach (var o in (IEnumerable)conventionInfo.SourceProp.Value)
|
||||
{
|
||||
var e = Activator.CreateInstance(t).InjectFrom<CloneInjection>(o);
|
||||
addMethod.Invoke(list, new[] { e }); // in 4.0 you can use dynamic and just do list.Add(e);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
//unhandled generic type, you could also return null or throw
|
||||
return conventionInfo.SourceProp.Value;
|
||||
}
|
||||
|
||||
//for simple object types create a new instace and apply the clone injection on it
|
||||
return Activator.CreateInstance(conventionInfo.SourceProp.Type)
|
||||
.InjectFrom<CloneInjection>(conventionInfo.SourceProp.Value);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue