Skip to content

Commit

Permalink
Add higher-level HTTP DSL methods (opensearch-project#447)
Browse files Browse the repository at this point in the history
* Lay foundations

Signed-off-by: Thomas Farr <[email protected]>

* Implement generation

Signed-off-by: Thomas Farr <[email protected]>

* Run generator

Signed-off-by: Thomas Farr <[email protected]>

* Update samples

Signed-off-by: Thomas Farr <[email protected]>

* Update guide

Signed-off-by: Thomas Farr <[email protected]>

* Add changelog entry

Signed-off-by: Thomas Farr <[email protected]>

* PR comments

Signed-off-by: Thomas Farr <[email protected]>

* Fix naming tests

Signed-off-by: Thomas Farr <[email protected]>

* Why is the ordering of these statements load-bearing???

Signed-off-by: Thomas Farr <[email protected]>

---------

Signed-off-by: Thomas Farr <[email protected]>
  • Loading branch information
Xtansia committed Dec 3, 2023
1 parent aa2d27f commit 6ca6ee4
Show file tree
Hide file tree
Showing 22 changed files with 233 additions and 172 deletions.
15 changes: 8 additions & 7 deletions src/ApiGenerator/CodeTemplatePage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@
using System.Threading.Tasks;
using RazorLight;

namespace ApiGenerator
namespace ApiGenerator;

public abstract class CodeTemplatePage<TModel> : TemplatePage<TModel>
{
public abstract class CodeTemplatePage<TModel> : TemplatePage<TModel>
{
protected new Task IncludeAsync(string key, object model = null)
=> base.IncludeAsync(key.Replace('/', '.'), model);
protected new Task IncludeAsync(string key, object model = null)
=> base.IncludeAsync(key.Replace('/', '.'), model);

protected async Task IncludeLegacyGeneratorNotice() => await IncludeAsync("GeneratorNotice", true);

protected async Task IncludeGeneratorNotice() => await IncludeAsync("GeneratorNotice");
}
protected async Task IncludeGeneratorNotice() => await IncludeAsync("GeneratorNotice", false);
}
45 changes: 27 additions & 18 deletions src/ApiGenerator/Generator/Razor/DescriptorsGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,39 @@
* under the License.
*/

using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using ApiGenerator.Configuration;
using ApiGenerator.Domain;
using ApiGenerator.Domain.Code;
using ShellProgressBar;

namespace ApiGenerator.Generator.Razor
namespace ApiGenerator.Generator.Razor;

public class DescriptorsGenerator : RazorGeneratorBase
{
public class DescriptorsGenerator : RazorGeneratorBase
{
public override string Title => "OpenSearch.Client descriptors";

public override async Task Generate(RestApiSpec spec, ProgressBar progressBar, CancellationToken token)
{
var view = ViewLocations.HighLevel("Descriptors", "RequestDescriptorBase.cshtml");
var target = GeneratorLocations.HighLevel("Descriptors.cs");
await DoRazor(spec, view, target, token);

var dependantView = ViewLocations.HighLevel("Descriptors", "Descriptors.cshtml");
string Target(string id) => GeneratorLocations.HighLevel($"Descriptors.{id}.cs");
var namespaced = spec.EndpointsPerNamespaceHighLevel.ToList();
await DoRazorDependantFiles(progressBar, namespaced, dependantView, kv => kv.Key, id => Target(id), token);
}
}
public override string Title => "OpenSearch.Client descriptors";

public override async Task Generate(RestApiSpec spec, ProgressBar progressBar, CancellationToken token)
{
await DoRazor(spec, View("RequestDescriptorBase"), Target(), token);

await DoRazor(HttpMethod.All, View("Descriptors.Http"), Target("Http"), token);

await DoRazorDependantFiles(
progressBar,
spec.EndpointsPerNamespaceHighLevel.ToList(),
View("Descriptors"),
kv => kv.Key,
Target,
token
);

return;

string View(string name) => ViewLocations.HighLevel("Descriptors", $"{name}.cshtml");

string Target(string id = null) => GeneratorLocations.HighLevel($"Descriptors{(id != null ? $".{id}" : string.Empty)}.cs");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,42 @@
* under the License.
*/

using System.IO;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using ApiGenerator.Configuration;
using ApiGenerator.Domain;
using ApiGenerator.Domain.Code;
using ApiGenerator.Domain.Specification;
using ShellProgressBar;

namespace ApiGenerator.Generator.Razor
namespace ApiGenerator.Generator.Razor;

public class HighLevelClientImplementationGenerator : RazorGeneratorBase
{
public class HighLevelClientImplementationGenerator : RazorGeneratorBase
{
public override string Title => "OpenSearch.Client client implementation";
public override string Title => "OpenSearch.Client client implementation";

public override async Task Generate(RestApiSpec spec, ProgressBar progressBar, CancellationToken token)
{
await DoRazor(spec, View(), Target(), token);

await DoRazor(HttpMethod.All, View("Http"), Target("Http"), token);

public override async Task Generate(RestApiSpec spec, ProgressBar progressBar, CancellationToken token)
{
var view = ViewLocations.HighLevel("Client", "Implementation", "OpenSearchClient.cshtml");
var target = GeneratorLocations.HighLevel($"OpenSearchClient.cs");
await DoRazor(spec, view, target, token);
await DoRazorDependantFiles(
progressBar,
spec.EndpointsPerNamespaceHighLevel.Where(kv => kv.Key != CsharpNames.RootNamespace).ToList(),
View("Namespace"),
kv => kv.Key,
Target,
token
);

string Target(string id) => GeneratorLocations.HighLevel($"OpenSearchClient.{id}.cs");
return;

var namespaced = spec.EndpointsPerNamespaceHighLevel.Where(kv => kv.Key != CsharpNames.RootNamespace).ToList();
var dependantView = ViewLocations.HighLevel("Client", "Implementation", "OpenSearchClient.Namespace.cshtml");
await DoRazorDependantFiles(progressBar, namespaced, dependantView, kv => kv.Key, id => Target(id), token);
string View(string ns = null) => ViewLocations.HighLevel("Client", "Implementation", $"OpenSearchClient{(ns != null ? $".{ns}" : string.Empty)}.cshtml");

}
}
string Target(string ns = null) => GeneratorLocations.HighLevel($"OpenSearchClient{(ns != null ? $".{ns}" : string.Empty)}.cs");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
* under the License.
*/

using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -35,22 +34,32 @@
using ApiGenerator.Domain.Code;
using ShellProgressBar;

namespace ApiGenerator.Generator.Razor
namespace ApiGenerator.Generator.Razor;

public class LowLevelClientImplementationGenerator : RazorGeneratorBase
{
public class LowLevelClientImplementationGenerator : RazorGeneratorBase
{
public override string Title { get; } = "OpenSearch.Net client implementation";

public override async Task Generate(RestApiSpec spec, ProgressBar progressBar, CancellationToken token)
{
var view = ViewLocations.LowLevel("Client", "Implementation", "OpenSearchLowLevelClient.cshtml");
var target = GeneratorLocations.LowLevel("OpenSearchLowLevelClient.cs");
await DoRazor(spec, view, target, token);

var namespaced = spec.EndpointsPerNamespaceLowLevel.Where(kv => kv.Key != CsharpNames.RootNamespace).ToList();
var namespacedView = ViewLocations.LowLevel("Client", "Implementation", "OpenSearchLowLevelClient.Namespace.cshtml");
await DoRazorDependantFiles(progressBar, namespaced, namespacedView, kv => kv.Key,
id => GeneratorLocations.LowLevel($"OpenSearchLowLevelClient.{id}.cs"), token);
}
}
public override string Title => "OpenSearch.Net client implementation";

public override async Task Generate(RestApiSpec spec, ProgressBar progressBar, CancellationToken token)
{
await DoRazor(spec, View(), Target(), token);

await DoRazor(HttpMethod.All, View("Http"), Target("Http"), token);

await DoRazorDependantFiles(
progressBar,
spec.EndpointsPerNamespaceLowLevel.Where(kv => kv.Key != CsharpNames.RootNamespace).ToList(),
View("Namespace"),
kv => kv.Key,
Target,
token
);

return;

string View(string id = null) =>
ViewLocations.LowLevel("Client", "Implementation", $"OpenSearchLowLevelClient{(id != null ? $".{id}" : string.Empty)}.cshtml");

string Target(string id = null) => GeneratorLocations.LowLevel($"OpenSearchLowLevelClient{(id != null ? $".{id}" : string.Empty)}.cs");
}
}
40 changes: 25 additions & 15 deletions src/ApiGenerator/Generator/Razor/RequestParametersGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,37 @@
* under the License.
*/

using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using ApiGenerator.Configuration;
using ApiGenerator.Domain;
using ApiGenerator.Domain.Code;
using ShellProgressBar;

namespace ApiGenerator.Generator.Razor
namespace ApiGenerator.Generator.Razor;

public class RequestParametersGenerator : RazorGeneratorBase
{
public class RequestParametersGenerator : RazorGeneratorBase
{
public override string Title { get; } = "OpenSearch.Net request parameters";

public override async Task Generate(RestApiSpec spec, ProgressBar progressBar, CancellationToken token)
{
var view = ViewLocations.LowLevel("RequestParameters", "RequestParameters.cshtml");
string Target(string id) => GeneratorLocations.LowLevel("Api", "RequestParameters", $"RequestParameters.{id}.cs");

var namespaced = spec.EndpointsPerNamespaceLowLevel.ToList();
await DoRazorDependantFiles(progressBar, namespaced, view, kv => kv.Key, id => Target(id), token);
}
}
public override string Title => "OpenSearch.Net request parameters";

public override async Task Generate(RestApiSpec spec, ProgressBar progressBar, CancellationToken token)
{
await DoRazor(HttpMethod.All, View("Http"), Target("Http"), token);

await DoRazorDependantFiles(
progressBar,
spec.EndpointsPerNamespaceLowLevel.ToList(),
View(),
kv => kv.Key,
Target,
token
);

return;

string View(string id = null) => ViewLocations.LowLevel("RequestParameters", $"RequestParameters{(id != null ? $".{id}" : string.Empty)}.cshtml");

string Target(string id) => GeneratorLocations.LowLevel("Api", "RequestParameters", $"RequestParameters.{id}.cs");
}
}
31 changes: 22 additions & 9 deletions src/ApiGenerator/Generator/Razor/RequestsGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@
* under the License.
*/

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using ApiGenerator.Configuration;
using ApiGenerator.Domain;
using ApiGenerator.Domain.Code;
using ApiGenerator.Domain.Specification;
using ShellProgressBar;

namespace ApiGenerator.Generator.Razor
Expand All @@ -42,14 +46,23 @@ public class RequestsGenerator : RazorGeneratorBase

public override async Task Generate(RestApiSpec spec, ProgressBar progressBar, CancellationToken token)
{
var view = ViewLocations.HighLevel("Requests", "PlainRequestBase.cshtml");
var target = GeneratorLocations.HighLevel("Requests.cs");
await DoRazor(spec, view, target, token);

var dependantView = ViewLocations.HighLevel("Requests", "Requests.cshtml");
string Target(string id) => GeneratorLocations.HighLevel($"Requests.{id}.cs");
var namespaced = spec.EndpointsPerNamespaceHighLevel.ToList();
await DoRazorDependantFiles(progressBar, namespaced, dependantView, kv => kv.Key, id => Target(id), token);
}
await DoRazor(spec, View("PlainRequestBase"), Target(), token);

await DoRazor(HttpMethod.All, View("Requests.Http"), Target("Http"), token);

await DoRazorDependantFiles(
progressBar,
spec.EndpointsPerNamespaceHighLevel.ToList(),
View("Requests"),
kv => kv.Key,
Target,
token);

return;

string View(string name) => ViewLocations.HighLevel("Requests", $"{name}.cshtml");

string Target(string id = null) => GeneratorLocations.HighLevel($"Requests{(id != null ? $".{id}" : string.Empty)}.cs");
}
}
}
14 changes: 8 additions & 6 deletions src/ApiGenerator/Views/GeneratorNotice.cshtml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
@using ApiGenerator
@inherits CodeTemplatePage<object>
@inherits CodeTemplatePage<bool>
/* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
*/@if (Model)
{<text>
/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
Expand All @@ -27,15 +28,16 @@
* specific language governing permissions and limitations
* under the License.
*/
</text>}
// ███╗ ██╗ ██████╗ ████████╗██╗ ██████╗███████╗
// ████╗ ██║██╔═══██╗╚══██╔══╝██║██╔════╝██╔════╝
// ██╔██╗ ██║██║ ██║ ██║ ██║██║ █████╗
// ██║╚██╗██║██║ ██║ ██║ ██║██║ ██╔══╝
// ██╔██╗ ██║██║ ██║ ██║ ██║██║ █████╗
// ██║╚██╗██║██║ ██║ ██║ ██║██║ ██╔══╝
// ██║ ╚████║╚██████╔╝ ██║ ██║╚██████╗███████╗
// ╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚═╝ ╚═════╝╚══════╝
// -----------------------------------------------
//
// This file is automatically generated
//
// This file is automatically generated
// Please do not edit these files manually
// Run the following in the root of the repos:
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
@{
var (ns, endpoints) = Model;
}
@{ await IncludeGeneratorNotice(); }
@{ await IncludeLegacyGeneratorNotice(); }
// ReSharper disable RedundantUsingDirective
using System;
using System.Threading;
Expand Down
Loading

0 comments on commit 6ca6ee4

Please sign in to comment.