コード例 #1
0
        private SyntaxList <AttributeListSyntax> DoRefactor(Dictionary <HttpStatusCode, AttributeMetadata> updatedList,
                                                            MethodDeclarationSyntax method)
        {
            /* These are the codes that will be added after updating the existing ones.
             * They MUST be set to false from this Dictionary as the existing attributes
             * are updated. */
            Dictionary <HttpStatusCode, bool> statusCodesToAdd = updatedList.ToDictionary(k => k.Key, v => true);
            /* First, update existing */
            SyntaxList <AttributeListSyntax> clonedAttributes = method.AttributeLists;

            for (int index = 0; index < clonedAttributes.Count; index++)
            {
                AttributeListSyntax att = clonedAttributes[index];
                if (att.GetName() != AttributeName)
                {
                    continue;
                }
                List <AttributeArgumentSyntax> arguments = GetAttributeArguments(att).ToList();

                HttpStatusCode code;
                if (!GetStatusCode(arguments[0], out code))
                {
                    throw new NotImplementedException();
                }
                AttributeMetadata newAttribute = updatedList[code];

                /* Create type definition */
                AttributeArgumentSyntax typeArgument = null;
                if (newAttribute.TypeName != null)
                {
                    typeArgument = SyntaxFactory.AttributeArgument(null, null,
                                                                   SyntaxFactory.TypeOfExpression(SyntaxFactory.ParseName(newAttribute.TypeName)));
                }
                SeparatedSyntaxList <AttributeArgumentSyntax> args =
                    new SeparatedSyntaxList <AttributeArgumentSyntax>();
                args = args.Add(arguments[0]);
                args = args.Add(arguments[1]);
                if (typeArgument != null)
                {
                    args = args.Add(typeArgument);
                }

                /* Compile arguments ito a new attribute */
                SeparatedSyntaxList <AttributeSyntax> attributeWhole = CreateAttribute(args);

                clonedAttributes = clonedAttributes.Replace(att, SyntaxFactory.AttributeList(att.OpenBracketToken,
                                                                                             att.Target, attributeWhole, att.CloseBracketToken));

                /* Update statusCodesToAdd */
                statusCodesToAdd[code] = false;
            }

            /* Return here if there are no attributes to add */
            if (!statusCodesToAdd.Any(v => v.Value))
            {
                return(clonedAttributes);
            }

            /* Add new attributes */
            IEnumerable <AttributeMetadata> attributes =
                updatedList.Where(kv => statusCodesToAdd[kv.Key]).Select(a => a.Value);

            foreach (AttributeMetadata attributeToAdd in attributes)
            {
                SeparatedSyntaxList <AttributeArgumentSyntax> args =
                    new SeparatedSyntaxList <AttributeArgumentSyntax>();
                /* Add status code */
                args = args.Add(SyntaxFactory.AttributeArgument(null, null,
                                                                SyntaxFactory.ParseName($"{StatusCodeEnumName}.{attributeToAdd.StatusCode}")));
                /* Add default description */
                args = args.Add(SyntaxFactory.AttributeArgument(null, null,
                                                                SyntaxFactory.ParseName($"\"{attributeToAdd.Description}\"")));
                /* And, if applicable, add type */
                if (attributeToAdd.TypeName != null)
                {
                    args = args.Add(SyntaxFactory.AttributeArgument(null, null,
                                                                    SyntaxFactory.TypeOfExpression(SyntaxFactory.ParseName(attributeToAdd.TypeName))));
                }

                AttributeListSyntax attribute =
                    SyntaxFactory.AttributeList(SyntaxFactory.Token(SyntaxKind.OpenBracketToken),
                                                null, CreateAttribute(args), SyntaxFactory.Token(SyntaxKind.CloseBracketToken));
                clonedAttributes = clonedAttributes.Add(attribute);
            }

            return(clonedAttributes);
        }