Skip to content

Commit

Permalink
Fixed source generator issues. (#7967)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Jan 30, 2025
1 parent 35c8091 commit d2be3dd
Show file tree
Hide file tree
Showing 80 changed files with 3,434 additions and 128 deletions.
1 change: 1 addition & 0 deletions src/CookieCrumble/src/CookieCrumble/MarkdownLanguages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ public static class MarkdownLanguages
public const string Json = "json";
public const string Yaml = "yaml";
public const string Text = "text";
public const string Sql = "sql";
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,39 @@ public static IQueryable<T> EnsureOrderPropsAreSelected<T>(
return ReplaceSelector(query, updatedSelector);
}

public static IQueryable<T> EnsureGroupPropsAreSelected<T, TKey>(
IQueryable<T> query,
Expression<Func<T, TKey>> keySelector)
{
var selector = ExtractCurrentSelector(query);
if (selector is null)
{
return query;
}

var body = GetMemberExpression(keySelector);
if (body is null)
{
return query;
}

var updatedSelector = AddPropertiesInSelector(selector, [body]);
return ReplaceSelector(query, updatedSelector);

static MemberExpression? GetMemberExpression(Expression<Func<T, TKey>> keySelector)
{
var body = keySelector.Body;

if (body is UnaryExpression unaryExpr)
{
body = unaryExpr.Operand;
}

return body as MemberExpression;
}

}

private static Expression<Func<T, T>>? ExtractCurrentSelector<T>(
IQueryable<T> query)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ public static async ValueTask<Dictionary<TKey, Page<TValue>>> ToBatchPageAsync<T
}

source = QueryHelpers.EnsureOrderPropsAreSelected(source);
source = QueryHelpers.EnsureGroupPropsAreSelected(source, keySelector);

// we need to move the ordering into the select expression we are constructing
// so that the groupBy will not remove it. The first thing we do here is to extract the order expressions
Expand Down Expand Up @@ -528,7 +529,7 @@ private sealed class InterceptorHolder
public PagingQueryInterceptor? Interceptor { get; set; }
}

private static PagingQueryInterceptor? TryGetQueryInterceptor()
internal static PagingQueryInterceptor? TryGetQueryInterceptor()
=> _interceptor.Value?.Interceptor;

internal static void SetQueryInterceptor(PagingQueryInterceptor pagingQueryInterceptor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,16 @@ public void Dispose()
_disposed = true;
}
}

/// <summary>
/// This method allows to publish a query to the current interceptor.
/// </summary>
/// <param name="query">
/// The query that is about to be executed.
/// </param>
/// <typeparam name="T">
/// The type of the items in the query.
/// </typeparam>
public static void Publish<T>(IQueryable<T> query)
=> PagingQueryableExtensions.TryGetQueryInterceptor()?.OnBeforeExecute(query);
}
28 changes: 28 additions & 0 deletions src/GreenDonut/src/GreenDonut.Data.Primitives/SortDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,34 @@ public SortDefinition<T> AddDescending<TResult>(
return new SortDefinition<T>(operations);
}

/// <summary>
/// Applies the sort definition if the definition is empty.
/// </summary>
/// <param name="applyIfEmpty">
/// The sort definition to apply if the current definition is empty.
/// </param>
/// <returns>
/// The updated sort definition.
/// </returns>
/// <exception cref="ArgumentNullException">
/// Thrown when the <paramref name="applyIfEmpty"/> is <see langword="null"/>.
/// </exception>
public SortDefinition<T> IfEmpty(
Func<SortDefinition<T>, SortDefinition<T>> applyIfEmpty)
{
if (applyIfEmpty == null)
{
throw new ArgumentNullException(nameof(applyIfEmpty));
}

if (Operations.Length == 0)
{
return applyIfEmpty(this);
}

return this;
}

/// <summary>
/// Returns a string representation of the sort definition.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Runtime.CompilerServices;
using GreenDonut.Data.Internal;
using static GreenDonut.Data.GreenDonutPredicateDataLoaderExtensions;
using static GreenDonut.Data.Internal.DataLoaderStateHelper;

// ReSharper disable once CheckNamespace
namespace GreenDonut.Data;
Expand Down Expand Up @@ -44,13 +45,13 @@ public static IDataLoader<TKey, Page<TValue>> WithPagingArguments<TKey, TValue>(
/// Branches a DataLoader with the provided <see cref="PagingArguments"/>.
/// </summary>
/// <param name="dataLoader">
/// The DataLoader that shall be branched.
/// The DataLoader that shall be branched.
/// </param>
/// <param name="pagingArguments">
/// The paging arguments that shall exist as state in the branched DataLoader.
/// The paging arguments that shall exist as state in the branched DataLoader.
/// </param>
/// <param name="context">
/// The query context that shall exist as state in the branched DataLoader.
/// The query context that shall exist as state in the branched DataLoader.
/// </param>
/// <typeparam name="TKey">
/// The key type of the DataLoader.
Expand Down Expand Up @@ -84,38 +85,6 @@ private static IDataLoader<TKey, Page<TValue>> WithInternal<TKey, TValue>(
var branchKey = pagingArguments.ComputeHash(context);
var state = new PagingState<TValue>(pagingArguments, context);
return (IQueryDataLoader<TKey, Page<TValue>>)dataLoader.Branch(branchKey, CreateBranch, state);

static IDataLoader CreateBranch(
string branchKey,
IDataLoader<TKey, Page<TValue>> dataLoader,
PagingState<TValue> state)
{
var branch = new QueryDataLoader<TKey, Page<TValue>>(
(DataLoaderBase<TKey, Page<TValue>>)dataLoader,
branchKey);

branch.SetState(DataLoaderStateKeys.PagingArgs, state.PagingArgs);

if (state.Context is not null)
{
if (state.Context.Selector is not null)
{
branch.SetState(DataLoaderStateKeys.Selector, state.Context.Selector);
}

if (state.Context.Predicate is not null)
{
branch.SetState(DataLoaderStateKeys.Predicate, state.Context.Predicate);
}

if (state.Context.Sorting is not null)
{
branch.SetState(DataLoaderStateKeys.Sorting, state.Context.Sorting);
}
}

return branch;
}
}

/// <summary>
Expand Down Expand Up @@ -166,7 +135,7 @@ public static IDataLoader<TKey, Page<TValue>> Select<TElement, TKey, TValue>(

var branchKey = selector.ComputeHash();
var state = new QueryState(DataLoaderStateKeys.Selector, new DefaultSelectorBuilder(selector));
return (IQueryDataLoader<TKey, Page<TValue>>)dataLoader.Branch(branchKey, DataLoaderStateHelper.CreateBranch,
return (IQueryDataLoader<TKey, Page<TValue>>)dataLoader.Branch(branchKey, CreateBranch,
state);
}

Expand Down Expand Up @@ -209,7 +178,7 @@ public static IDataLoader<TKey, Page<TValue>> Where<TKey, TValue>(
var branchKey = predicate.ComputeHash();
var state = new QueryState(DataLoaderStateKeys.Predicate,
GetOrCreateBuilder(dataLoader.ContextData, predicate));
return (IQueryDataLoader<TKey, Page<TValue>>)dataLoader.Branch(branchKey, DataLoaderStateHelper.CreateBranch,
return (IQueryDataLoader<TKey, Page<TValue>>)dataLoader.Branch(branchKey, CreateBranch,
state);
}

Expand Down Expand Up @@ -251,7 +220,7 @@ public static IDataLoader<TKey, Page<TValue>> OrderBy<TKey, TValue>(

var branchKey = sortDefinition.ComputeHash();
var state = new QueryState(DataLoaderStateKeys.Sorting, sortDefinition);
return (IQueryDataLoader<TKey, Page<TValue>>)dataLoader.Branch(branchKey, DataLoaderStateHelper.CreateBranch,
return (IQueryDataLoader<TKey, Page<TValue>>)dataLoader.Branch(branchKey, CreateBranch,
state);
}

Expand Down Expand Up @@ -391,6 +360,4 @@ private static int EstimateIntLength(int? value)

return length + 2;
}

private readonly record struct PagingState<T>(PagingArguments PagingArgs, QueryContext<T>? Context = null);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GreenDonut.Data.Internal;
using static GreenDonut.Data.Internal.DataLoaderStateHelper;

// ReSharper disable once CheckNamespace
namespace GreenDonut.Data;
Expand Down Expand Up @@ -48,7 +49,7 @@ public static IDataLoader<TKey, TValue> With<TKey, TValue>(
}

var branchKey = context.ComputeHash();
return (IQueryDataLoader<TKey, TValue>)dataLoader.Branch(branchKey, DataLoaderStateHelper.CreateBranch, context);
return (IQueryDataLoader<TKey, TValue>)dataLoader.Branch(branchKey, CreateBranch, context);
}

/// <summary>
Expand Down Expand Up @@ -91,7 +92,7 @@ public static IDataLoader<TKey, TValue[]> With<TKey, TValue>(
}

var branchKey = context.ComputeHash();
return (IQueryDataLoader<TKey, TValue[]>)dataLoader.Branch(branchKey, DataLoaderStateHelper.CreateBranch, context);
return (IQueryDataLoader<TKey, TValue[]>)dataLoader.Branch(branchKey, CreateBranch, context);
}

/// <summary>
Expand Down Expand Up @@ -134,6 +135,6 @@ public static IDataLoader<TKey, List<TValue>> With<TKey, TValue>(
}

var branchKey = context.ComputeHash();
return (IQueryDataLoader<TKey, List<TValue>>)dataLoader.Branch(branchKey, DataLoaderStateHelper.CreateBranch, context);
return (IQueryDataLoader<TKey, List<TValue>>)dataLoader.Branch(branchKey, CreateBranch, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ internal static IDataLoader CreateBranch<TKey, TValue, TElement>(

if (state.Selector is not null)
{
branch.SetState(DataLoaderStateKeys.Selector, state.Selector);
branch.SetState(DataLoaderStateKeys.Selector, new DefaultSelectorBuilder(state.Selector));
}

if (state.Predicate is not null)
{
branch.SetState(DataLoaderStateKeys.Predicate, state.Predicate);
branch.SetState(DataLoaderStateKeys.Predicate, new DefaultPredicateBuilder(state.Predicate));
}

if (state.Sorting is not null)
Expand All @@ -45,6 +45,39 @@ internal static IDataLoader CreateBranch<TKey, TValue, TElement>(
return branch;
}

internal static IDataLoader CreateBranch<TKey, TValue, TElement>(
string branchKey,
IDataLoader<TKey, Page<TElement>> dataLoader,
PagingState<TValue> state)
where TKey : notnull
{
var branch = new QueryDataLoader<TKey, Page<TValue>>(
(DataLoaderBase<TKey, Page<TValue>>)dataLoader,
branchKey);

branch.SetState(DataLoaderStateKeys.PagingArgs, state.PagingArgs);

if (state.Context is not null)
{
if (state.Context.Selector is not null)
{
branch.SetState(DataLoaderStateKeys.Selector, new DefaultSelectorBuilder(state.Context.Selector));
}

if (state.Context.Predicate is not null)
{
branch.SetState(DataLoaderStateKeys.Predicate, new DefaultPredicateBuilder(state.Context.Predicate));
}

if (state.Context.Sorting is not null)
{
branch.SetState(DataLoaderStateKeys.Sorting, state.Context.Sorting);
}
}

return branch;
}

internal static string ComputeHash<TValue>(this QueryContext<TValue> state)
{
var hasher = ExpressionHasherPool.Shared.Get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ internal static class DataLoaderStateKeys
public const string Selector = "GreenDonut.Data.Selector";
public const string Predicate = "GreenDonut.Data.Predicate";
public const string Sorting = "GreenDonut.Data.Sorting";
public const string PagingArgs = "GreenDonut.Data.Pagination.PagingArgs";
public const string PagingArgs = "GreenDonut.Data.PagingArgs";
}
3 changes: 3 additions & 0 deletions src/GreenDonut/src/GreenDonut.Data/Internal/PagingState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace GreenDonut.Data.Internal;

internal sealed record PagingState<T>(PagingArguments PagingArgs, QueryContext<T>? Context = null);
2 changes: 1 addition & 1 deletion src/GreenDonut/src/GreenDonut.Data/Internal/QueryState.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace GreenDonut.Data.Internal;

internal readonly record struct QueryState(string Key, object Value);
internal sealed record QueryState(string Key, object Value);
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private async ValueTask ExecuteResolverPipelineAsync(CancellationToken cancellat
var serviceScope = _operationContext.Services.CreateAsyncScope();
_context.Services = serviceScope.ServiceProvider;
_context.RegisterForCleanup(serviceScope.DisposeAsync);
_operationContext.ServiceScopeInitializer.Initialize(_context.RequestServices, _context.Services);
_operationContext.ServiceScopeInitializer.Initialize(_context, _context.RequestServices, _context.Services);
}

await _context.ResolverPipeline!(_context).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,24 +316,25 @@ public void WriteDataLoaderLoadMethod(
((INamedTypeSymbol)parameter.Type).TypeArguments[0].ToFullyQualified(),
DataLoaderInfo.Sorting);
_writer.WriteLine();
_writer.WriteIndentedLine(
"if({0}_selector is null && {0}_predicate is null && {0}_sortDefinition is null)");
_writer.WriteIndentedLine("{");
_writer.IncreaseIndent();

_writer.WriteIndentedLine(
"var {0} = global::{1}<{2}>.Empty;",
parameter.VariableName,
WellKnownTypes.QueryContext,
((INamedTypeSymbol)parameter.Type).TypeArguments[0].ToFullyQualified());
_writer.DecreaseIndent();
_writer.WriteIndentedLine("}");
_writer.WriteLine();
_writer.WriteIndentedLine(
"var {0} = new global::{1}<{2}>({0}_selector?, " +
"if({0}_selector is not null || {0}_predicate is not null || {0}_sortDefinition is not null)",
parameter.VariableName);
_writer.WriteIndentedLine("{");
_writer.IncreaseIndent();
_writer.WriteIndentedLine(
"{0} = new global::{1}<{2}>({0}_selector, " +
"{0}_predicate, {0}_sortDefinition);",
parameter.VariableName,
WellKnownTypes.QueryContext,
((INamedTypeSymbol)parameter.Type).TypeArguments[0].ToFullyQualified());
_writer.DecreaseIndent();
_writer.WriteIndentedLine("}");
}
else if (parameter.Kind is DataLoaderParameterKind.PagingArguments)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void WriteRegisterRootTypeExtension(string key, OperationType operation,
using (_writer.IncreaseIndent())
{
_writer.WriteIndentedLine("\"{0}\",", key);
_writer.WriteIndentedLine("global::HotChocolate.Types.OperationType.{0},", operation);
_writer.WriteIndentedLine("global::HotChocolate.Types.OperationTypeNames.{0},", operation);
_writer.WriteIndentedLine("() => {0}.Initialize));", extensionType);
}
}
Expand Down
Loading

0 comments on commit d2be3dd

Please sign in to comment.