Beispiel #1
0
        private void ParameterlessIndicesTypesConstructor(List <Constructor> ctors, string m)
        {
            var         generic = RequestTypeGeneric?.Replace("<", "").Replace(">", "");
            string      doc;
            Constructor c;

            if (string.IsNullOrEmpty(generic))
            {
                doc = $@"/// <summary>{Url.Path}</summary>";
                c   = new Constructor {
                    Generated = $"public {m}()", Description = doc
                };
            }
            else
            {
                doc =
                    $"///<summary>{Url.Path}<para><typeparamref name=\"{generic}\"/> describes an elasticsearch document type from which the index, type and id can be inferred</para></summary>";
                c = new Constructor {
                    Generated = $"public {m}() : this(typeof({generic}), typeof({generic}))", Description = doc,
                };
            }

            c.Body = IsDocumentRequest ? $"Q(\"routing\", new Routing(() => AutoRouteDocument()));" : string.Empty;
            ctors.Add(c);
        }
Beispiel #2
0
        public IEnumerable <Constructor> DescriptorConstructors()
        {
            var ctors = new List <Constructor>();

            if (IsPartless)
            {
                return(ctors);
            }

            var m = DescriptorType;

            foreach (var url in Url.Paths)
            {
                var requiredUrlRouteParameters = Url.Parts
                                                 .Where(p => !ApiUrl.BlackListRouteValues.Contains(p.Key))
                                                 .Where(p => p.Value.Required)
                                                 .Where(p => url.Contains($"{{{p.Value.Name}}}"))
                                                 .OrderBy(kv => url.IndexOf($"{{{kv.Value.Name}}}", StringComparison.Ordinal));

                var par     = string.Join(", ", requiredUrlRouteParameters.Select(p => $"{ClrParamType(p.Value.ClrTypeName)} {p.Key}"));
                var routing = string.Empty;
                //Routes that take {indices}/{types} and both are optional
                if (!requiredUrlRouteParameters.Any() && IndicesAndTypes)
                {
                    AddParameterlessIndicesTypesConstructor(ctors, m);
                    continue;
                }
                if (requiredUrlRouteParameters.Any())
                {
                    routing = "r=>r." + string.Join(".", requiredUrlRouteParameters
                                                    .Select(p => new
                    {
                        route = p.Key,
                        call  = p.Value.Required ? "Required" : "Optional",
                        v     = p.Key == "metric"
                                                                ? $"(Metrics){p.Key}"
                                                                : p.Key == "index_metric"
                                                                        ? $"(IndexMetrics){p.Key}"
                                                                        : p.Key
                    })
                                                    .Select(p => $"{p.call}(\"{p.route}\", {p.v})")
                                                    );
                }
                var doc = $@"/// <summary>{url}</summary>";
                if (requiredUrlRouteParameters.Any())
                {
                    doc += "\r\n\t\t" + string.Join("\r\n\t\t",
                                                    requiredUrlRouteParameters.Select(p => $"///<param name=\"{p.Key}\"> this parameter is required</param>"));
                }

                var generated = $"public {m}({par}) : base({routing})";
                var body      = IsDocumentRequest ? $"Q(\"routing\", new Routing(() => AutoRouteDocument()));" : string.Empty;

                // Add typeof(T) as the default type when only index specified
                if ((m == "DeleteByQueryDescriptor" || m == "UpdateByQueryDescriptor") && requiredUrlRouteParameters.Count() == 1 &&
                    !string.IsNullOrEmpty(RequestTypeGeneric))
                {
                    var generic = RequestTypeGeneric.Replace("<", "").Replace(">", "");
                    generated = $"public {m}({par}) : base({routing}.Required(\"type\", (Types)typeof({generic})))";
                }

                if (m == "SearchShardsDescriptor" && !string.IsNullOrEmpty(RequestTypeGeneric))
                {
                    var generic = RequestTypeGeneric.Replace("<", "").Replace(">", "");
                    doc       = AppendToSummary(doc, ". Will infer the index from the generic type");
                    generated = $"public {m}({par}) : base(r => r.Optional(\"index\", (Indices)typeof({generic})))";
                }

                // Use generic T to set the Indices and Types by default in the ctor
                if (m == "PutDatafeedDescriptor" || m == "UpdateDatafeedDescriptor")
                {
                    var generic = "T";
                    doc       = AppendToSummary(doc, ". Will infer the index and type from the generic type");
                    generated = $"public {m}({par}) : base({routing})";
                    body      = $"{{ Self.Indices = typeof({generic}); Self.Types = typeof({generic}); {body} }}";
                }

                var c = new Constructor
                {
                    Generated   = generated,
                    Description = doc,
                    Body        = !body.IsNullOrEmpty() && !body.StartsWith("{") ? "=> " + body : body
                };
                ctors.Add(c);
            }
            if (IsDocumentPath && !string.IsNullOrEmpty(DescriptorTypeGeneric))
            {
                var documentPathGeneric = Regex.Replace(DescriptorTypeGeneric, @"^<?([^\s,>]+).*$", "$1");
                var doc = $"/// <summary>{Url.Path}</summary>";
                doc += "\r\n\t\t"
                       + $"///<param name=\"document\"> describes an elasticsearch document of type <typeparamref name=\"{documentPathGeneric}\"/> from which the index, type and id can be inferred</param>";
                var documentRoute =
                    "r=>r.Required(\"index\", document.Self.Index).Required(\"type\", document.Self.Type).Required(\"id\", document.Self.Id)";
                var documentFromPath = $"partial void DocumentFromPath({documentPathGeneric} document);";
                var autoRoute        = IsDocumentRequest ? $"Q(\"routing\", new Routing(() => AutoRouteDocument() ?? document.Document));" : string.Empty;
                var c = new Constructor
                {
                    AdditionalCode = documentFromPath,
                    Generated      =
                        $"public {m}(DocumentPath<{documentPathGeneric}> document) : base({documentRoute})",
                    Description = doc,
                    Body        = $"{{ this.DocumentFromPath(document.Document); {autoRoute}}}"
                };
                ctors.Add(c);
            }

            return(ctors.DistinctBy(c => c.Generated));
        }
Beispiel #3
0
        public IEnumerable <Constructor> RequestConstructors()
        {
            var ctors = new List <Constructor>();

            if (IsPartless)
            {
                return(ctors);
            }

            // Do not generate ctors for scroll apis
            // Scroll ids should always be passed as part of the request body and enforced via manual ctors
            if (IsScroll)
            {
                return(ctors);
            }

            var m = RequestType;

            foreach (var url in Url.Paths)
            {
                var urlRouteParameters = Url.Parts
                                         .Where(p => !ApiUrl.BlackListRouteValues.Contains(p.Key))
                                         .Where(p => url.Contains($"{{{p.Value.Name}}}"))
                                         .OrderBy(kv => url.IndexOf($"{{{kv.Value.Name}}}", StringComparison.Ordinal));

                var par     = string.Join(", ", urlRouteParameters.Select(p => $"{ClrParamType(p.Value.ClrTypeName)} {p.Key}"));
                var routing = string.Empty;

                //Routes that take {indices}/{types} and both are optional
                //we rather not generate a parameterless constructor and force folks to call Indices.All
                if (!urlRouteParameters.Any() && IndicesAndTypes)
                {
                    ParameterlessIndicesTypesConstructor(ctors, m);
                    continue;
                }

                if (urlRouteParameters.Any())
                {
                    routing = "r=>r." + string.Join(".", urlRouteParameters
                                                    .Select(p => new
                    {
                        route = p.Key,
                        call  = p.Value.Required ? "Required" : "Optional",
                        v     = p.Key == "metric" || p.Key == "watcher_stats_metric"
                                                                ? $"(Metrics){p.Key}"
                                                                : p.Key == "index_metric"
                                                                        ? $"(IndexMetrics){p.Key}"
                                                                        : p.Key
                    })
                                                    .Select(p => $"{p.call}(\"{p.route}\", {p.v})")
                                                    );
                }

                var doc = $@"///<summary>{url}</summary>";
                if (urlRouteParameters.Any())
                {
                    doc += "\r\n\t\t" + string.Join("\r\n\t\t",
                                                    urlRouteParameters.Select(p =>
                                                                              $"///<param name=\"{p.Key}\">{(p.Value.Required ? "this parameter is required" : "Optional, accepts null")}</param>"));
                }
                var generated = $"public {m}({par}) : base({routing})";

                // special case SearchRequest<T> to pass the type of T as the type, when only the index is specified.
                if (m == "SearchRequest" && urlRouteParameters.Count() == 1 && !string.IsNullOrEmpty(RequestTypeGeneric))
                {
                    var generic = RequestTypeGeneric.Replace("<", "").Replace(">", "");
                    generated = $"public {m}({par}) : this({urlRouteParameters.First().Key}, typeof({generic}))";
                }

                if (string.IsNullOrEmpty(par) && !string.IsNullOrEmpty(RequestTypeGeneric))
                {
                    var generic = RequestTypeGeneric.Replace("<", "").Replace(">", "");
                    doc       = AppendToSummary(doc, ". Will infer the index from the generic type");
                    generated = $"public {m}({par}) : this(typeof({generic}))";
                }

                var c = new Constructor
                {
                    Generated   = generated,
                    Description = doc,
                    Body        = IsDocumentRequest ? $" => Q(\"routing\", new Routing(() => AutoRouteDocument()));" : string.Empty
                };

                ctors.Add(c);
            }
            if (IsDocumentPath && !string.IsNullOrEmpty(RequestTypeGeneric))
            {
                var documentPathGeneric = Regex.Replace(DescriptorTypeGeneric, @"^<?([^\s,>]+).*$", "$1");
                var doc = $"/// <summary>{Url.Path}</summary>";
                doc += "\r\n\t\t"
                       + $"///<param name=\"document\"> describes an elasticsearch document of type <typeparamref name=\"{documentPathGeneric}\"/> from which the index, type and id can be inferred</param>";
                var documentRoute =
                    "r=>r.Required(\"index\", index ?? document.Self.Index).Required(\"type\", type ?? document.Self.Type).Required(\"id\", id ?? document.Self.Id)";
                var documentFromPath = $"partial void DocumentFromPath({documentPathGeneric} document);";

                var constructor = $"DocumentPath<{documentPathGeneric}> document, IndexName index = null, TypeName type = null, Id id = null";

                var autoRoute = IsDocumentRequest ? "Q(\"routing\", new Routing(() => AutoRouteDocument() ?? document.Document));" : string.Empty;
                var body      = $"{{ this.DocumentFromPath(document.Document); {autoRoute} }}";

                var c = new Constructor
                {
                    AdditionalCode = documentFromPath,
                    Generated      = $"public {m}({constructor}) : base({documentRoute})",
                    Body           = body,
                    Description    = doc,
                };
                ctors.Add(c);
            }
            return(ctors.DistinctBy(c => c.Generated));
        }