Skip to content

Commit 018ca86

Browse files
committed
Merge pull request #473 from Mpdreamz/feature/generation-for-1.0
Feature/generation for 1.0
2 parents 3b08cc4 + 968a02e commit 018ca86

File tree

256 files changed

+25263
-8736
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

256 files changed

+25263
-8736
lines changed

src/CodeGeneration/CodeGeneration.LowLevelClient/ApiGenerator.cs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ namespace CodeGeneration.LowLevelClient
1616
{
1717
public static class ApiGenerator
1818
{
19-
private readonly static string _listingUrl = "https://github.com/elasticsearch/elasticsearch/tree/0.90/rest-api-spec/api";
20-
private readonly static string _rawUrlPrefix = "https://raw.github.com/elasticsearch/elasticsearch/0.90/rest-api-spec/api/";
21-
private readonly static string _nestFolder = @"..\..\..\..\src\Nest\";
19+
private readonly static string _listingUrl = "https://github.com/elasticsearch/elasticsearch/tree/v1.0.0/rest-api-spec/api";
20+
private readonly static string _rawUrlPrefix = "https://raw.github.com/elasticsearch/elasticsearch/v1.0.0/rest-api-spec/api/";
21+
private readonly static string _nestFolder = @"..\..\..\..\..\src\Nest\";
2222
private readonly static string _viewFolder = @"..\..\Views\";
2323
private readonly static string _cacheFolder = @"..\..\Cache\";
2424
private static readonly RazorMachine _razorMachine;
@@ -115,22 +115,38 @@ where f.FullName.EndsWith("Descriptor.cs")
115115
//or to get rid of double verbs in an method name i,e ClusterGetSettingsGet > ClusterGetSettings
116116
public static void PatchMethod(CsharpMethod method)
117117
{
118-
if (method.FullName.StartsWith("IndicesStats") && method.Path.Contains("{index}"))
119-
method.FullName = method.FullName.Replace("IndicesStats", "IndexStats");
118+
Func<string, bool> ms = (s) => method.FullName.StartsWith(s);
119+
Func<string, bool> mc = (s) => method.FullName.Contains(s);
120+
Func<string, bool> pc = (s) => method.Path.Contains(s);
121+
122+
//if (ms("IndicesStats") && pc("{index}"))
123+
//method.FullName = method.FullName.Replace("IndicesStats", "IndexStats");
120124
//IndicesPutAliasPutAsync
121-
if (method.FullName.StartsWith("IndicesPutAlias") && method.Path.Contains("{index}"))
122-
method.FullName = method.FullName.Replace("IndicesPutAlias", "IndexPutAlias");
125+
//if (ms("IndicesPutAlias") && pc("{index}"))
126+
//method.FullName = method.FullName.Replace("IndicesPutAlias", "IndexPutAlias");
123127

124-
if (method.FullName.StartsWith("IndicesStats") || method.FullName.StartsWith("IndexStats"))
128+
if (ms("IndicesStats") || ms("IndexStats"))
125129
{
126-
if (method.Path.Contains("/indexing/"))
130+
if (pc("/indexing/"))
127131
method.FullName = method.FullName.Replace("Stats", "IndexingStats");
128-
if (method.Path.Contains("/search/"))
132+
if (pc("/search/"))
129133
method.FullName = method.FullName.Replace("Stats", "SearchStats");
130-
if (method.Path.Contains("/fielddata/"))
134+
if (pc("/fielddata/"))
131135
method.FullName = method.FullName.Replace("Stats", "FieldDataStats");
132136
}
133137

138+
if (ms("Indices") && !pc("{index}"))
139+
method.FullName = (method.FullName + "ForAll").Replace("AsyncForAll", "ForAllAsync");
140+
141+
if (ms("Nodes") && !pc("{node_id}"))
142+
method.FullName = (method.FullName + "ForAll").Replace("AsyncForAll", "ForAllAsync");
143+
144+
//if (ms("IndicesGetSettings") && !pc("{index}") && pc("{"))
145+
// method.FullName = method.FullName.Replace("IndicesGetSettings", "IndicesSettings");
146+
147+
//if (ms("IndicesGetMapping") && !pc("{index}") && pc("{"))
148+
// method.FullName = method.FullName.Replace("IndicesGetMapping", "IndicesGetGlobalSettings");
149+
134150
//remove duplicate occurance of the HTTP method name
135151
var m = method.HttpMethod.ToPascalCase();
136152
method.FullName =
@@ -149,7 +165,7 @@ public static void PatchMethod(CsharpMethod method)
149165

150166
try
151167
{
152-
var typeName = "RawClientGenerator.Overrides.Descriptors." + method.DescriptorType + "Overrides";
168+
var typeName = "CodeGeneration.LowLevelClient.Overrides.Descriptors." + method.DescriptorType + "Overrides";
153169
var type = _assembly.GetType(typeName);
154170
if (type == null)
155171
return;

src/CodeGeneration/CodeGeneration.LowLevelClient/Domain/ApiUrl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class ApiUrl
77
{
88
//these are aliases we much rather pass along inside the querystring
99
//allowing these will cause too many overloads being generated which helps noone
10-
private static readonly string[] _blackList = {"{metric_family}", "{metric}", "{fields}", "{search_groups}", "{indexing_types}"};
10+
private static readonly string[] _blackList = { "{fields}", "{search_groups}", "{indexing_types}"};
1111
private IEnumerable<string> _paths;
1212

1313
public string Path { get; set; }

src/CodeGeneration/CodeGeneration.LowLevelClient/Views/Enums.Generated.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@using System.Collections.Generic
22
@using System.Linq
3-
@using Domain
3+
@using CodeGeneration.LowLevelClient.Domain
44
@using CodeGeneration.LowLevelClient
55

66
@functions {

src/CodeGeneration/CodeGeneration.LowLevelClient/Views/QueryStringParameters.Generated.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@using System.Collections.Generic
2-
@using Domain
2+
@using CodeGeneration.LowLevelClient.Domain
33
@using CodeGeneration.LowLevelClient
44

55
using System;

src/CodeGeneration/CodeGeneration.LowLevelClient/Views/RawDispatch.Generated.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@using System.Linq
2-
@using Domain
2+
@using CodeGeneration.LowLevelClient.Domain
33
@using CodeGeneration.LowLevelClient
44
using System;
55
using System.Collections.Generic;

src/CodeGeneration/CodeGeneration.LowLevelClient/Views/RawElasticClient.Generated.cshtml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@using System.Globalization
22
@using System.Linq
33
@using System.Text.RegularExpressions
4-
@using Domain
4+
@using CodeGeneration.LowLevelClient.Domain
55
@using CodeGeneration.LowLevelClient
66
using System;
77
using System.Collections.Generic;
@@ -45,7 +45,7 @@ namespace Nest
4545
{
4646
@foreach (ApiUrlPart part in method.Parts.Where(p=>p.Name != "body").ToList())
4747
{
48-
<text>@(part.Name).ThrowIfNull("@part.Name");</text>
48+
<text>@(part.Name).@(part.Type == "string" || part.Type == "list" ? "ThrowIfNullOrEmpty" : "ThrowIfNull")("@part.Name");</text>
4949
}
5050
@if (method.Parts.Any())
5151
{

src/CodeGeneration/CodeGeneration.LowLevelClient/Views/_Descriptors.Generated.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@using System.Collections.Generic
22
@using System.Linq
33
@using CsQuery.ExtensionMethods.Internal
4-
@using Domain
4+
@using CodeGeneration.LowLevelClient.Domain
55
@using CodeGeneration.LowLevelClient
66
using System;
77
using System.Collections.Generic;

src/CodeGeneration/CodeGeneration.YamlTestsRunner/Domain/DoStep.cs

Lines changed: 57 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Text.RegularExpressions;
5+
using CsQuery.ExtensionMethods.Internal;
46

57
namespace CodeGeneration.YamlTestsRunner.Domain
68
{
@@ -10,6 +12,7 @@ public class DoStep : ITestStep
1012
public string Type { get { return "do"; } }
1113
public string Call { get; set; }
1214
public string Catch { get; set; }
15+
public string TestDescription { get; set; }
1316

1417
private string _rawElasticSearchCall = null;
1518
public string RawElasticSearchCall
@@ -27,19 +30,21 @@ public string RawElasticSearchCall
2730

2831
private string FindBestRawElasticSearchMatch()
2932
{
30-
if (this.Call == "create")
31-
{
32-
this.Call = "index";
33-
if (this.QueryString == null)
34-
this.QueryString = new Dictionary<string, object>();
35-
this.QueryString.Add("op_type", "create");
36-
}
33+
this.PatchCall();
34+
35+
var re = "^" + this.Call.ToPascalCase() + @"(Get|Put|Head|Post|Delete)?(ForAll)?\(";
36+
var counts = YamlTestsGenerator.RawElasticCalls
37+
.Where(c => Regex.IsMatch(c, re))
38+
.Where(c=> this.Body == null || c.Contains("object body"))
39+
.Select(s => new {Method = s, Count = QueryStringCount(s)})
40+
.OrderByDescending(s=>s.Count)
41+
.ToList();
3742

38-
var re = "^" + this.Call.ToPascalCase() + @"(Get|Put|Head|Post|Delete)?\(";
3943
var calls = YamlTestsGenerator.RawElasticCalls
4044
.Where(c => Regex.IsMatch(c, re))
45+
.Where(c=> this.Body == null || c.Contains("object body"))
4146
.OrderByDescending(QueryStringCount)
42-
.ThenByDescending(MethodPreference)
47+
//.ThenByDescending(MethodPreference)
4348
.ToList();
4449

4550
var call = calls.FirstOrDefault();
@@ -51,6 +56,46 @@ private string FindBestRawElasticSearchMatch()
5156
return this.GenerateCall(call);
5257
}
5358

59+
private void PatchCall()
60+
{
61+
if (this.Call == "create")
62+
{
63+
this.Call = "index";
64+
if (this.QueryString == null)
65+
this.QueryString = new Dictionary<string, object>();
66+
this.QueryString.Add("op_type", "create");
67+
}
68+
else if (!this.Catch.IsNullOrEmpty() &&
69+
(this.Call == "indices.delete_alias"
70+
|| this.Call == "indices.delete_warmer"
71+
|| this.Call == "indices.delete_mapping"
72+
|| this.Call == "indices.put_alias"
73+
|| this.Call == "indices.put_warmer"
74+
|| this.Call == "indices.put_mapping"))
75+
{
76+
Func<string, bool> m = (s) => Regex.IsMatch(this.TestDescription, "(blank|empty|missing) " + s);
77+
78+
if (!this.QueryString.ContainsKey("index") && m("index"))
79+
this.QueryString["index"] = string.Empty;
80+
if (!this.QueryString.ContainsKey("type") && m("type"))
81+
this.QueryString["type"] = string.Empty;
82+
if (!this.QueryString.ContainsKey("name")
83+
&& (m("name") || m("alias") || m("(.+_)?warmer")))
84+
this.QueryString["name"] = string.Empty;
85+
}
86+
}
87+
private double QueryStringCount(string method)
88+
{
89+
var matches = (double)this.QueryString.Keys.Count(k => method.Contains(k + ","));
90+
return 1 + matches / (method.Count(c=>c==','));
91+
92+
}
93+
private string GetQueryStringValue(string key)
94+
{
95+
var value = this.QueryString[key].ToStringRepresentation("\t\t\t\t\t");
96+
97+
return value;
98+
}
5499

55100

56101
private string GenerateCall(string call)
@@ -87,6 +132,7 @@ private string GenerateCall(string call)
87132
s += ");";
88133
return s;
89134
}
135+
90136

91137
public static string SerializeBody(DoStep o)
92138
{
@@ -117,12 +163,6 @@ private string GetMethodArgument(string key)
117163
return "\"" + v + "\"";
118164
}
119165

120-
private string GetQueryStringValue(string key)
121-
{
122-
var value = this.QueryString[key].ToStringRepresentation("\t\t\t\t\t");
123-
124-
return value;
125-
}
126166

127167
private IEnumerable<string> CsharpArguments(string call, bool inverse = false)
128168
{
@@ -133,21 +173,6 @@ private IEnumerable<string> CsharpArguments(string call, bool inverse = false)
133173
.Select(ki => ki.Key);
134174
return csharpArguments.ToList();
135175
}
136-
137-
private int QueryStringCount(string method)
138-
{
139-
return QueryString.Keys.Count(k => method.Contains(k + ","));
140-
}
141-
private int MethodPreference(string method)
142-
{
143-
var postBoost = this.Body != null ? 10 : 0;
144-
var getBoost = this.Body == null ? 10 : 0;
145-
146-
if (method.Contains("Post(")) return 5 + postBoost;
147-
if (method.Contains("Put(")) return 4 + postBoost;
148-
if (method.Contains("Get(")) return 3 + getBoost;
149-
if (method.Contains("Head(")) return 2 + postBoost;
150-
return 0;
151-
}
176+
152177
}
153178
}

src/CodeGeneration/CodeGeneration.YamlTestsRunner/Domain/TestSuite.cs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ public static TestSuite CreateFrom(Dictionary<string, object> untyped, string ya
2222
var untypedSuite = untyped.First();
2323
var suite = new TestSuite();
2424
suite.Description = untypedSuite.Key;
25-
suite.Steps = Actions(untypedSuite).ToList();
25+
suite.Steps = Actions(untypedSuite, suite.Description).Where(s=>s != null).ToList();
2626
return suite;
2727

2828
}
2929

30-
private static IEnumerable<ITestStep> Actions(KeyValuePair<string, object> untypedSuite)
30+
private static IEnumerable<ITestStep> Actions(KeyValuePair<string, object> untypedSuite, string description)
3131
{
3232
var actions = untypedSuite.Value as List<object>;
3333
if (actions == null)
@@ -49,7 +49,7 @@ private static IEnumerable<ITestStep> Actions(KeyValuePair<string, object> untyp
4949
switch (testAction)
5050
{
5151
case "do":
52-
yield return CreateDoStep(kv.Value as Dictionary<object, object>);
52+
yield return CreateDoStep(kv.Value as Dictionary<object, object>, description);
5353
break;
5454
case "set":
5555
yield return CreateSetStep(kv.Value as Dictionary<object, object>);
@@ -82,6 +82,11 @@ private static IEnumerable<ITestStep> Actions(KeyValuePair<string, object> untyp
8282

8383
private static SkipStep CreateSkipStep(Dictionary<object, object> value)
8484
{
85+
if (!value.ContainsKey("version"))
86+
{
87+
return null;
88+
}
89+
8590
var version = value["version"] as string;
8691
var reason = value["reason"] as string;
8792
return new SkipStep { Version = version, Reason = reason};
@@ -136,7 +141,7 @@ private static string PropertyPath(string value)
136141
value = value.Replace(@"\.", "|||");
137142
value = Regex.Replace(value, @"\.([^\.]+)", m => "[" + m.Value.Trim('.').SurroundWithQuotes() + "]");
138143
value = value.Replace("|||", ".");
139-
return "_responseDictionary" + value;
144+
return "_response" + value;
140145
}
141146
return "_response" + value;
142147
}
@@ -153,7 +158,7 @@ private static SetStep CreateSetStep(Dictionary<object, object> value)
153158
return new SetStep() {VariableName = kv.Value as string, ResponseValue = PropertyPath(kv.Key as string)};
154159
}
155160

156-
private static DoStep CreateDoStep(Dictionary<object, object> value)
161+
private static DoStep CreateDoStep(Dictionary<object, object> value, string description)
157162
{
158163
if (value == null)
159164
return null;
@@ -171,7 +176,13 @@ private static DoStep CreateDoStep(Dictionary<object, object> value)
171176
var argumentString = arguments as string;
172177
if (argumentString != null)
173178
{
174-
return new DoStep {Call = call, Body = argumentString, Catch = catchException};
179+
return new DoStep
180+
{
181+
Call = call,
182+
Body = argumentString,
183+
Catch = catchException,
184+
TestDescription = description
185+
};
175186
}
176187
var complexArgument = arguments as Dictionary<object, object>;
177188
if (complexArgument != null)
@@ -186,9 +197,16 @@ private static DoStep CreateDoStep(Dictionary<object, object> value)
186197
foreach (var kv in complexArgument)
187198
nv.Add(kv.Key as string, kv.Value);
188199

189-
return new DoStep {Call = call, Body = body, QueryString = nv, Catch = catchException};
200+
return new DoStep
201+
{
202+
Call = call,
203+
Body = body,
204+
QueryString = nv,
205+
Catch = catchException,
206+
TestDescription = description
207+
};
190208
}
191-
return new DoStep { Call = call , Catch = catchException};
209+
return new DoStep { Call = call , Catch = catchException, TestDescription = description};
192210
}
193211
}
194212
}

0 commit comments

Comments
 (0)