Exemple #1
0
 public static CsharpMethod Clone(CsharpMethod method)
 {
     return(new CsharpMethod
     {
         Allow404 = method.Allow404,
         Path = method.Path,
         RequestType = method.RequestType,
         ReturnDescription = method.ReturnDescription,
         Arguments = method.Arguments,
         CallTypeGeneric = method.CallTypeGeneric,
         DescriptorType = method.DescriptorType,
         DescriptorTypeGeneric = method.DescriptorTypeGeneric,
         Documentation = method.Documentation,
         FullName = method.FullName,
         HttpMethod = method.HttpMethod,
         Parts = method.Parts,
         QueryStringParamName = method.QueryStringParamName,
         RequestTypeGeneric = method.RequestTypeGeneric,
         RequestTypeUnmapped = method.RequestTypeUnmapped,
         ReturnType = method.ReturnType,
         ReturnTypeGeneric = method.ReturnTypeGeneric,
         Unmapped = method.Unmapped,
         Url = method.Url
     });
 }
 public static CsharpMethod Clone(CsharpMethod method)
 {
     return new CsharpMethod
     {
         Allow404 = method.Allow404,
         Path = method.Path,
         RequestType = method.RequestType,
         ReturnDescription = method.ReturnDescription,
         Arguments = method.Arguments,
         CallTypeGeneric = method.CallTypeGeneric,
         DescriptorType = method.DescriptorType,
         DescriptorTypeGeneric = method.DescriptorTypeGeneric,
         Documentation = method.Documentation,
         FullName = method.FullName,
         HttpMethod = method.HttpMethod,
         Parts = method.Parts,
         QueryStringParamName = method.QueryStringParamName,
         RequestTypeGeneric = method.RequestTypeGeneric,
         RequestTypeUnmapped = method.RequestTypeUnmapped,
         ReturnType = method.ReturnType,
         ReturnTypeGeneric = method.ReturnTypeGeneric,
         Unmapped = method.Unmapped,
         Url = method.Url
     };
 }
        //Patches a method name for the exceptions (IndicesStats needs better unique names for all the url endpoints)
        //or to get rid of double verbs in an method name i,e ClusterGetSettingsGet > ClusterGetSettings
        public static void PatchMethod(CsharpMethod method)
        {
            Func<string, bool> ms = (s) => method.FullName.StartsWith(s);
            Func<string, bool> mc = (s) => method.FullName.Contains(s);
            Func<string, bool> pc = (s) => method.Path.Contains(s);

            if (ms("Indices") && !pc("{index}"))
                method.FullName = (method.FullName + "ForAll").Replace("AsyncForAll", "ForAllAsync");

            if (ms("Nodes") && !pc("{node_id}"))
                method.FullName = (method.FullName + "ForAll").Replace("AsyncForAll", "ForAllAsync");

            //remove duplicate occurance of the HTTP method name
            var m = method.HttpMethod.ToPascalCase();
            method.FullName =
                Regex.Replace(method.FullName, m, (a) => a.Index != method.FullName.IndexOf(m) ? "" : m);

            string manualOverride;
            var key = method.QueryStringParamName.Replace("RequestParameters", "");
            if (MethodNameOverrides.TryGetValue(key, out manualOverride))
                method.QueryStringParamName = manualOverride + "RequestParameters";

            method.DescriptorType = method.QueryStringParamName.Replace("RequestParameters", "Descriptor");
            method.RequestType = method.QueryStringParamName.Replace("RequestParameters", "Request");
            string requestGeneric;
            if (KnownRequests.TryGetValue("I" + method.RequestType, out requestGeneric))
                method.RequestTypeGeneric = requestGeneric;
            else method.RequestTypeUnmapped = true;

            method.Allow404 = ApiEndpointsThatAllow404.Endpoints.Contains(method.DescriptorType.Replace("Descriptor", ""));

            string generic;
            if (KnownDescriptors.TryGetValue(method.DescriptorType, out generic))
                method.DescriptorTypeGeneric = generic;
            else method.Unmapped = true;

            try
            {
                IEnumerable<string> skipList = new List<string>();
                IDictionary<string, string> renameList = new Dictionary<string, string>();

                var typeName = "CodeGeneration.Watcher.Overrides.Descriptors." + method.DescriptorType + "Overrides";
                var type = _assembly.GetType(typeName);
                if (type != null)
                {
                    var overrides = Activator.CreateInstance(type) as IDescriptorOverrides;
                    if (overrides != null)
                    {
                        skipList = overrides.SkipQueryStringParams ?? skipList;
                        renameList = overrides.RenameQueryStringParams ?? renameList;
                    }
                }

                var globalQueryStringRenames = new Dictionary<string, string>
                {
                    {"_source", "source_enabled"},
                    {"_source_include", "source_include"},
                    {"_source_exclude", "source_exclude"},
                };

                foreach (var kv in globalQueryStringRenames)
                    if (!renameList.ContainsKey(kv.Key))
                        renameList[kv.Key] = kv.Value;

                var patchedParams = new Dictionary<string, ApiQueryParameters>();
                foreach (var kv in method.Url.Params)
                {
                    if (kv.Value.OriginalQueryStringParamName.IsNullOrEmpty())
                        kv.Value.OriginalQueryStringParamName = kv.Key;
                    if (skipList.Contains(kv.Key))
                        continue;

                    string newName;
                    if (!renameList.TryGetValue(kv.Key, out newName))
                    {
                        patchedParams.Add(kv.Key, kv.Value);
                        continue;
                    }

                    patchedParams.Add(newName, kv.Value);

                    if (newName == "source_enabled")
                    {
                        kv.Value.DeprecatedInFavorOf = "EnableSource";
                        patchedParams.Add("enable_source", new ApiQueryParameters
                        {
                            Description = kv.Value.Description,
                            Options = kv.Value.Options,
                            Type = "boolean",
                            OriginalQueryStringParamName = "_source"
                        });
                    }
                }

                method.Url.Params = patchedParams;
            }
            // ReSharper disable once EmptyGeneralCatchClause
            catch
            {
            }
        }