Skip to content

Commit

Permalink
Updated gallery dialog package query to ensure all tags are correctly…
Browse files Browse the repository at this point in the history
… matched.
  • Loading branch information
glopesdev committed Jan 24, 2017
1 parent 8d74e04 commit 70e9b72
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 10 deletions.
1 change: 1 addition & 0 deletions Bonsai.NuGet/Bonsai.NuGet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="PackageManagerProxy.cs" />
<Compile Include="QueryHelper.cs" />
<Compile Include="RequiringLicenseAcceptanceEventArgs.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
9 changes: 1 addition & 8 deletions Bonsai.NuGet/PackageViewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,14 +262,7 @@ public Func<IQueryable<IPackage>> GetPackageFeed()
}
else
{
try
{
packages = selectedRepository.GetPackages().Find(searchTerm);
foreach (var term in tagSearchTerms)
{
packages = packages.Find(TagProperties, term);
}
}
try { packages = selectedRepository.GetPackages().Find(searchTerm).WithTags(tagSearchTerms); }
catch (WebException e) { return Observable.Throw<IPackage>(e).ToEnumerable().AsQueryable(); }
if (allowPrereleaseVersions) packages = packages.Where(p => p.IsAbsoluteLatestVersion);
else packages = packages.Where(p => p.IsLatestVersion);
Expand Down
51 changes: 51 additions & 0 deletions Bonsai.NuGet/QueryHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using NuGet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Bonsai.NuGet
{
static class QueryHelper
{
const string TagsProperty = "Tags";
static readonly MethodInfo stringConcat = typeof(string).GetMethod("Concat", new Type[] { typeof(string), typeof(string) });
static readonly MethodInfo stringContains = typeof(string).GetMethod("Contains", new Type[] { typeof(string) });
static readonly MethodInfo stringToLower = typeof(string).GetMethod("ToLower", Type.EmptyTypes);

public static IQueryable<T> WithTags<T>(this IQueryable<T> packages, IEnumerable<string> tags) where T : IPackage
{
if (!tags.Any())
{
return packages;
}

var package = Expression.Parameter(typeof(IPackageMetadata));
var property = Expression.Property(package, TagsProperty);
var condition = (from term in tags
select BuildTagExpression(property, term))
.Aggregate(Expression.AndAlso);

// Check that the tag string is not null
var notNull = Expression.NotEqual(property, Expression.Constant(null));
condition = Expression.AndAlso(notNull, condition);

var predicate = Expression.Lambda<Func<T, bool>>(condition, package);
return packages.Where(predicate);
}

static Expression BuildTagExpression(MemberExpression property, string term)
{
// Pad both the tag string and the search term with white space to ensure an exact match
var paddedLowercaseTags = Expression.Call(property, stringToLower);
paddedLowercaseTags = Expression.Call(stringConcat, Expression.Constant(" "), paddedLowercaseTags);
paddedLowercaseTags = Expression.Call(stringConcat, paddedLowercaseTags, Expression.Constant(" "));

term = " " + term + " ";
return Expression.Call(paddedLowercaseTags, stringContains, Expression.Constant(term.ToLower()));
}
}
}
2 changes: 1 addition & 1 deletion Bonsai/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.3.0.0")]
[assembly: AssemblyInformationalVersion("2.3.0")]
[assembly: AssemblyInformationalVersion("2.3.1")]
2 changes: 1 addition & 1 deletion Bonsai64/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.3.0.0")]
[assembly: AssemblyInformationalVersion("2.3.0")]
[assembly: AssemblyInformationalVersion("2.3.1")]

0 comments on commit 70e9b72

Please sign in to comment.