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 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 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)); }
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 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());