コード例 #1
0
        public bool GenerateCode()
        {
            // Create compilationUnit
            var compilationUnit = SyntaxFactory.CompilationUnit();

            // Create a namespace
            var @namespace = SyntaxProjectFactory.CreateNamespace(
                ApiProjectOptions,
                NameConstants.Endpoints,
                FocusOnSegmentName);

            // Create class
            // TODO: var classDeclaration = SyntaxClassDeclarationFactory.CreateWithInterface(classTypeName, interfaceTypeName)
            var classDeclaration = SyntaxClassDeclarationFactory.Create(EndpointTypeName)
                                   .AddGeneratedCodeAttribute(ApiProjectOptions.ToolName, ApiProjectOptions.ToolVersion.ToString())
                                   .WithLeadingTrivia(SyntaxDocumentationFactory.CreateForResults(ApiOperation, FocusOnSegmentName));

            classDeclaration = classDeclaration.AddMembers(CreateFieldIHttpClientFactory());
            classDeclaration = classDeclaration.AddMembers(CreateFieldIHttpExchangeFactory());
            classDeclaration = classDeclaration.AddMembers(CreateConstructor());
            classDeclaration = classDeclaration.AddMembers(CreateMembers());

            // TODO: Imp. this.
            var usedApiOperations = new List <OpenApiOperation>();

            // Add using statement to compilationUnit
            var includeRestResults = classDeclaration
                                     .Select <IdentifierNameSyntax>()
                                     .Any(x => x.Identifier.ValueText.Contains(
                                              $"({Microsoft.OpenApi.Models.NameConstants.Pagination}<",
                                              StringComparison.Ordinal));

            compilationUnit = compilationUnit.AddUsingStatements(
                ProjectEndpointsFactory.CreateUsingList(
                    ApiProjectOptions,
                    FocusOnSegmentName,
                    usedApiOperations,
                    includeRestResults,
                    HasSharedResponseContract(),
                    forClient: true));

            // Add the class to the namespace.
            @namespace = @namespace.AddMembers(classDeclaration);

            // Add namespace to compilationUnit
            compilationUnit = compilationUnit.AddMembers(@namespace);

            // Set code property
            Code = compilationUnit;
            return(true);
        }
コード例 #2
0
    public bool GenerateCode()
    {
        // Create compilationUnit
        var compilationUnit = SyntaxFactory.CompilationUnit();

        // Create a namespace
        var @namespace = SyntaxProjectFactory.CreateNamespace(
            ApiProjectOptions,
            NameConstants.Endpoints,
            FocusOnSegmentName);

        // Create interface
        var interfaceDeclaration = SyntaxInterfaceDeclarationFactory.Create(InterfaceTypeName)
                                   .AddGeneratedCodeAttribute(ApiProjectOptions.ToolName, ApiProjectOptions.ToolVersion.ToString())
                                   .WithLeadingTrivia(SyntaxDocumentationFactory.CreateForResults(ApiOperation, FocusOnSegmentName));

        // Create interface-method
        interfaceDeclaration = interfaceDeclaration.AddMembers(CreateMembers());
        //// TODO: var methodDeclaration = ...

        // Add using statement to compilationUnit
        var includeRestResults = interfaceDeclaration
                                 .Select <IdentifierNameSyntax>()
                                 .Any(x => x.Identifier.ValueText.Contains(
                                          $"({Microsoft.OpenApi.Models.NameConstants.Pagination}<",
                                          StringComparison.Ordinal));

        compilationUnit = compilationUnit.AddUsingStatements(
            ProjectApiClientFactory.CreateUsingListForEndpointInterface(
                ApiProjectOptions,
                includeRestResults,
                OpenApiDocumentSchemaModelNameHelper.HasList(ResultTypeName),
                OpenApiDocumentSchemaModelNameHelper.HasSharedResponseContract(
                    ApiProjectOptions.Document,
                    OperationSchemaMappings,
                    FocusOnSegmentName)));

        // Add interface-method to interface
        //// TODO: interfaceDeclaration = interfaceDeclaration.AddMembers(methodDeclaration);

        // Add the interface to the namespace.
        @namespace = @namespace.AddMembers(interfaceDeclaration);

        // Add namespace to compilationUnit
        compilationUnit = compilationUnit.AddMembers(@namespace);

        // Set code property
        Code = compilationUnit;
        return(true);
    }
コード例 #3
0
        public bool GenerateCode()
        {
            var resultTypeName = ApiOperation.GetOperationName() + NameConstants.ContractResult;

            // Create compilationUnit
            var compilationUnit = SyntaxFactory.CompilationUnit();

            // Create a namespace
            var @namespace = SyntaxProjectFactory.CreateNamespace(
                ApiProjectOptions,
                NameConstants.Contracts,
                FocusOnSegmentName);

            // Create class
            var classDeclaration = SyntaxClassDeclarationFactory.CreateWithInheritClassTypeAndSuppressMessageAttributeByCheckId(
                resultTypeName,
                "ResultBase",
                1062,
                "Should not throw ArgumentNullExceptions from implicit operators.")
                                   .AddGeneratedCodeAttribute(ApiProjectOptions.ToolName, ApiProjectOptions.ToolVersion.ToString())
                                   .WithLeadingTrivia(SyntaxDocumentationFactory.CreateForResults(ApiOperation, FocusOnSegmentName));

            // Create members
            var memberDeclarations = CreateMembers(resultTypeName);

            // Add members to class
            classDeclaration = memberDeclarations.Aggregate(
                classDeclaration,
                (current, memberDeclaration) => current.AddMembers(memberDeclaration));

            // Add using statement to compilationUnit
            compilationUnit = compilationUnit.AddUsingStatements(
                ProjectContractResultFactory.CreateUsingList(
                    ApiOperation.Responses,
                    ApiProjectOptions.ApiOptions.Generator.Response.UseProblemDetailsAsDefaultBody));

            // Add the class to the namespace.
            @namespace = @namespace.AddMembers(classDeclaration);

            // Add namespace to compilationUnit
            compilationUnit = compilationUnit.AddMembers(@namespace);

            // Set code property
            Code = compilationUnit;
            return(true);
        }