private MemberDeclarationSyntax CreateInvokeExecuteAsyncMethod(
        string parameterTypeName,
        string resultTypeName,
        bool hasParameters)
    {
        var arguments = hasParameters
            ? new SyntaxNodeOrToken[]
        {
            SyntaxParameterFactory.Create(parameterTypeName, "parameters"),
            SyntaxTokenFactory.Comma(),
            SyntaxParameterFactory.Create(nameof(CancellationToken), nameof(CancellationToken).EnsureFirstCharacterToLower()),
        }
            : new SyntaxNodeOrToken[]
        {
            SyntaxParameterFactory.Create(nameof(CancellationToken), nameof(CancellationToken).EnsureFirstCharacterToLower()),
        };

        var bodyBlockSyntaxStatements = CreateBodyBlockSyntaxStatements(resultTypeName);

        return(SyntaxFactory.MethodDeclaration(
                   SyntaxFactory.GenericName(SyntaxFactory.Identifier(nameof(Task)))
                   .WithTypeArgumentList(
                       SyntaxFactory.TypeArgumentList(
                           SyntaxFactory.SingletonSeparatedList <TypeSyntax>(
                               SyntaxFactory.IdentifierName(EndpointResultTypeName)))),
                   SyntaxFactory.Identifier("InvokeExecuteAsync"))
               .WithModifiers(SyntaxTokenListFactory.PrivateAsyncKeyword())
               .WithParameterList(
                   SyntaxFactory.ParameterList(SyntaxFactory.SeparatedList <ParameterSyntax>(arguments)))
               .WithBody(
                   SyntaxFactory.Block(bodyBlockSyntaxStatements)));
    }
Пример #2
0
    public static MethodDeclarationSyntax CreateInterfaceMethod(
        string parameterTypeName,
        string resultTypeName,
        bool hasParameters)
    {
        ArgumentNullException.ThrowIfNull(parameterTypeName);
        ArgumentNullException.ThrowIfNull(resultTypeName);

        var arguments = hasParameters
            ? new SyntaxNodeOrToken[]
        {
            SyntaxParameterFactory.Create(parameterTypeName, "parameters"),
            SyntaxTokenFactory.Comma(),
            SyntaxParameterFactory.Create(nameof(CancellationToken), nameof(CancellationToken).EnsureFirstCharacterToLower())
            .WithDefault(SyntaxFactory.EqualsValueClause(
                             SyntaxFactory.LiteralExpression(SyntaxKind.DefaultLiteralExpression, SyntaxTokenFactory.DefaultKeyword()))),
        }
            : new SyntaxNodeOrToken[]
        {
            SyntaxParameterFactory.Create(nameof(CancellationToken), nameof(CancellationToken).EnsureFirstCharacterToLower())
            .WithDefault(SyntaxFactory.EqualsValueClause(
                             SyntaxFactory.LiteralExpression(SyntaxKind.DefaultLiteralExpression, SyntaxTokenFactory.DefaultKeyword()))),
        };

        return(SyntaxFactory.MethodDeclaration(
                   SyntaxFactory.GenericName(SyntaxFactory.Identifier(nameof(Task)))
                   .WithTypeArgumentList(SyntaxTypeArgumentListFactory.CreateWithOneItem(resultTypeName)),
                   SyntaxFactory.Identifier("ExecuteAsync"))
               .WithParameterList(SyntaxFactory.ParameterList(SyntaxFactory.SeparatedList <ParameterSyntax>(arguments)))
               .WithSemicolonToken(SyntaxTokenFactory.Semicolon()));
    }
Пример #3
0
    private MemberDeclarationSyntax CreateExecuteAsyncMethod(
        string parameterTypeName,
        bool hasParameters)
    {
        var arguments = hasParameters
            ? new SyntaxNodeOrToken[]
        {
            SyntaxParameterFactory.Create(parameterTypeName, "parameters"),
            SyntaxTokenFactory.Comma(),
            SyntaxParameterFactory.Create(nameof(CancellationToken), nameof(CancellationToken).EnsureFirstCharacterToLower())
            .WithDefault(SyntaxFactory.EqualsValueClause(
                             SyntaxFactory.LiteralExpression(SyntaxKind.DefaultLiteralExpression, SyntaxTokenFactory.DefaultKeyword()))),
        }
            : new SyntaxNodeOrToken[]
        {
            SyntaxParameterFactory.Create(nameof(CancellationToken), nameof(CancellationToken).EnsureFirstCharacterToLower())
            .WithDefault(SyntaxFactory.EqualsValueClause(
                             SyntaxFactory.LiteralExpression(SyntaxKind.DefaultLiteralExpression, SyntaxTokenFactory.DefaultKeyword()))),
        };

        return(SyntaxFactory.MethodDeclaration(
                   SyntaxFactory.GenericName(SyntaxFactory.Identifier(nameof(Task)))
                   .WithTypeArgumentList(
                       SyntaxFactory.TypeArgumentList(
                           SyntaxFactory.SingletonSeparatedList <TypeSyntax>(
                               SyntaxFactory.IdentifierName(EndpointResultTypeName)))),
                   SyntaxFactory.Identifier("ExecuteAsync"))
               .WithModifiers(SyntaxTokenListFactory.PublicKeyword())
               .WithParameterList(SyntaxFactory.ParameterList(SyntaxFactory.SeparatedList <ParameterSyntax>(arguments)))
               .WithSemicolonToken(SyntaxTokenFactory.Semicolon()));
    }
Пример #4
0
        private MemberDeclarationSyntax CreateInvokeExecuteAsyncMethod(string parameterTypeName, string resultTypeName, bool hasParameters)
        {
            var arguments = hasParameters
                ? new SyntaxNodeOrToken[]
            {
                SyntaxParameterFactory.Create(parameterTypeName, "parameters"),
                SyntaxTokenFactory.Comma(),
                SyntaxParameterFactory.Create(nameof(CancellationToken), nameof(CancellationToken).EnsureFirstCharacterToLower()),
            }
                : new SyntaxNodeOrToken[]
            {
                SyntaxParameterFactory.Create(nameof(CancellationToken), nameof(CancellationToken).EnsureFirstCharacterToLower()),
            };

            return(SyntaxFactory.MethodDeclaration(
                       SyntaxFactory.GenericName(SyntaxFactory.Identifier(nameof(Task)))
                       .WithTypeArgumentList(SyntaxTypeArgumentListFactory.CreateWithOneItem(resultTypeName)),
                       SyntaxFactory.Identifier("InvokeExecuteAsync"))
                   .WithModifiers(
                       SyntaxFactory.TokenList(
                           SyntaxFactory.Token(
                               CreatePragmaWarningCodeStyle1998(true),
                               SyntaxKind.PrivateKeyword,
                               SyntaxFactory.TriviaList()), SyntaxFactory.Token(SyntaxKind.AsyncKeyword)))
                   .WithParameterList(SyntaxFactory.ParameterList(SyntaxFactory.SeparatedList <ParameterSyntax>(arguments)))
                   .WithBody(
                       SyntaxFactory.Block(SyntaxThrowStatementFactory.CreateNotImplementedException())
                       .WithOpenBraceToken(
                           SyntaxFactory.Token(
                               CreatePragmaWarningCodeStyle1998(false),
                               SyntaxKind.OpenBraceToken,
                               SyntaxFactory.TriviaList()))));
        }
    private MemberDeclarationSyntax CreateExecuteAsyncMethod(
        string parameterTypeName,
        string resultTypeName,
        bool hasParameters)
    {
        var arguments = hasParameters
            ? new SyntaxNodeOrToken[]
        {
            SyntaxParameterFactory.Create(parameterTypeName, "parameters"),
            SyntaxTokenFactory.Comma(),
            SyntaxParameterFactory.Create(nameof(CancellationToken), nameof(CancellationToken).EnsureFirstCharacterToLower())
            .WithDefault(SyntaxFactory.EqualsValueClause(
                             SyntaxFactory.LiteralExpression(SyntaxKind.DefaultLiteralExpression, SyntaxTokenFactory.DefaultKeyword()))),
        }
            : new SyntaxNodeOrToken[]
        {
            SyntaxParameterFactory.Create(nameof(CancellationToken), nameof(CancellationToken).EnsureFirstCharacterToLower())
            .WithDefault(SyntaxFactory.EqualsValueClause(
                             SyntaxFactory.LiteralExpression(SyntaxKind.DefaultLiteralExpression, SyntaxTokenFactory.DefaultKeyword()))),
        };

        SyntaxTokenList methodModifiers;
        BlockSyntax     codeBlockSyntax;

        if (hasParameters)
        {
            methodModifiers = SyntaxTokenListFactory.PublicKeyword();

            codeBlockSyntax = SyntaxFactory.Block(
                SyntaxIfStatementFactory.CreateParameterArgumentNullCheck("parameters", false),
                SyntaxFactory.ReturnStatement(
                    SyntaxFactory.InvocationExpression(SyntaxFactory.IdentifierName("InvokeExecuteAsync"))
                    .WithArgumentList(
                        SyntaxArgumentListFactory.CreateWithTwoItems("parameters", nameof(CancellationToken).EnsureFirstCharacterToLower()))));
        }
        else
        {
            methodModifiers = SyntaxTokenListFactory.PublicAsyncKeyword();

            var bodyBlockSyntaxStatements = CreateBodyBlockSyntaxStatements(resultTypeName);
            codeBlockSyntax = SyntaxFactory.Block(bodyBlockSyntaxStatements);
        }

        return(SyntaxFactory.MethodDeclaration(
                   SyntaxFactory.GenericName(SyntaxFactory.Identifier(nameof(Task)))
                   .WithTypeArgumentList(
                       SyntaxFactory.TypeArgumentList(
                           SyntaxFactory.SingletonSeparatedList <TypeSyntax>(
                               SyntaxFactory.IdentifierName(EndpointResultTypeName)))),
                   SyntaxFactory.Identifier("ExecuteAsync"))
               .WithModifiers(methodModifiers)
               .WithParameterList(SyntaxFactory.ParameterList(SyntaxFactory.SeparatedList <ParameterSyntax>(arguments)))
               .WithBody(codeBlockSyntax));
    }
Пример #6
0
        private MemberDeclarationSyntax CreateExecuteAsyncMethod(string parameterTypeName, string resultTypeName, bool hasParameters)
        {
            var arguments = hasParameters
                ? new SyntaxNodeOrToken[]
            {
                SyntaxParameterFactory.Create(parameterTypeName, "parameters"),
                SyntaxTokenFactory.Comma(),
                SyntaxParameterFactory.Create(nameof(CancellationToken), nameof(CancellationToken).EnsureFirstCharacterToLower())
                .WithDefault(SyntaxFactory.EqualsValueClause(
                                 SyntaxFactory.LiteralExpression(SyntaxKind.DefaultLiteralExpression, SyntaxTokenFactory.DefaultKeyword()))),
            }
                : new SyntaxNodeOrToken[]
            {
                SyntaxParameterFactory.Create(nameof(CancellationToken), nameof(CancellationToken).EnsureFirstCharacterToLower())
                .WithDefault(SyntaxFactory.EqualsValueClause(
                                 SyntaxFactory.LiteralExpression(SyntaxKind.DefaultLiteralExpression, SyntaxTokenFactory.DefaultKeyword()))),
            };

            var codeBody = hasParameters
                ? SyntaxFactory.Block(
                SyntaxIfStatementFactory.CreateParameterArgumentNullCheck("parameters"),
                SyntaxFactory.ReturnStatement(
                    SyntaxFactory.InvocationExpression(
                        SyntaxFactory.IdentifierName("InvokeExecuteAsync"))
                    .WithArgumentList(
                        SyntaxFactory.ArgumentList(
                            SyntaxFactory.SeparatedList <ArgumentSyntax>(
                                new SyntaxNodeOrToken[]
            {
                SyntaxFactory.Argument(SyntaxFactory.IdentifierName("parameters")),
                SyntaxTokenFactory.Comma(),
                SyntaxFactory.Argument(SyntaxFactory.IdentifierName(nameof(CancellationToken).EnsureFirstCharacterToLower())),
            })))))
                : SyntaxFactory.Block(
                SyntaxThrowStatementFactory.CreateNotImplementedException());

            return(SyntaxFactory.MethodDeclaration(
                       SyntaxFactory.GenericName(SyntaxFactory.Identifier(nameof(Task)))
                       .WithTypeArgumentList(
                           SyntaxFactory.TypeArgumentList(
                               SyntaxFactory.SingletonSeparatedList <TypeSyntax>(
                                   SyntaxFactory.GenericName(
                                       SyntaxFactory.Identifier("EndpointResult"))
                                   .WithTypeArgumentList(
                                       SyntaxFactory.TypeArgumentList(
                                           SyntaxFactory.SingletonSeparatedList <TypeSyntax>(
                                               SyntaxFactory.IdentifierName(resultTypeName))))))),
                       SyntaxFactory.Identifier("ExecuteAsync"))
                   .WithModifiers(SyntaxTokenListFactory.PublicKeyword())
                   .WithParameterList(SyntaxFactory.ParameterList(SyntaxFactory.SeparatedList <ParameterSyntax>(arguments)))
                   .WithBody(codeBody));
        }
        private static ParameterListSyntax CreateParameterList(
            bool hasParametersOrRequestBody,
            string parameterTypeName,
            string interfaceName,
            bool useFromServicesAttributeOnInterface)
        {
            ParameterListSyntax parameterList;

            if (hasParametersOrRequestBody)
            {
                if (useFromServicesAttributeOnInterface)
                {
                    parameterList = SyntaxFactory.ParameterList(
                        SyntaxFactory.SeparatedList <ParameterSyntax>(
                            new SyntaxNodeOrToken[]
                    {
                        SyntaxParameterFactory.Create(parameterTypeName, "parameters"),
                        SyntaxTokenFactory.Comma(),
                        SyntaxParameterFactory.CreateWithAttribute(nameof(FromServicesAttribute), interfaceName, "handler"),
                        SyntaxTokenFactory.Comma(),
                        SyntaxParameterFactory.Create(nameof(CancellationToken), nameof(CancellationToken).EnsureFirstCharacterToLower())
                    }));
                }
                else
                {
                    parameterList = SyntaxFactory.ParameterList(
                        SyntaxFactory.SeparatedList <ParameterSyntax>(
                            new SyntaxNodeOrToken[]
                    {
                        SyntaxParameterFactory.Create(parameterTypeName, "parameters"),
                        SyntaxTokenFactory.Comma(),
                        SyntaxParameterFactory.Create(interfaceName, "handler"),
                        SyntaxTokenFactory.Comma(),
                        SyntaxParameterFactory.Create(nameof(CancellationToken), nameof(CancellationToken).EnsureFirstCharacterToLower())
                    }));
                }
            }
            else
            {
                parameterList = SyntaxFactory.ParameterList(
                    SyntaxFactory.SeparatedList <ParameterSyntax>(
                        new SyntaxNodeOrToken[]
                {
                    SyntaxParameterFactory.CreateWithAttribute(nameof(FromServicesAttribute), interfaceName, "handler"),
                    SyntaxTokenFactory.Comma(),
                    SyntaxParameterFactory.Create(nameof(CancellationToken), nameof(CancellationToken).EnsureFirstCharacterToLower())
                }));
            }

            return(parameterList);
        }
 private static ConversionOperatorDeclarationSyntax CreateImplicitOperatorForActionResult(string className)
 {
     return(SyntaxFactory.ConversionOperatorDeclaration(
                SyntaxTokenFactory.ImplicitKeyword(),
                SyntaxFactory.IdentifierName(SyntaxFactory.Identifier(nameof(ActionResult))))
            .WithModifiers(SyntaxTokenListFactory.PublicStaticKeyword(true))
            .WithOperatorKeyword(SyntaxTokenFactory.OperatorKeyword())
            .AddParameterListParameters(SyntaxParameterFactory.Create(className, "x"))
            .WithExpressionBody(SyntaxFactory.ArrowExpressionClause(
                                    SyntaxFactory.MemberAccessExpression(
                                        SyntaxKind.SimpleMemberAccessExpression,
                                        SyntaxFactory.IdentifierName("x"),
                                        SyntaxFactory.IdentifierName("result")))
                                .WithArrowToken(SyntaxTokenFactory.EqualsGreaterThan()))
            .WithSemicolonToken(SyntaxTokenFactory.Semicolon()));
 }
        private static ConversionOperatorDeclarationSyntax CreateImplicitOperator(
            string className,
            string typeName,
            HttpStatusCode httpStatusCode,
            bool asGenericList       = false,
            bool asGenericPagination = false)
        {
            string?genericListTypeName = null;

            if (asGenericList)
            {
                genericListTypeName = Microsoft.OpenApi.Models.NameConstants.List;
            }
            else if (asGenericPagination)
            {
                genericListTypeName = Microsoft.OpenApi.Models.NameConstants.Pagination;
            }

            var httpStatus = httpStatusCode.ToNormalizedString();

            if (string.IsNullOrEmpty(typeName))
            {
                typeName = "string";
            }

            return(SyntaxFactory.ConversionOperatorDeclaration(
                       SyntaxTokenFactory.ImplicitKeyword(),
                       SyntaxFactory.IdentifierName(SyntaxFactory.Identifier(className)))
                   .WithModifiers(SyntaxTokenListFactory.PublicStaticKeyword(true))
                   .WithOperatorKeyword(SyntaxTokenFactory.OperatorKeyword())
                   .AddParameterListParameters(SyntaxParameterFactory.Create(typeName, "x", genericListTypeName))
                   .WithExpressionBody(SyntaxFactory.ArrowExpressionClause(
                                           SyntaxFactory.InvocationExpression(SyntaxFactory.IdentifierName(SyntaxFactory.Identifier(httpStatus)))
                                           .AddArgumentListArguments(SyntaxArgumentFactory.Create("x")))
                                       .WithArrowToken(SyntaxTokenFactory.EqualsGreaterThan()))
                   .WithSemicolonToken(SyntaxTokenFactory.Semicolon()));
        }