Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup compilation constants #6590

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 0 additions & 30 deletions src/CookieCrumble/src/CookieCrumble/Extensions/WriterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,9 @@ public static class WriterExtensions
public static void Append(this IBufferWriter<byte> snapshot, string value)
=> Append(snapshot, value.AsSpan());

#if NET5_0_OR_GREATER
public static void Append(this IBufferWriter<byte> snapshot, ReadOnlySpan<char> value)
#else
public static unsafe void Append(this IBufferWriter<byte> snapshot, ReadOnlySpan<char> value)
#endif
{
#if NET5_0_OR_GREATER
_utf8.GetBytes(value, snapshot);
#else
var length = _utf8.GetByteCount(value);
byte[]? buffer = null;
var span = length <= 256
? stackalloc byte[length]
: buffer = ArrayPool<byte>.Shared.Rent(length);
int written;

fixed (byte* bptr = span)
{
fixed (char* cprt = value)
{
written = _utf8.GetBytes(cprt, value.Length, bptr, length);
}
}

var snapshotSpan = snapshot.GetSpan(written);
span[..written].CopyTo(snapshotSpan);
snapshot.Advance(written);

if (buffer is not null)
{
ArrayPool<byte>.Shared.Return(buffer);
}
#endif
}

public static void AppendLine(this IBufferWriter<byte> snapshot)
Expand Down
12 changes: 1 addition & 11 deletions src/CookieCrumble/src/CookieCrumble/TestEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,7 @@ namespace CookieCrumble;
/// </summary>
public static class TestEnvironment
{
#if NETCOREAPP3_1
/// <summary>
/// The target framework identifier.
/// </summary>
public const string TargetFramework = "NETCOREAPP3_1";
#elif NET5_0
/// <summary>
/// The target framework identifier.
/// </summary>
public const string TargetFramework = "NET5_0";
#elif NET6_0
#if NET6_0
/// <summary>
/// The target framework identifier.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ internal static class HttpResponseExtensions
private const string _contentDepositionValue = "attachment; filename=\"{0}\"";
private static readonly JsonSerializerOptions _serializerOptions = new()
{
#if NETCOREAPP3_1
IgnoreNullValues = true,
#else
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
#endif
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};

Expand Down
2 changes: 0 additions & 2 deletions src/HotChocolate/AspNetCore/src/AspNetCore/HeaderUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,7 @@ public AcceptHeaderResult(string headerValue)

public IQueryResult? ErrorResult { get; }

#if NET5_0_OR_GREATER
[MemberNotNullWhen(true, nameof(ErrorResult))]
#endif
public bool HasError { get; }
}
}
14 changes: 5 additions & 9 deletions src/HotChocolate/AspNetCore/src/AspNetCore/HttpGetMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,7 @@ private async Task HandleRequestAsync(HttpContext context)
acceptMediaTypes = HeaderUtilities.GraphQLResponseContentTypes;
statusCode = HttpStatusCode.BadRequest;

#if NET5_0_OR_GREATER
var errors = headerResult.ErrorResult.Errors!;
#else
var errors = headerResult.ErrorResult!.Errors!;
#endif
result = headerResult.ErrorResult;
_diagnosticEvents.HttpRequestError(context, errors[0]);
goto HANDLE_RESULT;
Expand Down Expand Up @@ -169,23 +165,23 @@ private async Task HandleRequestAsync(HttpContext context)
{
var flags = options.AllowedGetOperations;
var newRequestFlags = GraphQLRequestFlags.None;

if ((flags & AllowedGetOperations.Query) == AllowedGetOperations.Query)
{
newRequestFlags |= AllowQuery;
}

if ((flags & AllowedGetOperations.Mutation) == AllowedGetOperations.Mutation)
{
newRequestFlags |= AllowMutation;
}

if ((flags & AllowedGetOperations.Subscription) == AllowedGetOperations.Subscription &&
(requestFlags & AllowSubscription) == AllowSubscription)
{
newRequestFlags |= AllowSubscription;
}

if((requestFlags & AllowStreams) == AllowStreams)
{
newRequestFlags |= AllowStreams;
Expand Down Expand Up @@ -260,4 +256,4 @@ await WriteResultAsync(
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,7 @@ protected async Task HandleRequestAsync(HttpContext context)
acceptMediaTypes = HeaderUtilities.GraphQLResponseContentTypes;
statusCode = HttpStatusCode.BadRequest;

#if NET5_0_OR_GREATER
var errors = headerResult.ErrorResult.Errors!;
#else
var errors = headerResult.ErrorResult!.Errors!;
#endif
result = headerResult.ErrorResult;
DiagnosticEvents.HttpRequestError(context, errors[0]);
goto HANDLE_RESULT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public DefaultHttpResponseFormatter(
{
Json = new JsonResultFormatterOptions
{
Indented = indented,
Indented = indented,
Encoder = encoder
}
})
Expand Down Expand Up @@ -171,11 +171,7 @@ public async ValueTask FormatAsync(
result.ContextData.TryGetValue(CacheControlHeaderValue, out var value) &&
value is string cacheControlHeaderValue)
{
#if NET6_0_OR_GREATER
response.Headers.CacheControl = cacheControlHeaderValue;
#else
response.Headers[HttpHeaderKeys.CacheControl] = cacheControlHeaderValue;
#endif
}

OnWriteResponseHeaders(queryResult, format, response.Headers);
Expand All @@ -189,13 +185,7 @@ public async ValueTask FormatAsync(

response.ContentType = format.ContentType;
response.StatusCode = statusCode;

#if NET6_0_OR_GREATER
response.Headers.CacheControl = HttpHeaderValues.NoCache;
#else
response.Headers[HttpHeaderKeys.CacheControl] = HttpHeaderValues.NoCache;
#endif

OnWriteResponseHeaders(responseStream, format, response.Headers);

await response.Body.FlushAsync(cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,7 @@ protected JsonOperationMessage(string typeName, JsonElement? payload = null)
{
}

#if NET6_0_OR_GREATER
/// <inheritdoc />
public T? As<T>() where T : class
=> Payload?.Deserialize<T>(SerializerOptions);
#else
/// <inheritdoc />
public T? As<T>() where T : class
{
return Payload is null
? null
: JsonSerializer.Deserialize<T>(Payload.Value.GetRawText(), SerializerOptions);
}
#endif
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
#if NET6_0_OR_GREATER
using System.Text.Json;
using HotChocolate.AspNetCore.Subscriptions.Protocols.GraphQLOverWebSocket;
using HotChocolate.Utilities;
#else
using System.Text.Json;
using System.Text.Json.Serialization;
using HotChocolate.AspNetCore.Subscriptions.Protocols.GraphQLOverWebSocket;
using HotChocolate.Utilities;
#endif

namespace HotChocolate.AspNetCore.Subscriptions.Protocols;

Expand All @@ -16,20 +9,8 @@ internal static class MessageUtilities
public static JsonWriterOptions WriterOptions { get; } =
new() { Indented = false };

#if NET6_0_OR_GREATER
public static JsonSerializerOptions SerializerOptions { get; } =
new(JsonSerializerDefaults.Web);
#else
public static JsonSerializerOptions SerializerOptions { get; } =
new()
{
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
#if NET6_0_OR_GREATER
NumberHandling = JsonNumberHandling.AllowReadingFromString
#endif
};
#endif

public static void SerializeMessage(
ArrayWriter arrayWriter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private OrderedDictionary(OrderedDictionary<TKey, TValue> source)
_map = new Dictionary<TKey, TValue>(source._map);
}

#if NETCOREAPP3_1_OR_GREATER
#if NET6_0_OR_GREATER
public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
=> _map.TryGetValue(key, out value);
#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static class NameUtils
/// </returns>
public static string EnsureGraphQLName(
this string? name,
#if NETCOREAPP3_1_OR_GREATER
#if NET6_0_OR_GREATER
[CallerArgumentExpression("name")]
#endif
string argumentName = "name")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void TryContinue()

if (continuation is not null)
{
#if NETCOREAPP3_1_OR_GREATER
#if NET6_0_OR_GREATER
ThreadPool.QueueUserWorkItem(c => c(), continuation, true);
#else
ThreadPool.QueueUserWorkItem(_ => continuation());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private static async Task SendKeepAliveMessagesAsync(
// we do not need try-finally here because we dispose the semaphore in the parent
// method.
await synchronization.WaitAsync(ct);

await WriteKeepAliveAndFlush(outputStream, ct);

synchronization.Release();
Expand Down Expand Up @@ -162,7 +162,7 @@ private async ValueTask WriteNextMessageAsync(
Stream outputStream,
CancellationToken ct)
{
#if NETCOREAPP3_1_OR_GREATER
#if NET6_0_OR_GREATER
await outputStream.WriteAsync(_eventField, ct).ConfigureAwait(false);
await outputStream.WriteAsync(_nextEvent, ct).ConfigureAwait(false);
await outputStream.WriteAsync(_newLine, ct).ConfigureAwait(false);
Expand All @@ -175,7 +175,7 @@ private async ValueTask WriteNextMessageAsync(
using var bufferWriter = new ArrayWriter();
FormatPayload(bufferWriter, result);

#if NETCOREAPP3_1_OR_GREATER
#if NET6_0_OR_GREATER
await outputStream.WriteAsync(_dataField, ct).ConfigureAwait(false);
await outputStream.WriteAsync(bufferWriter.GetWrittenMemory(), ct).ConfigureAwait(false);
await outputStream.WriteAsync(_newLine, ct).ConfigureAwait(false);
Expand All @@ -195,7 +195,7 @@ private void FormatPayload(ArrayWriter bufferWriter, IQueryResult result)

private static async ValueTask WriteCompleteMessage(Stream outputStream, CancellationToken ct)
{
#if NETCOREAPP3_1_OR_GREATER
#if NET6_0_OR_GREATER
await outputStream.WriteAsync(_eventField, ct).ConfigureAwait(false);
await outputStream.WriteAsync(_completeEvent, ct).ConfigureAwait(false);
await outputStream.WriteAsync(_newLine, ct).ConfigureAwait(false);
Expand All @@ -210,7 +210,7 @@ private static async ValueTask WriteNewLineAndFlushAsync(
Stream outputStream,
CancellationToken ct)
{
#if NETCOREAPP3_1_OR_GREATER
#if NET6_0_OR_GREATER
await outputStream.WriteAsync(_newLine, ct).ConfigureAwait(false);
#else
await outputStream.WriteAsync(_newLine, 0, _newLine.Length, ct).ConfigureAwait(false);
Expand All @@ -222,7 +222,7 @@ private static async ValueTask WriteKeepAliveAndFlush(
Stream outputStream,
CancellationToken ct)
{
#if NETCOREAPP3_1_OR_GREATER
#if NET6_0_OR_GREATER
await outputStream.WriteAsync(_keepAlive, ct).ConfigureAwait(false);
#else
await outputStream.WriteAsync(_keepAlive, 0, _keepAlive.Length, ct).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -807,9 +807,7 @@ public ref string ByRefReturn()
public async void GetAsyncVoid() { }
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously

#if !NETCOREAPP2_1
public string RefStruct(ReadOnlySpan<byte> bytes) => "";
#endif

public Action Action { get; }

Expand Down
6 changes: 0 additions & 6 deletions src/HotChocolate/Core/test/Types.Tests/Types/EnumTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,14 +291,8 @@ void Action() => SchemaBuilder.New()
.Create();

// assert
#if NETCOREAPP2_1
Assert.Throws<SchemaException>(Action)
.Errors.Single().Message.MatchSnapshot(
new SnapshotNameExtension("NETCOREAPP2_1"));
#else
Assert.Throws<SchemaException>(Action)
.Errors.Single().Message.MatchSnapshot();
#endif
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,11 @@ public class InputObjectTypeNonNullTests
[Fact]
public void Nullable_Dictionary_Is_Correctly_Detected()
{
#if NETCOREAPP2_1
SchemaBuilder.New()
.AddQueryType<Query>()
.Create()
.ToString()
.MatchSnapshot(new SnapshotNameExtension("NETCOREAPP2_1"));
#else
SchemaBuilder.New()
.AddQueryType<Query>()
.Create()
.ToString()
.MatchSnapshot();
#endif
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,11 @@ public void Infer_Field_Types_From_Expression()
{
d.Name("Query");
d.Field(t => t.Bar.Text);
#if !NETCOREAPP2_1 && !NETCOREAPP3_1
d.Field(t => t.Bars.Select(t => t.Text)).Name("texts");
#endif
})
.Create()
.ToString()
#if NETCOREAPP2_1 || NETCOREAPP3_1
.MatchSnapshot(new SnapshotNameExtension("NETCOREAPP2_1"));
#else
.MatchSnapshot();
#endif
}

[Fact]
Expand Down
Loading
Loading