private static MethodDeclarationSyntax CreateTypeRequestWithMessageAllowNull(
     string className,
     HttpStatusCode httpStatusCode,
     string typeRequestName,
     string parameterName = "message")
 {
     return(SyntaxFactory.MethodDeclaration(
                SyntaxFactory.IdentifierName(className),
                SyntaxFactory.Identifier(httpStatusCode.ToNormalizedString()))
            .WithModifiers(SyntaxTokenListFactory.PublicStaticKeyword())
            .WithParameterList(
                SyntaxFactory.ParameterList(
                    SyntaxFactory.SingletonSeparatedList(
                        SyntaxFactory.Parameter(SyntaxFactory.Identifier(parameterName))
                        .WithType(SyntaxFactory.NullableType(SyntaxFactory.PredefinedType(SyntaxTokenFactory.StringKeyword())))
                        .WithDefault(
                            SyntaxFactory.EqualsValueClause(
                                SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression))))))
            .WithExpressionBody(
                SyntaxFactory.ArrowExpressionClause(
                    SyntaxObjectCreationExpressionFactory.Create(className)
                    .WithArgumentList(
                        SyntaxFactory.ArgumentList(
                            SyntaxFactory.SingletonSeparatedList(
                                SyntaxFactory.Argument(
                                    SyntaxObjectCreationExpressionFactory.Create(typeRequestName)
                                    .WithArgumentList(
                                        SyntaxArgumentListFactory.CreateWithOneItem(parameterName))))))))
            .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)));
 }
    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)));
    }
예제 #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()));
    }
 private MemberDeclarationSyntax CreatePropertyForStatusCodeContent(
     HttpStatusCode statusCode,
     string resultTypeName)
 => SyntaxFactory.PropertyDeclaration(
     SyntaxFactory.IdentifierName(resultTypeName),
     SyntaxFactory.Identifier(statusCode.ToNormalizedString() + "Content"))
 .WithModifiers(SyntaxTokenListFactory.PublicKeyword())
 .WithExpressionBody(
     SyntaxFactory.ArrowExpressionClause(
         SyntaxFactory.ConditionalExpression(
             SyntaxFactory.BinaryExpression(
                 SyntaxKind.LogicalAndExpression,
                 SyntaxFactory.IdentifierName("Is" + statusCode.ToNormalizedString()),
                 SyntaxFactory.IsPatternExpression(
                     SyntaxFactory.IdentifierName("ContentObject"),
                     SyntaxFactory.DeclarationPattern(
                         SyntaxFactory.IdentifierName(resultTypeName),
                         SyntaxFactory.SingleVariableDesignation(
                             SyntaxFactory.Identifier("result"))))),
             SyntaxFactory.IdentifierName("result"),
             SyntaxFactory.ThrowExpression(
                 SyntaxFactory.ObjectCreationExpression(SyntaxFactory.IdentifierName(nameof(InvalidOperationException)))
                 .WithArgumentList(
                     SyntaxFactory.ArgumentList(
                         SyntaxFactory.SingletonSeparatedList(
                             SyntaxFactory.Argument(
                                 SyntaxFactory.LiteralExpression(
                                     SyntaxKind.StringLiteralExpression,
                                     SyntaxFactory.Literal($"Content is not the expected type - please use the Is{statusCode.ToNormalizedString()} property first."))))))))))
 .WithSemicolonToken(SyntaxTokenFactory.Semicolon());
 private MemberDeclarationSyntax CreateFieldIHttpMessageFactory()
 => SyntaxFactory.FieldDeclaration(
     SyntaxFactory.VariableDeclaration(SyntaxFactory.IdentifierName(nameof(IHttpMessageFactory)))
     .WithVariables(
         SyntaxFactory.SingletonSeparatedList(
             SyntaxFactory.VariableDeclarator(
                 SyntaxFactory.Identifier("httpMessageFactory")))))
 .WithModifiers(
     SyntaxTokenListFactory.PrivateReadonlyKeyword());
        public static MemberDeclarationSyntax?CreateToStringMethod(IDictionary <string, OpenApiSchema> apiSchemaProperties)
        {
            if (apiSchemaProperties == null)
            {
                throw new ArgumentNullException(nameof(apiSchemaProperties));
            }

            var content = new List <InterpolatedStringContentSyntax>();

            if (apiSchemaProperties.Count > 0)
            {
                var lastKey = apiSchemaProperties.Keys.Last();
                foreach (var schema in apiSchemaProperties)
                {
                    var name             = schema.Key.EnsureFirstCharacterToUpper();
                    var hasAnyProperties = schema.Value.HasAnyProperties();

                    if (!hasAnyProperties)
                    {
                        content.Add(SyntaxInterpolatedFactory.CreateNameOf(name));
                        content.Add(SyntaxInterpolatedFactory.StringTextColon());
                        content.Add(SyntaxFactory.Interpolation(SyntaxFactory.IdentifierName(name)));
                    }
                    else
                    {
                        content.Add(SyntaxInterpolatedFactory.CreateNameOf(name));
                        content.Add(SyntaxInterpolatedFactory.StringTextColonAndParenthesesStart());
                        content.Add(SyntaxFactory.Interpolation(SyntaxFactory.IdentifierName(name)));
                        content.Add(SyntaxInterpolatedFactory.StringTextParenthesesEnd());
                    }

                    if (schema.Key != lastKey)
                    {
                        content.Add(SyntaxInterpolatedFactory.StringTextComma());
                    }
                }
            }

            if (content.Count == 0)
            {
                return(null);
            }

            var codeBody = SyntaxFactory.Block(
                SyntaxFactory.SingletonList <StatementSyntax>(
                    SyntaxFactory.ReturnStatement(
                        SyntaxFactory.InterpolatedStringExpression(
                            SyntaxFactory.Token(SyntaxKind.InterpolatedStringStartToken))
                        .WithContents(SyntaxFactory.List(content)))));

            return(SyntaxFactory.MethodDeclaration(
                       SyntaxFactory.PredefinedType(SyntaxTokenFactory.StringKeyword()),
                       SyntaxFactory.Identifier("ToString"))
                   .WithModifiers(SyntaxTokenListFactory.PublicOverrideKeyword())
                   .WithBody(codeBody));
        }
    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));
    }
예제 #8
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 MethodDeclarationSyntax CreateTypeRequestWithObject(
     string className,
     HttpStatusCode httpStatusCode,
     string typeRequestName,
     string parameterName = "message")
 {
     return(SyntaxFactory.MethodDeclaration(
                SyntaxFactory.IdentifierName(className),
                SyntaxFactory.Identifier(httpStatusCode.ToNormalizedString()))
            .WithModifiers(SyntaxTokenListFactory.PublicStaticKeyword())
            .WithParameterList(
                SyntaxFactory.ParameterList(
                    SyntaxFactory.SingletonSeparatedList(
                        SyntaxFactory.Parameter(SyntaxFactory.Identifier(parameterName))
                        .WithType(SyntaxFactory.NullableType(SyntaxFactory.PredefinedType(SyntaxTokenFactory.StringKeyword())))
                        .WithDefault(
                            SyntaxFactory.EqualsValueClause(
                                SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression))))))
            .WithExpressionBody(
                SyntaxFactory.ArrowExpressionClause(
                    SyntaxObjectCreationExpressionFactory.Create(className)
                    .WithArgumentList(
                        SyntaxFactory.ArgumentList(
                            SyntaxFactory.SingletonSeparatedList(
                                SyntaxFactory.Argument(
                                    SyntaxObjectCreationExpressionFactory.Create(typeRequestName)
                                    .WithInitializer(
                                        SyntaxFactory.InitializerExpression(
                                            SyntaxKind.ObjectInitializerExpression,
                                            SyntaxFactory.SeparatedList <ExpressionSyntax>(
                                                new SyntaxNodeOrToken[]
     {
         SyntaxFactory.AssignmentExpression(
             SyntaxKind.SimpleAssignmentExpression,
             SyntaxFactory.IdentifierName("StatusCode"),
             SyntaxFactory.CastExpression(
                 SyntaxFactory.PredefinedType(
                     SyntaxFactory.Token(SyntaxKind.IntKeyword)),
                 SyntaxFactory.MemberAccessExpression(
                     SyntaxKind.SimpleMemberAccessExpression,
                     SyntaxFactory.IdentifierName(nameof(HttpStatusCode)),
                     SyntaxFactory.IdentifierName(httpStatusCode.ToNormalizedString())))),
         SyntaxTokenFactory.Comma(),
         SyntaxFactory.AssignmentExpression(
             SyntaxKind.SimpleAssignmentExpression,
             SyntaxFactory.IdentifierName("Content"),
             SyntaxFactory.IdentifierName(parameterName)),
     })))))))))
            .WithSemicolonToken(SyntaxTokenFactory.Semicolon()));
 }
예제 #10
0
 private MemberDeclarationSyntax CreateFieldIHttpClientFactory()
 {
     return
         (SyntaxFactory.FieldDeclaration(
              SyntaxFactory
              .VariableDeclaration(
                  SyntaxFactory.IdentifierName("IHttpClientFactory"))
              .WithVariables(
                  SyntaxFactory.SingletonSeparatedList(
                      SyntaxFactory.VariableDeclarator(
                          SyntaxFactory.Identifier("factory")))))
          .WithModifiers(
              SyntaxTokenListFactory.PrivateReadonlyKeyword()));
 }
 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 MethodDeclarationSyntax CreateTypeRequestWithSpecifiedResultFactoryMethodWithMessageAllowNull(
     string resultFactoryMethodName,
     string className,
     HttpStatusCode httpStatusCode,
     string parameterName = "message")
 {
     return(SyntaxFactory.MethodDeclaration(
                SyntaxFactory.IdentifierName(className),
                SyntaxFactory.Identifier(httpStatusCode.ToNormalizedString()))
            .WithModifiers(SyntaxTokenListFactory.PublicStaticKeyword())
            .WithParameterList(
                SyntaxFactory.ParameterList(
                    SyntaxFactory.SingletonSeparatedList(
                        SyntaxFactory.Parameter(SyntaxFactory.Identifier(parameterName))
                        .WithType(SyntaxFactory.NullableType(SyntaxFactory.PredefinedType(SyntaxTokenFactory.StringKeyword())))
                        .WithDefault(
                            SyntaxFactory.EqualsValueClause(
                                SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression))))))
            .WithExpressionBody(
                SyntaxFactory.ArrowExpressionClause(
                    SyntaxFactory.ObjectCreationExpression(
                        SyntaxFactory.IdentifierName(className))
                    .WithArgumentList(
                        SyntaxFactory.ArgumentList(
                            SyntaxFactory.SingletonSeparatedList(
                                SyntaxFactory.Argument(
                                    SyntaxFactory.InvocationExpression(
                                        SyntaxFactory.MemberAccessExpression(
                                            SyntaxKind.SimpleMemberAccessExpression,
                                            SyntaxFactory.IdentifierName("ResultFactory"),
                                            SyntaxFactory.IdentifierName(resultFactoryMethodName)))
                                    .WithArgumentList(
                                        SyntaxFactory.ArgumentList(
                                            SyntaxFactory.SeparatedList <ArgumentSyntax>(
                                                new SyntaxNodeOrToken[]
     {
         SyntaxFactory.Argument(
             SyntaxFactory.MemberAccessExpression(
                 SyntaxKind.SimpleMemberAccessExpression,
                 SyntaxFactory.IdentifierName(nameof(HttpStatusCode)),
                 SyntaxFactory.IdentifierName(httpStatusCode.ToString()))),
         SyntaxTokenFactory.Comma(),
         SyntaxFactory.Argument(SyntaxFactory.IdentifierName(parameterName)),
     })))))))))
            .WithSemicolonToken(SyntaxTokenFactory.Semicolon()));
 }
예제 #13
0
 private static MethodDeclarationSyntax CreateTypeRequestWithProblemDetailsWithMessage(
     string className,
     HttpStatusCode httpStatusCode,
     string parameterName = "message")
 {
     return(SyntaxFactory.MethodDeclaration(
                SyntaxFactory.IdentifierName(className),
                SyntaxFactory.Identifier(httpStatusCode.ToNormalizedString()))
            .WithModifiers(SyntaxTokenListFactory.PublicStaticKeyword())
            .WithParameterList(
                SyntaxFactory.ParameterList(
                    SyntaxFactory.SingletonSeparatedList(
                        SyntaxFactory.Parameter(SyntaxFactory.Identifier(parameterName))
                        .WithType(SyntaxFactory.PredefinedType(SyntaxTokenFactory.StringKeyword())))))
            .WithExpressionBody(
                SyntaxFactory.ArrowExpressionClause(
                    SyntaxFactory.ObjectCreationExpression(
                        SyntaxFactory.IdentifierName(className))
                    .WithArgumentList(
                        SyntaxFactory.ArgumentList(
                            SyntaxFactory.SingletonSeparatedList(
                                SyntaxFactory.Argument(
                                    SyntaxFactory.InvocationExpression(
                                        SyntaxFactory.MemberAccessExpression(
                                            SyntaxKind.SimpleMemberAccessExpression,
                                            SyntaxFactory.IdentifierName("ResultFactory"),
                                            SyntaxFactory.IdentifierName("CreateContentResultWithProblemDetails")))
                                    .WithArgumentList(
                                        SyntaxFactory.ArgumentList(
                                            SyntaxFactory.SeparatedList <ArgumentSyntax>(
                                                new SyntaxNodeOrToken[]
     {
         SyntaxFactory.Argument(
             SyntaxFactory.MemberAccessExpression(
                 SyntaxKind.SimpleMemberAccessExpression,
                 SyntaxFactory.IdentifierName(nameof(HttpStatusCode)),
                 SyntaxFactory.IdentifierName(httpStatusCode.ToString()))),
         SyntaxTokenFactory.Comma(),
         SyntaxFactory.Argument(SyntaxFactory.IdentifierName(parameterName))
     })))))))))
            .WithSemicolonToken(SyntaxTokenFactory.Semicolon()));
 }
 private MemberDeclarationSyntax CreateConstructor()
 => SyntaxFactory.ConstructorDeclaration(
     SyntaxFactory.Identifier(EndpointTypeName))
 .WithModifiers(
     SyntaxTokenListFactory.PublicKeyword())
 .WithParameterList(
     SyntaxFactory.ParameterList(
         SyntaxFactory.SingletonSeparatedList(
             SyntaxFactory.Parameter(
                 SyntaxFactory.Identifier("response"))
             .WithType(
                 SyntaxFactory.IdentifierName("EndpointResponse")))))
 .WithInitializer(
     SyntaxFactory.ConstructorInitializer(
         SyntaxKind.BaseConstructorInitializer,
         SyntaxFactory.ArgumentList(
             SyntaxFactory.SingletonSeparatedList(
                 SyntaxFactory.Argument(
                     SyntaxFactory.IdentifierName("response"))))))
 .WithBody(SyntaxFactory.Block());
 private static MethodDeclarationSyntax CreateTypeRequest(
     string className,
     HttpStatusCode httpStatusCode,
     string typeRequestName)
 {
     return(SyntaxFactory.MethodDeclaration(
                SyntaxFactory.IdentifierName(className),
                SyntaxFactory.Identifier(httpStatusCode.ToNormalizedString()))
            .WithModifiers(SyntaxTokenListFactory.PublicStaticKeyword())
            .WithExpressionBody(
                SyntaxFactory.ArrowExpressionClause(
                    SyntaxObjectCreationExpressionFactory.Create(className)
                    .WithArgumentList(
                        SyntaxFactory.ArgumentList(
                            SyntaxFactory.SingletonSeparatedList(
                                SyntaxFactory.Argument(
                                    SyntaxFactory.ObjectCreationExpression(
                                        SyntaxFactory.IdentifierName(typeRequestName))
                                    .WithArgumentList(SyntaxFactory.ArgumentList())))))))
            .WithSemicolonToken(SyntaxTokenFactory.Semicolon()));
 }
        private static MethodDeclarationSyntax CreateTypeRequestObjectResult(
            string className,
            string methodName,
            string parameterTypeName,
            string parameterName     = "response",
            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;
            }

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

            return(SyntaxFactory.MethodDeclaration(
                       SyntaxFactory.IdentifierName(className),
                       SyntaxFactory.Identifier(methodName))
                   .WithModifiers(SyntaxTokenListFactory.PublicStaticKeyword())
                   .WithParameterList(SyntaxParameterListFactory.CreateWithOneItem(parameterTypeName, parameterName, genericListTypeName))
                   .WithExpressionBody(
                       SyntaxFactory.ArrowExpressionClause(
                           SyntaxObjectCreationExpressionFactory.Create(className)
                           .WithArgumentList(
                               SyntaxFactory.ArgumentList(
                                   SyntaxFactory.SingletonSeparatedList(
                                       SyntaxFactory.Argument(
                                           SyntaxObjectCreationExpressionFactory.Create(methodName + nameof(ObjectResult))
                                           .WithArgumentList(SyntaxArgumentListFactory.CreateWithOneItem(parameterName))))))))
                   .WithSemicolonToken(SyntaxTokenFactory.Semicolon()));
        }
예제 #17
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(SyntaxTokenListFactory.PrivateAsyncKeyword())
                   .WithParameterList(SyntaxFactory.ParameterList(SyntaxFactory.SeparatedList <ParameterSyntax>(arguments)))
                   .WithBody(SyntaxFactory.Block(SyntaxThrowStatementFactory.CreateNotImplementedException())));
        }
        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()));
        }
 private static MethodDeclarationSyntax CreateStatusCodeResult(
     string className,
     HttpStatusCode httpStatusCode)
 {
     return(SyntaxFactory.MethodDeclaration(SyntaxFactory.IdentifierName(className), SyntaxFactory.Identifier(httpStatusCode.ToNormalizedString()))
            .WithModifiers(SyntaxTokenListFactory.PublicStaticKeyword())
            .WithExpressionBody(
                SyntaxFactory.ArrowExpressionClause(
                    SyntaxFactory.ObjectCreationExpression(SyntaxFactory.IdentifierName(className))
                    .WithArgumentList(
                        SyntaxFactory.ArgumentList(
                            SyntaxFactory.SingletonSeparatedList(
                                SyntaxFactory.Argument(
                                    SyntaxFactory.ObjectCreationExpression(SyntaxFactory.IdentifierName(nameof(StatusCodeResult)))
                                    .WithArgumentList(
                                        SyntaxFactory.ArgumentList(
                                            SyntaxFactory.SingletonSeparatedList(
                                                SyntaxFactory.Argument(
                                                    SyntaxFactory.MemberAccessExpression(
                                                        SyntaxKind.SimpleMemberAccessExpression,
                                                        SyntaxFactory.IdentifierName(nameof(StatusCodes)),
                                                        SyntaxFactory.IdentifierName($"Status{(int)httpStatusCode}{httpStatusCode}"))))))))))))
            .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)));
 }
 private static MemberDeclarationSyntax CreateActionResultField()
 {
     return(SyntaxFactory.FieldDeclaration(
                SyntaxVariableDeclarationFactory.Create(nameof(ActionResult), "result"))
            .WithModifiers(SyntaxTokenListFactory.PrivateReadonlyKeyword()));
 }