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

interface type fixes & unit test improvements #188

Open
wants to merge 25 commits 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
10 changes: 6 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ on:
- 'releases/*'

jobs:
ubuntu-latest:
name: ubuntu-latest
runs-on: ubuntu-latest
ubuntu-20_04:
name: ubuntu-20_04
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Run './build.cmd Ci'
run: ./build.cmd Ci
10 changes: 6 additions & 4 deletions .github/workflows/ci_publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ on:
- 'v*'

jobs:
ubuntu-latest:
name: ubuntu-latest
runs-on: ubuntu-latest
ubuntu-20_04:
name: ubuntu-20_04
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Run './build.cmd CiPublish'
run: ./build.cmd CiPublish
env:
Expand Down
10 changes: 9 additions & 1 deletion .nuke/build.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@
"AppVeyor",
"AzurePipelines",
"Bamboo",
"Bitbucket",
"Bitrise",
"GitHubActions",
"GitLab",
"Jenkins",
"Rider",
"SpaceAutomation",
"TeamCity",
"Terminal",
"TravisCI"
"TravisCI",
"VisualStudio",
"VSCode"
]
},
"NoLogo": {
Expand All @@ -46,6 +50,10 @@
"NUGET_API_KEY": {
"type": "string"
},
"Partition": {
"type": "string",
"description": "Partition to use on CI"
},
"Plan": {
"type": "boolean",
"description": "Shows the execution plan (HTML)"
Expand Down
8 changes: 4 additions & 4 deletions Sieve.Sample/Sieve.Sample.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.14">
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.14" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.14" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SieveUnitTests\SieveUnitTests.csproj" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,76 @@
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Sieve.Exceptions;

namespace Sieve.Extensions
{
public static partial class LinqExtensions
{
internal static Expression GeneratePropertyAccess
(
this Expression parameterExpression,
string propertyName
)
{
var propertyAccessor = parameterExpression;

try
{
propertyAccessor = Expression.PropertyOrField(propertyAccessor, propertyName);
}
catch (ArgumentException)
{
// propertyName is not a direct property of field of propertyAccessor expression's type.
// when propertyAccessor.Type is directly an interface, say typeof(ISomeEntity) and ISomeEntity is interfaced to IBaseEntity
// in which `propertyName` is defined in the first place.
// To solve this, search `propertyName` in all other implemented interfaces

var possibleInterfaceType = propertyAccessor.Type;

if (!possibleInterfaceType.IsInterface)
throw;

// get all implemented interface types
var implementedInterfaces = possibleInterfaceType.GetInterfaces();

try
{
// search propertyName in all interfaces
var interfacedExpression = implementedInterfaces
.Where
(
implementedInterface => implementedInterface
.GetProperties()
.Any(info => info.Name == propertyName)
)
.Select(implementedInterface => Expression.TypeAs(propertyAccessor, implementedInterface))
.SingleOrDefault();

if (interfacedExpression != null)
propertyAccessor = Expression.PropertyOrField(interfacedExpression, propertyName);
}
catch (InvalidOperationException ioe)
{
throw new SieveException
(
$"{propertyName} is repeated in interface hierarchy. Try renaming.",
ioe
);
}
}

return propertyAccessor;
}

public static IQueryable<TEntity> OrderByDynamic<TEntity>(
this IQueryable<TEntity> source,
string fullPropertyName,
PropertyInfo propertyInfo,
bool desc,
bool useThenBy,
bool disableNullableTypeExpression = false)
{
var lambda = GenerateLambdaWithSafeMemberAccess<TEntity>(fullPropertyName, propertyInfo, disableNullableTypeExpression);
var lambda = GenerateLambdaWithSafeMemberAccess<TEntity>(fullPropertyName, disableNullableTypeExpression);

var command = desc
? (useThenBy ? "ThenByDescending" : "OrderByDescending")
Expand All @@ -34,7 +89,6 @@ public static IQueryable<TEntity> OrderByDynamic<TEntity>(
private static Expression<Func<TEntity, object>> GenerateLambdaWithSafeMemberAccess<TEntity>
(
string fullPropertyName,
PropertyInfo propertyInfo,
bool disableNullableTypeExpression
)
{
Expand All @@ -44,15 +98,7 @@ bool disableNullableTypeExpression

foreach (var name in fullPropertyName.Split('.'))
{
try
{
propertyValue = Expression.PropertyOrField(propertyValue, name);
}
catch (ArgumentException)
{
// name is not a direct property of field of propertyValue expression. construct a memberAccess then.
propertyValue = Expression.MakeMemberAccess(propertyValue, propertyInfo);
}
propertyValue = propertyValue.GeneratePropertyAccess(name);

if (propertyValue.Type.IsNullable() && !disableNullableTypeExpression)
{
Expand Down
4 changes: 2 additions & 2 deletions Sieve/Services/SieveProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ private static (Expression propertyValue, Expression nullCheck) GetPropertyValue
var names = fullPropertyName.Split('.');
for (var i = 0; i < names.Length; i++)
{
propertyValue = Expression.PropertyOrField(propertyValue, names[i]);
propertyValue = propertyValue.GeneratePropertyAccess(names[i]);

if (i != names.Length - 1 && propertyValue.Type.IsNullable())
{
Expand Down Expand Up @@ -368,7 +368,7 @@ protected virtual IQueryable<TEntity> ApplySorting<TEntity>(TSieveModel model, I

if (property != null)
{
result = result.OrderByDynamic(fullName, property, sortTerm.Descending, useThenBy, Options.Value.DisableNullableTypeExpressionForSorting);
result = result.OrderByDynamic(fullName, sortTerm.Descending, useThenBy, Options.Value.DisableNullableTypeExpressionForSorting);
}
else
{
Expand Down
6 changes: 3 additions & 3 deletions Sieve/Sieve.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.4.1" />
<PackageReference Include="Microsoft.Extensions.Options" Version="2.0.0" />
<PackageReference Include="System.ComponentModel.Annotations" Version="4.4.1" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="7.0.1" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
</ItemGroup>

</Project>
3 changes: 2 additions & 1 deletion SieveUnitTests/Abstractions/Entity/IBaseEntity.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System;
using SieveUnitTests.Abstractions.Strategy;

namespace SieveUnitTests.Abstractions.Entity
{
public interface IBaseEntity
public interface IBaseEntity: ISupportSoftDelete
{
int Id { get; set; }
DateTimeOffset DateCreated { get; set; }
Expand Down
3 changes: 2 additions & 1 deletion SieveUnitTests/Abstractions/Entity/IPost.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using Sieve.Attributes;
using SieveUnitTests.Abstractions.Strategy;
using SieveUnitTests.Entities;

namespace SieveUnitTests.Abstractions.Entity
{
public interface IPost: IBaseEntity
public interface IPost: IBaseEntity, IAudit
{
[Sieve(CanFilter = true, CanSort = true)]
string Title { get; set; }
Expand Down
23 changes: 21 additions & 2 deletions SieveUnitTests/Abstractions/Entity/SieveConfigurationForIPost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ namespace SieveUnitTests.Abstractions.Entity
{
public class SieveConfigurationForIPost : ISieveConfiguration
{
public void Configure(SievePropertyMapper mapper)
public static void ConfigureStatic(SievePropertyMapper mapper)
{
mapper
.Property<IPost>(p => p.Id)
.CanSort();

mapper.Property<IPost>(p => p.ThisHasNoAttributeButIsAccessible)
.CanSort()
.CanFilter()
.HasName("shortname");

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

Expand All @@ -32,6 +36,21 @@ public void Configure(SievePropertyMapper mapper)
.Property<IPost>(p => p.DateCreated)
.CanSort()
.HasName("CreateDate");

mapper
.Property<IPost>(post => post.DeletedBy)
.CanSort()
.HasName("DeletedBy");

mapper
.Property<IPost>(post => post.UpdatedBy)
.CanFilter()
.HasName("UpdatedBy");
}

public void Configure(SievePropertyMapper mapper)
{
ConfigureStatic(mapper);
}
}
}
12 changes: 12 additions & 0 deletions SieveUnitTests/Abstractions/Strategy/IAudit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace SieveUnitTests.Abstractions.Strategy
{
public interface IAudit
{
string CreatedBy { get; }
DateTime? CreatedAt { get; }
string UpdatedBy { get; }
DateTime? UpdatedAt { get; }
}
}
10 changes: 10 additions & 0 deletions SieveUnitTests/Abstractions/Strategy/ISupportSoftDelete.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace SieveUnitTests.Abstractions.Strategy
{
public interface ISupportSoftDelete
{
string DeletedBy { get; }
DateTime? DeletedAt { get; }
}
}
3 changes: 3 additions & 0 deletions SieveUnitTests/Entities/BaseEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ public class BaseEntity : IBaseEntity

[Sieve(CanFilter = true, CanSort = true)]
public DateTimeOffset DateCreated { get; set; } = DateTimeOffset.UtcNow;

public string DeletedBy { get; set; }
public DateTime? DeletedAt { get; set; }
}
}
5 changes: 5 additions & 0 deletions SieveUnitTests/Entities/Post.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,10 @@ public class Post : BaseEntity, IPost

public Comment TopComment { get; set; }
public Comment FeaturedComment { get; set; }

public string CreatedBy { get; set; }
public DateTime? CreatedAt { get; set; }
public string UpdatedBy { get; set; }
public DateTime? UpdatedAt { get; set; }
}
}
21 changes: 20 additions & 1 deletion SieveUnitTests/Entities/SieveConfigurationForPost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ namespace SieveUnitTests.Entities
{
public class SieveConfigurationForPost : ISieveConfiguration
{
public void Configure(SievePropertyMapper mapper)
public static void ConfigureStatic(SievePropertyMapper mapper)
{
mapper
.Property<Post>(p => p.Id)
.CanSort();

mapper.Property<Post>(p => p.ThisHasNoAttributeButIsAccessible)
.CanSort()
.CanFilter()
Expand All @@ -32,6 +36,21 @@ public void Configure(SievePropertyMapper mapper)
.Property<Post>(p => p.DateCreated)
.CanSort()
.HasName("CreateDate");

mapper
.Property<Post>(post => post.DeletedBy)
.CanSort()
.HasName("DeletedBy");

mapper
.Property<Post>(post => post.UpdatedBy)
.CanFilter()
.HasName("UpdatedBy");
}

public void Configure(SievePropertyMapper mapper)
{
ConfigureStatic(mapper);
}
}
}
Loading