Exemple #1
0
        public override IType NormalizeTypeDeclaration(IType type)
        {
            if (type == null)
            {
                return(null);
            }

            if (type is ITypeModel)
            {
                return(type);
            }
            if (_visited.ContainsKey(type))
            {
                return(_visited[type]);
            }

            if (type is PrimaryType)
            {
                _visited[type] = new PrimaryTypeModel(type as PrimaryType);
                return(_visited[type]);
            }
            if (type is SequenceType)
            {
                SequenceTypeModel model = new SequenceTypeModel(type as SequenceType);
                _visited[type] = model;
                return(NormalizeSequenceType(model));
            }
            if (type is DictionaryType)
            {
                DictionaryTypeModel model = new DictionaryTypeModel(type as DictionaryType);
                _visited[type] = model;
                return(NormalizeDictionaryType(model));
            }
            if (type is CompositeType)
            {
                CompositeTypeModel model = NewCompositeTypeModel(type as CompositeType);
                _visited[type] = model;
                return(NormalizeCompositeType(model));
            }
            if (type is EnumType)
            {
                EnumTypeModel model = NewEnumTypeModel(type as EnumType);
                _visited[type] = model;
                return(NormalizeEnumType(model));
            }


            throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture,
                                                          "Type {0} is not supported.", type.GetType()));
        }
Exemple #2
0
        /// <summary>
        /// Changes paginated method signatures to return Page type.
        /// </summary>
        /// <param name="serviceClient"></param>
        /// <param name="pageClasses"></param>
        public virtual void NormalizePaginatedMethods(ServiceClient serviceClient, IDictionary <KeyValuePair <string, string>, string> pageClasses)
        {
            if (serviceClient == null)
            {
                throw new ArgumentNullException("serviceClient");
            }

            var convertedTypes = new Dictionary <ITypeModel, ITypeModel>();

            foreach (var method in serviceClient.Methods.Where(m => m.Extensions.ContainsKey(AzureExtensions.PageableExtension)))
            {
                string nextLinkString;
                string pageClassName = GetPagingSetting(method.Extensions, pageClasses, out nextLinkString);
                if (string.IsNullOrEmpty(pageClassName))
                {
                    continue;
                }
                if (string.IsNullOrEmpty(nextLinkString))
                {
                    method.Extensions[AzureExtensions.PageableExtension] = null;
                }

                foreach (var responseStatus in method.Responses.Where(r => r.Value.Body is CompositeTypeModel).Select(s => s.Key).ToArray())
                {
                    var compositType = (CompositeTypeModel)method.Responses[responseStatus].Body;
                    var sequenceType = compositType.Properties.Select(p => p.Type).FirstOrDefault(t => t is SequenceTypeModel) as SequenceTypeModel;

                    // if the type is a wrapper over page-able response
                    if (sequenceType != null)
                    {
                        ITypeModel pagedResult;
                        pagedResult = new SequenceTypeModel
                        {
                            ElementType = sequenceType.ElementType,
                            NameFormat  = "List<{0}>"
                        };

                        convertedTypes[(ITypeModel)method.Responses[responseStatus].Body] = pagedResult;
                        method.Responses[responseStatus] = new Response(pagedResult, method.Responses[responseStatus].Headers);
                    }
                }

                if (convertedTypes.ContainsKey((ITypeModel)method.ReturnType.Body))
                {
                    method.ReturnType = new Response(convertedTypes[(ITypeModel)method.ReturnType.Body], method.ReturnType.Headers);
                }
            }

            Extensions.RemoveUnreferencedTypes(serviceClient, new HashSet <string>(convertedTypes.Keys.Cast <CompositeTypeModel>().Select(t => t.Name)));
        }
        /// <summary>
        /// Changes paginated method signatures to return Page type.
        /// </summary>
        /// <param name="serviceClient"></param>
        /// <param name="pageClasses"></param>
        public virtual void NormalizePaginatedMethods(ServiceClient serviceClient, IDictionary<KeyValuePair<string, string>, string> pageClasses)
        {
            if (serviceClient == null)
            {
                throw new ArgumentNullException("serviceClient");
            }

            var convertedTypes = new Dictionary<ITypeModel, ITypeModel>();

            foreach (var method in serviceClient.Methods.Where(m => m.Extensions.ContainsKey(AzureExtensions.PageableExtension)))
            {
                string nextLinkString;
                string pageClassName = GetPagingSetting(method.Extensions, pageClasses, out nextLinkString);
                if (string.IsNullOrEmpty(pageClassName))
                {
                    continue;
                }
                if (string.IsNullOrEmpty(nextLinkString))
                {
                    method.Extensions[AzureExtensions.PageableExtension] = null;
                }

                foreach (var responseStatus in method.Responses.Where(r => r.Value.Body is CompositeTypeModel).Select(s => s.Key).ToArray())
                {
                    var compositType = (CompositeTypeModel)method.Responses[responseStatus].Body;
                    var sequenceType = compositType.Properties.Select(p => p.Type).FirstOrDefault(t => t is SequenceTypeModel) as SequenceTypeModel;

                    // if the type is a wrapper over page-able response
                    if (sequenceType != null)
                    {
                        ITypeModel pagedResult;
                        pagedResult = new SequenceTypeModel
                        {
                            ElementType = sequenceType.ElementType,
                            NameFormat = "List<{0}>"
                        };

                        convertedTypes[(ITypeModel)method.Responses[responseStatus].Body] = pagedResult;
                        method.Responses[responseStatus] = new Response(pagedResult, method.Responses[responseStatus].Headers);
                    }
                }

                if (convertedTypes.ContainsKey((ITypeModel) method.ReturnType.Body))
                {
                    method.ReturnType = new Response(convertedTypes[(ITypeModel)method.ReturnType.Body], method.ReturnType.Headers);
                }
            }

            Extensions.RemoveUnreferencedTypes(serviceClient, new HashSet<string>(convertedTypes.Keys.Cast<CompositeTypeModel>().Select(t => t.Name)));
        }