Exemplo n.º 1
0
        public override void NormalizeClientModel(ServiceClient client)
        {
            PackageName = PackageNameFromNamespace(client.Namespace);

            base.NormalizeClientModel(client);

            List <SyntheticType> syntheticTypes = new List <SyntheticType>();

            // Trim the package name from exported types; append a suitable qualifier, if needed, to avoid conflicts.
            var exportedTypes = new HashSet <object>();

            exportedTypes.UnionWith(client.EnumTypes);
            exportedTypes.UnionWith(client.Methods);
            exportedTypes.UnionWith(client.ModelTypes);

            var stutteringTypes = exportedTypes
                                  .Where(exported =>
                                         (exported is IType && (exported as IType).Name.StartsWith(PackageName, StringComparison.InvariantCultureIgnoreCase)) ||
                                         (exported is Method && (exported as Method).Name.StartsWith(PackageName, StringComparison.InvariantCultureIgnoreCase)));

            if (stutteringTypes.Count() > 0)
            {
                Logger.LogWarning(string.Format(CultureInfo.InvariantCulture, Resources.NamesStutter, stutteringTypes.Count()));
                stutteringTypes
                .ToList().ForEach(exported =>
                {
                    var name = exported is IType
                                        ? (exported as IType).Name
                                        : (exported as Method).Name;

                    Logger.LogWarning(string.Format(CultureInfo.InvariantCulture, Resources.StutteringName, name));

                    name = name.TrimPackageName(PackageName);

                    var nameInUse = exportedTypes
                                    .Any(et => (et is IType && (et as IType).Name.Equals(name)) || (et is Method && (et as Method).Name.Equals(name)));
                    if (exported is EnumType)
                    {
                        (exported as EnumType).Name = AttachTypeName(name, PackageName, nameInUse, "Enum");
                    }
                    else if (exported is CompositeType)
                    {
                        (exported as CompositeType).Name = AttachTypeName(name, PackageName, nameInUse, "Type");
                    }
                    else if (exported is Method)
                    {
                        (exported as Method).Name = AttachTypeName(name, PackageName, nameInUse, "Method");
                    }
                });
            }

            foreach (var method in client.Methods)
            {
                var scope = new VariableScopeProvider();
                foreach (var parameter in method.Parameters)
                {
                    parameter.Name = scope.GetVariableName(parameter.Name);
                }

                if (SyntheticType.ShouldBeSyntheticType(method.ReturnType.Body))
                {
                    SyntheticType st = new SyntheticType(method.ReturnType.Body);
                    if (syntheticTypes.Contains(st))
                    {
                        method.ReturnType = new Response(syntheticTypes.Find(i => i.Equals(st)), method.ReturnType.Headers);
                    }
                    else
                    {
                        syntheticTypes.Add(st);
                        client.ModelTypes.Add(st);
                        method.ReturnType = new Response(st, method.ReturnType.Headers);
                    }
                }
            }

            normalizedTypesForUserDefinedNames(client);
        }
Exemplo n.º 2
0
        public override void NormalizeClientModel(ServiceClient client)
        {
            PackageName = PackageNameFromNamespace(client.Namespace);

            base.NormalizeClientModel(client);

            List<SyntheticType> syntheticTypes = new List<SyntheticType>();

            // Trim the package name from exported types; append a suitable qualifier, if needed, to avoid conflicts.
            var exportedTypes = new HashSet<object>();
            exportedTypes.UnionWith(client.EnumTypes);
            exportedTypes.UnionWith(client.Methods);
            exportedTypes.UnionWith(client.ModelTypes);

            var stutteringTypes = exportedTypes
                                    .Where(exported =>
                                        (exported is IType && (exported as IType).Name.StartsWith(PackageName, StringComparison.InvariantCultureIgnoreCase)) ||
                                        (exported is Method && (exported as Method).Name.StartsWith(PackageName, StringComparison.InvariantCultureIgnoreCase)));

            if (stutteringTypes.Count() > 0)
            {
                Logger.LogWarning(string.Format(CultureInfo.InvariantCulture, Resources.NamesStutter, stutteringTypes.Count()));
                stutteringTypes
                    .ToList().ForEach(exported =>
                    {
                        var name = exported is IType
                                        ? (exported as IType).Name
                                        : (exported as Method).Name;

                        Logger.LogWarning(string.Format(CultureInfo.InvariantCulture, Resources.StutteringName, name));

                        name = name.TrimPackageName(PackageName);

                        var nameInUse = exportedTypes
                                            .Any(et => (et is IType && (et as IType).Name.Equals(name)) || (et is Method && (et as Method).Name.Equals(name)));
                        if (exported is EnumType)
                        {
                            (exported as EnumType).Name = AttachTypeName(name, PackageName, nameInUse, "Enum");
                        }
                        else if (exported is CompositeType)
                        {
                            (exported as CompositeType).Name = AttachTypeName(name, PackageName, nameInUse, "Type");
                        }
                        else if (exported is Method)
                        {
                            (exported as Method).Name = AttachTypeName(name, PackageName, nameInUse, "Method");
                        }
                    });
            }

            foreach (var method in client.Methods)
            {
                var scope = new VariableScopeProvider();
                foreach (var parameter in method.Parameters)
                {
                    parameter.Name = scope.GetVariableName(parameter.Name);
                }

                if (SyntheticType.ShouldBeSyntheticType(method.ReturnType.Body))
                {
                    SyntheticType st = new SyntheticType(method.ReturnType.Body);
                    if (syntheticTypes.Contains(st))
                    {
                        method.ReturnType = new Response(syntheticTypes.Find(i => i.Equals(st)), method.ReturnType.Headers);
                    }
                    else
                    {
                        syntheticTypes.Add(st);
                        client.ModelTypes.Add(st);
                        method.ReturnType = new Response(st, method.ReturnType.Headers);
                    }
                }
            }

            normalizedTypesForUserDefinedNames(client);
        }