Skip to content

Commit

Permalink
Added ability to ignore lettere case for properties names.
Browse files Browse the repository at this point in the history
  • Loading branch information
omaxel committed Oct 13, 2017
1 parent 56f7e5d commit 8396a5c
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 7 deletions.
5 changes: 4 additions & 1 deletion Changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
1.2.2 - Oct 12, 2017
* Added ability to ignore lettere case for properties names.

1.2.1 - Sep 14, 2017
* Added support for Guid.
* Added DateTime and Guid fileds in example projects.
Expand All @@ -13,4 +16,4 @@
* Documentation comments translated to English.

1.0 - Jul 27, 2017
* First public release.
* First public release.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ public async Task<IHttpActionResult> PatchMultiple(DeltaCollection<PersonEF> peo
```

## Configuration

#### Exclude properties
You can exclude one or more properties of an entity while applying the changes to the original entity to preserve the original value of the property. This might be useful for properties used to uniquely identify the entity.

**Global.asax** or **Startup.cs**
Expand All @@ -165,3 +167,15 @@ DeltaConfig.Init((cfg) =>
```

**Note:** When a property is marked as *excluded* it will still be present in the `Delta <T>` object, but it will be ignored when the changes are applied (`Patch` method) to the original entity.

#### Ignore letter case for property names
You can ignore letter case for property names. This is useful when you have different name convention between client code and server code.
For example, the property `name` sent by the client wouldn't be detected as part of an entity which has a property named `Name` (note the first letter is **upper case**).

**Global.asax** or **Startup.cs**
```
DeltaConfig.Init((cfg) =>
{
cfg.IgnoreLetterCase();
});
```
2 changes: 1 addition & 1 deletion src/SimplePatch.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26730.10
VisualStudioVersion = 15.0.27004.2002
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimplePatch", "SimplePatch\SimplePatch.csproj", "{71D49858-8706-4E19-AB53-022320B263CD}"
EndProject
Expand Down
48 changes: 45 additions & 3 deletions src/SimplePatch/Delta.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,18 @@ public object this[string key]
get => dict[key];
set
{
if (IsPropertyAllowed(key)) dict[key] = value;
if (DeltaConfig.IgnoreLetterCase)
{
//Get the correct case property name
var propertyName = GetCorrectCasePropertyName(key);

//Set the value if the property name is found in the entity (ignoring letter case)
if (propertyName != null) dict[propertyName] = value;
}
else
{
if (IsPropertyAllowed(key)) dict[key] = value;
}
}
}

Expand Down Expand Up @@ -87,7 +98,7 @@ public bool TryGetPropertyValue(string propertyName, out object propertyValue)
/// <param name="item">Item to be added. The element will not be added if <paramref name="item"/>.Value is null or it is equal to <see cref="string.Empty"/>. See <see cref="IsPropertyAllowed(string)".</param>
public void Add(KeyValuePair<string, object> item)
{
if (IsPropertyAllowed(item.Key)) dict.Add(item.Key, item.Value);
Add(item.Key, item.Value);
}

/// <summary>
Expand All @@ -97,7 +108,18 @@ public void Add(KeyValuePair<string, object> item)
/// <param name="value">Value of element to be added. The element will not be added if null or equal to <see cref="string.Empty"/>. See <see cref="IsPropertyAllowed(string)".</param>
public void Add(string key, object value)
{
if (IsPropertyAllowed(key)) dict.Add(key, value);
if (DeltaConfig.IgnoreLetterCase)
{
//Get the correct case property name
var propertyName = GetCorrectCasePropertyName(key);

//Set the value if the property name is found in the entity (ignoring letter case)
if (propertyName != null) dict[propertyName] = value;
}
else
{
if (IsPropertyAllowed(key)) dict.Add(key, value);
}
}

/// <summary>
Expand Down Expand Up @@ -133,6 +155,26 @@ private bool IsPropertyAllowed(string propertyName)
return !string.IsNullOrEmpty(propertyName) && DeltaCache.entityProperties[typeFullName].Any(x => x.Name == propertyName);
}

/// <summary>
/// Return the property name with correct case starting from an incorrect case name.
/// </summary>
/// <param name="propertyName">The property name </param>
/// <returns>The correct case property name. If no property found, null.</returns>
private string GetCorrectCasePropertyName(string propertyName)
{
if (string.IsNullOrEmpty(propertyName)) return null;

var properties = DeltaCache.entityProperties[typeFullName];

var propertyNameLowerCase = propertyName.ToLower();
foreach (var property in properties)
{
if (property.Name.ToLower() == propertyNameLowerCase) return property.Name;
}

return null;
}

/// <summary>
/// Set the value for each property of <see cref="TEntity" /> for which there is a reference in <see cref="dict" />.
/// </summary>
Expand Down
13 changes: 13 additions & 0 deletions src/SimplePatch/DeltaConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace SimplePatch
{
public class DeltaConfig
{
internal static bool IgnoreLetterCase = false;

public static void Init(Action<Config> config)
{
config(new Config());
Expand Down Expand Up @@ -38,6 +40,17 @@ public Config ExcludeProperties<T>(params Expression<Func<T, object>>[] properti
return this;
}

/// <summary>
/// If enabled, the properties names comparing function will ignore letter case.
/// </summary>
/// <param name="enabled">Whetever to ignore letter case for properties.</param>
/// <returns></returns>
public Config IgnoreLetterCase(bool enabled = true)
{
DeltaConfig.IgnoreLetterCase = enabled;
return this;
}

private static MemberExpression GetMemberExpression<T>(Expression<Func<T, object>> exp)
{
var member = exp.Body as MemberExpression;
Expand Down
4 changes: 2 additions & 2 deletions src/SimplePatch/SimplePatch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

<PropertyGroup>
<TargetFrameworks>net451;netstandard1.4</TargetFrameworks>
<Version>1.2.1</Version>
<Version>1.2.2</Version>
<Authors>Omar Muscatello</Authors>
<Company>Omar Muscatello</Company>
<Description>A simple library for partial entity changes in ASP.NET and ASP.NET Core.</Description>
<Copyright>Copyright (c) Omar Muscatello 2017</Copyright>
<PackageLicenseUrl>https://github.com/OmarMuscatello/SimplePatch/blob/master/LICENSE</PackageLicenseUrl>
<PackageReleaseNotes>Added support for Guid.</PackageReleaseNotes>
<PackageReleaseNotes>Added ability to ignore lettere case for properties names.</PackageReleaseNotes>
<PackageProjectUrl>https://github.com/OmarMuscatello/SimplePatch</PackageProjectUrl>
<RepositoryUrl>https://github.com/OmarMuscatello/SimplePatch</RepositoryUrl>
<PackageIconUrl>http://raw.github.com/OmarMuscatello/SimplePatch/master/simplepatch-icon.png</PackageIconUrl>
Expand Down

0 comments on commit 8396a5c

Please sign in to comment.