From 7f8ba171f4ae3dbb052e278f1f3d90fedaf0a2b8 Mon Sep 17 00:00:00 2001 From: Jass Bagga Date: Mon, 13 Nov 2017 11:40:26 -0800 Subject: [PATCH] Move UrlMatchingTree to shared source (#492) Addresses #489 --- NuGetPackageVerifier.json | 3 +- Routing.sln | 4 +- .../InboundMatch.cs | 39 ++++++++- .../InboundRouteEntry.cs | 82 ++++++++++++++----- .../UrlMatchingNode.cs | 55 +++++++++---- .../UrlMatchingTree.cs | 20 +++-- .../Microsoft.AspNetCore.Dispatcher.csproj | 5 ++ .../Tree/InboundMatch.cs | 29 ------- .../Tree/InboundRouteEntry.cs | 51 ------------ .../Tree/TreeMatcher.cs | 6 +- .../Tree/UrlMatchingNode.cs | 81 ------------------ .../Tree/UrlMatchingTree.cs | 30 ------- .../Microsoft.AspNetCore.Routing.csproj | 2 + 13 files changed, 169 insertions(+), 238 deletions(-) rename {src/Microsoft.AspNetCore.Routing/Tree => shared/Microsoft.AspNetCore.Routing.UrlMatchingTree.Sources}/InboundMatch.cs (57%) rename {src/Microsoft.AspNetCore.Routing/Tree => shared/Microsoft.AspNetCore.Routing.UrlMatchingTree.Sources}/InboundRouteEntry.cs (58%) rename {src/Microsoft.AspNetCore.Routing/Tree => shared/Microsoft.AspNetCore.Routing.UrlMatchingTree.Sources}/UrlMatchingNode.cs (71%) rename {src/Microsoft.AspNetCore.Routing/Tree => shared/Microsoft.AspNetCore.Routing.UrlMatchingTree.Sources}/UrlMatchingTree.cs (81%) delete mode 100644 src/Microsoft.AspNetCore.Dispatcher/Tree/InboundMatch.cs delete mode 100644 src/Microsoft.AspNetCore.Dispatcher/Tree/InboundRouteEntry.cs delete mode 100644 src/Microsoft.AspNetCore.Dispatcher/Tree/UrlMatchingNode.cs delete mode 100644 src/Microsoft.AspNetCore.Dispatcher/Tree/UrlMatchingTree.cs diff --git a/NuGetPackageVerifier.json b/NuGetPackageVerifier.json index 9ed455034c..fab2174a91 100644 --- a/NuGetPackageVerifier.json +++ b/NuGetPackageVerifier.json @@ -2,7 +2,8 @@ "adx-nonshipping": { "rules": [], "packages": { - "Microsoft.AspNetCore.Routing.DecisionTree.Sources": {} + "Microsoft.AspNetCore.Routing.DecisionTree.Sources": {}, + "Microsoft.AspNetCore.Routing.UrlMatchingTree.Sources": {} } }, "Default": { diff --git a/Routing.sln b/Routing.sln index 4e09536d3e..32219e6abc 100644 --- a/Routing.sln +++ b/Routing.sln @@ -1,6 +1,6 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.26927.0 +VisualStudioVersion = 15.0.27106.3000 MinimumVisualStudioVersion = 15.0.26730.03 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{0E966C37-7334-4D96-AAF6-9F49FBD166E3}" ProjectSection(SolutionItems) = preProject @@ -57,7 +57,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DispatcherSample", "samples EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Dispatcher.Performance", "benchmarks\Microsoft.AspNetCore.Dispatcher.Performance\Microsoft.AspNetCore.Dispatcher.Performance.csproj", "{30AF355D-E3AB-4FF5-8A59-A253AFEBA26A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AspNetCore.Dispatcher.FunctionalTest", "test\Microsoft.AspNetCore.Dispatcher.FunctionalTest\Microsoft.AspNetCore.Dispatcher.FunctionalTest.csproj", "{32107601-C9BE-467B-894C-C9F2E35F03E4}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Dispatcher.FunctionalTest", "test\Microsoft.AspNetCore.Dispatcher.FunctionalTest\Microsoft.AspNetCore.Dispatcher.FunctionalTest.csproj", "{32107601-C9BE-467B-894C-C9F2E35F03E4}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/src/Microsoft.AspNetCore.Routing/Tree/InboundMatch.cs b/shared/Microsoft.AspNetCore.Routing.UrlMatchingTree.Sources/InboundMatch.cs similarity index 57% rename from src/Microsoft.AspNetCore.Routing/Tree/InboundMatch.cs rename to shared/Microsoft.AspNetCore.Routing.UrlMatchingTree.Sources/InboundMatch.cs index 57f1b6db7b..4e20ef93e6 100644 --- a/src/Microsoft.AspNetCore.Routing/Tree/InboundMatch.cs +++ b/shared/Microsoft.AspNetCore.Routing.UrlMatchingTree.Sources/InboundMatch.cs @@ -2,21 +2,42 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Diagnostics; +#if ROUTING using Microsoft.AspNetCore.Routing.Template; +#elif DISPATCHER +using Microsoft.AspNetCore.Dispatcher; +#else +#error +#endif +#if ROUTING namespace Microsoft.AspNetCore.Routing.Tree +#elif DISPATCHER +namespace Microsoft.AspNetCore.Dispatcher +#else +#error +#endif { +#if ROUTING /// /// A candidate route to match incoming URLs in a . /// [DebuggerDisplay("{DebuggerToString(),nq}")] - public class InboundMatch + public +#elif DISPATCHER + [DebuggerDisplay("{DebuggerToString(),nq}")] + internal +#else +#error +#endif + class InboundMatch { /// /// Gets or sets the . /// public InboundRouteEntry Entry { get; set; } +#if ROUTING /// /// Gets or sets the . /// @@ -26,5 +47,19 @@ namespace Microsoft.AspNetCore.Routing.Tree { return TemplateMatcher?.Template?.TemplateText; } - } + +#elif DISPATCHER + /// + /// Gets or sets the . + /// + public RoutePatternMatcher RoutePatternMatcher { get; set; } + + private string DebuggerToString() + { + return RoutePatternMatcher?.RoutePattern?.RawText; + } +#else +#error +#endif +} } diff --git a/src/Microsoft.AspNetCore.Routing/Tree/InboundRouteEntry.cs b/shared/Microsoft.AspNetCore.Routing.UrlMatchingTree.Sources/InboundRouteEntry.cs similarity index 58% rename from src/Microsoft.AspNetCore.Routing/Tree/InboundRouteEntry.cs rename to shared/Microsoft.AspNetCore.Routing.UrlMatchingTree.Sources/InboundRouteEntry.cs index 67db0d5ad5..ee76a752af 100644 --- a/src/Microsoft.AspNetCore.Routing/Tree/InboundRouteEntry.cs +++ b/shared/Microsoft.AspNetCore.Routing.UrlMatchingTree.Sources/InboundRouteEntry.cs @@ -2,31 +2,40 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Collections.Generic; +using Microsoft.AspNetCore.Dispatcher.Patterns; +#if ROUTING using Microsoft.AspNetCore.Routing.Template; +#elif DISPATCHER +using Microsoft.AspNetCore.Dispatcher; +#else +#error +#endif +#if ROUTING namespace Microsoft.AspNetCore.Routing.Tree +#elif DISPATCHER +namespace Microsoft.AspNetCore.Dispatcher +#else +#error +#endif { +#if ROUTING /// - /// Used to build an . Represents a URL template tha will be used to match incoming + /// Used to build a . Represents a route template that will be used to match incoming /// request URLs. /// - public class InboundRouteEntry + public +#elif DISPATCHER + /// + /// Used to build a . Represents a route pattern that will be used to match incoming + /// request URLs. + /// + internal +#else +#error +#endif + class InboundRouteEntry { - /// - /// Gets or sets the route constraints. - /// - public IDictionary Constraints { get; set; } - - /// - /// Gets or sets the route defaults. - /// - public RouteValueDictionary Defaults { get; set; } - - /// - /// Gets or sets the to invoke when this entry matches. - /// - public IRouter Handler { get; set; } - /// /// Gets or sets the order of the entry. /// @@ -43,19 +52,54 @@ namespace Microsoft.AspNetCore.Routing.Tree /// public decimal Precedence { get; set; } +#if ROUTING /// /// Gets or sets the name of the route. /// public string RouteName { get; set; } + /// + /// Gets or sets the route constraints. + /// + public IDictionary Constraints { get; set; } + /// /// Gets or sets the . /// public RouteTemplate RouteTemplate { get; set; } /// - /// Gets or sets an arbitrary value associated with the entry. + /// Gets or sets the route defaults. /// - public object Tag { get; set; } + public RouteValueDictionary Defaults { get; set; } + + /// + /// Gets or sets the to invoke when this entry matches. + /// + public IRouter Handler { get; set; } + +#elif DISPATCHER + /// + /// Gets or sets the array of endpoints associated with the entry. + /// + public Endpoint[] Endpoints { get; set; } + + /// + /// Gets or sets the dispatcher value constraints. + /// + public IDictionary Constraints { get; set; } + + /// + /// Gets or sets the dispatcher value defaults. + /// + public DispatcherValueCollection Defaults { get; set; } + + /// + /// Gets or sets the . + /// + public RoutePattern RoutePattern { get; set; } +#else +#error +#endif } } diff --git a/src/Microsoft.AspNetCore.Routing/Tree/UrlMatchingNode.cs b/shared/Microsoft.AspNetCore.Routing.UrlMatchingTree.Sources/UrlMatchingNode.cs similarity index 71% rename from src/Microsoft.AspNetCore.Routing/Tree/UrlMatchingNode.cs rename to shared/Microsoft.AspNetCore.Routing.UrlMatchingTree.Sources/UrlMatchingNode.cs index ffc387efe9..ca0f9b80fa 100644 --- a/src/Microsoft.AspNetCore.Routing/Tree/UrlMatchingNode.cs +++ b/shared/Microsoft.AspNetCore.Routing.UrlMatchingTree.Sources/UrlMatchingNode.cs @@ -6,12 +6,16 @@ using System.Collections.Generic; using System.Diagnostics; using System.Linq; +#if ROUTING namespace Microsoft.AspNetCore.Routing.Tree +#elif DISPATCHER +namespace Microsoft.AspNetCore.Dispatcher +#else +#error +#endif { - /// - /// A node in a . - /// [DebuggerDisplay("{DebuggerToString(),nq}")] +#if ROUTING public class UrlMatchingNode { /// @@ -25,11 +29,37 @@ namespace Microsoft.AspNetCore.Routing.Tree Matches = new List(); Literals = new Dictionary(StringComparer.OrdinalIgnoreCase); } - + + private string DebuggerToString() + { + return $"Length: {Depth}, Matches: {string.Join(" | ", Matches?.Select(m => $"({m.TemplateMatcher.Template.TemplateText})"))}"; + } +#elif DISPATCHER + internal class UrlMatchingNode + { /// - /// Gets the length of the path to this node in the . + /// Initializes a new instance of . /// - public int Depth { get; } + /// The length of the path to this node in the . + public UrlMatchingNode(int depth) + { + Depth = depth; + + Matches = new List(); + Literals = new Dictionary(StringComparer.OrdinalIgnoreCase); + } + + private string DebuggerToString() + { + return $"Length: {Depth}, Matches: {string.Join(" | ", Matches?.Select(m => $"({m.RoutePatternMatcher.RoutePattern.RawText})"))}"; + } +#else +#error +#endif + /// + /// Gets the length of the path to this node in the . + /// + public int Depth { get; } /// /// Gets or sets a value indicating whether this node represents a catch all segment. @@ -51,31 +81,26 @@ namespace Microsoft.AspNetCore.Routing.Tree /// /// Gets or sets the representing - /// parameter segments with constraints following this segment in the . + /// parameter segments with constraints following this segment. /// public UrlMatchingNode ConstrainedParameters { get; set; } /// /// Gets or sets the representing - /// parameter segments following this segment in the . + /// parameter segments following this segment. /// public UrlMatchingNode Parameters { get; set; } /// /// Gets or sets the representing - /// catch all parameter segments with constraints following this segment in the . + /// catch all parameter segments with constraints following this segment. /// public UrlMatchingNode ConstrainedCatchAlls { get; set; } /// /// Gets or sets the representing - /// catch all parameter segments following this segment in the . + /// catch all parameter segments following this segment. /// public UrlMatchingNode CatchAlls { get; set; } - - private string DebuggerToString() - { - return $"Length: {Depth}, Matches: {string.Join(" | ", Matches?.Select(m => $"({m.TemplateMatcher.Template.TemplateText})"))}"; - } } } diff --git a/src/Microsoft.AspNetCore.Routing/Tree/UrlMatchingTree.cs b/shared/Microsoft.AspNetCore.Routing.UrlMatchingTree.Sources/UrlMatchingTree.cs similarity index 81% rename from src/Microsoft.AspNetCore.Routing/Tree/UrlMatchingTree.cs rename to shared/Microsoft.AspNetCore.Routing.UrlMatchingTree.Sources/UrlMatchingTree.cs index 90528d75b9..d3aa7dba05 100644 --- a/src/Microsoft.AspNetCore.Routing/Tree/UrlMatchingTree.cs +++ b/shared/Microsoft.AspNetCore.Routing.UrlMatchingTree.Sources/UrlMatchingTree.cs @@ -1,12 +1,22 @@ // 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. +#if ROUTING namespace Microsoft.AspNetCore.Routing.Tree +#elif DISPATCHER +namespace Microsoft.AspNetCore.Dispatcher +#else +#error +#endif { - /// - /// A tree part of a . - /// - public class UrlMatchingTree +#if ROUTING + public +#elif DISPATCHER + internal +#else +#error +#endif + class UrlMatchingTree { /// /// Initializes a new instance of . @@ -25,6 +35,6 @@ namespace Microsoft.AspNetCore.Routing.Tree /// /// Gets the root of the . /// - public UrlMatchingNode Root { get; } = new UrlMatchingNode(length: 0); + public UrlMatchingNode Root { get; } = new UrlMatchingNode(0); } } diff --git a/src/Microsoft.AspNetCore.Dispatcher/Microsoft.AspNetCore.Dispatcher.csproj b/src/Microsoft.AspNetCore.Dispatcher/Microsoft.AspNetCore.Dispatcher.csproj index bbde532177..9b48781a72 100644 --- a/src/Microsoft.AspNetCore.Dispatcher/Microsoft.AspNetCore.Dispatcher.csproj +++ b/src/Microsoft.AspNetCore.Dispatcher/Microsoft.AspNetCore.Dispatcher.csproj @@ -3,11 +3,16 @@ API for dispatching. netstandard2.0 + $(DefineConstants);DISPATCHER $(NoWarn);CS1591 true aspnetcore;routing + + + + diff --git a/src/Microsoft.AspNetCore.Dispatcher/Tree/InboundMatch.cs b/src/Microsoft.AspNetCore.Dispatcher/Tree/InboundMatch.cs deleted file mode 100644 index 018fb02e9e..0000000000 --- a/src/Microsoft.AspNetCore.Dispatcher/Tree/InboundMatch.cs +++ /dev/null @@ -1,29 +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.Diagnostics; - -namespace Microsoft.AspNetCore.Dispatcher -{ - /// - /// A candidate endpoint to match incoming URLs in a TreeMatcher. - /// - [DebuggerDisplay("{DebuggerToString(),nq}")] - public class InboundMatch - { - /// - /// Gets or sets the . - /// - public InboundRouteEntry Entry { get; set; } - - /// - /// Gets or sets the . - /// - public RoutePatternMatcher RoutePatternMatcher { get; set; } - - private string DebuggerToString() - { - return RoutePatternMatcher?.RoutePattern?.RawText; - } - } -} diff --git a/src/Microsoft.AspNetCore.Dispatcher/Tree/InboundRouteEntry.cs b/src/Microsoft.AspNetCore.Dispatcher/Tree/InboundRouteEntry.cs deleted file mode 100644 index e54d626d80..0000000000 --- a/src/Microsoft.AspNetCore.Dispatcher/Tree/InboundRouteEntry.cs +++ /dev/null @@ -1,51 +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.Collections.Generic; -using Microsoft.AspNetCore.Dispatcher.Patterns; - -namespace Microsoft.AspNetCore.Dispatcher -{ - /// - /// Used to build a . Represents a route pattern that will be used to match incoming - /// request URLs. - /// - public class InboundRouteEntry - { - /// - /// Gets or sets the dispatcher value constraints. - /// - public IDictionary Constraints { get; set; } - - /// - /// Gets or sets the dispatcher value defaults. - /// - public DispatcherValueCollection Defaults { get; set; } - - /// - /// Gets or sets the order of the entry. - /// - /// - /// Entries are ordered first by (ascending) then by (descending). - /// - public int Order { get; set; } - - /// - /// Gets or sets the precedence of the entry. - /// - /// - /// Entries are ordered first by (ascending) then by (descending). - /// - public decimal Precedence { get; set; } - - /// - /// Gets or sets the . - /// - public RoutePattern RoutePattern { get; set; } - - /// - /// Gets or sets an arbitrary value associated with the entry. - /// - public object Tag { get; set; } - } -} diff --git a/src/Microsoft.AspNetCore.Dispatcher/Tree/TreeMatcher.cs b/src/Microsoft.AspNetCore.Dispatcher/Tree/TreeMatcher.cs index c53c2aa6fa..ea96a14509 100644 --- a/src/Microsoft.AspNetCore.Dispatcher/Tree/TreeMatcher.cs +++ b/src/Microsoft.AspNetCore.Dispatcher/Tree/TreeMatcher.cs @@ -75,7 +75,7 @@ namespace Microsoft.AspNetCore.Dispatcher continue; } - await SelectEndpointAsync(context, (Endpoint[])entry.Tag); + await SelectEndpointAsync(context, (entry.Endpoints)); if (context.ShortCircuit != null) { Logger.RequestShortCircuited(context); @@ -184,7 +184,7 @@ namespace Microsoft.AspNetCore.Dispatcher private InboundRouteEntry MapInbound( RoutePattern routePattern, - object tag, + Endpoint[] endpoints, int order) { if (routePattern == null) @@ -197,7 +197,7 @@ namespace Microsoft.AspNetCore.Dispatcher Precedence = RoutePrecedence.ComputeInbound(routePattern), RoutePattern = routePattern, Order = order, - Tag = tag + Endpoints = endpoints, }; var constraintBuilder = new DispatcherValueConstraintBuilder(_constraintFactory, routePattern.RawText); diff --git a/src/Microsoft.AspNetCore.Dispatcher/Tree/UrlMatchingNode.cs b/src/Microsoft.AspNetCore.Dispatcher/Tree/UrlMatchingNode.cs deleted file mode 100644 index 84150fde77..0000000000 --- a/src/Microsoft.AspNetCore.Dispatcher/Tree/UrlMatchingNode.cs +++ /dev/null @@ -1,81 +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.Diagnostics; -using System.Linq; - -namespace Microsoft.AspNetCore.Dispatcher -{ - /// - /// A node in a . - /// - [DebuggerDisplay("{DebuggerToString(),nq}")] - public class UrlMatchingNode - { - /// - /// Initializes a new instance of . - /// - /// The length of the path to this node in the . - public UrlMatchingNode(int depth) - { - Depth = depth; - - Matches = new List(); - Literals = new Dictionary(StringComparer.OrdinalIgnoreCase); - } - - /// - /// Gets the length of the path to this node in the . - /// - public int Depth { get; } - - /// - /// Gets or sets a value indicating whether this node represents a catch all segment. - /// - public bool IsCatchAll { get; set; } - - /// - /// Gets the list of matching route entries associated with this node. - /// - /// - /// These entries are sorted by precedence then template. - /// - public List Matches { get; } - - /// - /// Gets the literal segments following this segment. - /// - public Dictionary Literals { get; } - - /// - /// Gets or sets the representing - /// parameter segments with constraints following this segment in the . - /// - public UrlMatchingNode ConstrainedParameters { get; set; } - - /// - /// Gets or sets the representing - /// parameter segments following this segment in the . - /// - public UrlMatchingNode Parameters { get; set; } - - /// - /// Gets or sets the representing - /// catch all parameter segments with constraints following this segment in the . - /// - public UrlMatchingNode ConstrainedCatchAlls { get; set; } - - /// - /// Gets or sets the representing - /// catch all parameter segments following this segment in the . - /// - public UrlMatchingNode CatchAlls { get; set; } - - private string DebuggerToString() - { - return $"Length: {Depth}, Matches: {string.Join(" | ", Matches?.Select(m => $"({m.RoutePatternMatcher.RoutePattern.RawText})"))}"; - } - } -} diff --git a/src/Microsoft.AspNetCore.Dispatcher/Tree/UrlMatchingTree.cs b/src/Microsoft.AspNetCore.Dispatcher/Tree/UrlMatchingTree.cs deleted file mode 100644 index 5685b2aa71..0000000000 --- a/src/Microsoft.AspNetCore.Dispatcher/Tree/UrlMatchingTree.cs +++ /dev/null @@ -1,30 +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.Dispatcher -{ - /// - /// A tree part of a . - /// - public class UrlMatchingTree - { - /// - /// Initializes a new instance of . - /// - /// The order associated with endpoints in this . - public UrlMatchingTree(int order) - { - Order = order; - } - - /// - /// Gets the order of the endpoints associated with this . - /// - public int Order { get; } - - /// - /// Gets the root of the . - /// - public UrlMatchingNode Root { get; } = new UrlMatchingNode(depth: 0); - } -} diff --git a/src/Microsoft.AspNetCore.Routing/Microsoft.AspNetCore.Routing.csproj b/src/Microsoft.AspNetCore.Routing/Microsoft.AspNetCore.Routing.csproj index 5ecd1df412..1319abc7d9 100644 --- a/src/Microsoft.AspNetCore.Routing/Microsoft.AspNetCore.Routing.csproj +++ b/src/Microsoft.AspNetCore.Routing/Microsoft.AspNetCore.Routing.csproj @@ -6,6 +6,7 @@ Commonly used types: Microsoft.AspNetCore.Routing.Route Microsoft.AspNetCore.Routing.RouteCollection netstandard2.0 + $(DefineConstants);ROUTING $(NoWarn);CS1591 true aspnetcore;routing @@ -13,6 +14,7 @@ Microsoft.AspNetCore.Routing.RouteCollection +