Пример #1
0
        public static async Task <string> CreateComplexTypeClass(MethodDeclarationSyntax wcfMethod, string methodName, bool isActiveWcfMethod)
        {
            if (!isActiveWcfMethod)
            {
                return("");
            }

            var parameters = wcfMethod.ParameterList.Parameters;

            // add return type as class property if has 'out' keyword
            if (!OutKeywordGenerator.AnyHaveOutKeyword(parameters))
            {
                return("");
            }

            // create complex class
            var className = ComplexTypeNamesMapper.MapToClientComplexClassName(methodName);
            var complexClassDeclaration = await MultipleComplexTypesGenerator.CreateClassFromComplexTypes(parameters, className);

            // create return type property
            var wcfReturnType          = wcfMethod.ReturnType.NormalizeWhitespace().ToFullString();
            var returnTypePropertyCode = $"public {wcfReturnType} Result {{ get; set;}}";
            var returnTypeProperty     = (await CodeParser.ConvertToMemberDeclarations(returnTypePropertyCode)).Single();

            // add return type property to complex class
            var members = complexClassDeclaration.Members.Add(returnTypeProperty);

            complexClassDeclaration = complexClassDeclaration.WithMembers(members);
            var complexTypeClass = complexClassDeclaration?.NormalizeWhitespace().ToFullString();

            return(complexTypeClass);
        }
        private static async Task <IEnumerable <MemberDeclarationSyntax> > CreateMethods(string controllerName,
                                                                                         MethodDeclarationSyntax wcfMethod, string wcfClassName, IDictionary <string, string> duplicateMethodNamesMap, bool isAsmx)
        {
            var members = new List <MemberDeclarationSyntax>();

            var wcfMethodName = wcfMethod.Identifier.ValueText;
            var wcfParameters = wcfMethod.ParameterList.Parameters;

            // create method & parameter names
            var methodName = DuplicateMethodNamesGenerator.TransformMethodNameIfDuplicate(duplicateMethodNamesMap, wcfMethodName, wcfParameters);
            var parameters = ServiceCodeParametersGenerator.CreateParameters(wcfParameters);

            parameters = ServiceCodeMultipleComplexTypesGenerator.TransformMultipleComplexTypeParameters(parameters, methodName);

            // create service type for method comment
            var serviceType = ServiceNamesMapper.GetServiceTypeName(isAsmx);

            // create http verb
            var httpVerb = CreateHttpVerb(wcfMethodName, parameters);

            // create route name
            var routeUriTemplate = ServiceNamesMapper.MapToRouteUriTemplate(controllerName, methodName);

            // create return type
            var wcfReturnType = wcfMethod
                                .ReturnType
                                .NormalizeWhitespace()
                                .ToFullString();
            var returnType = ServiceCodeOutKeywordGenerator.TransformOutKeywordReturnType(wcfReturnType, wcfParameters, methodName);

            // create method block arguments
            var arguments = CreateWcfArguments(wcfParameters);

            arguments = ServiceCodeMultipleComplexTypesGenerator.TransformMultipleComplexTypeArguments(arguments, wcfParameters, methodName);

            // create method block
            var block = CreateMethodBlock(wcfClassName, wcfMethodName, arguments, returnType);

            block = ServiceCodeOutKeywordGenerator.TransformBlockWithOutKeyword(block, wcfParameters, arguments, methodName);

            // create new method
            var method = await CreateMethodDeclaration(methodName, httpVerb, routeUriTemplate, parameters, block, returnType, serviceType);

            // add nested class model if have multiple complex params
            var className    = ComplexTypeNamesMapper.MapToServiceComplexClassType(methodName);
            var complexClass = await MultipleComplexTypesGenerator.CreateClassFromComplexTypes(wcfParameters, className);

            if (complexClass != null)
            {
                complexClass = ServiceCodeOutKeywordGenerator.TransformComplexClassWithOutKeyword(complexClass, wcfParameters, wcfReturnType);

                members.Add(complexClass);
            }

            // add new method
            members.Add(method);

            return(members);
        }
Пример #3
0
        private static IEnumerable <StatementSyntax> CreateEndStatements(IEnumerable <string> parameterNames, SeparatedSyntaxList <ParameterSyntax> wcfParameters,
                                                                         MethodDeclarationSyntax wcfMethod)
        {
            string CreatePropertyStatement(string variableName, bool isInitialized = false)
            {
                var initializeType = isInitialized ? "var " : "";
                var propertyName   = ComplexTypeNamesMapper.MapToComplexClassPropertyName(variableName);

                var statement = $"{initializeType}{variableName} = deserializeObject.{propertyName};";

                return(statement);
            }

            var methodName       = wcfMethod.Identifier.ValueText;
            var complexClassType = ComplexTypeNamesMapper.MapToClientComplexClassName(methodName);

            var jsonDeserializeStatements = new List <string>
            {
                $"var deserializeObject = JsonConvert.DeserializeObject<{complexClassType}>(jsonResponse, _jsonSerializerSettings);",
                CreatePropertyStatement("result", isInitialized: true),
            };

            var propertyStatements = wcfParameters
                                     .Where(parameter =>
            {
                var parameterName = parameter.Identifier.ValueText;
                var hasOutKeyword = parameterNames.Contains(parameterName);

                return(hasOutKeyword);
            })
                                     .Select(parameter =>
            {
                var parameterName = parameter.Identifier.ValueText;
                var statement     = CreatePropertyStatement(parameterName);

                return(statement);
            });

            var endStatements = jsonDeserializeStatements
                                .Union(propertyStatements)
                                .Select(statement =>
            {
                var statementDeclaration = SyntaxFactory.ParseStatement(statement);

                return(statementDeclaration);
            });

            return(endStatements);
        }
Пример #4
0
        public static string TransformOutKeywordReturnType(string wcfReturnType, SeparatedSyntaxList <ParameterSyntax> wcfParameters, string wcfMethodName)
        {
            var isVoidReturnType = wcfReturnType == "void";

            if (!OutKeywordGenerator.AnyHaveOutKeyword(wcfParameters) || isVoidReturnType)
            {
                return(wcfReturnType);
            }

            var complexVariableName = ComplexTypeNamesMapper.MapToServiceComplexClassType(wcfMethodName);

            var returnType = complexVariableName;

            return(returnType);
        }
Пример #5
0
        private static BlockSyntax TransformReturnStatement(BlockSyntax block, string methodName)
        {
            var lastStatement = block.Statements.Last();

            if (!(lastStatement is ReturnStatementSyntax))
            {
                return(block);
            }

            var complexVariableName       = ComplexTypeNamesMapper.MapToComplexClassParameterName(methodName);
            var outKeywordReturnStatement = $"return Ok({complexVariableName});";
            var newStatements             = block.Statements.Replace(lastStatement, SyntaxFactory.ParseStatement(outKeywordReturnStatement));

            var newBlock = block.WithStatements(newStatements);

            return(newBlock);
        }
Пример #6
0
        public static List <SyntaxNodeOrToken> TransformMultipleComplexTypeArguments(List <SyntaxNodeOrToken> arguments,
                                                                                     SeparatedSyntaxList <ParameterSyntax> wcfParameters, string wcfMethodName)
        {
            var complexParameters = MultipleComplexTypesGenerator.FindMultipleComplexTypesOrOutKeyword(wcfParameters);

            if (!complexParameters.Any())
            {
                return(arguments);
            }

            // replace all complex type arguments with complex type class properties
            // if any arguments already have 'out' keyword then will be ignored
            var newArguments = arguments
                               .Select(nodeOrToken =>
            {
                var argument   = (ArgumentSyntax)nodeOrToken;
                var isArgument = argument != null;

                if (!isArgument)
                {
                    return(nodeOrToken);
                }

                var argumentName = argument
                                   .NormalizeWhitespace()
                                   .ToFullString();

                var isComplexArgument = complexParameters
                                        .Select(parameter => parameter.Identifier.ValueText)
                                        .Contains(argumentName);

                if (!isComplexArgument)
                {
                    return(argument);
                }

                var complexTypeArgumentName = ComplexTypeNamesMapper.MapToComplexTypeArgument(wcfMethodName, argumentName);
                var complexTypeArgument     = SyntaxFactory.Argument(SyntaxFactory.IdentifierName(complexTypeArgumentName));

                return(complexTypeArgument);
            })
                               .ToList();

            return(newArguments);
        }
Пример #7
0
        private static IEnumerable <StatementSyntax> CreateEndStatements(IEnumerable <string> argumentNames, string wcfMethodName)
        {
            var resultStatement = $"{ComplexTypeNamesMapper.MapToComplexTypeArgument(wcfMethodName, "Result")} = result;";

            var endStatements = argumentNames
                                .Select(argumentName =>
            {
                var complexTypeArgument = ComplexTypeNamesMapper.MapToComplexTypeArgument(wcfMethodName, argumentName);

                var statement            = $"{complexTypeArgument} = {argumentName};";
                var statementDeclaration = SyntaxFactory.ParseStatement(statement);

                return(statementDeclaration);
            })
                                .Prepend(SyntaxFactory.ParseStatement(resultStatement));

            return(endStatements);
        }
Пример #8
0
        public static SeparatedSyntaxList <ParameterSyntax> TransformMultipleComplexTypeParameters(SeparatedSyntaxList <ParameterSyntax> originalParameters, string methodName)
        {
            // handle if parameters have more two or more (i.e. multiple) complex types

            var complexParameters = MultipleComplexTypesGenerator.FindMultipleComplexTypesOrOutKeyword(originalParameters);

            if (!complexParameters.Any())
            {
                return(originalParameters);
            }

            var parameters = new SeparatedSyntaxList <ParameterSyntax>();

            // remove all complex types from parameters
            var complexNames = complexParameters
                               .Select(complex => complex.Identifier.ToFullString());

            var filteredParameters = originalParameters
                                     .Where(parameter =>
            {
                var name = parameter.Identifier.ToFullString();

                return(complexNames.All(complexName => complexName != name));
            });

            parameters = parameters.AddRange(filteredParameters);

            // add single complex class type parameter
            var complexType          = ComplexTypeNamesMapper.MapToServiceComplexClassType(methodName);
            var complexParameterName = ComplexTypeNamesMapper.MapToComplexClassParameterName(methodName);

            var complexTypeClass = SyntaxFactory.Parameter(SyntaxFactory.Identifier(complexParameterName))
                                   .WithType(SyntaxFactory.IdentifierName(complexType));

            // insert complex type before any optional parameters with default values
            // otherwise insert at the end
            var insertIndex = ParametersGenerator.FindIndexBeforeFirstOptionalParam(parameters);

            parameters = parameters.Insert(insertIndex, complexTypeClass);

            return(parameters);
        }