Exemplo n.º 1
0
    /// <summary>
    /// Prepare the invoke parameters. Adds default values if a parameter is missing.
    /// </summary>
    /// <param name="originalParameters">Original parameters.</param>
    /// <param name="executor">Handler executor.</param>
    /// <returns>Handler parameters.</returns>
    private object[] PrepareParameters(object[] originalParameters, HandlerExecutor executor)
    {
        if (!executor.MethodParameters.Any())
        {
            return(null);
        }

        var parameters = new object[executor.MethodParameters.Length];

        for (var i = 0; i < parameters.Length; i++)
        {
            ParameterInfo methodParameterInfo = executor.MethodParameters.ElementAt(i);

            if (i < originalParameters.Length)
            {
                Type originalObjectType = originalParameters[i]?.GetType();

                if (!methodParameterInfo.ParameterType.IsAssignableFrom(originalObjectType))
                {
                    object transformedParameter = _parameterTransformer?.Transform(originalParameters[i], methodParameterInfo.ParameterType.GetTypeInfo());

                    parameters[i] = transformedParameter ?? executor.GetDefaultValueForParameter(i);
                }
                else
                {
                    parameters[i] = originalParameters[i];
                }
            }
            else
            {
                parameters[i] = executor.GetDefaultValueForParameter(i);
            }
        }

        return(parameters);
    }
Exemplo n.º 2
0
        public static string ReplaceTokens(string template, IDictionary <string, string> values, IParameterTransformer routeTokenTransformer)
        {
            var builder = new StringBuilder();
            var state   = TemplateParserState.Plaintext;

            int?tokenStart = null;

            // We'll run the loop one extra time with 'null' to detect the end of the string.
            for (var i = 0; i <= template.Length; i++)
            {
                var c = i < template.Length ? (char?)template[i] : null;
                switch (state)
                {
                case TemplateParserState.Plaintext:
                    if (c == '[')
                    {
                        state = TemplateParserState.SeenLeft;
                        break;
                    }
                    else if (c == ']')
                    {
                        state = TemplateParserState.SeenRight;
                        break;
                    }
                    else if (c == null)
                    {
                        // We're at the end of the string, nothing left to do.
                        break;
                    }
                    else
                    {
                        builder.Append(c);
                        break;
                    }

                case TemplateParserState.SeenLeft:
                    if (c == '[')
                    {
                        // This is an escaped left-bracket
                        builder.Append(c);
                        state = TemplateParserState.Plaintext;
                        break;
                    }
                    else if (c == ']')
                    {
                        // This is zero-width parameter - not allowed.
                        var message = Resources.FormatAttributeRoute_TokenReplacement_InvalidSyntax(
                            template,
                            Resources.AttributeRoute_TokenReplacement_EmptyTokenNotAllowed);
                        throw new InvalidOperationException(message);
                    }
                    else if (c == null)
                    {
                        // This is a left-bracket at the end of the string.
                        var message = Resources.FormatAttributeRoute_TokenReplacement_InvalidSyntax(
                            template,
                            Resources.AttributeRoute_TokenReplacement_UnclosedToken);
                        throw new InvalidOperationException(message);
                    }
                    else
                    {
                        tokenStart = i;
                        state      = TemplateParserState.InsideToken;
                        break;
                    }

                case TemplateParserState.SeenRight:
                    if (c == ']')
                    {
                        // This is an escaped right-bracket
                        builder.Append(c);
                        state = TemplateParserState.Plaintext;
                        break;
                    }
                    else if (c == null)
                    {
                        // This is an imbalanced right-bracket at the end of the string.
                        var message = Resources.FormatAttributeRoute_TokenReplacement_InvalidSyntax(
                            template,
                            Resources.AttributeRoute_TokenReplacement_ImbalancedSquareBrackets);
                        throw new InvalidOperationException(message);
                    }
                    else
                    {
                        // This is an imbalanced right-bracket.
                        var message = Resources.FormatAttributeRoute_TokenReplacement_InvalidSyntax(
                            template,
                            Resources.AttributeRoute_TokenReplacement_ImbalancedSquareBrackets);
                        throw new InvalidOperationException(message);
                    }

                case TemplateParserState.InsideToken:
                    if (c == '[')
                    {
                        state = TemplateParserState.InsideToken | TemplateParserState.SeenLeft;
                        break;
                    }
                    else if (c == ']')
                    {
                        state = TemplateParserState.InsideToken | TemplateParserState.SeenRight;
                        break;
                    }
                    else if (c == null)
                    {
                        // This is an unclosed replacement token
                        var message = Resources.FormatAttributeRoute_TokenReplacement_InvalidSyntax(
                            template,
                            Resources.AttributeRoute_TokenReplacement_UnclosedToken);
                        throw new InvalidOperationException(message);
                    }
                    else
                    {
                        // This is a just part of the parameter
                        break;
                    }

                case TemplateParserState.InsideToken | TemplateParserState.SeenLeft:
                    if (c == '[')
                    {
                        // This is an escaped left-bracket
                        state = TemplateParserState.InsideToken;
                        break;
                    }
                    else
                    {
                        // Unescaped left-bracket is not allowed inside a token.
                        var message = Resources.FormatAttributeRoute_TokenReplacement_InvalidSyntax(
                            template,
                            Resources.AttributeRoute_TokenReplacement_UnescapedBraceInToken);
                        throw new InvalidOperationException(message);
                    }

                case TemplateParserState.InsideToken | TemplateParserState.SeenRight:
                    if (c == ']')
                    {
                        // This is an escaped right-bracket
                        state = TemplateParserState.InsideToken;
                        break;
                    }
                    else
                    {
                        // This is the end of a replacement token.
                        var token = template
                                    .Substring(tokenStart.Value, i - tokenStart.Value - 1)
                                    .Replace("[[", "[")
                                    .Replace("]]", "]");

                        if (!values.TryGetValue(token, out var value))
                        {
                            // Value not found
                            var message = Resources.FormatAttributeRoute_TokenReplacement_ReplacementValueNotFound(
                                template,
                                token,
                                string.Join(", ", values.Keys.OrderBy(k => k, StringComparer.OrdinalIgnoreCase)));
                            throw new InvalidOperationException(message);
                        }

                        if (routeTokenTransformer != null)
                        {
                            value = routeTokenTransformer.Transform(value);
                        }

                        builder.Append(value);

                        if (c == '[')
                        {
                            state = TemplateParserState.SeenLeft;
                        }
                        else if (c == ']')
                        {
                            state = TemplateParserState.SeenRight;
                        }
                        else if (c == null)
                        {
                            state = TemplateParserState.Plaintext;
                        }
                        else
                        {
                            builder.Append(c);
                            state = TemplateParserState.Plaintext;
                        }

                        tokenStart = null;
                        break;
                    }
                }
            }

            return(builder.ToString());
        }