Skip to content

Commit

Permalink
Merge pull request #39 from kluhman/umbraco-file-support
Browse files Browse the repository at this point in the history
Updates value transformation process
  • Loading branch information
kluhman authored Jan 31, 2017
2 parents 0d83b31 + a2ca535 commit 6e8c325
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## v1.3.2

* Updates UmbracoWebContext.GetCurrent to use the PublishedContent from the PublishedContentRequest rather than getting a page by the current id.
* Updated value transformation process to execute type handler even if value type already matches target type to ensure all proper transforms are applied.

## v1.3.1

Expand Down
24 changes: 24 additions & 0 deletions UmbracoVault.Tests/Attributes/TrimStringPropertyAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using UmbracoVault.Attributes;
using UmbracoVault.Tests.Handlers;

namespace UmbracoVault.Tests.Attributes
{
public class TrimStringPropertyAttribute : UmbracoPropertyAttribute
{
public TrimStringPropertyAttribute()
{
TypeHandler = new TrimStringTypeHandler();
}

public TrimStringPropertyAttribute(string alias) : base(alias)
{
TypeHandler = new TrimStringTypeHandler();
}
}
}
20 changes: 20 additions & 0 deletions UmbracoVault.Tests/Handlers/TrimStringTypeHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using UmbracoVault.TypeHandlers;

namespace UmbracoVault.Tests.Handlers
{
class TrimStringTypeHandler : ITypeHandler
{
public object GetAsType<T>(object input)
{
return input?.ToString()?.Trim();
}

public Type TypeSupported => typeof(string);
}
}
4 changes: 4 additions & 0 deletions UmbracoVault.Tests/Models/ExampleModelAllTypes.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using UmbracoVault.Attributes;
using UmbracoVault.Tests.Attributes;

namespace UmbracoVault.Tests.Models
{
Expand Down Expand Up @@ -57,5 +58,8 @@ public class ExampleModelAllTypes

[UmbracoEnumProperty]
public ExampleEnum ExampleEnum { get; set; }

[TrimStringProperty]
public string TypeHandledString { get; set; }
}
}
7 changes: 7 additions & 0 deletions UmbracoVault.Tests/UmbracoContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public void Initialize()
Source.StringArray = new[] {"100", "200", "300"};
Source.Object = new { Name = "TestObject" };
Source.ExampleEnum = ExampleEnum.Harry;
Source.TypeHandledString = " this string needs to be trimmed ";

var specialCases = new NameValueCollection
{
Expand Down Expand Up @@ -170,6 +171,12 @@ public void String_IsSet()
Assert.AreEqual(Source.String, Destination.String);
}

[TestMethod]
public void TypeHandler_ShouldExecute_EvenWhenTypesAlreadyMatch()
{
Assert.AreEqual(Source.TypeHandledString.Trim(), Destination.TypeHandledString);
}

[TestMethod]
public void UInt_IsSet()
{
Expand Down
2 changes: 2 additions & 0 deletions UmbracoVault.Tests/UmbracoVault.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@
</Otherwise>
</Choose>
<ItemGroup>
<Compile Include="Attributes\TrimStringPropertyAttribute.cs" />
<Compile Include="DefaultInstanceFactoryTests.cs" />
<Compile Include="Handlers\TrimStringTypeHandler.cs" />
<Compile Include="ProxyInstanceInterfaceFactoryTests.cs" />
<Compile Include="DefaultInstanceInterfaceFactoryTests.cs" />
<Compile Include="Models\AutoRegisteredTypeHandler.cs" />
Expand Down
20 changes: 10 additions & 10 deletions UmbracoVault/Base/BaseUmbracoContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,19 +286,21 @@ protected object TransformParsedValue(PropertyInfo propertyInfo, UmbracoProperty
(current, transform) => transform.Transform(current));
*/

if (value.GetType() == propertyType)
var typeHandler = GetTypeHandler(propertyType, propertyMetaData);

//unsupported if the value is not target type and there is no handler
if (value.GetType() != propertyType && typeHandler == null)
{
return value;
throw new NotSupportedException($"The property type {propertyType} is not supported by Umbraco Vault.");
}

var typeHandler = GetTypeHandler(propertyType, propertyMetaData);

//if there is no handler, but value is already the target type, return value
if (typeHandler == null)
{
throw new NotSupportedException(
$"The property type {propertyType} is not supported by Umbraco Vault.");
return value;
}

//apply handler
if (typeHandler is EnumTypeHandler)
{
// Unfortunately, the EnumTypeHandler currently requires special attention because the GetAsType has a "where T : class" constraint.
Expand All @@ -314,16 +316,14 @@ protected object TransformParsedValue(PropertyInfo propertyInfo, UmbracoProperty
{
var method = typeHandler.GetType().GetMethod("GetAsType");
var generic = method.MakeGenericMethod(propertyInfo.PropertyType.GetGenericArguments()[0]);
value = generic.Invoke(typeHandler, new[] { value });
return generic.Invoke(typeHandler, new[] { value });
}
else
{
var method = typeHandler.GetType().GetMethod("GetAsType");
var generic = method.MakeGenericMethod(propertyInfo.PropertyType);
value = generic.Invoke(typeHandler, new[] { value });
return generic.Invoke(typeHandler, new[] { value });
}

return value;
}

private ITypeHandler GetTypeHandler(Type propertyType, UmbracoPropertyAttribute propertyMetaData)
Expand Down
1 change: 1 addition & 0 deletions UmbracoVault/UmbracoVault.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<summary>Vault for Umbraco is an easy-to-use, extensible ORM to quickly and easily get strongly-typed Umbraco CMS data into your views.</summary>
<releaseNotes>
* Updates UmbracoWebContext.GetCurrent to use the PublishedContent from the PublishedContentRequest rather than getting a page by the current id.
* Updated value transformation process to execute type handler even if value type already matches target type to ensure all proper transforms are applied.
</releaseNotes>
<copyright>(c) The Nerdery LLC 2016. All Rights Reserved.</copyright>
<tags>Umbraco UmbracoVault Mapping ObjectMapper ORM CMS</tags>
Expand Down

0 comments on commit 6e8c325

Please sign in to comment.