-
-
Notifications
You must be signed in to change notification settings - Fork 748
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Interface Field Inheritance (#7237)
- Loading branch information
1 parent
20e69df
commit 5536d20
Showing
41 changed files
with
2,458 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/HotChocolate/Core/src/Types.Analyzers/FileBuilders/IOutputTypeFileBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using HotChocolate.Types.Analyzers.Models; | ||
|
||
namespace HotChocolate.Types.Analyzers.FileBuilders; | ||
|
||
public interface IOutputTypeFileBuilder | ||
{ | ||
void WriteHeader(); | ||
|
||
void WriteBeginNamespace(); | ||
void WriteEndNamespace(); | ||
|
||
string WriteBeginClass(string typeName); | ||
void WriteEndClass(); | ||
|
||
void WriteInitializeMethod(IOutputTypeInfo typeInfo); | ||
|
||
void WriteConfigureMethod(IOutputTypeInfo typeInfo); | ||
} |
132 changes: 132 additions & 0 deletions
132
src/HotChocolate/Core/src/Types.Analyzers/FileBuilders/InterfaceTypeExtensionFileBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
using System.Text; | ||
using HotChocolate.Types.Analyzers.Helpers; | ||
using HotChocolate.Types.Analyzers.Models; | ||
|
||
namespace HotChocolate.Types.Analyzers.FileBuilders; | ||
|
||
public sealed class InterfaceTypeExtensionFileBuilder(StringBuilder sb, string ns) : IOutputTypeFileBuilder | ||
{ | ||
private readonly CodeWriter _writer = new(sb); | ||
|
||
public void WriteHeader() | ||
{ | ||
_writer.WriteFileHeader(); | ||
_writer.WriteIndentedLine("using Microsoft.Extensions.DependencyInjection;"); | ||
_writer.WriteLine(); | ||
} | ||
|
||
public void WriteBeginNamespace() | ||
{ | ||
_writer.WriteIndentedLine("namespace {0}", ns); | ||
_writer.WriteIndentedLine("{"); | ||
_writer.IncreaseIndent(); | ||
} | ||
|
||
public void WriteEndNamespace() | ||
{ | ||
_writer.DecreaseIndent(); | ||
_writer.WriteIndentedLine("}"); | ||
_writer.WriteLine(); | ||
} | ||
|
||
public string WriteBeginClass(string typeName) | ||
{ | ||
_writer.WriteIndentedLine("public static partial class {0}", typeName); | ||
_writer.WriteIndentedLine("{"); | ||
_writer.IncreaseIndent(); | ||
return typeName; | ||
} | ||
|
||
public void WriteEndClass() | ||
{ | ||
_writer.DecreaseIndent(); | ||
_writer.WriteIndentedLine("}"); | ||
} | ||
|
||
public void WriteInitializeMethod(IOutputTypeInfo typeInfo) | ||
{ | ||
_writer.WriteIndentedLine( | ||
"internal static void Initialize(global::HotChocolate.Types.IInterfaceTypeDescriptor<{0}> descriptor)", | ||
typeInfo.RuntimeType.ToFullyQualified()); | ||
_writer.WriteIndentedLine("{"); | ||
|
||
using (_writer.IncreaseIndent()) | ||
{ | ||
if (typeInfo.Resolvers.Length > 0) | ||
{ | ||
_writer.WriteIndentedLine("const global::{0} bindingFlags =", WellKnownTypes.BindingFlags); | ||
using (_writer.IncreaseIndent()) | ||
{ | ||
_writer.WriteIndentedLine("global::{0}.Public", WellKnownTypes.BindingFlags); | ||
using (_writer.IncreaseIndent()) | ||
{ | ||
_writer.WriteIndentedLine("| global::{0}.NonPublic", WellKnownTypes.BindingFlags); | ||
_writer.WriteIndentedLine("| global::{0}.Static;", WellKnownTypes.BindingFlags); | ||
} | ||
} | ||
|
||
_writer.WriteLine(); | ||
|
||
_writer.WriteIndentedLine( | ||
"var thisType = typeof({0});", | ||
typeInfo.Type.ToFullyQualified()); | ||
_writer.WriteIndentedLine( | ||
"var bindingResolver = descriptor.Extend().Context.ParameterBindingResolver;"); | ||
_writer.WriteIndentedLine( | ||
"global::{0}Resolvers.InitializeBindings(bindingResolver);", | ||
typeInfo.Type.ToDisplayString()); | ||
} | ||
|
||
if (typeInfo.Resolvers.Length > 0) | ||
{ | ||
foreach (var resolver in typeInfo.Resolvers) | ||
{ | ||
_writer.WriteLine(); | ||
_writer.WriteIndentedLine("descriptor"); | ||
|
||
using (_writer.IncreaseIndent()) | ||
{ | ||
_writer.WriteIndentedLine( | ||
".Field(thisType.GetMember(\"{0}\", bindingFlags)[0])", | ||
resolver.Member.Name); | ||
|
||
_writer.WriteIndentedLine(".ExtendWith(c =>"); | ||
_writer.WriteIndentedLine("{"); | ||
using (_writer.IncreaseIndent()) | ||
{ | ||
_writer.WriteIndentedLine("c.Definition.SetSourceGeneratorFlags();"); | ||
_writer.WriteIndentedLine( | ||
"c.Definition.Resolvers = {0}Resolvers.{1}_{2}();", | ||
typeInfo.Type.ToFullyQualified(), | ||
typeInfo.Type.Name, | ||
resolver.Member.Name); | ||
|
||
if (resolver.ResultKind is not ResolverResultKind.Pure | ||
&& !resolver.Member.HasPostProcessorAttribute() | ||
&& resolver.Member.IsListType(out var elementType)) | ||
{ | ||
_writer.WriteIndentedLine( | ||
"c.Definition.ResultPostProcessor = global::{0}<{1}>.Default;", | ||
WellKnownTypes.ListPostProcessor, | ||
elementType); | ||
} | ||
} | ||
_writer.WriteIndentedLine("});"); | ||
} | ||
} | ||
} | ||
|
||
_writer.WriteLine(); | ||
_writer.WriteIndentedLine("Configure(descriptor);"); | ||
} | ||
|
||
_writer.WriteIndentedLine("}"); | ||
} | ||
|
||
public void WriteConfigureMethod(IOutputTypeInfo typeInfo) | ||
{ | ||
_writer.WriteIndentedLine( | ||
"static partial void Configure(global::HotChocolate.Types.IInterfaceTypeDescriptor<{0}> descriptor);", | ||
typeInfo.RuntimeType.ToFullyQualified()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.