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 PropertyDeclarationSyntax CreateListAuto( string dataType, string propertyName, bool initializeList = true) { var propertyDeclaration = SyntaxFactory.PropertyDeclaration( SyntaxFactory.GenericName(SyntaxFactory.Identifier(Microsoft.OpenApi.Models.NameConstants.List)) .WithTypeArgumentList(SyntaxTypeArgumentListFactory.CreateWithOneItem(dataType)), SyntaxFactory.Identifier(propertyName)) .AddModifiers(SyntaxTokenFactory.PublicKeyword()) .WithAccessorList( SyntaxFactory.AccessorList( SyntaxFactory.List( new[] { SyntaxAccessorDeclarationFactory.Get(), SyntaxAccessorDeclarationFactory.Set(), }))); if (initializeList) { propertyDeclaration = propertyDeclaration.WithInitializer( SyntaxFactory.EqualsValueClause( SyntaxFactory.ObjectCreationExpression( SyntaxFactory.GenericName(SyntaxFactory.Identifier(Microsoft.OpenApi.Models.NameConstants.List)) .WithTypeArgumentList(SyntaxTypeArgumentListFactory.CreateWithOneItem(dataType))) .WithArgumentList( SyntaxFactory.ArgumentList()))) .WithSemicolonToken(SyntaxTokenFactory.Semicolon()); } return(propertyDeclaration); }
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())); }
public static PropertyDeclarationSyntax CreateAuto( ParameterLocation?parameterLocation, bool isNullable, bool isRequired, string dataType, string propertyName, bool useNullableReferenceTypes, IOpenApiAny?initializer) { if (useNullableReferenceTypes && (isNullable || parameterLocation == ParameterLocation.Query) && !isRequired) { dataType += "?"; } var propertyDeclaration = CreateAuto(dataType, propertyName); if (initializer != null) { propertyDeclaration = initializer switch { OpenApiInteger apiInteger => propertyDeclaration.WithInitializer( SyntaxFactory.EqualsValueClause(SyntaxFactory.LiteralExpression( SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(apiInteger !.Value)))) .WithSemicolonToken(SyntaxTokenFactory.Semicolon()), OpenApiString apiString => propertyDeclaration.WithInitializer( SyntaxFactory.EqualsValueClause(SyntaxFactory.LiteralExpression( SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(apiString !.Value)))) .WithSemicolonToken(SyntaxTokenFactory.Semicolon()), _ => throw new NotImplementedException("Property initializer: " + initializer.GetType()) }; } return(propertyDeclaration); }
private static MemberDeclarationSyntax CreatePropertyForStatusCodeContent( HttpStatusCode statusCode, string resultTypeName) => SyntaxFactory.PropertyDeclaration( SyntaxFactory.IdentifierName(resultTypeName), SyntaxFactory.Identifier(statusCode.ToNormalizedString() + "Content")) .WithAccessorList( SyntaxFactory.AccessorList( SyntaxFactory.SingletonList( SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration) .WithSemicolonToken(SyntaxTokenFactory.Semicolon()))));
private static MemberDeclarationSyntax CreatePropertyForIsStatusCode( HttpStatusCode statusCode) => SyntaxFactory.PropertyDeclaration( SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.BoolKeyword)), SyntaxFactory.Identifier("Is" + statusCode.ToNormalizedString())) .WithAccessorList( SyntaxFactory.AccessorList( SyntaxFactory.SingletonList( SyntaxFactory.AccessorDeclaration( SyntaxKind.GetAccessorDeclaration) .WithSemicolonToken(SyntaxTokenFactory.Semicolon()))));
private MemberDeclarationSyntax CreatePropertyForIsStatusCode( HttpStatusCode statusCode) => SyntaxFactory.PropertyDeclaration( SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.BoolKeyword)), SyntaxFactory.Identifier("Is" + statusCode.ToNormalizedString())) .WithModifiers(SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.PublicKeyword))) .WithExpressionBody( SyntaxFactory.ArrowExpressionClause( SyntaxFactory.BinaryExpression( SyntaxKind.EqualsExpression, SyntaxFactory.IdentifierName("StatusCode"), SyntaxMemberAccessExpressionFactory.Create(statusCode.ToString(), nameof(HttpStatusCode))))) .WithSemicolonToken(SyntaxTokenFactory.Semicolon());
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 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())); }
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 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())); }
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); }