Skip to content
Browse files

Removing remove version sort

  • Loading branch information...
1 parent e8a6ef4 commit b27a1b1ca36ce416f4952aa5be7ba00533400470 @pranavkm pranavkm committed
View
1 Facts/Facts.csproj
@@ -139,7 +139,6 @@
<Compile Include="Services\FileSystemFileStorageServiceFacts.cs" />
<Compile Include="Services\MessageServiceFacts.cs" />
<Compile Include="Services\NuGetExeDownloaderServiceFacts.cs" />
- <Compile Include="Services\ODataRemoveVersionSorterFacts.cs" />
<Compile Include="Services\PackageServiceFacts.cs" />
<Compile Include="Services\UploadFileServiceFacts.cs" />
<Compile Include="Services\UsersServiceFacts.cs" />
View
87 Facts/Services/ODataRemoveVersionSorterFacts.cs
@@ -1,87 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using Xunit;
-
-namespace NuGetGallery
-{
- public class ODataRemoveVersionSorterFacts
- {
- [Fact]
- public void RemoveVersionSortRemovesThenByOnVersion()
- {
- // Arrange
- var package_AB = new V2FeedPackage { Id = "A", Version = "B"};
- var package_AA = new V2FeedPackage { Id = "A", Version = "A"};
- var package_CA = new V2FeedPackage { Id = "C", Version = "A"};
-
- var source = new[] { package_AB, package_CA, package_AA }.AsQueryable();
-
- // Act
- var resultA = source.OrderBy(p => p.Id).ThenBy(p => p.Version);
- var resultB = source.WithoutVersionSort().OrderBy(p => p.Id).ThenBy(p => p.Version);
-
- // Assert
- Assert.Equal(new[] { package_AA, package_AB, package_CA }, resultA);
- Assert.Equal(new[] { package_AB, package_AA, package_CA }, resultB);
- }
-
- [Fact]
- public void RemoveVersionSortRemovesThenByDescendingOnVersion()
- {
- // Arrange
- var package_AB = new V2FeedPackage { Id = "A", Version = "B" };
- var package_AA = new V2FeedPackage { Id = "A", Version = "A" };
- var package_AC = new V2FeedPackage { Id = "A", Version = "C" };
-
- var source = new[] { package_AB, package_AC, package_AA }.AsQueryable();
-
- // Act
- var resultA = source.OrderBy(p => p.Id).ThenByDescending(p => p.Version);
- var resultB = source.WithoutVersionSort().OrderBy(p => p.Id).ThenByDescending(p => p.Version);
-
- // Assert
- Assert.Equal(new[] { package_AC, package_AB, package_AA }, resultA);
- Assert.Equal(new[] { package_AB, package_AC, package_AA }, resultB);
- }
-
- [Fact]
- public void RemoveVersionSortRemovesThenByWhenItIsNestedInsideAnotherThenBy()
- {
- // Arrange
- var package_AB = new V2FeedPackage { Id = "A", Version = "B" };
- var package_AA = new V2FeedPackage { Id = "A", Version = "A" };
- var package_AC = new V2FeedPackage { Id = "A", Version = "C" };
-
- var source = new[] { package_AB, package_AC, package_AA }.AsQueryable();
-
- // Act
- var resultA = source.OrderBy(p => p.Id).ThenBy(p => p.Id).ThenByDescending(p => p.Version);
- var resultB = source.WithoutVersionSort().OrderBy(p => p.Id).ThenBy(p => p.Id).ThenByDescending(p => p.Version);
-
- // Assert
- Assert.Equal(new[] { package_AC, package_AB, package_AA }, resultA);
- Assert.Equal(new[] { package_AB, package_AC, package_AA }, resultB);
- }
-
- [Fact]
- public void RemoveVersionSortRemovesThenByWhenVersionIsRepresentedInAWrapperObject()
- {
- // Arrange
- var package_AB = new { Id = "A", WrapperObject = new { Version = "B" } };
- var package_AA = new { Id = "A", WrapperObject = new { Version = "A" } };
- var package_AC = new { Id = "A", WrapperObject = new { Version = "C" } };
-
- var source = new[] { package_AB, package_AC, package_AA }.AsQueryable();
-
- // Act
- var resultA = source.OrderBy(p => p.Id).ThenBy(p => p.Id).ThenByDescending(p => p.WrapperObject.Version);
- var resultB = source.WithoutVersionSort().OrderBy(p => p.Id).ThenBy(p => p.Id).ThenByDescending(p => p.WrapperObject.Version);
-
- // Assert
- Assert.Equal(new[] { package_AC, package_AB, package_AA }, resultA);
- Assert.Equal(new[] { package_AB, package_AC, package_AA }, resultB);
- }
- }
-}
View
61 Website/DataServices/ODataRemoveVersionSorter.cs
@@ -1,61 +0,0 @@
-using System;
-using System.Linq;
-using System.Linq.Expressions;
-
-namespace NuGetGallery
-{
- public class ODataRemoveVersionSorter : ExpressionVisitor
- {
- protected override Expression VisitMethodCall(MethodCallExpression node)
- {
- if (IsSortingOnVersion(node))
- {
- // The expression is of the format Queryable.ThenBy(OrderBy(<Expression>, <Order-by-params>), <Then-by-params>). To avoid performing the
- // method, we ignore it, traversing the passed in expression instead.
- return Visit(node.Arguments[0]);
- }
- return base.VisitMethodCall(node);
- }
-
- private bool IsSortingOnVersion(MethodCallExpression expression)
- {
- var methodsToIgnore = new[] { "ThenBy", "ThenByDescending" };
- var method = expression.Method;
-
- if (method.DeclaringType == typeof(Queryable) && methodsToIgnore.Contains(method.Name, StringComparer.Ordinal))
- {
- return IsVersionArgument(expression);
- }
-
- return false;
- }
-
- private bool IsVersionArgument(MethodCallExpression expression)
- {
- if (expression.Arguments.Count == 2)
- {
- var memberVisitor = new MemberVisitor();
- memberVisitor.Visit(expression.Arguments[1]);
- return memberVisitor.Flag;
- }
-
- return false;
- }
-
- private sealed class MemberVisitor : ExpressionVisitor
- {
- public bool Flag { get; set; }
-
- protected override Expression VisitMember(MemberExpression node)
- {
- // Note that if Flag has already been set to true, we need to retain that state
- // as our visitor can be called multiple times.
- // Example: The expression can either be p => p.Version or p => p.ExpandedWrapper.Version where the
- // latter is some funky OData type wrapper. We need to ensure we handle both these cases
- Flag = Flag || String.Equals(node.Member.Name, "Version", StringComparison.Ordinal);
- return base.VisitMember(node);
- }
- }
-
- }
-}
View
7 Website/DataServices/PackageExtensions.cs
@@ -17,7 +17,6 @@ public static IQueryable<V1FeedPackage> ToV1FeedPackageQuery(this IQueryable<Pac
siteRoot = EnsureTrailingSlash(siteRoot);
return packages
.WithoutNullPropagation()
- .WithoutVersionSort()
.Include(p => p.PackageRegistration)
.Select(p => new V1FeedPackage
{
@@ -57,7 +56,6 @@ public static IQueryable<V2FeedPackage> ToV2FeedPackageQuery(this IQueryable<Pac
siteRoot = EnsureTrailingSlash(siteRoot);
return packages
.WithoutNullPropagation()
- .WithoutVersionSort()
.Include(p => p.PackageRegistration)
.Select(p => new V2FeedPackage
{
@@ -92,11 +90,6 @@ public static IQueryable<V2FeedPackage> ToV2FeedPackageQuery(this IQueryable<Pac
});
}
- internal static IQueryable<TVal> WithoutVersionSort<TVal>(this IQueryable<TVal> feedQuery)
- {
- return feedQuery.InterceptWith(new ODataRemoveVersionSorter());
- }
-
private static string EnsureTrailingSlash(string siteRoot)
{
if (!siteRoot.EndsWith("/", StringComparison.Ordinal))
View
1 Website/Website.csproj
@@ -214,7 +214,6 @@
<Compile Include="CuratedPackagesController.generated.cs">
<DependentUpon>T4MVC.tt</DependentUpon>
</Compile>
- <Compile Include="DataServices\ODataRemoveVersionSorter.cs" />
<Compile Include="DataServices\V2CuratedFeed.svc.cs">
<DependentUpon>V2CuratedFeed.svc</DependentUpon>
</Compile>

0 comments on commit b27a1b1

Please sign in to comment.
Something went wrong with that request. Please try again.