Api clean up

This commit is contained in:
Kiran Challa 2018-08-02 16:33:07 -07:00
parent 091cb94094
commit 09ce9c3041
7 changed files with 57 additions and 141 deletions

View File

@ -3,12 +3,14 @@
using System;
using System.Globalization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Routing.Matching;
using Microsoft.Extensions.Logging;
namespace RoutingSample.Web
{
internal class EndsWithStringMatchProcessor : MatchProcessorBase
internal class EndsWithStringMatchProcessor : MatchProcessor
{
private readonly ILogger<EndsWithStringMatchProcessor> _logger;
@ -17,15 +19,34 @@ namespace RoutingSample.Web
_logger = logger;
}
public override bool Process(object value)
public string ParameterName { get; private set; }
public string ConstraintArgument { get; private set; }
public override void Initialize(string parameterName, string constraintArgument)
{
if (value == null)
ParameterName = parameterName;
ConstraintArgument = constraintArgument;
}
public override bool ProcessInbound(HttpContext httpContext, RouteValueDictionary values)
{
return Process(values);
}
public override bool ProcessOutbound(HttpContext httpContext, RouteValueDictionary values)
{
return Process(values);
}
private bool Process(RouteValueDictionary values)
{
if (!values.TryGetValue(ParameterName, out var value) || value == null)
{
return false;
}
var valueString = Convert.ToString(value, CultureInfo.InvariantCulture);
var endsWith = valueString.EndsWith(ConstraintArgument, StringComparison.OrdinalIgnoreCase);
if (!endsWith)

View File

@ -67,7 +67,6 @@ namespace Microsoft.Extensions.DependencyInjection
services.TryAddTransient<DfaMatcherBuilder>();
// Link generation related services
services.TryAddSingleton<IEndpointFinder<string>, NameBasedEndpointFinder>();
services.TryAddSingleton<IEndpointFinder<RouteValuesAddress>, RouteValuesBasedEndpointFinder>();
services.TryAddSingleton<LinkGenerator, DefaultLinkGenerator>();

View File

@ -1,10 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
namespace Microsoft.AspNetCore.Routing
{
public interface INameMetadata
{
string Name { get; }
}
}

View File

@ -1,42 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNetCore.Http;
namespace Microsoft.AspNetCore.Routing.Matching
{
public abstract class MatchProcessorBase : MatchProcessor
{
public string ParameterName { get; private set; }
public string ConstraintArgument { get; private set; }
public override void Initialize(string parameterName, string constraintArgument)
{
ParameterName = parameterName;
ConstraintArgument = constraintArgument;
}
public abstract bool Process(object value);
public override bool ProcessInbound(HttpContext httpContext, RouteValueDictionary values)
{
return Process(values);
}
public override bool ProcessOutbound(HttpContext httpContext, RouteValueDictionary values)
{
return Process(values);
}
private bool Process(RouteValueDictionary values)
{
if (!values.TryGetValue(ParameterName, out var value) || value == null)
{
return false;
}
return Process(value);
}
}
}

View File

@ -1,46 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Routing.Matching;
using Microsoft.Extensions.Logging;
namespace Microsoft.AspNetCore.Routing
{
internal class NameBasedEndpointFinder : IEndpointFinder<string>
{
private readonly CompositeEndpointDataSource _endpointDatasource;
private readonly ILogger<NameBasedEndpointFinder> _logger;
public NameBasedEndpointFinder(
CompositeEndpointDataSource endpointDataSource,
ILogger<NameBasedEndpointFinder> logger)
{
_endpointDatasource = endpointDataSource;
_logger = logger;
}
public IEnumerable<Endpoint> FindEndpoints(string endpointName)
{
if (string.IsNullOrEmpty(endpointName))
{
return Array.Empty<Endpoint>();
}
var endpoints = _endpointDatasource.Endpoints.OfType<MatcherEndpoint>();
foreach (var endpoint in endpoints)
{
var nameMetadata = endpoint.Metadata.GetMetadata<INameMetadata>();
if (nameMetadata != null &&
string.Equals(endpointName, nameMetadata.Name, StringComparison.OrdinalIgnoreCase))
{
return new[] { endpoint };
}
}
return Array.Empty<Endpoint>();
}
}
}

View File

@ -61,8 +61,8 @@ namespace Microsoft.AspNetCore.Routing.Matching
var factory = GetMatchProcessorFactory();
var parameter = RoutePatternFactory.ParameterPart(
"id",
@default: null,
"id",
@default: null,
parameterKind: RoutePatternParameterKind.Standard,
constraints: new[] { RoutePatternFactory.Constraint("int"), });
@ -282,12 +282,38 @@ namespace Microsoft.AspNetCore.Routing.Matching
}
}
private class EndsWithStringMatchProcessor : MatchProcessorBase
private class EndsWithStringMatchProcessor : MatchProcessor
{
public override bool Process(object value)
public string ParameterName { get; private set; }
public string ConstraintArgument { get; private set; }
public override void Initialize(string parameterName, string constraintArgument)
{
ParameterName = parameterName;
ConstraintArgument = constraintArgument;
}
public override bool ProcessInbound(HttpContext httpContext, RouteValueDictionary values)
{
return Process(values);
}
public override bool ProcessOutbound(HttpContext httpContext, RouteValueDictionary values)
{
return Process(values);
}
private bool Process(RouteValueDictionary values)
{
if (!values.TryGetValue(ParameterName, out var value) || value == null)
{
return false;
}
var valueString = Convert.ToString(value, CultureInfo.InvariantCulture);
return valueString.EndsWith(ConstraintArgument);
var endsWith = valueString.EndsWith(ConstraintArgument, StringComparison.OrdinalIgnoreCase);
return endsWith;
}
}
}

View File

@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
@ -79,29 +80,6 @@ namespace Microsoft.AspNetCore.Routing
Assert.Same(endpoint3, Assert.IsType<MatcherEndpoint>(namedMatches[1].Match.Entry.Data));
}
[Fact]
public void GetOutboundMatches_DoesNotGetNamedMatchesFor_EndpointsHaving_INameMetadata()
{
// Arrange
var endpoint1 = CreateEndpoint("/a");
var endpoint2 = CreateEndpoint("/a", routeName: "named");
var endpoint3 = CreateEndpoint(
"/b",
metadataCollection: new EndpointMetadataCollection(new[] { new NameMetadata("named") }));
// Act
var finder = CreateEndpointFinder(endpoint1, endpoint2);
// Assert
Assert.NotNull(finder.AllMatches);
Assert.Equal(2, finder.AllMatches.Count());
Assert.NotNull(finder.NamedMatches);
Assert.True(finder.NamedMatches.TryGetValue("named", out var namedMatches));
var namedMatch = Assert.Single(namedMatches);
var actual = Assert.IsType<MatcherEndpoint>(namedMatch.Match.Entry.Data);
Assert.Same(endpoint2, actual);
}
[Fact]
public void EndpointDataSource_ChangeCallback_Refreshes_OutboundMatches()
{
@ -279,16 +257,6 @@ namespace Microsoft.AspNetCore.Routing
null);
}
private class NameMetadata : INameMetadata
{
public NameMetadata(string name)
{
Name = name;
}
public string Name { get; }
}
private class CustomRouteValuesBasedEndpointFinder : RouteValuesBasedEndpointFinder
{
public CustomRouteValuesBasedEndpointFinder(