Skip to content

Commit

Permalink
Refactor package type as a single value property
Browse files Browse the repository at this point in the history
  • Loading branch information
glopesdev committed Dec 28, 2024
1 parent c678fa4 commit cf99bb6
Show file tree
Hide file tree
Showing 14 changed files with 30 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal sealed class SearchFilterFormatter : NuGetMessagePackFormatter<SearchFi
{
private const string IncludePrereleasePropertyName = "includeprerelease";
private const string IncludeDelistedPropertyName = "includedelisted";
private const string PackageTypesPropertyName = "packagetypes";
private const string PackageTypePropertyName = "packagetype";
private const string FilterPropertyName = "filter";
private const string OrderByPropertyName = "orderby";
private const string SupportedFrameworksPropertyName = "supportedframeworks";
Expand All @@ -30,7 +30,7 @@ private SearchFilterFormatter()
SearchFilterType? filterType = null;
SearchOrderBy? searchOrderBy = null;
IEnumerable<string>? supportedFrameworks = null;
IEnumerable<string>? packageTypes = null;
string? packageType = null;

int propertyCount = reader.ReadMapHeader();
for (int propertyIndex = 0; propertyIndex < propertyCount; propertyIndex++)
Expand All @@ -43,8 +43,8 @@ private SearchFilterFormatter()
case IncludeDelistedPropertyName:
includeDelisted = reader.ReadBoolean();
break;
case PackageTypesPropertyName:
packageTypes = options.Resolver.GetFormatter<IEnumerable<string>>()!.Deserialize(ref reader, options);
case PackageTypePropertyName:
packageType = reader.ReadString();
break;
case FilterPropertyName:
filterType = options.Resolver.GetFormatter<SearchFilterType?>()!.Deserialize(ref reader, options);
Expand All @@ -65,7 +65,7 @@ private SearchFilterFormatter()
{
SupportedFrameworks = supportedFrameworks,
OrderBy = searchOrderBy,
PackageTypes = packageTypes,
PackageType = packageType,
IncludeDelisted = includeDelisted,
};
}
Expand All @@ -77,8 +77,8 @@ protected override void SerializeCore(ref MessagePackWriter writer, SearchFilter
writer.Write(value.IncludePrerelease);
writer.Write(IncludeDelistedPropertyName);
writer.Write(value.IncludeDelisted);
writer.Write(PackageTypesPropertyName);
options.Resolver.GetFormatter<IEnumerable<string>>()!.Serialize(ref writer, value.PackageTypes, options);
writer.Write(PackageTypePropertyName);
writer.Write(value.PackageType);
writer.Write(FilterPropertyName);
options.Resolver.GetFormatter<SearchFilterType?>()!.Serialize(ref writer, value.Filter, options);
writer.Write(OrderByPropertyName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,14 @@ public async override Task<IEnumerable<IPackageSearchMetadata>> SearchAsync(
}

// Filter on package types
if (filters?.PackageTypes != null
&& filters.PackageTypes.Any())
if (!string.IsNullOrEmpty(filters.PackageType))
{
foreach (var packageTypeName in filters.PackageTypes)
{
query = query
.Where(package => package.Nuspec
.GetPackageTypes()
.Any(packageType => StringComparer.OrdinalIgnoreCase.Equals(
packageType.Name,
packageTypeName)));
}
query = query
.Where(package => package.Nuspec
.GetPackageTypes()
.Any(packageType => StringComparer.OrdinalIgnoreCase.Equals(
packageType.Name,
filters.PackageType)));
}

// Collapse to the highest version per id, if necessary
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,6 @@ NuGet.Protocol.Core.Types.SearchFilter.IncludeDelisted.set -> void
NuGet.Protocol.Core.Types.SearchFilter.IncludePrerelease.get -> bool
NuGet.Protocol.Core.Types.SearchFilter.OrderBy.get -> NuGet.Protocol.Core.Types.SearchOrderBy?
NuGet.Protocol.Core.Types.SearchFilter.OrderBy.set -> void
~NuGet.Protocol.Core.Types.SearchFilter.PackageTypes.get -> System.Collections.Generic.IEnumerable<string>
~NuGet.Protocol.Core.Types.SearchFilter.PackageTypes.set -> void
NuGet.Protocol.Core.Types.SearchFilter.SearchFilter(bool includePrerelease) -> void
NuGet.Protocol.Core.Types.SearchFilter.SearchFilter(bool includePrerelease, NuGet.Protocol.Core.Types.SearchFilterType? filter) -> void
~NuGet.Protocol.Core.Types.SearchFilter.SupportedFrameworks.get -> System.Collections.Generic.IEnumerable<string>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#nullable enable
NuGet.Protocol.Plugins.PluginDiscoverer.PluginDiscoverer() -> void
virtual NuGet.Protocol.Plugins.PluginFactory.Dispose() -> void
~NuGet.Protocol.Core.Types.SearchFilter.PackageType.get -> string
~NuGet.Protocol.Core.Types.SearchFilter.PackageType.set -> void
~NuGet.Protocol.Plugins.PluginFile.PluginFile(string filePath, System.Lazy<NuGet.Protocol.Plugins.PluginFileState> state, bool requiresDotnetHost) -> void
~NuGet.Protocol.Plugins.PluginManager.PluginManager(NuGet.Common.IEnvironmentVariableReader reader, System.Lazy<NuGet.Protocol.Plugins.IPluginDiscoverer> pluginDiscoverer, System.Func<System.TimeSpan, NuGet.Protocol.Plugins.PluginFactory> pluginFactoryCreator, System.Lazy<string> pluginsCacheDirectoryPath) -> void
~virtual NuGet.Protocol.Plugins.PluginFactory.GetOrCreateAsync(NuGet.Protocol.Plugins.PluginFile pluginFile, System.Collections.Generic.IEnumerable<string> arguments, NuGet.Protocol.Plugins.IRequestHandlers requestHandlers, NuGet.Protocol.Plugins.ConnectionOptions options, System.Threading.CancellationToken sessionCancellationToken) -> System.Threading.Tasks.Task<NuGet.Protocol.Plugins.IPlugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,6 @@ NuGet.Protocol.Core.Types.SearchFilter.IncludeDelisted.set -> void
NuGet.Protocol.Core.Types.SearchFilter.IncludePrerelease.get -> bool
NuGet.Protocol.Core.Types.SearchFilter.OrderBy.get -> NuGet.Protocol.Core.Types.SearchOrderBy?
NuGet.Protocol.Core.Types.SearchFilter.OrderBy.set -> void
~NuGet.Protocol.Core.Types.SearchFilter.PackageTypes.get -> System.Collections.Generic.IEnumerable<string>
~NuGet.Protocol.Core.Types.SearchFilter.PackageTypes.set -> void
NuGet.Protocol.Core.Types.SearchFilter.SearchFilter(bool includePrerelease) -> void
NuGet.Protocol.Core.Types.SearchFilter.SearchFilter(bool includePrerelease, NuGet.Protocol.Core.Types.SearchFilterType? filter) -> void
~NuGet.Protocol.Core.Types.SearchFilter.SupportedFrameworks.get -> System.Collections.Generic.IEnumerable<string>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#nullable enable
NuGet.Protocol.Plugins.PluginDiscoverer.PluginDiscoverer() -> void
virtual NuGet.Protocol.Plugins.PluginFactory.Dispose() -> void
~NuGet.Protocol.Core.Types.SearchFilter.PackageType.get -> string
~NuGet.Protocol.Core.Types.SearchFilter.PackageType.set -> void
~NuGet.Protocol.Plugins.PluginFile.PluginFile(string filePath, System.Lazy<NuGet.Protocol.Plugins.PluginFileState> state, bool requiresDotnetHost) -> void
~NuGet.Protocol.Plugins.PluginManager.PluginManager(NuGet.Common.IEnvironmentVariableReader reader, System.Lazy<NuGet.Protocol.Plugins.IPluginDiscoverer> pluginDiscoverer, System.Func<System.TimeSpan, NuGet.Protocol.Plugins.PluginFactory> pluginFactoryCreator, System.Lazy<string> pluginsCacheDirectoryPath) -> void
~virtual NuGet.Protocol.Plugins.PluginFactory.GetOrCreateAsync(NuGet.Protocol.Plugins.PluginFile pluginFile, System.Collections.Generic.IEnumerable<string> arguments, NuGet.Protocol.Plugins.IRequestHandlers requestHandlers, NuGet.Protocol.Plugins.ConnectionOptions options, System.Threading.CancellationToken sessionCancellationToken) -> System.Threading.Tasks.Task<NuGet.Protocol.Plugins.IPlugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,6 @@ NuGet.Protocol.Core.Types.SearchFilter.IncludeDelisted.set -> void
NuGet.Protocol.Core.Types.SearchFilter.IncludePrerelease.get -> bool
NuGet.Protocol.Core.Types.SearchFilter.OrderBy.get -> NuGet.Protocol.Core.Types.SearchOrderBy?
NuGet.Protocol.Core.Types.SearchFilter.OrderBy.set -> void
~NuGet.Protocol.Core.Types.SearchFilter.PackageTypes.get -> System.Collections.Generic.IEnumerable<string>
~NuGet.Protocol.Core.Types.SearchFilter.PackageTypes.set -> void
NuGet.Protocol.Core.Types.SearchFilter.SearchFilter(bool includePrerelease) -> void
NuGet.Protocol.Core.Types.SearchFilter.SearchFilter(bool includePrerelease, NuGet.Protocol.Core.Types.SearchFilterType? filter) -> void
~NuGet.Protocol.Core.Types.SearchFilter.SupportedFrameworks.get -> System.Collections.Generic.IEnumerable<string>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#nullable enable
NuGet.Protocol.Plugins.PluginDiscoverer.PluginDiscoverer() -> void
virtual NuGet.Protocol.Plugins.PluginFactory.Dispose() -> void
~NuGet.Protocol.Core.Types.SearchFilter.PackageType.get -> string
~NuGet.Protocol.Core.Types.SearchFilter.PackageType.set -> void
~NuGet.Protocol.Plugins.PluginFile.PluginFile(string filePath, System.Lazy<NuGet.Protocol.Plugins.PluginFileState> state, bool requiresDotnetHost) -> void
~NuGet.Protocol.Plugins.PluginManager.PluginManager(NuGet.Common.IEnvironmentVariableReader reader, System.Lazy<NuGet.Protocol.Plugins.IPluginDiscoverer> pluginDiscoverer, System.Func<System.TimeSpan, NuGet.Protocol.Plugins.PluginFactory> pluginFactoryCreator, System.Lazy<string> pluginsCacheDirectoryPath) -> void
~virtual NuGet.Protocol.Plugins.PluginFactory.GetOrCreateAsync(NuGet.Protocol.Plugins.PluginFile pluginFile, System.Collections.Generic.IEnumerable<string> arguments, NuGet.Protocol.Plugins.IRequestHandlers requestHandlers, NuGet.Protocol.Plugins.ConnectionOptions options, System.Threading.CancellationToken sessionCancellationToken) -> System.Threading.Tasks.Task<NuGet.Protocol.Plugins.IPlugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,9 @@ private async Task<T> SearchPage<T>(
queryString += "&" + frameworks;
}

if (filters.PackageTypes != null
&& filters.PackageTypes.Any())
if (!string.IsNullOrEmpty(filters.PackageType))
{
var types = string.Join("&",
filters.PackageTypes.Select(
s => "packageType=" + s));
queryString += "&" + types;
queryString += "&packageType=" + filters.PackageType;
}

queryString += "&semVerLevel=2.0.0";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,9 @@ public virtual async Task<JObject> SearchPage(string searchTerm, SearchFilter fi
queryString += "&" + frameworks;
}

if (filters.PackageTypes != null
&& filters.PackageTypes.Any())
if (!string.IsNullOrEmpty(filters.PackageType))
{
var types = string.Join("&",
filters.PackageTypes.Select(
s => "packageTypeFilter=" + s));
queryString += "&" + types;
queryString += "&packageType=" + filters.PackageType;
}

queryString += "&semVerLevel=2.0.0";
Expand Down
4 changes: 2 additions & 2 deletions src/NuGet.Core/NuGet.Protocol/SearchFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ public SearchFilter(bool includePrerelease, SearchFilterType? filter)
public bool IncludeDelisted { get; set; } = false;

/// <summary>
/// Restrict the search to certain package types.
/// Restrict the search to a specific package type.
/// </summary>
public IEnumerable<string> PackageTypes { get; set; } = Enumerable.Empty<string>();
public string PackageType { get; set; } = null;

/// <summary>
/// The optional filter type. Absense of this value indicates that all versions should be returned.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void SerializeThenDeserialize_WithValidArguments_RoundTrips(SearchFilter
Assert.Equal(expectedResult.IncludeDelisted, actualResult.IncludeDelisted);
Assert.Equal(expectedResult.IncludePrerelease, actualResult.IncludePrerelease);
Assert.Equal(expectedResult.OrderBy, actualResult.OrderBy);
Assert.Equal(expectedResult.PackageTypes, actualResult.PackageTypes);
Assert.Equal(expectedResult.PackageType, actualResult.PackageType);
Assert.Equal(expectedResult.SupportedFrameworks, actualResult.SupportedFrameworks);
}

Expand All @@ -31,7 +31,7 @@ public void SerializeThenDeserialize_WithValidArguments_RoundTrips(SearchFilter
{
IncludeDelisted = true,
OrderBy = SearchOrderBy.Id,
PackageTypes = new List<string>() { "packageType1", "packageType2" },
PackageType = "packageType",
SupportedFrameworks = new List<string>() { ".Net451", ".Net452" }
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public async Task LocalPackageSearchResource_MatchOnPackageTypeAsync()

var filter = new SearchFilter(includePrerelease: true)
{
PackageTypes = new[] { "test" }
PackageType = "test"
};

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public async Task PackageSearchResourceV3_SearchPackageType()

var searchFilter = new SearchFilter(includePrerelease: false)
{
PackageTypes = new[] { "test" }
PackageType = "test"
};

// Act
Expand Down

0 comments on commit cf99bb6

Please sign in to comment.