Skip to content

Commit bf221c0

Browse files
committed
feat(): clean up validations
Signed-off-by: Joe Feser <[email protected]> feat(): clean up generator so we can format the files correctly Signed-off-by: Joe Feser <[email protected]>
1 parent 38966ce commit bf221c0

File tree

20 files changed

+68
-1776
lines changed

20 files changed

+68
-1776
lines changed

src/ApiGenerator/ApiGenerator.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
<PreserveCompilationContext>true</PreserveCompilationContext>
1010
</PropertyGroup>
1111
<ItemGroup>
12-
<PackageReference Include="CSharpier.Core" Version="0.28.2" />
1312
<PackageReference Include="Glob" Version="1.1.9" />
1413
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
1514
<PackageReference Include="NSwag.Core" Version="14.1.0" />

src/ApiGenerator/Generator/CodeGenerator.cs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,22 @@ public static class CodeGenerator
3939
{
4040
public static string CatFormatPropertyGenerator(string type, string name, string key, string setter) =>
4141
$"public {type} {name} {{ "
42-
+ $" get => Q<{type}>(\"{key}\");"
42+
+ $" get => Q<{type}>(\"{key}\");{Environment.NewLine}"
4343
+ $" set {{ Q(\"{key}\", {setter}); SetAcceptHeader({setter}); }}"
4444
+ $"}}";
4545

4646
public static string PropertyGenerator(string type, string name, string key, string setter) =>
47-
$"public {type} {name} {{ get => Q<{type}>(\"{key}\"); set => Q(\"{key}\", {setter}); }}";
47+
$"public {type} {name} {{ get => Q<{type}>(\"{key}\");{Environment.NewLine} set => Q(\"{key}\", {setter}); }}";
4848

4949
public static string Property(string @namespace, string type, string name, string key, string setter, string obsolete, Version versionAdded, params string[] doc)
5050
{
5151
var components = new List<string>();
52-
foreach (var d in RenderDocumentation(doc)) A(d);
53-
if (versionAdded != null) A($"/// <remarks>Supported by OpenSearch servers of version {versionAdded} or greater.</remarks>");
54-
if (!string.IsNullOrWhiteSpace(obsolete)) A($"[Obsolete(\"{obsolete}\")]");
52+
foreach (var d in RenderDocumentation(doc))
53+
A(d);
54+
if (versionAdded != null)
55+
A($"/// <remarks>Supported by OpenSearch servers of version {versionAdded} or greater.</remarks>");
56+
if (!string.IsNullOrWhiteSpace(obsolete))
57+
A($"[Obsolete(\"{obsolete}\")]");
5558

5659
var generated = @namespace != null && @namespace == "Cat" && name == "Format"
5760
? CatFormatPropertyGenerator(type, name, key, setter)
@@ -69,12 +72,16 @@ void A(string s)
6972
public static string Constructor(Constructor c)
7073
{
7174
var components = new List<string>();
72-
if (!c.Description.IsNullOrEmpty()) A(c.Description);
75+
if (!c.Description.IsNullOrEmpty())
76+
A(c.Description);
7377
var generated = c.Generated;
74-
if (c.Body.IsNullOrEmpty()) generated += "{}";
78+
if (c.Body.IsNullOrEmpty())
79+
generated += "{}";
7580
A(generated);
76-
if (!c.Body.IsNullOrEmpty()) A(c.Body);
77-
if (!c.AdditionalCode.IsNullOrEmpty()) A(c.AdditionalCode);
81+
if (!c.Body.IsNullOrEmpty())
82+
A(c.Body);
83+
if (!c.AdditionalCode.IsNullOrEmpty())
84+
A(c.AdditionalCode);
7885
return string.Join($"{Environment.NewLine}\t\t", components);
7986

8087
void A(string s)
@@ -88,15 +95,17 @@ private static IEnumerable<string> RenderDocumentation(params string[] doc)
8895
doc = (doc?.SelectMany(WrapDocumentation) ?? Enumerable.Empty<string>()).ToArray();
8996
switch (doc.Length)
9097
{
91-
case 0: yield break;
98+
case 0:
99+
yield break;
92100
case 1:
93101
yield return $"/// <summary>{doc[0]}</summary>";
94102

95103
yield break;
96104
default:
97105
yield return "/// <summary>";
98106

99-
foreach (var d in doc) yield return $"/// {d}";
107+
foreach (var d in doc)
108+
yield return $"/// {d}";
100109

101110
yield return "/// </summary>";
102111

@@ -106,7 +115,8 @@ private static IEnumerable<string> RenderDocumentation(params string[] doc)
106115

107116
private static string[] WrapDocumentation(string documentation)
108117
{
109-
if (string.IsNullOrWhiteSpace(documentation)) return Array.Empty<string>();
118+
if (string.IsNullOrWhiteSpace(documentation))
119+
return Array.Empty<string>();
110120
const int max = 140;
111121
var lines = documentation.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
112122
var charCount = 0;

src/ApiGenerator/Generator/Razor/RazorGeneratorBase.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
using System.Threading;
3333
using System.Threading.Tasks;
3434
using ApiGenerator.Domain;
35-
using CSharpier;
3635
using RazorLight;
3736
using RazorLight.Generation;
3837
using RazorLight.Razor;
@@ -59,7 +58,8 @@ protected static async Task DoRazor<TModel>(TModel model, string viewLocation, s
5958
}
6059
catch (TemplateGenerationException e)
6160
{
62-
foreach (var d in e.Diagnostics) Console.WriteLine(d.GetMessage());
61+
foreach (var d in e.Diagnostics)
62+
Console.WriteLine(d.GetMessage());
6363
throw;
6464
}
6565
}
@@ -86,14 +86,17 @@ CancellationToken token
8686

8787
private static async Task WriteFormattedCsharpFile(string path, string contents)
8888
{
89-
contents = (await CodeFormatter.FormatAsync(contents)).Code;
90-
91-
if (Directory.GetParent(path) is { Exists: false } dir) dir.Create();
89+
if (Directory.GetParent(path) is { Exists: false } dir)
90+
dir.Create();
9291

9392
await File.WriteAllTextAsync(path, contents);
9493
}
9594

96-
public abstract string Title { get; }
95+
public abstract string Title
96+
{
97+
get;
98+
}
99+
97100
public abstract Task Generate(RestApiSpec spec, ProgressBar progressBar, CancellationToken token);
98101
}
99102
}

src/OpenSearch.Client/CommonAbstractions/Extensions/TypeExtensions.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,6 @@ internal static object CreateInstance(this Type t, params object[] args)
8585
where p.Length == args.Length
8686
select c;
8787

88-
89-
/* Unmerged change from project 'OpenSearch.Client(netstandard2.1)'
90-
Before:
91-
var ctor = constructors.FirstOrDefault();
92-
if (ctor == null)
93-
throw new Exception($"Cannot create an instance of {t.FullName} because it has no constructor taking {args.Length} arguments");
94-
95-
activator = (ObjectActivator<object>)generic.Invoke(null, new object[] { ctor });
96-
After:
97-
var ctor = constructors.FirstOrDefault() ?? throw new Exception($"Cannot create an instance of {t.FullName} because it has no constructor taking {args.Length} arguments");
98-
activator = (ObjectActivator<object>)generic.Invoke(null, new object[] { ctor });
99-
*/
10088
var ctor = constructors.FirstOrDefault() ?? throw new Exception($"Cannot create an instance of {t.FullName} because it has no constructor taking {args.Length} arguments");
10189
activator = (ObjectActivator<object>)generic.Invoke(null, new object[] { ctor });
10290
CachedActivators.TryAdd(key, activator);

src/OpenSearch.Client/CommonAbstractions/Infer/Field/FieldResolver.cs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,27 +86,13 @@ public string Resolve(PropertyName property)
8686
private string Resolve(Expression expression, MemberInfo member, bool toLastToken = false)
8787
{
8888
var visitor = new FieldExpressionVisitor(_settings);
89-
90-
/* Unmerged change from project 'OpenSearch.Client(netstandard2.1)'
91-
Before:
92-
var name = expression != null
93-
? visitor.Resolve(expression, toLastToken)
94-
: member != null
95-
? visitor.Resolve(member)
96-
: null;
97-
After:
98-
var name = (expression != null
99-
? visitor.Resolve(expression, toLastToken)
100-
: member != null
101-
? visitor.Resolve(member)
102-
: null) ?? throw new ArgumentException("Name resolved to null for the given Expression or MemberInfo.");
103-
*/
104-
var name = (expression != null
89+
var name = expression != null
10590
? visitor.Resolve(expression, toLastToken)
10691
: member != null
10792
? visitor.Resolve(member)
108-
: null) ?? throw new ArgumentException("Name resolved to null for the given Expression or MemberInfo.");
109-
return name;
93+
: null;
94+
95+
return name ?? throw new ArgumentException("Name resolved to null for the given Expression or MemberInfo.");
11096
}
11197
}
11298
}

src/OpenSearch.Client/CommonAbstractions/SerializationBehavior/JsonFormatters/InterfaceGenericDictionaryResolver.cs

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,6 @@ internal static object GetFormatter(Type t)
7070

7171
Type implementationType;
7272
if (t.IsInterface)
73-
74-
/* Unmerged change from project 'OpenSearch.Client(netstandard2.1)'
75-
Before:
76-
var readAsAttribute = t.GetCustomAttribute<ReadAsAttribute>();
77-
if (readAsAttribute == null)
78-
throw new Exception($"Unable to deserialize interface {t.FullName}");
79-
80-
implementationType = readAsAttribute.Type.IsGenericType
81-
After:
82-
var readAsAttribute = t.GetCustomAttribute<ReadAsAttribute>() ?? throw new Exception($"Unable to deserialize interface {t.FullName}");
83-
implementationType = readAsAttribute.Type.IsGenericType
84-
*/
8573
{
8674
// need an implementation to deserialize interface to
8775
var readAsAttribute = t.GetCustomAttribute<ReadAsAttribute>() ?? throw new Exception($"Unable to deserialize interface {t.FullName}");
@@ -99,31 +87,6 @@ internal static object GetFormatter(Type t)
9987
orderby p.Length descending
10088
select c;
10189

102-
103-
/* Unmerged change from project 'OpenSearch.Client(netstandard2.1)'
104-
Before:
105-
var ctor = constructors.FirstOrDefault();
106-
if (ctor == null)
107-
throw new Exception($"Cannot create an instance of {t.FullName} because it does not "
108-
+ $"have a public constructor accepting "
109-
+ $"IDictionary<{typeArguments[0].FullName},{typeArguments[1].FullName}> argument "
110-
+ $"or a public parameterless constructor");
111-
112-
// construct a delegate for the ctor
113-
var activatorMethod = TypeExtensions.GetActivatorMethodInfo.MakeGenericMethod(t);
114-
var activator = activatorMethod.Invoke(null, new object[] { ctor });
115-
return CreateInstance(genericTypeArgs, activator, ctor.GetParameters().Length == 0);
116-
After:
117-
var ctor = constructors.FirstOrDefault() ?? throw new Exception($"Cannot create an instance of {t.FullName} because it does not "
118-
+ $"have a public constructor accepting "
119-
+ $"IDictionary<{typeArguments[0].FullName},{typeArguments[1].FullName}> argument "
120-
+ $"or a public parameterless constructor");
121-
122-
// construct a delegate for the ctor
123-
var activatorMethod = activatorMethod.Invoke(null, new object[] { ctor });
124-
var activator = activatorMethod.Invoke(null, new object[] { ctor });
125-
return CreateInstance(genericTypeArgs, activator, ctor.GetParameters().Length == 0);
126-
*/
12790
var ctor = constructors.FirstOrDefault() ?? throw new Exception($"Cannot create an instance of {t.FullName} because it does not "
12891
+ $"have a public constructor accepting "
12992
+ $"IDictionary<{typeArguments[0].FullName},{typeArguments[1].FullName}> argument "
@@ -173,18 +136,6 @@ internal static object GetFormatter(Type t)
173136

174137
Type implementationType;
175138
if (t.IsInterface)
176-
177-
/* Unmerged change from project 'OpenSearch.Client(netstandard2.1)'
178-
Before:
179-
var readAsAttribute = t.GetCustomAttribute<ReadAsAttribute>();
180-
if (readAsAttribute == null)
181-
throw new Exception($"Unable to deserialize interface {t.FullName}");
182-
183-
implementationType = readAsAttribute.Type.IsGenericType
184-
After:
185-
var readAsAttribute = t.GetCustomAttribute<ReadAsAttribute>() ?? throw new Exception($"Unable to deserialize interface {t.FullName}");
186-
implementationType = readAsAttribute.Type.IsGenericType
187-
*/
188139
{
189140
// need an implementation to deserialize interface to
190141
var readAsAttribute = t.GetCustomAttribute<ReadAsAttribute>() ?? throw new Exception($"Unable to deserialize interface {t.FullName}");
@@ -204,27 +155,6 @@ internal static object GetFormatter(Type t)
204155
orderby p.Length descending
205156
select c;
206157

207-
208-
/* Unmerged change from project 'OpenSearch.Client(netstandard2.1)'
209-
Before:
210-
var ctor = constructors.FirstOrDefault();
211-
if (ctor == null)
212-
throw new Exception($"Cannot create an instance of {t.FullName} because it does not "
213-
+ $"have a public constructor accepting IDictionary<{typeArguments[0].FullName},{typeArguments[1].FullName}> argument");
214-
215-
// construct a delegate for the ctor
216-
var activatorMethod = TypeExtensions.GetActivatorMethodInfo.MakeGenericMethod(t);
217-
var activator = activatorMethod.Invoke(null, new object[] { ctor });
218-
return CreateInstance(genericTypeArgs, activator, ctor.GetParameters().Length == 0);
219-
After:
220-
var ctor = constructors.FirstOrDefault() ?? throw new Exception($"Cannot create an instance of {t.FullName} because it does not "
221-
+ $"have a public constructor accepting IDictionary<{typeArguments[0].FullName},{typeArguments[1].FullName}> argument");
222-
223-
// construct a delegate for the ctor
224-
var activatorMethod = activatorMethod.Invoke(null, new object[] { ctor });
225-
var activator = activatorMethod.Invoke(null, new object[] { ctor });
226-
return CreateInstance(genericTypeArgs, activator, ctor.GetParameters().Length == 0);
227-
*/
228158
var ctor = constructors.FirstOrDefault() ?? throw new Exception($"Cannot create an instance of {t.FullName} because it does not "
229159
+ $"have a public constructor accepting IDictionary<{typeArguments[0].FullName},{typeArguments[1].FullName}> argument");
230160

@@ -297,16 +227,6 @@ public void Serialize(ref JsonWriter writer, TDictionary value, IJsonFormatterRe
297227
// TODO mutator is not used, should it?
298228
var mutator = formatterResolver.GetConnectionSettings().DefaultFieldNameInferrer;
299229
var valueFormatter = formatterResolver.GetFormatterWithVerify<TValue>();
300-
301-
/* Unmerged change from project 'OpenSearch.Client(netstandard2.1)'
302-
Before:
303-
var valueFormatter = formatterResolver.GetFormatterWithVerify<TValue>();
304-
305-
writer.WriteBeginObject();
306-
After:
307-
writer.WriteBeginObject();
308-
*/
309-
310230
writer.WriteBeginObject();
311231

312232
var e = GetSourceEnumerator(value);

src/OpenSearch.Client/CommonAbstractions/SerializationBehavior/JsonFormatters/IsADictionaryFormatterResolver.cs

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,6 @@ internal static object GetFormatter(Type t)
6666

6767
Type implementationType;
6868
if (t.IsInterface)
69-
70-
/* Unmerged change from project 'OpenSearch.Client(netstandard2.1)'
71-
Before:
72-
var readAsAttribute = t.GetCustomAttribute<ReadAsAttribute>();
73-
if (readAsAttribute == null)
74-
throw new Exception($"Unable to deserialize interface {t.FullName}");
75-
76-
implementationType = readAsAttribute.Type.IsGenericType
77-
After:
78-
var readAsAttribute = t.GetCustomAttribute<ReadAsAttribute>() ?? throw new Exception($"Unable to deserialize interface {t.FullName}");
79-
implementationType = readAsAttribute.Type.IsGenericType
80-
*/
8169
{
8270
// need an implementation to deserialize interface to
8371
var readAsAttribute = t.GetCustomAttribute<ReadAsAttribute>() ?? throw new Exception($"Unable to deserialize interface {t.FullName}");
@@ -95,31 +83,6 @@ internal static object GetFormatter(Type t)
9583
orderby p.Length descending
9684
select c;
9785

98-
99-
/* Unmerged change from project 'OpenSearch.Client(netstandard2.1)'
100-
Before:
101-
var ctor = constructors.FirstOrDefault();
102-
if (ctor == null)
103-
throw new Exception($"Cannot create an instance of {t.FullName} because it does not "
104-
+ $"have a constructor accepting "
105-
+ $"{genericDictionaryInterface.FullName} argument "
106-
+ $"or a parameterless constructor");
107-
108-
// construct a delegate for the ctor
109-
var activatorMethod = TypeExtensions.GetActivatorMethodInfo.MakeGenericMethod(t);
110-
var activator = activatorMethod.Invoke(null, new object[] { ctor });
111-
return CreateInstance(genericTypeArgs.ToArray(), activator, ctor.GetParameters().Length == 0);
112-
After:
113-
var ctor = constructors.FirstOrDefault() ?? throw new Exception($"Cannot create an instance of {t.FullName} because it does not "
114-
+ $"have a constructor accepting "
115-
+ $"{genericDictionaryInterface.FullName} argument "
116-
+ $"or a parameterless constructor");
117-
118-
// construct a delegate for the ctor
119-
var activatorMethod = activatorMethod.Invoke(null, new object[] { ctor });
120-
var activator = activatorMethod.Invoke(null, new object[] { ctor });
121-
return CreateInstance(genericTypeArgs.ToArray(), activator, ctor.GetParameters().Length == 0);
122-
*/
12386
var ctor = constructors.FirstOrDefault() ?? throw new Exception($"Cannot create an instance of {t.FullName} because it does not "
12487
+ $"have a constructor accepting "
12588
+ $"{genericDictionaryInterface.FullName} argument "
@@ -144,25 +107,6 @@ internal class IsADictionaryBaseFormatter<TKey, TValue, TDictionary>
144107
private readonly bool _parameterlessCtor;
145108

146109
public IsADictionaryBaseFormatter(TypeExtensions.ObjectActivator<TDictionary> activator, bool parameterlessCtor)
147-
148-
/* Unmerged change from project 'OpenSearch.Client(netstandard2.1)'
149-
Before:
150-
var keyFormatter = formatterResolver.GetFormatterWithVerify<TKey>() as IObjectPropertyNameFormatter<TKey>;
151-
var valueFormatter = formatterResolver.GetFormatterWithVerify<TValue>();
152-
153-
writer.WriteBeginObject();
154-
After:
155-
var valueFormatter = formatterResolver.GetFormatterWithVerify<TValue>();
156-
157-
writer.WriteBeginObject();
158-
*/
159-
160-
/* Unmerged change from project 'OpenSearch.Client(netstandard2.1)'
161-
Before:
162-
if (keyFormatter != null)
163-
After:
164-
if (formatterResolver.GetFormatterWithVerify<TKey>() is IObjectPropertyNameFormatter<TKey> keyFormatter)
165-
*/
166110
{
167111
_activator = activator;
168112
_parameterlessCtor = parameterlessCtor;

src/OpenSearch.Client/CommonOptions/Geo/Distance.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,14 @@ public Distance(string distanceUnit)
6767
return;
6868
}
6969

70-
var unitMeasure = unit.ToEnum<DistanceUnit>() ?? throw new InvalidCastException($"cannot parse {typeof(DistanceUnit).Name} from string '{unit}'");
70+
var unitMeasure = unit.ToEnum<DistanceUnit>();
71+
#pragma warning disable IDE0270 // Use coalesce expression
72+
if (unitMeasure == null)
73+
{
74+
throw new InvalidCastException($"cannot parse {typeof(DistanceUnit).Name} from string '{unit}'");
75+
}
76+
#pragma warning restore IDE0270 // Use coalesce expression
77+
7178
Unit = unitMeasure.Value;
7279
}
7380

0 commit comments

Comments
 (0)