From 0d7326c798e4e813615f21fcfc83f5096333bdb5 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Fri, 31 May 2013 21:59:54 -0700 Subject: [PATCH] fixed more injection issues --- .../MappingTests/ResourceMappingFixture.cs | 69 +++++++++++++++++-- NzbDrone.Api/Mapping/CloneInjection.cs | 34 ++++----- 2 files changed, 82 insertions(+), 21 deletions(-) diff --git a/NzbDrone.Api.Test/MappingTests/ResourceMappingFixture.cs b/NzbDrone.Api.Test/MappingTests/ResourceMappingFixture.cs index 3df5ec33c..0dd0ef7c4 100644 --- a/NzbDrone.Api.Test/MappingTests/ResourceMappingFixture.cs +++ b/NzbDrone.Api.Test/MappingTests/ResourceMappingFixture.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using FizzWare.NBuilder; using FluentAssertions; using Marr.Data; using NUnit.Framework; @@ -19,6 +21,7 @@ using NzbDrone.Core.RootFolders; using NzbDrone.Core.Update; using NzbDrone.Test.Common; +using System.Linq; namespace NzbDrone.Api.Test.MappingTests { @@ -72,6 +75,44 @@ public void should_map_lay_loaded_values_should_be_inject_if_loaded() modelWithLazy.Guid.IsLoaded.Should().BeTrue(); } + + [Test] + public void should_be_able_to_map_lists() + { + var modelList = Builder.CreateListOfSize(10).Build(); + + var resourceList = modelList.InjectTo>(); + + resourceList.Should().HaveSameCount(modelList); + } + + [Test] + public void should_map_wrapped_models() + { + var modelList = Builder.CreateListOfSize(10).Build().ToList(); + + var wrapper = new TestModelWrapper + { + TestlList = modelList + }; + + wrapper.InjectTo(); + } + + + [Test] + public void should_map_qualityprofile() + { + + var profileResource = new QualityProfileResource + { + Allowed = Builder.CreateListOfSize(1).Build().ToList(), + }; + + + profileResource.InjectTo(); + + } } public class ModelWithLazy @@ -86,14 +127,32 @@ public class ModelWithNoLazy public class TestLazyLoaded : LazyLoaded { - public TestLazyLoaded() - { - - } - public override void Prepare(Func dataMapperFactory, object parent) { throw new InvalidOperationException(); } } + + + public class TestModelWrapper + { + public List TestlList { get; set; } + } + + public class TestResourceWrapper + { + public List TestList { get; set; } + } + + public class TestModel + { + public string Field1 { get; set; } + public string Field2 { get; set; } + } + + public class TestResource + { + public string Field1 { get; set; } + public string Field2 { get; set; } + } } \ No newline at end of file diff --git a/NzbDrone.Api/Mapping/CloneInjection.cs b/NzbDrone.Api/Mapping/CloneInjection.cs index 0e07e1c04..71bbd4bbe 100644 --- a/NzbDrone.Api/Mapping/CloneInjection.cs +++ b/NzbDrone.Api/Mapping/CloneInjection.cs @@ -17,11 +17,9 @@ protected override bool Match(ConventionInfo conventionInfo) 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; @@ -39,11 +37,9 @@ protected override object SetValue(ConventionInfo conventionInfo) return clone; } - if (conventionInfo.SourceProp.Type.IsGenericType) { var genericInterfaces = conventionInfo.SourceProp.Type.GetGenericTypeDefinition().GetInterfaces(); - //handle IEnumerable<> also ICollection<> IList<> List<> if (genericInterfaces.Any(d => d == typeof(IEnumerable))) { return MapLists(conventionInfo); @@ -59,7 +55,7 @@ protected override object SetValue(ConventionInfo conventionInfo) } //for simple object types create a new instace and apply the clone injection on it - return Activator.CreateInstance(conventionInfo.SourceProp.Type) + return Activator.CreateInstance(conventionInfo.TargetProp.Type) .InjectFrom(conventionInfo.SourceProp.Value); } @@ -80,20 +76,26 @@ private static object MapLazy(ConventionInfo conventionInfo) private static object MapLists(ConventionInfo conventionInfo) { - 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 genericArgument = conventionInfo.TargetProp.Type.GetGenericArguments()[0]; + if (genericArgument.IsValueType || genericArgument == typeof(string)) { - var e = Activator.CreateInstance(t).InjectFrom(o); - addMethod.Invoke(list, new[] { e }); // in 4.0 you can use dynamic and just do list.Add(e); + return conventionInfo.SourceProp.Value; } - return list; + + var listType = typeof(List<>).MakeGenericType(genericArgument); + var addMethod = listType.GetMethod("Add"); + + var result = Activator.CreateInstance(listType); + + foreach (var sourceItem in (IEnumerable)conventionInfo.SourceProp.Value) + { + var e = Activator.CreateInstance(genericArgument).InjectFrom(sourceItem); + addMethod.Invoke(result, new[] {e }); + } + + return result; + } } } \ No newline at end of file