Skip to content

Commit b55d403

Browse files
committed
Fixed Compile Issues
1 parent b2ae925 commit b55d403

File tree

14 files changed

+108
-26
lines changed

14 files changed

+108
-26
lines changed

src/GreenDonut/src/Core/CreateDataLoaderBranch.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#if NET6_0_OR_GREATER
21
namespace GreenDonut;
32

43
/// <summary>
@@ -18,4 +17,3 @@ public delegate IDataLoader CreateDataLoaderBranch<out TKey, TValue, in TState>(
1817
IDataLoader<TKey, TValue> dataLoader,
1918
TState state)
2019
where TKey : notnull;
21-
#endif

src/GreenDonut/src/Core/Data/ExpressionHasher.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,14 @@ public ExpressionHasher Add(ReadOnlySpan<char> span)
7272

7373
public string Compute()
7474
{
75+
#if NET8_0_OR_GREATER
7576
var hashBytes = MD5.HashData(_buffer.AsSpan().Slice(0, _start));
7677
var hashString = Convert.ToHexString(hashBytes).ToLowerInvariant();
78+
#else
79+
var md5 = MD5.Create();
80+
var hashBytes = md5.ComputeHash(_buffer, 0, _start);
81+
var hashString = BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant();
82+
#endif
7783

7884
_buffer.AsSpan().Slice(0, _start).Clear();
7985

@@ -281,6 +287,7 @@ private void Append(char a, char b)
281287
_buffer[_start++] = (byte)b;
282288
}
283289

290+
#if NET8_0_OR_GREATER
284291
private void Append(string s)
285292
{
286293
var span = _buffer.AsSpan().Slice(_start);
@@ -316,6 +323,47 @@ private void Append(ReadOnlySpan<char> s)
316323
_start += written;
317324
}
318325

326+
#else
327+
private void Append(string s)
328+
{
329+
byte[]? utf8Bytes = null;
330+
331+
try
332+
{
333+
utf8Bytes = Encoding.UTF8.GetBytes(s);
334+
EnsureBufferCapacity(utf8Bytes.Length);
335+
336+
Buffer.BlockCopy(utf8Bytes, 0, _buffer, _start, utf8Bytes.Length);
337+
_start += utf8Bytes.Length;
338+
}
339+
finally
340+
{
341+
if (utf8Bytes != null)
342+
{
343+
ArrayPool<byte>.Shared.Return(utf8Bytes);
344+
}
345+
}
346+
}
347+
348+
private void Append(ReadOnlySpan<char> s)
349+
{
350+
// Converting ReadOnlySpan<char> to string for .NET Standard 2.0 compatibility
351+
var str = new string(s.ToArray());
352+
Append(str);
353+
}
354+
355+
private void EnsureBufferCapacity(int requiredCapacity)
356+
{
357+
while (_buffer.Length - _start < requiredCapacity)
358+
{
359+
var newBuffer = ArrayPool<byte>.Shared.Rent(_buffer.Length * 2);
360+
Buffer.BlockCopy(_buffer, 0, newBuffer, 0, _start);
361+
ArrayPool<byte>.Shared.Return(_buffer);
362+
_buffer = newBuffer;
363+
}
364+
}
365+
#endif
366+
319367
private void Append(byte b)
320368
{
321369
if (_start == _buffer.Length)
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
namespace GreenDonut.Data;
22

3-
internal readonly record struct QueryState(string Key, object Value);
3+
internal readonly struct QueryState
4+
{
5+
public QueryState(string key, object value)
6+
{
7+
Key = key;
8+
Value = value;
9+
}
10+
11+
public string Key { get; }
12+
13+
public object Value { get; }
14+
}

src/GreenDonut/src/Core/DataLoaderBase.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,8 @@ public abstract partial class DataLoaderBase<TKey, TValue>
2929
private readonly IBatchScheduler _batchScheduler;
3030
private readonly int _maxBatchSize;
3131
private readonly IDataLoaderDiagnosticEvents _diagnosticEvents;
32-
#if NET6_0_OR_GREATER
3332
private ImmutableDictionary<string, IDataLoader> _branches =
3433
ImmutableDictionary<string, IDataLoader>.Empty;
35-
#endif
3634
private Batch<TKey>? _currentBatch;
3735

3836
/// <summary>
@@ -228,7 +226,7 @@ void Initialize()
228226
ct);
229227
}
230228
}
231-
229+
232230
/// <inheritdoc />
233231
public void Remove(TKey key)
234232
{
@@ -263,7 +261,6 @@ public void Set(TKey key, Task<TValue?> value)
263261
Cache.TryAdd(cacheKey, new Promise<TValue?>(value));
264262
}
265263
}
266-
#if NET6_0_OR_GREATER
267264

268265
/// <inheritdoc />
269266
public IDataLoader Branch<TState>(
@@ -302,7 +299,6 @@ public IDataLoader Branch<TState>(
302299

303300
return branch;
304301
}
305-
#endif
306302

307303
private void BatchOperationFailed(
308304
Batch<TKey> batch,

src/GreenDonut/src/Core/ExpressionHelpers.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#if NET6_0_OR_GREATER
21
using System.Linq.Expressions;
32
using System.Reflection;
43

@@ -240,4 +239,3 @@ protected override Expression VisitParameter(ParameterExpression node)
240239
=> node == toReplace ? replacement : base.VisitParameter(node);
241240
}
242241
}
243-
#endif

src/GreenDonut/src/Core/IDataLoader.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
using System.Collections.Immutable;
2-
#if NET8_0_OR_GREATER
3-
using GreenDonut.Selectors;
4-
#endif
52

63
namespace GreenDonut;
74

@@ -165,7 +162,6 @@ public interface IDataLoader<in TKey, TValue>
165162
/// </exception>
166163
void Set(TKey key, Task<TValue?> value);
167164

168-
#if NET6_0_OR_GREATER
169165
/// <summary>
170166
/// Branches the current <c>DataLoader</c>.
171167
/// </summary>
@@ -185,5 +181,4 @@ IDataLoader Branch<TState>(
185181
string key,
186182
CreateDataLoaderBranch<TKey, TValue, TState> createBranch,
187183
TState state);
188-
#endif
189184
}

src/GreenDonut/src/Data.Primitives/GreenDonut.Data.Primitives.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@
77
<Description>This package contains the basic building blocks of the DataLoader linq query integration.</Description>
88
</PropertyGroup>
99

10+
<ItemGroup Condition="'$(TargetFramework)' == 'NETSTANDARD2.0'">
11+
<PackageReference Include="System.Collections.Immutable" />
12+
</ItemGroup>
13+
1014
</Project>

src/GreenDonut/src/Data.Primitives/QueryContext.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace GreenDonut.Data;
44

5+
#if NET6_0_OR_GREATER
56
/// <summary>
67
/// Represents the context for constructing queries against a data source for <typeparamref name="TEntity"/>.
78
/// This record contains the projection (selector), filtering (predicate), and sorting instructions
@@ -29,3 +30,35 @@ public record QueryContext<TEntity>(
2930
/// </summary>
3031
public static QueryContext<TEntity> Empty { get; } = new();
3132
}
33+
#else
34+
/// <summary>
35+
/// Represents the context for constructing queries against a data source for <typeparamref name="TEntity"/>.
36+
/// This record contains the projection (selector), filtering (predicate), and sorting instructions
37+
/// (<see cref="SortDefinition{TEntity}"/>) for building a query.
38+
/// </summary>
39+
public class QueryContext<TEntity>(
40+
Expression<Func<TEntity, TEntity>>? selector = null,
41+
Expression<Func<TEntity, bool>>? predicate = null,
42+
SortDefinition<TEntity>? sorting = null)
43+
{
44+
/// <summary>
45+
/// An expression that defines what data shall be selected from <typeparamref name="TEntity"/>.
46+
/// </summary>
47+
public Expression<Func<TEntity, TEntity>>? Selector { get; } = selector;
48+
49+
/// <summary>
50+
/// An expression that defines the filtering condition for <typeparamref name="TEntity"/>.
51+
/// </summary>
52+
public Expression<Func<TEntity, bool>>? Predicate { get; } = predicate;
53+
54+
/// <summary>
55+
/// The sorting instructions (see <see cref="SortDefinition{TEntity}"/>) for <typeparamref name="TEntity"/>.
56+
/// </summary>
57+
public SortDefinition<TEntity>? Sorting { get; } = sorting;
58+
59+
/// <summary>
60+
/// An empty query context.
61+
/// </summary>
62+
public static QueryContext<TEntity> Empty { get; } = new();
63+
}
64+
#endif

src/GreenDonut/src/Data.Primitives/SortBy.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ public SortBy(Expression<Func<TEntity, TValue>> keySelector, bool ascending = tr
2828
/// <summary>
2929
/// Gets the field on which the sort operation is applied.
3030
/// </summary>
31-
public Expression<Func<TEntity, TValue>> KeySelector { get; init; }
31+
public Expression<Func<TEntity, TValue>> KeySelector { get; }
3232

3333
LambdaExpression ISortBy<TEntity>.KeySelector => KeySelector;
3434

3535
/// <summary>
3636
/// Gets the sort direction.
3737
/// </summary>
38-
public bool Ascending { get => field; init; }
38+
public bool Ascending { get; }
3939

4040
/// <summary>
4141
/// Applies the sort operation to the queryable.

src/GreenDonut/src/Data.Primitives/SortDefinition.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace GreenDonut.Data;
99
/// <typeparam name="T">
1010
/// The entity type on which the sort operations are applied.
1111
/// </typeparam>
12-
public sealed record SortDefinition<T>
12+
public sealed class SortDefinition<T>
1313
{
1414
/// <summary>
1515
/// Initializes a new instance of <see cref="SortDefinition{T}"/>.
@@ -19,7 +19,7 @@ public sealed record SortDefinition<T>
1919
/// </param>
2020
public SortDefinition(params ISortBy<T>[] operations)
2121
{
22-
Operations = [..operations];
22+
Operations = ImmutableArray.Create(operations);
2323
}
2424

2525
/// <summary>
@@ -30,13 +30,13 @@ public SortDefinition(params ISortBy<T>[] operations)
3030
/// </param>
3131
public SortDefinition(IEnumerable<ISortBy<T>> operations)
3232
{
33-
Operations = [..operations];
33+
Operations = ImmutableArray.CreateRange(operations);
3434
}
3535

3636
/// <summary>
3737
/// The sort operations.
3838
/// </summary>
39-
public ImmutableArray<ISortBy<T>> Operations { get; init; }
39+
public ImmutableArray<ISortBy<T>> Operations { get; }
4040

4141
/// <summary>
4242
/// Deconstructs the sort operations.

0 commit comments

Comments
 (0)