Skip to content

Commit

Permalink
alter the c# templates to allow net core and net framework targets to…
Browse files Browse the repository at this point in the history
… exist in the same output. (uses defines in the c# instead of directives in the mustache processor).
  • Loading branch information
Bryan Aldrich committed Dec 3, 2024
1 parent 423ba67 commit 9879fa5
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ using System.Text;
using System.Threading;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
{{^net60OrLater}}
#if !NET6_0_OR_GREATER
using System.Web;
{{/net60OrLater}}
#endif
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using RestSharp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ using System.Text;
using System.Threading;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
{{^net60OrLater}}
#if !NET6_0_OR_GREATER
using System.Web;
{{/net60OrLater}}
#endif
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using RestSharp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,14 @@ namespace {{packageName}}.Client
// https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Anchor_8
// For example: 2009-06-15T13:45:30.0000000
return dateTimeOffset.ToString((configuration ?? GlobalConfiguration.Instance).DateTimeFormat);
{{#net60OrLater}}
#if NET6_0_OR_GREATER
if (obj is DateOnly dateOnly)
// Return a formatted date string - Can be customized with Configuration.DateTimeFormat
// Defaults to an ISO 8601, using the known as a Round-trip date/time pattern ("o")
// https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Anchor_8
// For example: 2009-06-15
return dateOnly.ToString((configuration ?? GlobalConfiguration.Instance).DateTimeFormat);
{{/net60OrLater}}
#endif
if (obj is bool boolean)
return boolean ? "true" : "false";
if (obj is ICollection collection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ namespace {{packageName}}.Client
/// <returns>ECDSA signature</returns>
private string GetECDSASignature(byte[] dataToSign)
{
{{#net60OrLater}}
#if NET6_0_OR_GREATER
if (!File.Exists(KeyFilePath) && string.IsNullOrEmpty(KeyString))
{
throw new Exception("No API key has been provided.");
Expand Down Expand Up @@ -382,10 +382,10 @@ namespace {{packageName}}.Client
var signedString = System.Convert.ToBase64String(derBytes);

return signedString;
{{/net60OrLater}}
{{^net60OrLater}}
#endif
#if !NET6_0_OR_GREATER
throw new Exception("ECDSA signing is supported only on NETCOREAPP3_0 and above");
{{/net60OrLater}}
#endif
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,11 @@ namespace {{packageName}}.{{clientPackage}}
/// </summary>
/// <param name="result"></param>
/// <returns></returns>
bool Try{{.}}({{#net60OrLater}}[NotNullWhen(true)]{{/net60OrLater}}out TType{{nrt?}} result);
bool Try{{.}}(
#if NET6_0_OR_GREATER
[NotNullWhen(true)]
#endif
out TType{{nrt?}} result);
}
{{/x-http-statuses-with-return}}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
// This logic may be modified with the AsModel.mustache template
return Is{{vendorExtensions.x-http-status}}
? System.Text.Json.JsonSerializer.Deserialize<{{#isModel}}{{^containerType}}{{packageName}}.{{modelPackage}}.{{/containerType}}{{/isModel}}{{{dataType}}}>(RawContent, _jsonSerializerOptions)
: {{#net60OrLater}}null{{/net60OrLater}}{{^net60OrLater}}default{{/net60OrLater}};
:
#if NET6_0_OR_GREATER
null
#else
default
#endif;
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,15 @@ using System.Runtime.CompilerServices;
/// <param name="options"></param>
/// <param name="result"></param>
/// <returns></returns>
public static bool TryDeserialize<T>(string json, JsonSerializerOptions options, {{#net60OrLater}}[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] {{/net60OrLater}}out T{{#nrt}}{{#net60OrLater}}?{{/net60OrLater}}{{/nrt}} result)
public static bool TryDeserialize<T>(string json, JsonSerializerOptions options,
#if NET6_0_OR_GREATER
[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
#endif
out T{{#nrt}}
#if NET6_0_OR_GREATER
?
#endif
{{/nrt}} result)
{
try
{
Expand All @@ -129,7 +137,15 @@ using System.Runtime.CompilerServices;
/// <param name="options"></param>
/// <param name="result"></param>
/// <returns></returns>
public static bool TryDeserialize<T>(ref Utf8JsonReader reader, JsonSerializerOptions options, {{#net60OrLater}}[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)] {{/net60OrLater}}out T{{#nrt}}{{#net60OrLater}}?{{/net60OrLater}}{{/nrt}} result)
public static bool TryDeserialize<T>(ref Utf8JsonReader reader, JsonSerializerOptions options,
#if NET6_0_OR_GREATER
[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
#endif
out T{{#nrt}}
#if NET6_0_OR_GREATER
?
#endif
{{/nrt}} result)
{
try
{
Expand Down Expand Up @@ -176,10 +192,10 @@ using System.Runtime.CompilerServices;
// https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Anchor_8
// For example: 2009-06-15T13:45:30.0000000
return dateTimeOffset.ToString(format);
{{#net60OrLater}}
#if NET6_0_OR_GREATER
if (obj is DateOnly dateOnly)
return dateOnly.ToString(format);
{{/net60OrLater}}
#endif
if (obj is bool boolean)
return boolean
? "true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace {{packageName}}.{{clientPackage}}
{{/isEnum}}
{{/model}}
{{/models}}
JsonSerializerOptionsProvider jsonSerializerOptionsProvider = new{{^net60OrLater}} JsonSerializerOptionsProvider{{/net60OrLater}}(_jsonOptions);
JsonSerializerOptionsProvider jsonSerializerOptionsProvider = new JsonSerializerOptionsProvider(_jsonOptions);
_services.AddSingleton(jsonSerializerOptionsProvider);
{{#useSourceGeneration}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ namespace {{packageName}}.{{clientPackage}}
/// <returns></returns>
private string GetECDSASignature(byte[] dataToSign)
{
{{#net60OrLater}}
#if NET6_0_OR_GREATER
if (!File.Exists(KeyFilePath))
throw new Exception("key file path does not exist.");
Expand Down Expand Up @@ -310,10 +310,10 @@ namespace {{packageName}}.{{clientPackage}}
var signedString = System.Convert.ToBase64String(derBytes);

return signedString;
{{/net60OrLater}}
{{^net60OrLater}}
#endif
#if !NET6_0_OR_GREATER
throw new Exception("ECDSA signing is supported only on NETCOREAPP3_0 and above");
{{/net60OrLater}}
#endif
}

private byte[] ConvertToECDSAANS1Format(byte[] signedBytes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,11 @@ namespace {{packageName}}.{{apiPackage}}
tokenBaseLocalVars.Add(httpSignatureTokenLocalVar{{-index}});

if (httpRequestMessageLocalVar.Content != null) {
string requestBodyLocalVar = await httpRequestMessageLocalVar.Content.ReadAsStringAsync({{#net60OrLater}}cancellationToken{{/net60OrLater}}).ConfigureAwait(false);
string requestBodyLocalVar = await httpRequestMessageLocalVar.Content.ReadAsStringAsync(
#if NET6_0_OR_GREATER
cancellationToken
#endif
).ConfigureAwait(false);
httpSignatureTokenLocalVar{{-index}}.UseInHeader(httpRequestMessageLocalVar, requestBodyLocalVar, cancellationToken);
}
Expand Down Expand Up @@ -607,23 +611,25 @@ namespace {{packageName}}.{{apiPackage}}
httpRequestMessageLocalVar.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(acceptLocalVar));
{{/-first}}
{{/produces}}
{{#net60OrLater}}

#if NET6_0_OR_GREATER
httpRequestMessageLocalVar.Method = HttpMethod.{{#lambda.titlecase}}{{#lambda.lowercase}}{{httpMethod}}{{/lambda.lowercase}}{{/lambda.titlecase}};
{{/net60OrLater}}
{{^net60OrLater}}
#else
httpRequestMessageLocalVar.Method = new HttpMethod("{{#lambda.uppercase}}{{httpMethod}}{{/lambda.uppercase}}");
{{/net60OrLater}}
#endif

DateTime requestedAtLocalVar = DateTime.UtcNow;

using (HttpResponseMessage httpResponseMessageLocalVar = await HttpClient.SendAsync(httpRequestMessageLocalVar, cancellationToken).ConfigureAwait(false))
{
string responseContentLocalVar = await httpResponseMessageLocalVar.Content.ReadAsStringAsync({{#net60OrLater}}cancellationToken{{/net60OrLater}}).ConfigureAwait(false);
string responseContentLocalVar = await httpResponseMessageLocalVar.Content.ReadAsStringAsync(
#if NET6_0_OR_GREATER
cancellationToken
#endif
).ConfigureAwait(false);
ILogger<{{#vendorExtensions.x-duplicates}}{{.}}.{{/vendorExtensions.x-duplicates}}{{operationId}}ApiResponse> apiResponseLoggerLocalVar = LoggerFactory.CreateLogger<{{#vendorExtensions.x-duplicates}}{{.}}.{{/vendorExtensions.x-duplicates}}{{operationId}}ApiResponse>();

{{#vendorExtensions.x-duplicates}}{{.}}.{{/vendorExtensions.x-duplicates}}{{operationId}}ApiResponse apiResponseLocalVar = new{{^net60OrLater}} {{operationId}}ApiResponse{{/net60OrLater}}(apiResponseLoggerLocalVar, httpRequestMessageLocalVar, httpResponseMessageLocalVar, responseContentLocalVar, "{{{path}}}", requestedAtLocalVar, _jsonSerializerOptions);
{{#vendorExtensions.x-duplicates}}{{.}}.{{/vendorExtensions.x-duplicates}}{{operationId}}ApiResponse apiResponseLocalVar = new {{operationId}}ApiResponse(apiResponseLoggerLocalVar, httpRequestMessageLocalVar, httpResponseMessageLocalVar, responseContentLocalVar, "{{{path}}}", requestedAtLocalVar, _jsonSerializerOptions);

After{{operationId}}DefaultImplementation({{#lambda.joinWithComma}}apiResponseLocalVar {{#allParams}}{{paramName}} {{/allParams}}{{/lambda.joinWithComma}});

Expand Down Expand Up @@ -761,7 +767,11 @@ namespace {{packageName}}.{{apiPackage}}
/// </summary>
/// <param name="result"></param>
/// <returns></returns>
public bool Try{{vendorExtensions.x-http-status}}({{#net60OrLater}}[NotNullWhen(true)]{{/net60OrLater}}out {{#isModel}}{{^containerType}}{{packageName}}.{{modelPackage}}.{{/containerType}}{{/isModel}}{{{dataType}}}{{#nrt}}?{{/nrt}}{{^nrt}}{{#vendorExtensions.x-is-value-type}}?{{/vendorExtensions.x-is-value-type}}{{/nrt}} result)
public bool Try{{vendorExtensions.x-http-status}}(
#if NET6_0_OR_GREATER
[NotNullWhen(true)]
#endif
out {{#isModel}}{{^containerType}}{{packageName}}.{{modelPackage}}.{{/containerType}}{{/isModel}}{{{dataType}}}{{#nrt}}?{{/nrt}}{{^nrt}}{{#vendorExtensions.x-is-value-type}}?{{/vendorExtensions.x-is-value-type}}{{/nrt}} result)
{
result = null;
Expand Down

0 comments on commit 9879fa5

Please sign in to comment.