Skip to content

Commit

Permalink
Generate all missing low-level methods
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Farr <[email protected]>
  • Loading branch information
Xtansia committed Sep 23, 2024
1 parent cb2ef42 commit 6e4163b
Show file tree
Hide file tree
Showing 39 changed files with 10,279 additions and 68 deletions.
11 changes: 2 additions & 9 deletions src/ApiGenerator/Configuration/CodeConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,10 @@ public static class CodeConfiguration
{
private static readonly Glob[] OperationsToInclude =
[
new("cat.*"),
new("cluster.*"),
new("dangling_indices.*"),
new("indices.*"),
new("ingest.*"),
new("nodes.*"),
new("snapshot.*"),
new("tasks.*")
new("*")
];

public static bool IncludeOperation(string name) => !name.Contains('.') || OperationsToInclude.Any(g => g.IsMatch(name));
public static bool IncludeOperation(string name) => OperationsToInclude.Any(g => g.IsMatch(name));

/// <summary>
/// Map API default names for API's we are only supporting on the low level client first
Expand Down
5 changes: 4 additions & 1 deletion src/ApiGenerator/Domain/Code/CsharpNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,17 @@ public string PerPathMethodName(string path)
var method = MethodName;
// This is temporary for transition period
// TODO: remove in branch once it in opensearch is scrubbed
if (path.Contains("{type}") && !method.Contains("Type")) method += "UsingType";
if (pc("{type}") && !method.Contains("Type")) method += "UsingType";

if (ms("Indices") && !pc("{index}"))
return (method + "ForAll").Replace("AsyncForAll", "ForAllAsync");

if (ms("Nodes") && !pc("{node_id}"))
return (method + "ForAll").Replace("AsyncForAll", "ForAllAsync");

if (Namespace == "Knn" && method.StartsWith("Stats") && !pc("{node_id}"))
return method.Replace("Stats", "StatsForAll");

return method;
}

Expand Down
29 changes: 22 additions & 7 deletions src/ApiGenerator/Generator/ApiEndpointFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,14 +238,17 @@ private static string GetOpenSearchType(JsonSchema schema, Action<string, bool>
if (first.EndsWith("?")) return first;
if (first == second) return first;

switch (first, second)
return (first, second) switch
{
case ("string", "list"): return second;
case ("boolean", "string"): return first;
case ("number", _): return "string";
case (_,"number"): return "string";
}
(_, "list") => second,
("boolean", "string") => first,
("number", _) => "string",
(_, "number") => "string",
(_, _) => throw new Exception($"Unable to determine type of: {first} and {second}")
};
}

throw new Exception("Unable to determine type of oneOf");
}

var enumOptions = schema.Enumeration.Where(e => e != null).Select(e => e.ToString()).ToList();
Expand All @@ -259,7 +262,19 @@ private static string GetOpenSearchType(JsonSchema schema, Action<string, bool>
if (schema.Type == JsonObjectType.Array && (schema.Item?.HasReference ?? false))
_ = GetOpenSearchType(schema.Item, trackEnumToGenerate, true);

return schema.Type switch
var types = Enum.GetValues<JsonObjectType>()
.Where(t => t != JsonObjectType.None && schema.Type.HasFlag(t))
.ToHashSet();

var type = types.Count switch
{
0 => throw new Exception("No type specified"),
1 => types.First(),
2 when types.Contains(JsonObjectType.Boolean) && types.Contains(JsonObjectType.String) => JsonObjectType.String,
_ => throw new Exception($"Unable to determine type of: {string.Join(", ", types)}")
};

return type switch
{
JsonObjectType.Integer => "number",
JsonObjectType.Array => "list",
Expand Down
18 changes: 3 additions & 15 deletions src/ApiGenerator/Views/HighLevel/Requests/PlainRequestBase.cshtml
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
@using ApiGenerator.Domain
@inherits ApiGenerator.CodeTemplatePage<RestApiSpec>
@{ await IncludeLegacyGeneratorNotice(); }
// ReSharper disable RedundantUsingDirective
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Runtime.Serialization;
using OpenSearch.Net;
using OpenSearch.Net.Utf8Json;
@{ await IncludeAsync("LowLevel/Client/Usings.cshtml", Model);}

// ReSharper disable UnusedTypeParameter
namespace OpenSearch.Client
namespace OpenSearch.Client;

public abstract partial class @Raw("PlainRequestBase<TParameters>")
{
public abstract partial class @Raw("PlainRequestBase<TParameters>")
{
}
}
26 changes: 3 additions & 23 deletions src/OpenSearch.Client/_Generated/Requests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,7 @@
// Windows : build.bat codegen
//
// -----------------------------------------------
// ReSharper disable RedundantUsingDirective
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.Serialization;
using System.Text;
using OpenSearch.Net;
using OpenSearch.Net.Specification.CatApi;
using OpenSearch.Net.Specification.ClusterApi;
using OpenSearch.Net.Specification.DanglingIndicesApi;
using OpenSearch.Net.Specification.HttpApi;
using OpenSearch.Net.Specification.IndicesApi;
using OpenSearch.Net.Specification.IngestApi;
using OpenSearch.Net.Specification.NodesApi;
using OpenSearch.Net.Specification.SnapshotApi;
using OpenSearch.Net.Specification.TasksApi;
using OpenSearch.Net.Utf8Json;

// ReSharper disable UnusedTypeParameter
namespace OpenSearch.Client
{
public abstract partial class PlainRequestBase<TParameters> { }
}
namespace OpenSearch.Client;

public abstract partial class PlainRequestBase<TParameters> { }
116 changes: 116 additions & 0 deletions src/OpenSearch.Net/_Generated/Api/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,39 @@ public enum IndicesStatsMetric
All = 1 << 17,
}

[StringEnum]
public enum KnnDefaultOperator
{
[EnumMember(Value = "AND")]
And,

[EnumMember(Value = "OR")]
Or,
}

[StringEnum]
public enum KnnSearchType
{
[EnumMember(Value = "dfs_query_then_fetch")]
DfsQueryThenFetch,

[EnumMember(Value = "query_then_fetch")]
QueryThenFetch,
}

[StringEnum]
public enum KnnSuggestMode
{
[EnumMember(Value = "always")]
Always,

[EnumMember(Value = "missing")]
Missing,

[EnumMember(Value = "popular")]
Popular,
}

[StringEnum]
public enum Level
{
Expand Down Expand Up @@ -534,6 +567,37 @@ public enum NodesUsageMetric
All = 1 << 1,
}

[StringEnum]
public enum NotificationsNotificationConfigType
{
[EnumMember(Value = "chime")]
Chime,

[EnumMember(Value = "email")]
Email,

[EnumMember(Value = "email_group")]
EmailGroup,

[EnumMember(Value = "microsoft_teams")]
MicrosoftTeams,

[EnumMember(Value = "ses_account")]
SesAccount,

[EnumMember(Value = "slack")]
Slack,

[EnumMember(Value = "smtp_account")]
SmtpAccount,

[EnumMember(Value = "sns")]
Sns,

[EnumMember(Value = "webhook")]
Webhook,
}

[StringEnum]
public enum OpType
{
Expand Down Expand Up @@ -688,12 +752,16 @@ static partial void RegisterEnumStringResolvers()
AddEnumStringResolver<IndexApiBlock>(GetStringValue);
AddEnumStringResolver<IndicesShardStoresStatus>(GetStringValue);
AddEnumStringResolver<IndicesStatsMetric>(GetStringValue);
AddEnumStringResolver<KnnDefaultOperator>(GetStringValue);
AddEnumStringResolver<KnnSearchType>(GetStringValue);
AddEnumStringResolver<KnnSuggestMode>(GetStringValue);
AddEnumStringResolver<Level>(GetStringValue);
AddEnumStringResolver<NodesInfoMetric>(GetStringValue);
AddEnumStringResolver<NodesSampleType>(GetStringValue);
AddEnumStringResolver<NodesStatsIndexMetric>(GetStringValue);
AddEnumStringResolver<NodesStatsMetric>(GetStringValue);
AddEnumStringResolver<NodesUsageMetric>(GetStringValue);
AddEnumStringResolver<NotificationsNotificationConfigType>(GetStringValue);
AddEnumStringResolver<OpType>(GetStringValue);
AddEnumStringResolver<Refresh>(GetStringValue);
AddEnumStringResolver<SearchType>(GetStringValue);
Expand Down Expand Up @@ -900,6 +968,37 @@ public static string GetStringValue(this IndicesStatsMetric enumValue)
return string.Join(",", list);
}

public static string GetStringValue(this KnnDefaultOperator enumValue) =>
enumValue switch
{
KnnDefaultOperator.And => "AND",
KnnDefaultOperator.Or => "OR",
_ => throw new ArgumentException(
$"'{enumValue.ToString()}' is not a valid value for enum 'KnnDefaultOperator'"
),
};

public static string GetStringValue(this KnnSearchType enumValue) =>
enumValue switch
{
KnnSearchType.DfsQueryThenFetch => "dfs_query_then_fetch",
KnnSearchType.QueryThenFetch => "query_then_fetch",
_ => throw new ArgumentException(
$"'{enumValue.ToString()}' is not a valid value for enum 'KnnSearchType'"
),
};

public static string GetStringValue(this KnnSuggestMode enumValue) =>
enumValue switch
{
KnnSuggestMode.Always => "always",
KnnSuggestMode.Missing => "missing",
KnnSuggestMode.Popular => "popular",
_ => throw new ArgumentException(
$"'{enumValue.ToString()}' is not a valid value for enum 'KnnSuggestMode'"
),
};

public static string GetStringValue(this Level enumValue) =>
enumValue switch
{
Expand Down Expand Up @@ -1068,6 +1167,23 @@ public static string GetStringValue(this NodesUsageMetric enumValue)
return string.Join(",", list);
}

public static string GetStringValue(this NotificationsNotificationConfigType enumValue) =>
enumValue switch
{
NotificationsNotificationConfigType.Chime => "chime",
NotificationsNotificationConfigType.Email => "email",
NotificationsNotificationConfigType.EmailGroup => "email_group",
NotificationsNotificationConfigType.MicrosoftTeams => "microsoft_teams",
NotificationsNotificationConfigType.SesAccount => "ses_account",
NotificationsNotificationConfigType.Slack => "slack",
NotificationsNotificationConfigType.SmtpAccount => "smtp_account",
NotificationsNotificationConfigType.Sns => "sns",
NotificationsNotificationConfigType.Webhook => "webhook",
_ => throw new ArgumentException(
$"'{enumValue.ToString()}' is not a valid value for enum 'NotificationsNotificationConfigType'"
),
};

public static string GetStringValue(this OpType enumValue) =>
enumValue switch
{
Expand Down
Loading

0 comments on commit 6e4163b

Please sign in to comment.