Skip to content

Commit

Permalink
Fixed DataLoader Generator Issues (#7385)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Aug 20, 2024
1 parent 9ff9a52 commit 6a6d7e6
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public sealed class DataLoaderFileBuilder : IDisposable

public DataLoaderFileBuilder()
{
_sb = StringBuilderPool.Get();
_sb = PooledObjects.GetStringBuilder();
_writer = new CodeWriter(_sb);
}

Expand Down Expand Up @@ -220,10 +220,11 @@ public void WriteDataLoaderLoadMethod(
WellKnownTypes.ReadOnlyList,
key.ToFullyQualified());
_writer.WriteIndentedLine(
"global::{0}<{1}<{2}>> results,",
"global::{0}<{1}<{2}{3}>> results,",
WellKnownTypes.Memory,
WellKnownTypes.Result,
value.ToFullyQualified());
value.ToFullyQualified(),
kind is DataLoaderKind.Group ? "[]" : string.Empty);
_writer.WriteIndentedLine(
"global::{0} ct)",
WellKnownTypes.CancellationToken);
Expand Down Expand Up @@ -321,10 +322,11 @@ public void WriteDataLoaderLoadMethod(
WellKnownTypes.ReadOnlyList,
key.ToFullyQualified());
_writer.WriteIndentedLine(
"global::{0}<{1}<{2}>> results,",
"global::{0}<{1}<{2}{3}>> results,",
WellKnownTypes.Span,
WellKnownTypes.Result,
value.ToFullyQualified());
value.ToFullyQualified(),
kind is DataLoaderKind.Group ? "[]" : string.Empty);
_writer.WriteIndentedLine(
"global::{0} resultMap)",
ExtractMapType(method.ReturnType));
Expand Down Expand Up @@ -482,7 +484,7 @@ public void Dispose()
return;
}

StringBuilderPool.Return(_sb);
PooledObjects.Return(_sb);
_sb = default!;
_writer = default!;
_disposed = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public sealed class DataLoaderModuleFileBuilder : IDisposable
public DataLoaderModuleFileBuilder(string moduleName)
{
_moduleName = moduleName;
_sb = StringBuilderPool.Get();
_sb = PooledObjects.GetStringBuilder();
_writer = new CodeWriter(_sb);
}

Expand Down Expand Up @@ -106,7 +106,7 @@ public void Dispose()
return;
}

StringBuilderPool.Return(_sb);
PooledObjects.Return(_sb);
_sb = default!;
_writer = default!;
_disposed = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public ModuleFileBuilder(string moduleName, string ns)
{
_moduleName = moduleName;
_ns = ns;
_sb = StringBuilderPool.Get();
_sb = PooledObjects.GetStringBuilder();
_writer = new CodeWriter(_sb);
}

Expand Down Expand Up @@ -304,7 +304,7 @@ public void Dispose()
return;
}

StringBuilderPool.Return(_sb);
PooledObjects.Return(_sb);
_sb = default!;
_writer = default!;
_disposed = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public sealed class OperationFieldFileBuilder : IDisposable

public OperationFieldFileBuilder()
{
_sb = StringBuilderPool.Get();
_sb = PooledObjects.GetStringBuilder();
_writer = new CodeWriter(_sb);
}

Expand Down Expand Up @@ -125,7 +125,7 @@ public void Dispose()
return;
}

StringBuilderPool.Return(_sb);
PooledObjects.Return(_sb);
_sb = default!;
_writer = default!;
_disposed = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public RequestMiddlewareFileBuilder(string moduleName, string ns)
_ns = ns;

_id = Guid.NewGuid().ToString("N");
_sb = StringBuilderPool.Get();
_sb = PooledObjects.GetStringBuilder();
_writer = new(_sb);
}

Expand Down Expand Up @@ -290,7 +290,7 @@ public void Dispose()
return;
}

StringBuilderPool.Return(_sb);
PooledObjects.Return(_sb);
_sb = default!;
_writer = default!;
_disposed = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ public void Generate(
return;
}

var sb = StringBuilderPool.Get();
var sb = PooledObjects.GetStringBuilder();

WriteTypes(context, syntaxInfos, sb);

sb.Clear();

WriteResolvers(context, syntaxInfos, sb);

StringBuilderPool.Return(sb);
PooledObjects.Return(sb);
}

private static void WriteTypes(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace HotChocolate.Types.Analyzers.Helpers;

public static class StringBuilderPool
public static class PooledObjects
{
private static StringBuilder? _stringBuilder;

public static StringBuilder Get()
public static StringBuilder GetStringBuilder()
{
var stringBuilder = Interlocked.Exchange(ref _stringBuilder, null);
return stringBuilder ?? new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Diagnostics.CodeAnalysis;
using HotChocolate.Types.Analyzers.Filters;
using HotChocolate.Types.Analyzers.Helpers;
using HotChocolate.Types.Analyzers.Models;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
Expand All @@ -16,7 +17,7 @@ public bool TryHandle(
GeneratorSyntaxContext context,
[NotNullWhen(true)] out SyntaxInfo? syntaxInfo)
{
if (context.Node is MethodDeclarationSyntax { AttributeLists.Count: > 0, } methodSyntax)
if (context.Node is MethodDeclarationSyntax { AttributeLists.Count: > 0 } methodSyntax)
{
foreach (var attributeListSyntax in methodSyntax.AttributeLists)
{
Expand All @@ -35,13 +36,11 @@ public bool TryHandle(
if (fullName.Equals(WellKnownAttributes.DataLoaderAttribute, Ordinal) &&
context.SemanticModel.GetDeclaredSymbol(methodSyntax) is { } methodSymbol)
{
var dataLoader = new DataLoaderInfo(
syntaxInfo = new DataLoaderInfo(
attributeSyntax,
attributeSymbol,
methodSymbol,
methodSyntax);

syntaxInfo = dataLoader;
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,14 @@ public static int CreateLookupKey(string key)

public static int CreateLookupKey(Guid key)
=> default!;

[DataLoader]
public static Task<ILookup<int, string>> GetSomeInfoGroupedById(
IReadOnlyList<int> keys)
=> default!;

[DataLoader]
public static Task<string> GetSomeInfoCacheById(
int key)
=> default!;
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace TestNamespace

protected override async global::System.Threading.Tasks.ValueTask FetchAsync(
global::System.Collections.Generic.IReadOnlyList<int> keys,
global::System.Memory<GreenDonut.Result<Entity>> results,
global::System.Memory<GreenDonut.Result<Entity[]>> results,
global::System.Threading.CancellationToken ct)
{
var temp = await TestNamespace.TestClass.GetEntitiesByIdAsync(keys, ct).ConfigureAwait(false);
Expand All @@ -47,7 +47,7 @@ namespace TestNamespace

private void CopyResults(
global::System.Collections.Generic.IReadOnlyList<int> keys,
global::System.Span<GreenDonut.Result<Entity>> results,
global::System.Span<GreenDonut.Result<Entity[]>> results,
global::System.Linq.ILookup<int, Entity> resultMap)
{
for (var i = 0; i < keys.Count; i++)
Expand Down

0 comments on commit 6a6d7e6

Please sign in to comment.