Skip to content

Commit f398f16

Browse files
committed
Allow API's to be renamed by using their rest-spec name
1 parent 9c212b5 commit f398f16

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

src/CodeGeneration/ApiGenerator/ApiGenerator.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,15 @@ private static KeyValuePair<string, ApiEndpoint> CreateApiEndpoint(string jsonFi
152152
{
153153
var replaceSpec = JObject.Parse(File.ReadAllText(replaceFile));
154154
var endpointReplaced = replaceSpec.ToObject<Dictionary<string, ApiEndpoint>>().First();
155+
endpointReplaced.Value.RestSpecName = endpointReplaced.Key;
155156
endpointReplaced.Value.CsharpMethodName = CreateMethodName(endpointReplaced.Key);
156157
return endpointReplaced;
157158
}
158159

159160
var officialJsonSpec = JObject.Parse(File.ReadAllText(jsonFile));
160161
PatchOfficialSpec(officialJsonSpec, jsonFile);
161162
var endpoint = officialJsonSpec.ToObject<Dictionary<string, ApiEndpoint>>().First();
163+
endpoint.Value.RestSpecName = endpoint.Key;
162164
endpoint.Value.CsharpMethodName = CreateMethodName(endpoint.Key);
163165
return endpoint;
164166
}

src/CodeGeneration/ApiGenerator/CodeConfiguration.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ private static string Root
3535
public static string RestSpecificationFolder { get; } = $@"{Root}RestSpecification\";
3636
public static string LastDownloadedVersionFile { get; } = Path.Combine(Root, "last_downloaded_version.txt");
3737

38+
public static readonly Dictionary<string, string> ApiNameMapping =
39+
(from f in new DirectoryInfo(NestFolder).GetFiles("*.cs", SearchOption.AllDirectories)
40+
let contents = File.ReadAllText(f.FullName)
41+
let c = Regex.Replace(contents, @"^.+\[MapsApi\(""([^ \r\n]+)""\)\].*$", "$1", RegexOptions.Singleline)
42+
where !c.Contains(" ") //filter results that did not match
43+
select new { Value = f.Name.Replace("Request", ""), Key = c.Replace(".json", "") })
44+
.DistinctBy(v => v.Key)
45+
.ToDictionary(k => k.Key, v => v.Value.Replace(".cs", ""));
46+
3847
public static readonly Dictionary<string, string> MethodNameOverrides =
3948
(from f in new DirectoryInfo(NestFolder).GetFiles("*.cs", SearchOption.AllDirectories)
4049
let contents = File.ReadAllText(f.FullName)

src/CodeGeneration/ApiGenerator/Domain/ApiEndpoint.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public class ApiEndpoint
5858
{
5959
private List<CsharpMethod> _csharpMethods;
6060

61+
public string RestSpecName { get; set; }
6162
public string CsharpMethodName { get; set; }
6263
public string Documentation { get; set; }
6364
public IEnumerable<string> Methods { get; set; }
@@ -221,10 +222,13 @@ public IEnumerable<CsharpMethod> CsharpMethods
221222
}
222223
}
223224

225+
224226
private IEndpointOverrides GetOverrides()
225227
{
226228
var method = this.CsharpMethodName;
227-
if (CodeConfiguration.MethodNameOverrides.TryGetValue(method, out var manualOverride))
229+
if (CodeConfiguration.ApiNameMapping.TryGetValue(this.RestSpecName, out var mapsApiMethodName))
230+
method = mapsApiMethodName;
231+
else if (CodeConfiguration.MethodNameOverrides.TryGetValue(method, out var manualOverride))
228232
method = manualOverride;
229233

230234
var typeName = "ApiGenerator.Overrides.Endpoints." + method + "Overrides";
@@ -293,7 +297,9 @@ public void PatchMethod(CsharpMethod method)
293297
method = this.GetOverrides()?.PatchMethod(method) ?? method;
294298

295299
var key = method.QueryStringParamName.Replace("RequestParameters", "");
296-
if (CodeConfiguration.MethodNameOverrides.TryGetValue(key, out var manualOverride))
300+
if (CodeConfiguration.ApiNameMapping.TryGetValue(this.RestSpecName, out var mapsApiMethodName))
301+
method.QueryStringParamName = mapsApiMethodName + "RequestParameters";
302+
else if (CodeConfiguration.MethodNameOverrides.TryGetValue(key, out var manualOverride))
297303
method.QueryStringParamName = manualOverride + "RequestParameters";
298304

299305
method.DescriptorType = method.QueryStringParamName.Replace("RequestParameters", "Descriptor");

src/Nest/CommonAbstractions/ForAttribute.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace Nest
44
{
55
/// <summary>
66
/// DescriptorFor is a marker to rename unintuitive generated elasticsearch operation names
7-
/// This is used by the code generator and is only meant for internal use to map our more aptly named requests to
7+
/// This is used by the code generator and is only meant for internal use to map our more aptly named requests to
88
/// the original elasticsearch rest spec
99
/// </summary>
1010
[AttributeUsage(System.AttributeTargets.Class, AllowMultiple = false)]
@@ -15,4 +15,17 @@ internal class DescriptorForAttribute : Attribute
1515
public DescriptorForAttribute (string operation) { }
1616

1717
}
18+
/// <summary>
19+
/// DescriptorFor is a marker to rename unintuitive generated elasticsearch operation names
20+
/// This is used by the code generator and is only meant for internal use to map our more aptly named requests to
21+
/// the original elasticsearch rest spec
22+
/// </summary>
23+
[AttributeUsage(System.AttributeTargets.Interface, AllowMultiple = false)]
24+
internal class MapsApiAttribute : Attribute
25+
{
26+
27+
// ReSharper disable once UnusedParameter.Local
28+
public MapsApiAttribute (string restSpecName) { }
29+
30+
}
1831
}

0 commit comments

Comments
 (0)