Ejemplo n.º 1
0
        internal void Transform(CodeModelGo cmg)
        {
            Owner              = (MethodGroup as MethodGroupGo).ClientName;
            PackageName        = cmg.Namespace;
            NextAlreadyDefined = NextMethodExists(cmg.Methods.Cast <MethodGo>());

            var parameter = Parameters.ToList().Find(p => p.ModelType.PrimaryType(KnownPrimaryType.Stream) &&
                                                     !(p.Location == ParameterLocation.Body || p.Location == ParameterLocation.FormData));

            if (parameter != null)
            {
                throw new ArgumentException(string.Format(CultureInfo.InvariantCulture,
                                                          Resources.IllegalStreamingParameter, parameter.Name));
            }

            if (string.IsNullOrEmpty(Description))
            {
                Description = string.Format("sends the {0} request.", Name.ToString().ToPhrase());
            }

            if (IsLongRunningOperation())
            {
                Description += lroDescription;
            }
        }
Ejemplo n.º 2
0
        internal void Transform(CodeModelGo cmg)
        {
            Owner              = (MethodGroup as MethodGroupGo).ClientName;
            PackageName        = cmg.Namespace;
            NextAlreadyDefined = NextMethodExists(cmg.Methods.Cast <MethodGo>());
            var apiVersionParam =
                from p in Parameters
                let name = p.SerializedName
                           where name != null && name.IsApiVersion()
                           select p.DefaultValue.Value?.Trim(new[] { '"' });

            // When APIVersion is blank, it means that it was unavailable at the method level
            // and we should default back to whatever is present at the client level. However,
            // we will continue embedding that in each method to have broader support.
            APIVersion = apiVersionParam.SingleOrDefault();
            if (APIVersion == default(string))
            {
                APIVersion = cmg.ApiVersion;
            }

            var parameter = Parameters.ToList().Find(p => p.ModelType.PrimaryType(KnownPrimaryType.Stream) &&
                                                     !(p.Location == ParameterLocation.Body || p.Location == ParameterLocation.FormData));

            if (parameter != null)
            {
                throw new ArgumentException(string.Format(CultureInfo.InvariantCulture,
                                                          Resources.IllegalStreamingParameter, parameter.Name));
            }
            if (string.IsNullOrEmpty(Description))
            {
                Description = string.Format("sends the {0} request.", Name.ToString().ToPhrase());
            }

            // Registering Azure resource providers should only happen with Azure resource manager REST APIs
            // This depends on go-autorest here:
            // https://github.com/Azure/go-autorest/blob/c0eb859387e57a164bf64171da307e2ef8168b58/autorest/azure/rp.go#L30
            // As registering needs the Azure subscription ID, we take it from the operation path, on the
            // assumption that ARM APIs should include the subscription ID right after `subscriptions`
            RegisterRP = cmg.APIType.EqualsIgnoreCase("arm") && Url.Split("/").Any(p => p.EqualsIgnoreCase("subscriptions"));

            // Fixing the returnType in modeler
            // find all the non-error responses with non-empty body
            // we have to ignore those non-error responses with empty bodies, because there are quite plenty of swaggers with one response with body and the other without body
            Logger.Instance.Log(Category.Debug, $"All responses of {SerializedName}: {string.Join(", ", Responses.Select(kv => $"{(int)kv.Key}: {kv.Value?.Body?.Name}"))}");
            var nonErrorNonEmptyStatusCodes = NonErrorNonEmptyStatusCodeDict;

            Logger.Instance.Log(Category.Debug, $"Method {SerializedName} has the following non-error & non-empty responses (total {nonErrorNonEmptyStatusCodes.Count()}): {string.Join(", ", nonErrorNonEmptyStatusCodes.Select(resp => resp.Value?.Body?.Name))}");
            // categorize the models by its name
            var nonErrorNonEmptyResponses = NonErrorNonEmptyResponses.ToList();

            Logger.Instance.Log(Category.Debug, $"Non-error & non-empty models: {string.Join(", ", nonErrorNonEmptyResponses.Select(model => model.Body.Name))}");
            // fix the original return type from the modeler
            ReturnType = FixedReturnType();
            Logger.Instance.Log(Category.Debug, $"Return Type of {SerializedName}: {ReturnType?.Body?.Name}");
        }
Ejemplo n.º 3
0
        internal void Transform(CodeModelGo cmg)
        {
            Owner              = (MethodGroup as MethodGroupGo).ClientName;
            PackageName        = cmg.Namespace;
            NextAlreadyDefined = NextMethodExists(cmg.Methods.Cast <MethodGo>());

            var apiVersionParam =
                from p in Parameters
                let name = p.SerializedName
                           where name != null && name.IsApiVersion()
                           select p.DefaultValue.Value?.Trim(new[] { '"' });

            // When APIVersion is blank, it means that it was unavailable at the method level
            // and we should default back to whatever is present at the client level. However,
            // we will continue embedding that in each method to have broader support.
            APIVersion = apiVersionParam.SingleOrDefault();
            if (APIVersion == default(string))
            {
                APIVersion = cmg.ApiVersion;
            }

            var parameter = Parameters.ToList().Find(p => p.ModelType.PrimaryType(KnownPrimaryType.Stream) &&
                                                     !(p.Location == ParameterLocation.Body || p.Location == ParameterLocation.FormData));

            if (parameter != null)
            {
                throw new ArgumentException(string.Format(CultureInfo.InvariantCulture,
                                                          Resources.IllegalStreamingParameter, parameter.Name));
            }
            if (string.IsNullOrEmpty(Description))
            {
                Description = string.Format("sends the {0} request.", Name.ToString().ToPhrase());
            }

            // Registering Azure resource providers should only happen with Azure resource manager REST APIs
            // This depends on go-autorest here:
            // https://github.com/Azure/go-autorest/blob/c0eb859387e57a164bf64171da307e2ef8168b58/autorest/azure/rp.go#L30
            // As registering needs the Azure subscription ID, we take it from the operation path, on the
            // assumption that ARM APIs should include the subscription ID right after `subscriptions`
            RegisterRP = cmg.APIType.EqualsIgnoreCase("arm") && Url.Split("/").Any(p => p.EqualsIgnoreCase("subscriptions"));
        }
Ejemplo n.º 4
0
        internal void Transform(CodeModelGo cmg)
        {
            Owner              = (MethodGroup as MethodGroupGo).ClientName;
            PackageName        = cmg.Namespace;
            NextAlreadyDefined = NextMethodExists(cmg.Methods.Cast <MethodGo>());

            var apiVersionParam =
                from p in Parameters
                let name = p.SerializedName.Value
                           where name != null && name.IsApiVersion()
                           select p.DefaultValue.Value?.Trim(new[] { '"' });

            // When APIVersion is blank, it means that it was unavailable at the method level
            // and we should default back to whatever is present at the client level. However,
            // we will continue embedding that in each method to have broader support.
            APIVersion = apiVersionParam.SingleOrDefault();
            if (APIVersion == default(string))
            {
                APIVersion = cmg.ApiVersion;
            }

            var parameter = Parameters.ToList().Find(p => p.ModelType.PrimaryType(KnownPrimaryType.Stream) &&
                                                     !(p.Location == ParameterLocation.Body || p.Location == ParameterLocation.FormData));

            if (parameter != null)
            {
                throw new ArgumentException(string.Format(CultureInfo.InvariantCulture,
                                                          Resources.IllegalStreamingParameter, parameter.Name));
            }

            if (string.IsNullOrEmpty(Description))
            {
                Description = string.Format("sends the {0} request.", Name.ToString().ToPhrase());
            }

            if (IsLongRunningOperation())
            {
                Description += lroDescription;
            }
        }
Ejemplo n.º 5
0
        internal void Transform(CodeModelGo cmg)
        {
            var originalName = Name.Value;

            Name = Name.Value.TrimPackageName(cmg.Namespace);

            // use the API version as the prefix to the group name; this is because
            // in batch mode the generator isn't recycled per batch so you can end up
            // with erroneous collisions.  note that for composite packages (web)
            // there will be no API version so fall back to using the batch tag.
            var prefix = cmg.ApiVersion;

            if (string.IsNullOrWhiteSpace(prefix))
            {
                prefix = cmg.Tag;
            }

            // keep a list of all method group names as trimming the package name
            // can introduce collisions.  if there's a collision append "Group" to
            // the name.  unfortunately we can't do this in the namer as we don't
            // have access to the package name.
            if (s_AllNames.Contains($"{prefix}_{Name.Value}"))
            {
                Name += "Group";
            }
            s_AllNames.Add($"{prefix}_{Name.Value}");

            if (Name != originalName)
            {
                // fix up the method group names
                cmg.Methods.Where(m => m.Group.Value == originalName)
                .ForEach(m =>
                {
                    m.Group = Name;
                });
            }

            ClientName = string.IsNullOrEmpty(Name)
                            ? cmg.BaseClient
                            : TypeName.Value.IsNamePlural(cmg.Namespace)
                                             ? Name + "Client"
                                             : (Name + "Client").TrimPackageName(cmg.Namespace);

            Documentation = string.Format("{0} is the {1} ", ClientName,
                                          string.IsNullOrEmpty(cmg.Documentation)
                                        ? string.Format("client for the {0} methods of the {1} service.", TypeName, cmg.ServiceName)
                                        : cmg.Documentation.ToSentence());

            PackageName            = cmg.Namespace;
            BaseClient             = cmg.BaseClient;
            GlobalParameters       = cmg.GlobalParameters;
            HelperGlobalParameters = cmg.HelperGlobalParameters;

            //Imports
            var imports = new HashSet <string>();

            imports.UnionWith(CodeNamerGo.Instance.AutorestImports);
            imports.UnionWith(CodeNamerGo.Instance.StandardImports);

            cmg.Methods.Where(m => m.Group.Value == Name)
            .ForEach(m =>
            {
                var mg = m as MethodGo;
                if ((CodeModel as CodeModelGo).ShouldValidate && !mg.ParameterValidations.IsNullOrEmpty())
                {
                    imports.UnionWith(CodeNamerGo.Instance.ValidationImport);
                }
                mg.ParametersGo.ForEach(p => p.AddImports(imports));
                if (mg.HasReturnValue() && !mg.ReturnValue().Body.PrimaryType(KnownPrimaryType.Stream))
                {
                    mg.ReturnType.Body.AddImports(imports);
                }
                if (mg.IsNextMethod)
                {
                    imports.UnionWith(CodeNamerGo.Instance.PageableImports);
                }
            });

            foreach (var p in cmg.Properties)
            {
                p.ModelType.AddImports(imports);
            }

            imports.OrderBy(i => i);
            Imports = imports;
        }
Ejemplo n.º 6
0
        internal void Transform(CodeModelGo cmg)
        {
            var originalName = Name.Value;

            Name = Name.FixedValue.TrimPackageName(cmg.Namespace);
            if (Name != originalName)
            {
                // fix up the method group names
                cmg.Methods.Where(m => m.Group.Value == originalName)
                .ForEach(m =>
                {
                    m.Group = Name;
                });
            }

            ClientName = string.IsNullOrEmpty(Name)
                      ? cmg.BaseClient
                      : TypeName.Value.IsNamePlural(cmg.Namespace)
                                       ? Name + "Client"
                                       : (Name + "Client").TrimPackageName(cmg.Namespace);

            Documentation = string.Format("{0} is the {1} ", ClientName,
                                          string.IsNullOrEmpty(cmg.Documentation)
                                  ? string.Format("client for the {0} methods of the {1} service.", TypeName, cmg.ServiceName)
                                  : cmg.Documentation.ToSentence());

            PackageName                   = cmg.Namespace;
            BaseClient                    = cmg.BaseClient;
            GlobalParameters              = cmg.GlobalParameters;
            HelperGlobalParameters        = cmg.HelperGlobalParameters;
            GlobalDefaultParameters       = cmg.GlobalDefaultParameters;
            HelperGlobalDefaultParameters = cmg.HelperGlobalDefaultParameters;
            ConstGlobalDefaultParameters  = cmg.ConstGlobalDefaultParameters;



            //Imports
            var imports = new HashSet <string>();

            imports.UnionWith(CodeNamerGo.Instance.AutorestImports);
            imports.UnionWith(CodeNamerGo.Instance.StandardImports);

            bool validationImports = false;

            cmg.Methods.Where(m => m.Group.Value == Name)
            .ForEach(m =>
            {
                var mg = m as MethodGo;
                mg.ParametersGo.ForEach(p => p.AddImports(imports));
                if (mg.HasReturnValue() && !mg.ReturnValue().Body.PrimaryType(KnownPrimaryType.Stream))
                {
                    mg.ReturnType.Body.AddImports(imports);
                }
                if (!string.IsNullOrEmpty(mg.ParameterValidations))
                {
                    validationImports = true;
                }
            });

            if (validationImports)
            {
                imports.UnionWith(CodeNamerGo.Instance.ValidationImport);
            }

            foreach (var p in cmg.Properties)
            {
                p.ModelType.AddImports(imports);
            }

            imports.OrderBy(i => i);
            Imports = imports;
        }