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

feat: Implement support for mapping PropertyInfo directly #201

Closed
wants to merge 1 commit into from
Closed
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
19 changes: 19 additions & 0 deletions Sieve/Services/SievePropertyMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ public PropertyFluentApi<TEntity> Property<TEntity>(Expression<Func<TEntity, obj
return new PropertyFluentApi<TEntity>(this, expression);
}

public PropertyFluentApi<TEntity> Property<TEntity>(PropertyInfo propertyInfo)
{
if (!_map.ContainsKey(typeof(TEntity)))
{
_map.Add(typeof(TEntity), new List<KeyValuePair<PropertyInfo, ISievePropertyMetadata>>());
}

return new PropertyFluentApi<TEntity>(this, propertyInfo);
}

public class PropertyFluentApi<TEntity>
{
private readonly SievePropertyMapper _sievePropertyMapper;
Expand All @@ -36,6 +46,15 @@ public PropertyFluentApi(SievePropertyMapper sievePropertyMapper, Expression<Fun
_canSort = false;
}

public PropertyFluentApi(SievePropertyMapper sievePropertyMapper, PropertyInfo propertyInfo)
{
_sievePropertyMapper = sievePropertyMapper;
(_fullName, _property) = (propertyInfo.Name, propertyInfo);
_name = _fullName;
_canFilter = false;
_canSort = false;
}

private string _name;
private readonly string _fullName;
private bool _canFilter;
Expand Down
1 change: 1 addition & 0 deletions SieveUnitTests/Abstractions/Entity/IPost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public interface IPost: IBaseEntity
bool IsDraft { get; set; }
string ThisHasNoAttribute { get; set; }
string ThisHasNoAttributeButIsAccessible { get; set; }
string ThisHasNoAttributeButIsAccessible2 { get; set; }
int OnlySortableViaFluentApi { get; set; }
Comment TopComment { get; set; }
Comment FeaturedComment { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Sieve.Services;
using SieveUnitTests.Entities;

namespace SieveUnitTests.Abstractions.Entity
{
Expand All @@ -10,7 +11,12 @@ public void Configure(SievePropertyMapper mapper)
.CanSort()
.CanFilter()
.HasName("shortname");


mapper.Property<IPost>(typeof(IPost).GetProperty(nameof(IPost.ThisHasNoAttributeButIsAccessible2)))
.CanSort()
.CanFilter()
.HasName("shortname2");

mapper.Property<IPost>(p => p.TopComment.Text)
.CanFilter();

Expand Down
2 changes: 2 additions & 0 deletions SieveUnitTests/Entities/Post.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class Post : BaseEntity, IPost

public string ThisHasNoAttributeButIsAccessible { get; set; }

public string ThisHasNoAttributeButIsAccessible2 { get; set; }

public int OnlySortableViaFluentApi { get; set; }

public Comment TopComment { get; set; }
Expand Down
5 changes: 5 additions & 0 deletions SieveUnitTests/Entities/SieveConfigurationForPost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ public void Configure(SievePropertyMapper mapper)
.CanFilter()
.HasName("shortname");

mapper.Property<Post>(typeof(Post).GetProperty(nameof(Post.ThisHasNoAttributeButIsAccessible2)))
.CanSort()
.CanFilter()
.HasName("shortname2");

mapper.Property<Post>(p => p.TopComment.Text)
.CanFilter();

Expand Down
21 changes: 20 additions & 1 deletion SieveUnitTests/Mapper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using Sieve.Exceptions;
using Sieve.Models;
Expand All @@ -21,20 +21,23 @@ public Mapper()
{
Id = 1,
ThisHasNoAttributeButIsAccessible = "A",
ThisHasNoAttributeButIsAccessible2 = "A",
ThisHasNoAttribute = "A",
OnlySortableViaFluentApi = 100
},
new Post
{
Id = 2,
ThisHasNoAttributeButIsAccessible = "B",
ThisHasNoAttributeButIsAccessible2 = "B",
ThisHasNoAttribute = "B",
OnlySortableViaFluentApi = 50
},
new Post
{
Id = 3,
ThisHasNoAttributeButIsAccessible = "C",
ThisHasNoAttributeButIsAccessible2 = "C",
ThisHasNoAttribute = "C",
OnlySortableViaFluentApi = 0
},
Expand Down Expand Up @@ -81,6 +84,22 @@ public void MapperWorks(ISieveProcessor processor)
Assert.True(result.Count() == 1);
}

[Theory]
[MemberData(nameof(GetProcessors))]
public void MapperWorksWithPropertyInfo(ISieveProcessor processor)
{
var model = new SieveModel
{
Filters = "shortname2@=A",
};

var result = processor.Apply(model, _posts);

Assert.Equal("A", result.First().ThisHasNoAttributeButIsAccessible2);

Assert.True(result.Count() == 1);
}

[Theory]
[MemberData(nameof(GetProcessors))]
public void MapperSortOnlyWorks(ISieveProcessor processor)
Expand Down
10 changes: 10 additions & 0 deletions SieveUnitTests/Services/ApplicationSieveProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ protected override SievePropertyMapper MapProperties(SievePropertyMapper mapper)
.CanFilter()
.HasName("shortname");

mapper.Property<Post>(typeof(Post).GetProperty(nameof(Post.ThisHasNoAttributeButIsAccessible2)))
.CanSort()
.CanFilter()
.HasName("shortname2");

mapper.Property<Post>(p => p.TopComment.Text)
.CanFilter();

Expand Down Expand Up @@ -51,6 +56,11 @@ protected override SievePropertyMapper MapProperties(SievePropertyMapper mapper)
.CanFilter()
.HasName("shortname");

mapper.Property<IPost>(typeof(IPost).GetProperty(nameof(IPost.ThisHasNoAttributeButIsAccessible2)))
.CanSort()
.CanFilter()
.HasName("shortname2");

mapper.Property<IPost>(p => p.TopComment.Text)
.CanFilter();

Expand Down