Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Support for Patch properties if Mapping returns Delta<> #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 67 additions & 24 deletions src/SimplePatch.Tests/ConfigurationTests/Globals.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json.Linq;
using SimplePatch.Mapping;
using static SimplePatch.Tests.DeltaUtils;

Expand All @@ -7,6 +9,13 @@ namespace SimplePatch.Tests.ConfigurationTests
[TestClass, TestCategory(TestCategories.Configuration)]
public class Globals : TestBase
{
internal class Data
{
public string Category { get; set; }

public Person PersonInfo { get; set; }
}

[TestCleanup]
public void TestCleanup()
{
Expand All @@ -19,8 +28,8 @@ public void IgnoreLetterCase()
DeltaConfig.Init(cfg =>
{
cfg
.IgnoreLetterCase()
.AddEntity<Person>();
.IgnoreLetterCase()
.AddEntity<Person>();
});

CreateDelta<Person>("AgE", 23).Patch(John);
Expand All @@ -34,35 +43,35 @@ public void MappingFunction()
{
cfg

/* When the target property type is int and the input is string,
then the assigned value will be the length of the input string*/
.AddMapping((propType, newValue) =>
{
var result = new MapResult<object>();
/* When the target property type is int and the input is string,
then the assigned value will be the length of the input string*/
.AddMapping((propType, newValue) =>
{
var result = new MapResult<object>();

if (propType != typeof(int)) return result.SkipMap();
if (newValue.GetType() != typeof(string)) return result.SkipMap();
if (propType != typeof(int)) return result.SkipMap();
if (newValue.GetType() != typeof(string)) return result.SkipMap();

result.Value = newValue.ToString().Length;
result.Value = newValue.ToString().Length;

return result;
})
return result;
})

/* When the target property is double and the input is string,
then the assigned value will be the length of the string + 0.5*/
.AddMapping((propType, newValue) =>
{
var result = new MapResult<object>();
/* When the target property is double and the input is string,
then the assigned value will be the length of the string + 0.5*/
.AddMapping((propType, newValue) =>
{
var result = new MapResult<object>();

if (propType != typeof(double)) return result.SkipMap();
if (propType != typeof(double)) return result.SkipMap();

if (newValue.GetType() != typeof(string)) return result.SkipMap();
if (newValue.GetType() != typeof(string)) return result.SkipMap();

result.Value = newValue.ToString().Length + 0.5;
result.Value = newValue.ToString().Length + 0.5;

return result;
})
.AddEntity<Person>();
return result;
})
.AddEntity<Person>();
});

// First mapping function will be executed here, Age type is int
Expand All @@ -73,5 +82,39 @@ then the assigned value will be the length of the string + 0.5*/
CreateDelta<Person, double>(x => x.Height, "abcdef").Patch(John);
Assert.AreEqual("abcdef".Length + 0.5, John.Height);
}

[TestMethod]
public void MappingToDeltaType()
{
DeltaConfig.Init(cfg =>
{
cfg.AddEntity<Person>();
cfg.AddEntity<Data>();
cfg.AddMapping((propType, newValue) =>
{
var result = new MapResult<object>();
if (!(newValue is JToken jToken)) return result.SkipMap();

var deltaOfEntity = (typeof(Delta<>)).MakeGenericType(propType);
result.Value = jToken.ToObject(deltaOfEntity);
return result;
});
});

var data = new Data
{
Category = "Test",
PersonInfo = John
};

var dataDelta = new Delta<Data>
{
[nameof(Data.PersonInfo)] = JObject.FromObject(new Dictionary<string, object>{[nameof(Person.Name)] = "Foo"})
};

dataDelta.Patch(data);
Assert.AreEqual("Foo", data.PersonInfo.Name);
Assert.AreEqual("Doe", data.PersonInfo.Surname);
}
}
}
3 changes: 3 additions & 0 deletions src/SimplePatch.Tests/SimplePatch.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
</ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions src/SimplePatch.Tests/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<packages>
<package id="MSTest.TestAdapter" version="1.3.2" targetFramework="net47" />
<package id="MSTest.TestFramework" version="1.3.2" targetFramework="net47" />
<package id="Newtonsoft.Json" version="12.0.2" targetFramework="net47" />
</packages>
15 changes: 12 additions & 3 deletions src/SimplePatch/Delta.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,19 @@ private TEntity SetPropertiesValue(TEntity entity)

if (!valueFromMappings.Skip)
{
propertyInfo.SetValue(entity, valueFromMappings.Value);
var value = valueFromMappings.Value;
var valueType = value.GetType();
if (valueType.GenericTypeArguments.Any() && valueType.GetGenericTypeDefinition() == typeof(Delta<>))
{
valueType.GetMethod(nameof(Patch)).Invoke(value, new[] { propertyInfo.GetValue(entity, null) });
}
else
{
propertyInfo.SetValue(entity, valueFromMappings.Value);
}
}
// If no mapping function assigned a value to the property, use the default mapping
else
// If no mapping function assigned a value to the property, use the default mapping
else
{
var newPropertyValueType = newPropertyValue.GetType();

Expand Down