Skip to content

Commit

Permalink
Use pattern matching (#5045)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma authored Nov 22, 2024
1 parent 9ce0218 commit 2ab9847
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 39 deletions.
4 changes: 1 addition & 3 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@
[IDE0060] Remove unused parameter
[IDE0061] Use block body for local function
[IDE0074] Use compound assignment
[IDE0078] Use pattern matching
[IDE0083] Use pattern matching
[IDE0090] 'new' expression can be simplified
[IDE0130] Namespace does not match folder structure
[IDE0160] Convert to block scoped namespace
Expand Down Expand Up @@ -105,7 +103,7 @@
[SYSLIB0012] 'Assembly.CodeBase' is obsolete
-->
<NoWarn>$(NoWarn);IDE0005;IDE0008;IDE0011;IDE0017;IDE0019;IDE0021;IDE0022;IDE0025;IDE0027;IDE0028;IDE0029;IDE0032;IDE0039;IDE0045;IDE0046;IDE0055;IDE0056;IDE0057;IDE0059;IDE0060;IDE0061;IDE0074;IDE0078;IDE0083;IDE0090;IDE0130;IDE0160;IDE0200;IDE0260;IDE0270;IDE0290;IDE0330</NoWarn>
<NoWarn>$(NoWarn);IDE0005;IDE0008;IDE0011;IDE0017;IDE0019;IDE0021;IDE0022;IDE0025;IDE0027;IDE0028;IDE0029;IDE0032;IDE0039;IDE0045;IDE0046;IDE0055;IDE0056;IDE0057;IDE0059;IDE0060;IDE0061;IDE0074;IDE0090;IDE0130;IDE0160;IDE0200;IDE0260;IDE0270;IDE0290;IDE0330</NoWarn>
<NoWarn>$(NoWarn);CA1200;CA1304;CA1305;CA1310;CA1311;CA1507;CA1510;CA1514;CA1710;CA1716;CA1720;CA1725;CA1805;CA1834;CA1845;CA1847;CA1861;CA1862;CA1865;CA1866;CA1870;CA2249;CA2263;SYSLIB0012</NoWarn>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,10 @@ public CSharpFileTemplateModel(
public bool GenerateNullableReferenceTypes => _settings.CSharpGeneratorSettings.GenerateNullableReferenceTypes;

/// <summary>Gets a value indicating whether to generate contract code.</summary>
public bool GenerateContracts =>
_outputType == ClientGeneratorOutputType.Full ||
_outputType == ClientGeneratorOutputType.Contracts;
public bool GenerateContracts => _outputType is ClientGeneratorOutputType.Full or ClientGeneratorOutputType.Contracts;

/// <summary>Gets a value indicating whether to generate implementation code.</summary>
public bool GenerateImplementation =>
_outputType == ClientGeneratorOutputType.Full ||
_outputType == ClientGeneratorOutputType.Implementation;
public bool GenerateImplementation => _outputType is ClientGeneratorOutputType.Full or ClientGeneratorOutputType.Implementation;

/// <summary>Gets or sets a value indicating whether to generate client types.</summary>
public bool GenerateClientClasses => _settings.GenerateClientClasses;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,16 @@ internal TypeScriptFrameworkModel(TypeScriptClientGeneratorSettings settings)
public bool IsKnockout => _settings.TypeScriptGeneratorSettings.TypeStyle == TypeScriptTypeStyle.KnockoutClass;

/// <summary>Gets a value indicating whether to render for JQuery.</summary>
public bool IsJQuery => _settings.Template == TypeScriptTemplate.JQueryCallbacks || _settings.Template == TypeScriptTemplate.JQueryPromises;
public bool IsJQuery => _settings.Template is TypeScriptTemplate.JQueryCallbacks or TypeScriptTemplate.JQueryPromises;

/// <summary>Gets a value indicating whether to render for Fetch or Aurelia</summary>
public bool IsFetchOrAurelia => _settings.Template == TypeScriptTemplate.Fetch ||
_settings.Template == TypeScriptTemplate.Aurelia;
public bool IsFetchOrAurelia => _settings.Template is TypeScriptTemplate.Fetch or TypeScriptTemplate.Aurelia;

/// <summary>Gets a value indicating whether to render for Axios.</summary>
public bool IsAxios => _settings.Template == TypeScriptTemplate.Axios;

/// <summary>Gets a value indicating whether MomentJS is required.</summary>
public bool UseMomentJS => _settings.TypeScriptGeneratorSettings.DateTimeType == TypeScriptDateTimeType.MomentJS ||
_settings.TypeScriptGeneratorSettings.DateTimeType == TypeScriptDateTimeType.OffsetMomentJS;
public bool UseMomentJS => _settings.TypeScriptGeneratorSettings.DateTimeType is TypeScriptDateTimeType.MomentJS or TypeScriptDateTimeType.OffsetMomentJS;

/// <summary>Gets a value indicating whether DayJS is required.</summary>
public bool UseDayJS => _settings.TypeScriptGeneratorSettings.DateTimeType == TypeScriptDateTimeType.DayJS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,10 @@ public override string ResultType
public bool IsAngular => _settings.Template == TypeScriptTemplate.Angular;

/// <summary>Gets a value indicating whether to render for JQuery.</summary>
public bool IsJQuery => _settings.Template == TypeScriptTemplate.JQueryCallbacks ||
_settings.Template == TypeScriptTemplate.JQueryPromises;
public bool IsJQuery => _settings.Template is TypeScriptTemplate.JQueryCallbacks or TypeScriptTemplate.JQueryPromises;

/// <summary>Gets a value indicating whether to render for Fetch or Aurelia</summary>
public bool IsFetchOrAurelia => _settings.Template == TypeScriptTemplate.Fetch ||
_settings.Template == TypeScriptTemplate.Aurelia;
public bool IsFetchOrAurelia => _settings.Template is TypeScriptTemplate.Fetch or TypeScriptTemplate.Aurelia;

/// <summary>Gets a value indicating whether to use HttpClient with the Angular template.</summary>
public bool UseAngularHttpClient => IsAngular && _settings.HttpClass == HttpClass.HttpClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ public override string GetTypeName(JsonSchema schema, bool isNullable, string ty
/// <returns>The type name.</returns>
public override string GetBinaryResponseTypeName()
{
return Settings.Template != TypeScriptTemplate.JQueryCallbacks &&
Settings.Template != TypeScriptTemplate.JQueryPromises ? "FileResponse" : "any";
return Settings.Template is not TypeScriptTemplate.JQueryCallbacks and not TypeScriptTemplate.JQueryPromises
? "FileResponse"
: "any";
}

/// <summary>Generates the file.</summary>
Expand Down
9 changes: 3 additions & 6 deletions src/NSwag.CodeGeneration/Models/OperationModelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,10 @@ protected abstract TResponseModel CreateResponseModel(OpenApiOperation operation
public string HttpMethodLower => ConversionUtilities.ConvertToLowerCamelCase(HttpMethod.ToString(), false);

/// <summary>Gets a value indicating whether the HTTP method is GET or DELETE or HEAD.</summary>
public bool IsGetOrDeleteOrHead =>
HttpMethod == OpenApiOperationMethod.Get ||
HttpMethod == OpenApiOperationMethod.Delete ||
HttpMethod == OpenApiOperationMethod.Head;
public bool IsGetOrDeleteOrHead => HttpMethod is OpenApiOperationMethod.Get or OpenApiOperationMethod.Delete or OpenApiOperationMethod.Head;

/// <summary>Gets a value indicating whether the HTTP method is GET or HEAD.</summary>
public bool IsGetOrHead => HttpMethod == OpenApiOperationMethod.Get || HttpMethod == OpenApiOperationMethod.Head;
public bool IsGetOrHead => HttpMethod is OpenApiOperationMethod.Get or OpenApiOperationMethod.Head;

// TODO: Remove this (may not work correctly)
/// <summary>Gets or sets a value indicating whether the operation has a result type (i.e. not void).</summary>
Expand Down Expand Up @@ -196,7 +193,7 @@ public TParameterModel ContentParameter
public IEnumerable<TParameterModel> PathParameters => Parameters.Where(p => p.Kind == OpenApiParameterKind.Path);

/// <summary>Gets the query parameters.</summary>
public IEnumerable<TParameterModel> QueryParameters => Parameters.Where(p => p.Kind == OpenApiParameterKind.Query || p.Kind == OpenApiParameterKind.ModelBinding);
public IEnumerable<TParameterModel> QueryParameters => Parameters.Where(p => p.Kind is OpenApiParameterKind.Query or OpenApiParameterKind.ModelBinding);

/// <summary>Gets a value indicating whether the operation has query parameters.</summary>
public bool HasQueryParameters => QueryParameters.Any();
Expand Down
7 changes: 3 additions & 4 deletions src/NSwag.Commands/NSwagDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,17 +255,16 @@ private string GetProgramName()
#pragma warning restore CA1822
{
#if NET462

var runtime = Runtime != Runtime.Default ? Runtime : RuntimeUtilities.CurrentRuntime;
if (runtime == Runtime.WinX64 || runtime == Runtime.Debug)
if (runtime is Runtime.WinX64 or Runtime.Debug)
{
return System.IO.Path.Combine(RootBinaryDirectory, "Win/nswag.exe");
}
else if (runtime == Runtime.WinX86)

if (runtime == Runtime.WinX86)
{
return System.IO.Path.Combine(RootBinaryDirectory, "Win/nswag.x86.exe");
}
else
#endif
return "dotnet";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public OperationResponseProcessor(AspNetCoreOpenApiDocumentGeneratorSettings set
/// <returns>true if the operation should be added to the Swagger specification.</returns>
public bool Process(OperationProcessorContext operationProcessorContext)
{
if (!(operationProcessorContext is AspNetCoreOperationProcessorContext context))
if (operationProcessorContext is not AspNetCoreOperationProcessorContext context)
{
return false;
}
Expand Down
8 changes: 2 additions & 6 deletions src/NSwag.Generation/GenericResultWrapperTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
{
internal static class GenericResultWrapperTypes
{
internal static bool IsGenericWrapperType(string typeName) =>
typeName == "Task`1" ||
typeName == "ValueTask`1" ||
typeName == "JsonResult`1" ||
typeName == "ActionResult`1";
private static bool IsGenericWrapperType(string typeName) => typeName is "Task`1" or "ValueTask`1" or "JsonResult`1" or "ActionResult`1";

internal static T RemoveGenericWrapperTypes<T>(T type, Func<T, string> getName, Func<T, T> unwrap)
{
Expand All @@ -19,4 +15,4 @@ internal static T RemoveGenericWrapperTypes<T>(T type, Func<T, string> getName,
return type;
}
}
}
}
4 changes: 2 additions & 2 deletions src/NSwag.Generation/OpenApiDocumentGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ public OpenApiParameter CreateUntypedPathParameter(string parameterName, string
parameter.Type = JsonObjectType.String;
parameter.Format = JsonFormatStrings.Guid;
}
else if (parameterType == "int" || parameterType == "integer" || parameterType == "short" || parameterType == "long")
else if (parameterType is "int" or "integer" or "short" or "long")
{
parameter.Type = JsonObjectType.Integer;
}
else if (parameterType == "number" || parameterType == "decimal" || parameterType == "double")
else if (parameterType is "number" or "decimal" or "double")
{
parameter.Type = JsonObjectType.Number;
}
Expand Down

0 comments on commit 2ab9847

Please sign in to comment.