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 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())); }
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 MemberDeclarationSyntax CreateConstructor() { return (SyntaxFactory.ConstructorDeclaration( SyntaxFactory.Identifier(EndpointTypeName)) .WithModifiers( SyntaxFactory.TokenList( SyntaxTokenFactory.PublicKeyword())) .WithParameterList( SyntaxFactory.ParameterList( SyntaxFactory.SeparatedList <ParameterSyntax>( new SyntaxNodeOrToken[] { SyntaxFactory.Parameter(SyntaxFactory.Identifier("factory")) .WithType(SyntaxFactory.IdentifierName("IHttpClientFactory")), SyntaxTokenFactory.Comma(), SyntaxFactory .Parameter(SyntaxFactory.Identifier("httpExchangeFactory")) .WithType(SyntaxFactory.IdentifierName("IHttpExchangeFactory")), }))) .WithBody( SyntaxFactory.Block( SyntaxFactory.ExpressionStatement( SyntaxFactory.AssignmentExpression( SyntaxKind.SimpleAssignmentExpression, SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.ThisExpression(), SyntaxFactory.IdentifierName("factory")), SyntaxFactory.IdentifierName("factory"))), SyntaxFactory.ExpressionStatement( SyntaxFactory.AssignmentExpression( SyntaxKind.SimpleAssignmentExpression, SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.ThisExpression(), SyntaxFactory.IdentifierName("httpExchangeFactory")), SyntaxFactory.IdentifierName("httpExchangeFactory")))))); }
private static ReturnStatementSyntax CreateCodeBlockReturnStatementForHelper( bool hasParameters) { var arguments = hasParameters ? new SyntaxNodeOrToken[] { SyntaxArgumentFactory.Create("parameters"), SyntaxTokenFactory.Comma(), SyntaxArgumentFactory.Create(nameof(CancellationToken).EnsureFirstCharacterToLower()), } : new SyntaxNodeOrToken[] { SyntaxArgumentFactory.Create(nameof(CancellationToken).EnsureFirstCharacterToLower()), }; return(SyntaxFactory.ReturnStatement( SyntaxFactory.AwaitExpression( SyntaxFactory.InvocationExpression( SyntaxMemberAccessExpressionFactory.Create("ExecuteAsync", "handler")) .WithArgumentList( SyntaxFactory.ArgumentList( SyntaxFactory.SeparatedList <ArgumentSyntax>(arguments)))))); }
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( SyntaxFactory.TypeArgumentList( SyntaxFactory.SingletonSeparatedList <TypeSyntax>( SyntaxFactory.GenericName( SyntaxFactory.Identifier("EndpointResult")) .WithTypeArgumentList( SyntaxFactory.TypeArgumentList( SyntaxFactory.SingletonSeparatedList <TypeSyntax>( SyntaxFactory.IdentifierName(resultTypeName))))))), SyntaxFactory.Identifier("InvokeExecuteAsync")) .WithModifiers(SyntaxTokenListFactory.PrivateAsyncKeyword()) .WithParameterList( SyntaxFactory.ParameterList(SyntaxFactory.SeparatedList <ParameterSyntax>(arguments))) .WithBody( SyntaxFactory.Block( CreateInvokeExecuteAsyncMethodBlockLocalClient(), CreateInvokeExecuteAsyncMethodBlockLocalRequestBuilder(), CreateInvokeExecuteAsyncMethodBlockLocalRequestMessage(), CreateInvokeExecuteAsyncMethodBlockLocalResponse(), CreateInvokeExecuteAsyncMethodBlockReturn(resultTypeName)))); }
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 CreateTypeRequestWithSpecifiedResultFactoryMethod( string resultFactoryMethodName, 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.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.LiteralExpression(SyntaxKind.NullLiteralExpression)), }))))))))) .WithSemicolonToken(SyntaxTokenFactory.Semicolon())); }
private static ReturnStatementSyntax CreateCodeBlockReturnStatement(string helperMethodName, bool hasParameters) { var arguments = hasParameters ? new SyntaxNodeOrToken[] { SyntaxArgumentFactory.Create("parameters"), SyntaxTokenFactory.Comma(), SyntaxArgumentFactory.Create("handler"), SyntaxTokenFactory.Comma(), SyntaxArgumentFactory.Create("cancellationToken") } : new SyntaxNodeOrToken[] { SyntaxArgumentFactory.Create("handler"), SyntaxTokenFactory.Comma(), SyntaxArgumentFactory.Create("cancellationToken") }; return(SyntaxFactory.ReturnStatement( SyntaxFactory.InvocationExpression(SyntaxFactory.IdentifierName(helperMethodName)) .WithArgumentList( SyntaxFactory.ArgumentList( SyntaxFactory.SeparatedList <ArgumentSyntax>(arguments))))); }
public static MemberDeclarationSyntax?CreateToStringMethod( IDictionary <string, OpenApiSchema> apiSchemaProperties) { ArgumentNullException.ThrowIfNull(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 (schema.Value.IsTypeArray()) { content.Add(SyntaxInterpolatedFactory.CreateNameOf(name)); content.Add(SyntaxInterpolatedFactory.StringText(".Count: ")); var countCoalesceExpression = SyntaxFactory.ParseExpression($"{name}?.Count ?? 0"); content.Add(SyntaxFactory.Interpolation(countCoalesceExpression)); } else 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 ReturnStatementSyntax CreateInvokeExecuteAsyncMethodBlockReturn(string resultTypeName) { return(SyntaxFactory.ReturnStatement( SyntaxFactory.AwaitExpression( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName("httpExchangeFactory"), SyntaxFactory.IdentifierName("FromResponse"))) .WithArgumentList( SyntaxFactory.ArgumentList( SyntaxFactory.SingletonSeparatedList( SyntaxFactory.Argument( SyntaxFactory.IdentifierName("response"))))), SyntaxFactory.GenericName( SyntaxFactory.Identifier("AddSuccessResponse")) // TODO: nameof .WithTypeArgumentList( SyntaxFactory.TypeArgumentList( SyntaxFactory.SingletonSeparatedList <TypeSyntax>( SyntaxFactory.IdentifierName(resultTypeName)))))) .WithArgumentList(CreateResponseArgHttpStatusCode(HttpStatusCode.OK)), SyntaxFactory.GenericName( SyntaxFactory.Identifier("AddErrorResponse")) // TODO: nameof .WithTypeArgumentList( SyntaxFactory.TypeArgumentList( SyntaxFactory.SingletonSeparatedList <TypeSyntax>( SyntaxFactory.IdentifierName("ProblemDetails")))))) // TODO: nameof .WithArgumentList(CreateResponseArgHttpStatusCode(HttpStatusCode.BadRequest)), SyntaxFactory.IdentifierName("AddErrorResponse"))) // TODO: nameof .WithArgumentList(CreateResponseArgHttpStatusCode(HttpStatusCode.NotFound)), SyntaxFactory.IdentifierName("BuildResponseAsync"))) // TODO: nameof .WithArgumentList( SyntaxFactory.ArgumentList( SyntaxFactory.SeparatedList <ArgumentSyntax>( new SyntaxNodeOrToken[] { SyntaxFactory.Argument( SyntaxFactory.SimpleLambdaExpression( SyntaxFactory.Parameter( SyntaxFactory.Identifier("x"))) .WithExpressionBody( SyntaxFactory.ObjectCreationExpression( SyntaxFactory.GenericName( SyntaxFactory.Identifier("EndpointResult")) // TODO: nameof .WithTypeArgumentList( SyntaxFactory.TypeArgumentList( SyntaxFactory.SingletonSeparatedList <TypeSyntax>( SyntaxFactory.IdentifierName(resultTypeName))))) .WithArgumentList( SyntaxFactory.ArgumentList( SyntaxFactory.SingletonSeparatedList( SyntaxFactory.Argument( SyntaxFactory.IdentifierName("x"))))))), SyntaxTokenFactory.Comma(), SyntaxFactory.Argument(SyntaxFactory.IdentifierName("cancellationToken")), })))))); }
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)); }
public static PropertyDeclarationSyntax CreateAuto( ParameterLocation?parameterLocation, bool isNullable, bool isRequired, string dataType, string propertyName, bool useNullableReferenceTypes, IOpenApiAny?initializer) { switch (useNullableReferenceTypes) { case true when !isRequired && (isNullable || parameterLocation == ParameterLocation.Query): case true when isRequired && isNullable: dataType += "?"; break; } var propertyDeclaration = CreateAuto(dataType, propertyName); if (initializer is null) { return(propertyDeclaration); } switch (initializer) { case OpenApiInteger apiInteger: propertyDeclaration = propertyDeclaration.WithInitializer( SyntaxFactory.EqualsValueClause( SyntaxFactory.LiteralExpression( SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(apiInteger !.Value)))) .WithSemicolonToken(SyntaxTokenFactory.Semicolon()); break; case OpenApiString apiString: var expressionSyntax = string.IsNullOrEmpty(apiString !.Value) ? (ExpressionSyntax)SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.PredefinedType(SyntaxTokenFactory.StringKeyword()), SyntaxFactory.IdentifierName("Empty")) : SyntaxFactory.LiteralExpression( SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(apiString !.Value)); propertyDeclaration = propertyDeclaration.WithInitializer( SyntaxFactory.EqualsValueClause(expressionSyntax)) .WithSemicolonToken(SyntaxTokenFactory.Semicolon()); break; case OpenApiBoolean apiBoolean when apiBoolean.Value: propertyDeclaration = propertyDeclaration.WithInitializer( SyntaxFactory.EqualsValueClause( SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression))) .WithSemicolonToken(SyntaxTokenFactory.Semicolon()); break; case OpenApiBoolean apiBoolean when !apiBoolean.Value: propertyDeclaration = propertyDeclaration.WithInitializer( SyntaxFactory.EqualsValueClause( SyntaxFactory.LiteralExpression(SyntaxKind.FalseLiteralExpression))) .WithSemicolonToken(SyntaxTokenFactory.Semicolon()); break; default: throw new NotImplementedException("Property initializer: " + initializer.GetType()); } return(propertyDeclaration); }
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)); }
public static EnumDeclarationSyntax Create(string title, IList <IOpenApiAny> apiSchemaEnums) { if (title == null) { throw new ArgumentNullException(nameof(title)); } if (apiSchemaEnums == null) { throw new ArgumentNullException(nameof(apiSchemaEnums)); } // Create an enum var enumDeclaration = SyntaxFactory.EnumDeclaration(title) .AddModifiers(SyntaxTokenFactory.PublicKeyword()); // Find values to the enum var containTypeName = false; var lines = new List <string>(); var intValues = new List <int>(); foreach (var item in apiSchemaEnums) { if (!(item is OpenApiString openApiString)) { continue; } lines.Add(openApiString.Value.Trim()); if (!openApiString.Value.Contains("=", StringComparison.Ordinal)) { continue; } var sa = openApiString.Value.Split('=', StringSplitOptions.RemoveEmptyEntries); var s = sa.Last().Trim(); if (int.TryParse(s, out int val)) { intValues.Add(val); } } // Add values to the enum foreach (var line in lines) { enumDeclaration = enumDeclaration.AddMembers(SyntaxFactory.EnumMemberDeclaration(line)); var sa = line.Split(' ', StringSplitOptions.RemoveEmptyEntries); if (sa.Length > 0 && sa.First().Equals("Object", StringComparison.Ordinal)) { containTypeName = true; } } // Add SuppressMessageAttribute if (containTypeName) { enumDeclaration = enumDeclaration .AddSuppressMessageAttribute(SuppressMessageAttributeFactory.Create(1720, null)); } // Add FlagAttribute if (intValues.Count > 0) { bool isAllValidBinarySequence = intValues .Where(x => x != 0) .All(x => x.IsBinarySequence()); if (isAllValidBinarySequence) { enumDeclaration = enumDeclaration.AddFlagAttribute(); } } return(enumDeclaration); }
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())); }
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))); }
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())); }
public static ClassDeclarationSyntax AddSuppressMessageAttribute(this ClassDeclarationSyntax classDeclaration, SuppressMessageAttribute suppressMessage) { if (classDeclaration == null) { throw new ArgumentNullException(nameof(classDeclaration)); } if (suppressMessage == null) { throw new ArgumentNullException(nameof(suppressMessage)); } if (string.IsNullOrEmpty(suppressMessage.Justification)) { throw new ArgumentException(nameof(suppressMessage.Justification)); } var attributeArgumentList = SyntaxFactory.AttributeArgumentList( SyntaxFactory.SeparatedList <AttributeArgumentSyntax>(SyntaxFactory.NodeOrTokenList( SyntaxFactory.AttributeArgument(SyntaxLiteralExpressionFactory.Create(suppressMessage.Category)), SyntaxTokenFactory.Comma(), SyntaxFactory.AttributeArgument(SyntaxLiteralExpressionFactory.Create(suppressMessage.CheckId)), SyntaxTokenFactory.Comma(), SyntaxFactory.AttributeArgument(SyntaxLiteralExpressionFactory.Create(suppressMessage.Justification !)) .WithNameEquals( SyntaxNameEqualsFactory.Create(nameof(SuppressMessageAttribute.Justification)) .WithEqualsToken(SyntaxTokenFactory.Equals()))))); return(classDeclaration .AddAttributeLists(SyntaxAttributeListFactory.Create(nameof(SuppressMessageAttribute), attributeArgumentList))); }
public static EnumDeclarationSyntax Create( string title, IList <IOpenApiAny> apiSchemaEnums) { ArgumentNullException.ThrowIfNull(title); ArgumentNullException.ThrowIfNull(apiSchemaEnums); // Create an enum var enumDeclaration = SyntaxFactory.EnumDeclaration(title) .AddModifiers(SyntaxTokenFactory.PublicKeyword()); // Find values to the enum var containTypeName = false; var lines = new List <string>(); var intValues = new List <int>(); foreach (var item in apiSchemaEnums) { if (item is not OpenApiString openApiString) { continue; } lines.Add(openApiString.Value.Trim()); if (!openApiString.Value.Contains('=', StringComparison.Ordinal)) { continue; } var sa = openApiString.Value.Split('=', StringSplitOptions.RemoveEmptyEntries); var s = sa.Last().Trim(); if (int.TryParse(s, NumberStyles.Integer, GlobalizationConstants.EnglishCultureInfo, out var val)) { intValues.Add(val); } } // Add values to the enum foreach (var line in lines) { enumDeclaration = enumDeclaration.AddMembers(SyntaxFactory.EnumMemberDeclaration(line)); var sa = line.Split(' ', StringSplitOptions.RemoveEmptyEntries); if (sa.Length > 0 && sa.First().Equals("Object", StringComparison.Ordinal)) { containTypeName = true; } } enumDeclaration = enumDeclaration .AddSuppressMessageAttribute(CodeAnalysis.CSharp.Factories.SuppressMessageAttributeFactory.CreateStyleCopSuppression(1413, justification: null)); // Add SuppressMessageAttribute if (containTypeName) { enumDeclaration = enumDeclaration .AddSuppressMessageAttribute(CodeAnalysis.CSharp.Factories.SuppressMessageAttributeFactory.CreateCodeAnalysisSuppression(1720, justification: null)); } // Add FlagAttribute if (intValues.Count > 0) { var isAllValidBinarySequence = intValues .Where(x => x != 0) .All(x => x.IsBinarySequence()); if (isAllValidBinarySequence) { enumDeclaration = enumDeclaration.AddFlagAttribute(); } } return(enumDeclaration); }
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())); }