예제 #1
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 = this.RequestType;

            foreach (var url in this.Url.Paths)
            {
                var urlRouteParameters = 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(", ", 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" + string.Join("\t\t\r\n", 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(this.RequestTypeGeneric))
                {
                    var generic = this.RequestTypeGeneric.Replace("<", "").Replace(">", "");
                    generated = $"public {m}({par}) : this({urlRouteParameters.First().Key}, typeof({generic}))";
                }

                if ((m == "SuggestRequest") && string.IsNullOrEmpty(par) && !string.IsNullOrEmpty(this.RequestTypeGeneric))
                {
                    var generic = this.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        = this.IsDocumentRequest ? $" => Q(\"routing\", new Routing(() => AutoRouteDocument()));" : string.Empty
                };

                ctors.Add(c);
            }
            if (IsDocumentPath && !string.IsNullOrEmpty(this.RequestTypeGeneric))
            {
                var documentPathGeneric = Regex.Replace(this.DescriptorTypeGeneric, @"^<?([^\s,>]+).*$", "$1");
                var doc = $@"/// <summary>{this.Url.Path}</summary>";
                doc += "\r\n\t\t\r\n" + $"///<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 = this.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));
        }
예제 #2
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 requiredUrlRouteParameters = 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(", ", 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" + string.Join("\t\t\r\n", requiredUrlRouteParameters.Select(p => $"///<param name=\"{p.Key}\"> this parameter is required</param>"));
                }

                var generated = $"public {m}({par}) : base({routing})";
                var body      = this.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(this.RequestTypeGeneric))
                {
                    var generic = this.RequestTypeGeneric.Replace("<", "").Replace(">", "");
                    generated = $"public {m}({par}) : base({routing}.Required(\"type\", (Types)typeof({generic})))";
                }

                // Add typeof(T) as the default index to use for Suggest
                if ((m == "SuggestDescriptor") && !string.IsNullOrEmpty(this.RequestTypeGeneric))
                {
                    var generic = this.RequestTypeGeneric.Replace("<", "").Replace(">", "");
                    doc       = AppendToSummary(doc, ". Will infer the index from the generic type");
                    generated = $"public {m}({par}) : base(r => r.Required(\"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(this.DescriptorTypeGeneric))
            {
                var documentPathGeneric = Regex.Replace(this.DescriptorTypeGeneric, @"^<?([^\s,>]+).*$", "$1");
                var doc = $@"/// <summary>{this.Url.Path}</summary>";
                doc += "\r\n\t\t\r\n" + $"///<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        = this.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));
        }
예제 #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 = this.RequestType;
			foreach (var url in this.Url.Paths)
			{
				var urlRouteParameters = 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(", ", 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" + string.Join("\t\t\r\n", 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(this.RequestTypeGeneric))
				{
					var generic = this.RequestTypeGeneric.Replace("<", "").Replace(">", "");
					generated = $"public {m}({par}) : this({urlRouteParameters.First().Key}, typeof({generic})){{}}";
				}

				if ((m == "SuggestRequest") && string.IsNullOrEmpty(par) && !string.IsNullOrEmpty(this.RequestTypeGeneric))
				{
					var generic = this.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 };
				ctors.Add(c);
			}
			if (IsDocumentPath && !string.IsNullOrEmpty(this.RequestTypeGeneric))
			{
				var documentPathGeneric = Regex.Replace(this.DescriptorTypeGeneric, @"^<?([^\s,>]+).*$", "$1");
				var doc = $@"/// <summary>{this.Url.Path}</summary>";
				doc += "\r\n\t\t\r\n" + $"///<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 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);
		}
예제 #4
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);
		}
예제 #5
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 requiredUrlRouteParameters = 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(", ", 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" + string.Join("\t\t\r\n", requiredUrlRouteParameters.Select(p => $"///<param name=\"{p.Key}\"> this parameter is required</param>"));
				}

				var generated = $"public {m}({par}) : base({routing}){{}}";

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

				// Add typeof(T) as the default index to use for Suggest
				if ((m == "SuggestDescriptor") && !string.IsNullOrEmpty(this.RequestTypeGeneric))
				{
					var generic = this.RequestTypeGeneric.Replace("<", "").Replace(">", "");
					doc = AppendToSummary(doc, ". Will infer the index from the generic type");
					generated = $"public {m}({par}) : base(r => r.Required(\"index\", (Indices)typeof({generic}))){{}}";
				}

				var c = new Constructor { Generated = generated, Description = doc };
				ctors.Add(c);
			}
			if (IsDocumentPath && !string.IsNullOrEmpty(this.DescriptorTypeGeneric))
			{
				var documentPathGeneric = Regex.Replace(this.DescriptorTypeGeneric, @"^<?([^\s,>]+).*$", "$1");
				var doc = $@"/// <summary>{this.Url.Path}</summary>";
				doc += "\r\n\t\t\r\n" + $"///<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 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);
		}
예제 #6
0
		private void ParameterlessIndicesTypesConstructor(List<Constructor> ctors, string m)
		{
			var generic = this.RequestTypeGeneric?.Replace("<", "").Replace(">", "");
			string doc;
			Constructor c;
			if (string.IsNullOrEmpty(generic))
			{
				doc = $@"/// <summary>{this.Url.Path}</summary>";
				c = new Constructor { Generated = $"public {m}() {{}}", Description = doc };
			}
			else
			{
				doc = $"///<summary>{this.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, };
			}
			ctors.Add(c);
		}
예제 #7
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 generated = $"public {m}({par}) : base({routing}){{}}";

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

                var c = new Constructor {
                    Generated = generated, 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));
        }