Ejemplo n.º 1
0
        /// <summary>
        /// Constructs a new <see cref="TemplatePart"/> instance given a <paramref name="other"/>.
        /// </summary>
        /// <param name="other">A <see cref="RoutePatternPart"/> instance representing the route part.</param>
        public TemplatePart(RoutePatternPart other)
        {
            IsLiteral   = other.IsLiteral || other.IsSeparator;
            IsParameter = other.IsParameter;

            if (other.IsLiteral && other is RoutePatternLiteralPart literal)
            {
                Text = literal.Content;
            }
            else if (other.IsParameter && other is RoutePatternParameterPart parameter)
            {
                // Text is unused by TemplatePart and assumed to be null when the part is a parameter.
                Name              = parameter.Name;
                IsCatchAll        = parameter.IsCatchAll;
                IsOptional        = parameter.IsOptional;
                DefaultValue      = parameter.Default;
                InlineConstraints = parameter.ParameterPolicies?.Select(p => new InlineConstraint(p)) ?? Enumerable.Empty <InlineConstraint>();
            }
            else if (other.IsSeparator && other is RoutePatternSeparatorPart separator)
            {
                Text = separator.Content;
                IsOptionalSeperator = true;
            }
            else
            {
                // Unreachable
                throw new NotSupportedException();
            }
        }
 private static string BuildRoutePart(RoutePatternPart part)
 {
     return(part switch
     {
         RoutePatternLiteralPart lit => lit.Content,
         RoutePatternParameterPart param => $"{{{param.Name}}}",
         RoutePatternSeparatorPart sep => sep.Content,
         _ => string.Empty
     });
Ejemplo n.º 3
0
 static string ToString(RoutePatternPart part)
 {
     if (part is RoutePatternLiteralPart literal)
     {
         return($"Literal: {literal.Content}");
     }
     if (part is RoutePatternSeparatorPart separator)
     {
         return($"Separator: {separator.Content}");
     }
     else
     {
         var parameter = (RoutePatternParameterPart)part;
         return($"Parameter: Name = {parameter.Name}; Default = {parameter.Default}; IsOptional = { parameter.IsOptional}; IsCatchAll = { parameter.IsCatchAll}; ParameterKind = { parameter.ParameterKind}");
     }
 }
        private string GetRoutePatternPartPropertyValue(RoutePatternPart part, out bool isOptional)
        {
            var value = string.Empty;

            if (part is RoutePatternLiteralPart literalPart)
            {
                value = literalPart.Content;
            }
            else if (part is RoutePatternParameterPart parameterPart)
            {
                value = parameterPart.Name;
            }

            bool.TryParse(part.GetType().GetProperty("IsOptional")?.GetValue(part).ToString(), out isOptional);

            return(value);
        }
Ejemplo n.º 5
0
 private static void WriteString(StringBuilder sb, RoutePatternPart part)
 {
     if (part.IsParameter && part is RoutePatternParameterPart parameterPart)
     {
         sb.Append('{');
         if (parameterPart.IsCatchAll)
         {
             sb.Append('*');
             if (!parameterPart.EncodeSlashes)
             {
                 sb.Append('*');
             }
         }
         sb.Append(parameterPart.Name);
         foreach (var item in parameterPart.ParameterPolicies)
         {
             sb.Append(':');
             sb.Append(item.Content);
         }
         if (parameterPart.Default != null)
         {
             sb.Append('=');
             sb.Append(parameterPart.Default);
         }
         if (parameterPart.IsOptional)
         {
             sb.Append('?');
         }
         sb.Append('}');
     }
     else if (part is RoutePatternLiteralPart literalPart)
     {
         sb.Append(literalPart.Content);
     }
     else if (part is RoutePatternSeparatorPart separatorPart)
     {
         sb.Append(separatorPart.Content);
     }
     else
     {
         throw new NotSupportedException();
     }
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Converts the <see cref="RouteTemplate"/> to the equivalent
        /// <see cref="RoutePattern"/>
        /// </summary>
        /// <returns>A <see cref="RoutePattern"/>.</returns>
        public Other ToRoutePattern()
        {
            var builder = RoutePatternBuilder.Create(TemplateText);

            for (var i = 0; i < Segments.Count; i++)
            {
                var segment = Segments[i];

                var parts = new List <RoutePatternPart>();
                for (var j = 0; j < segment.Parts.Count; j++)
                {
                    var part = segment.Parts[j];
                    if (part.IsLiteral && part.IsOptionalSeperator)
                    {
                        parts.Add(RoutePatternPart.CreateSeparator(part.Text));
                    }
                    else if (part.IsLiteral)
                    {
                        parts.Add(RoutePatternPart.CreateLiteral(part.Text));
                    }
                    else
                    {
                        var kind = part.IsCatchAll ?
                                   RoutePatternParameterKind.CatchAll :
                                   part.IsOptional ?
                                   RoutePatternParameterKind.Optional :
                                   RoutePatternParameterKind.Standard;

                        var constraints = part.InlineConstraints.Select(c => ConstraintReference.Create(c.Constraint)).ToArray();

                        parts.Add(RoutePatternPart.CreateParameter(part.Name, part.DefaultValue, kind, constraints));
                    }
                }

                builder.AddPathSegment(parts.ToArray());
            }

            return(builder.Build());
        }
Ejemplo n.º 7
0
        private bool Equals(RoutePatternPart x, RoutePatternPart y)
        {
            if (x.GetType() != y.GetType())
            {
                return(false);
            }

            if (x.IsLiteral && y.IsLiteral)
            {
                return(Equals((RoutePatternLiteralPart)x, (RoutePatternLiteralPart)y));
            }
            else if (x.IsParameter && y.IsParameter)
            {
                return(Equals((RoutePatternParameterPart)x, (RoutePatternParameterPart)y));
            }
            else if (x.IsSeparator && y.IsSeparator)
            {
                return(Equals((RoutePatternSeparatorPart)x, (RoutePatternSeparatorPart)y));
            }

            Debug.Fail("This should not be reachable. Do you need to update the comparison logic?");
            return(false);
        }