示例#1
0
		public IEnumerable<Constructor> RequestConstructors()
		{
			var ctors = new List<Constructor>();
			if (IsPartless) return ctors;
			var m = this.RequestType;
			foreach (var url in this.Url.Paths)
			{
				var cp = this.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(", ", cp.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 (!cp.Any() && IndicesAndTypes)
				{
					ParameterlessIndicesTypesConstructor(ctors, m);
					continue;
				}

				if (cp.Any())
				{
					routing = "r=>r." + string.Join(".", cp
						.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 (cp.Any())
				{
					doc += "\r\n" + string.Join("\t\t\r\n", cp.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" && cp.Count() == 1 && !string.IsNullOrEmpty(this.RequestTypeGeneric))
				{
					var generic = this.RequestTypeGeneric.Replace("<", "").Replace(">", "");
					generated = $"public {m}({par}) : this({cp.First().Key}, typeof({generic})){{}}";
                }

				var c = new Constructor { Generated = generated, Description = doc };
				ctors.Add(c);
			}
			if (IsDocumentPath && !string.IsNullOrEmpty(this.RequestTypeGeneric))
			{
				var doc = $@"/// <summary>{this.Url.Path}</summary>";
				doc += "\r\n\t\t\r\n" + "///<param name=\"document\"> describes an elasticsearch document of type T, allows implicit conversion from numeric and string ids </param>";
				var documentRoute = "r=>r.Required(\"index\", index ?? document.Self.Index).Required(\"type\", type ?? document.Self.Type).Required(\"id\", id ?? document.Self.Id)";
				var documentPathGeneric = Regex.Replace(this.DescriptorTypeGeneric, @"^<?([^\s,>]+).*$", "$1");
				var documentFromPath = $"partial void DocumentFromPath({documentPathGeneric} document);";

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

				var c = new Constructor { AdditionalCode = documentFromPath, Generated = $"public {m}({constructor}) : base({documentRoute}){{ this.DocumentFromPath(document.Document); }}", Description = doc, };
				ctors.Add(c);
			}
			return ctors.DistinctBy(c => c.Generated);
		}
示例#2
0
		private void AddParameterlessIndicesTypesConstructor(List<Constructor> ctors, string m)
		{
			var generic = this.DescriptorTypeGeneric?.Replace("<", "").Replace(">", "");
			var doc = $@"/// <summary>{this.Url.Path}</summary>";
			var documentRoute = $"r=> r.Required(\"index\", (Indices)typeof({generic})).Required(\"type\", (Types)typeof({generic}))";
			var c = new Constructor { Generated = $"public {m}() : base({documentRoute}){{}}", Description = doc };
			if (string.IsNullOrEmpty(generic))
				c = new Constructor { Generated = $"public {m}() {{}}", Description = doc };
			ctors.Add(c);
		}
示例#3
0
		public IEnumerable<Constructor> DescriptorConstructors()
		{
			var ctors = new List<Constructor>();
			if (IsPartless) return ctors;
			var m = this.DescriptorType;
			foreach (var url in this.Url.Paths)
			{
				var cp = this.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(", ", cp.Select(p => $"{ClrParamType(p.Value.ClrTypeName)} {p.Key}"));
				var routing = string.Empty;
				//Routes that take {indices}/{types} and both are optional
				if (!cp.Any() && IndicesAndTypes)
				{
					AddParameterlessIndicesTypesConstructor(ctors, m);
					continue;
				}
				if (cp.Any())
					routing = "r=>r." + string.Join(".", cp
						.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 (cp.Any())
				{
					doc += "\r\n" + string.Join("\t\t\r\n", cp.Select(p => $"///<param name=\"{p.Key}\"> this parameter is required</param>"));
				}
				var c = new Constructor { Generated = $"public {m}({par}) : base({routing}){{}}", Description = doc };
				ctors.Add(c);
			}
			if (IsDocumentPath && !string.IsNullOrEmpty(this.DescriptorTypeGeneric))
			{
				var doc = $@"/// <summary>{this.Url.Path}</summary>";
				doc += "\r\n\t\t\r\n" + "///<param name=\"document\"> describes an elasticsearch document of type T, allows implicit conversion from numeric and string ids </param>";
				var documentRoute = "r=>r.Required(\"index\", document.Self.Index).Required(\"type\", document.Self.Type).Required(\"id\", document.Self.Id)";
				var documentPathGeneric = Regex.Replace(this.DescriptorTypeGeneric, @"^<?([^\s,>]+).*$", "$1");
				var documentFromPath = $"partial void DocumentFromPath({documentPathGeneric} document);";
				var c = new Constructor { AdditionalCode = documentFromPath, Generated = $"public {m}(DocumentPath<{documentPathGeneric}> document) : base({documentRoute}){{ this.DocumentFromPath(document.Document); }}", Description = doc };
				ctors.Add(c);
			}

			return ctors.DistinctBy(c => c.Generated);
		}
示例#4
0
		private void ParameterlessIndicesTypesConstructor(List<Constructor> ctors, string m)
		{
			var generic = this.RequestTypeGeneric?.Replace("<", "").Replace(">", "");
			var doc = $@"/// <summary>{this.Url.Path}</summary>";
			doc += "\r\n\t\t\r\n" + "///<param name=\"document\"> describes an elasticsearch document of type T, allows implicit conversion from numeric and string ids </param>";
			var c = new Constructor { Generated = $"public {m}() {{}}", Description = doc, };
			if (!string.IsNullOrEmpty(generic))
				c = new Constructor { Generated = $"public {m}() : this(typeof({generic}), typeof({generic})) {{}}", Description = doc, };
			ctors.Add(c);
		}
        public IEnumerable <Constructor> DescriptorConstructors()
        {
            var ctors = new List <Constructor>();

            if (IsPartless)
            {
                return(ctors);
            }
            var m = this.DescriptorType;

            foreach (var url in this.Url.Paths)
            {
                var cp = this.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(", ", cp.Select(p => $"{ClrParamType(p.Value.ClrTypeName)} {p.Key}"));
                var routing = string.Empty;
                //Routes that take {indices}/{types} and both are optional
                if (!cp.Any() && IndicesAndTypes)
                {
                    AddParameterlessIndicesTypesConstructor(ctors, m);
                    continue;
                }
                if (cp.Any())
                {
                    routing = "r=>r." + string.Join(".", cp
                                                    .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 (cp.Any())
                {
                    doc += "\r\n" + string.Join("\t\t\r\n", cp.Select(p => $"///<param name=\"{p.Key}\"> this parameter is required</param>"));
                }
                var c = new Constructor {
                    Generated = $"public {m}({par}) : base({routing}){{}}", Description = doc
                };
                ctors.Add(c);
            }
            if (IsDocumentPath && !string.IsNullOrEmpty(this.DescriptorTypeGeneric))
            {
                var doc = $@"/// <summary>{this.Url.Path}</summary>";
                doc += "\r\n\t\t\r\n" + "///<param name=\"document\"> describes an elasticsearch document of type T, allows implicit conversion from numeric and string ids </param>";
                var documentRoute       = "r=>r.Required(\"index\", document.Self.Index).Required(\"type\", document.Self.Type).Required(\"id\", document.Self.Id)";
                var documentPathGeneric = Regex.Replace(this.DescriptorTypeGeneric, @"^<?([^\s,>]+).*$", "$1");
                var documentFromPath    = $"partial void DocumentFromPath({documentPathGeneric} document);";
                var c = new Constructor {
                    AdditionalCode = documentFromPath, Generated = $"public {m}(DocumentPath<{documentPathGeneric}> document) : base({documentRoute}){{ this.DocumentFromPath(document.Document); }}", Description = doc
                };
                ctors.Add(c);
            }

            return(ctors.DistinctBy(c => c.Generated));
        }
        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 = this.RequestType;

            foreach (var url in this.Url.Paths)
            {
                var cp = this.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(", ", cp.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 (!cp.Any() && IndicesAndTypes)
                {
                    ParameterlessIndicesTypesConstructor(ctors, m);
                    continue;
                }

                if (cp.Any())
                {
                    routing = "r=>r." + string.Join(".", cp
                                                    .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 (cp.Any())
                {
                    doc += "\r\n" + string.Join("\t\t\r\n", cp.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") && cp.Count() == 1 && !string.IsNullOrEmpty(this.RequestTypeGeneric))
                {
                    var generic = this.RequestTypeGeneric.Replace("<", "").Replace(">", "");
                    generated = $"public {m}({par}) : this({cp.First().Key}, typeof({generic})){{}}";
                }

                var c = new Constructor {
                    Generated = generated, Description = doc
                };
                ctors.Add(c);
            }
            if (IsDocumentPath && !string.IsNullOrEmpty(this.RequestTypeGeneric))
            {
                var doc = $@"/// <summary>{this.Url.Path}</summary>";
                doc += "\r\n\t\t\r\n" + "///<param name=\"document\"> describes an elasticsearch document of type T, allows implicit conversion from numeric and string ids </param>";
                var documentRoute       = "r=>r.Required(\"index\", index ?? document.Self.Index).Required(\"type\", type ?? document.Self.Type).Required(\"id\", id ?? document.Self.Id)";
                var documentPathGeneric = Regex.Replace(this.DescriptorTypeGeneric, @"^<?([^\s,>]+).*$", "$1");
                var documentFromPath    = $"partial void DocumentFromPath({documentPathGeneric} document);";

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

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