Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Models\MethodModel.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Models\ParameterModel.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Models\TypeConstraint.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Models\WellKnownTYpes.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Parser.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UniqueNameBuilder.cs" />
</ItemGroup>
Expand Down
30 changes: 30 additions & 0 deletions InterfaceStubGenerator.Shared/Models/WellKnownTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Microsoft.CodeAnalysis;

namespace Refit.Generator;

public class WellKnownTypes(Compilation compilation)
{
readonly Dictionary<string, INamedTypeSymbol?> cachedTypes = new();

public INamedTypeSymbol Get<T>() => Get(typeof(T));
public INamedTypeSymbol Get(Type type)
{
return Get(type.FullName ?? throw new InvalidOperationException("Could not get name of type " + type));
}

public INamedTypeSymbol? TryGet(string typeFullName)
{
if (cachedTypes.TryGetValue(typeFullName, out var typeSymbol))
{
return typeSymbol;
}

typeSymbol = compilation.GetTypeByMetadataName(typeFullName);
cachedTypes.Add(typeFullName, typeSymbol);

return typeSymbol;
}

INamedTypeSymbol Get(string typeFullName) =>
TryGet(typeFullName) ?? throw new InvalidOperationException("Could not get type " + typeFullName);
}
6 changes: 4 additions & 2 deletions InterfaceStubGenerator.Shared/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ CancellationToken cancellationToken
if (compilation == null)
throw new ArgumentNullException(nameof(compilation));

var wellKnownTypes = new WellKnownTypes(compilation);

refitInternalNamespace = $"{refitInternalNamespace ?? string.Empty}RefitInternalGenerated";

// Remove - as they are valid in csproj, but invalid in a namespace
Expand All @@ -41,8 +43,8 @@ CancellationToken cancellationToken
// TODO: we should allow source generators to provide source during initialize, so that this step isn't required.
var options = (CSharpParseOptions)compilation.SyntaxTrees[0].Options;

var disposableInterfaceSymbol = compilation.GetTypeByMetadataName("System.IDisposable")!;
var httpMethodBaseAttributeSymbol = compilation.GetTypeByMetadataName(
var disposableInterfaceSymbol = wellKnownTypes.Get(typeof(IDisposable));
var httpMethodBaseAttributeSymbol = wellKnownTypes.TryGet(
"Refit.HttpMethodAttribute"
);

Expand Down
Loading