Skip to content

Commit 9418be1

Browse files
feat: added WellKnownType (#1962)
1 parent 68f6f36 commit 9418be1

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

InterfaceStubGenerator.Shared/InterfaceStubGenerator.Shared.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<Compile Include="$(MSBuildThisFileDirectory)Models\MethodModel.cs" />
2222
<Compile Include="$(MSBuildThisFileDirectory)Models\ParameterModel.cs" />
2323
<Compile Include="$(MSBuildThisFileDirectory)Models\TypeConstraint.cs" />
24+
<Compile Include="$(MSBuildThisFileDirectory)Models\WellKnownTYpes.cs" />
2425
<Compile Include="$(MSBuildThisFileDirectory)Parser.cs" />
2526
<Compile Include="$(MSBuildThisFileDirectory)UniqueNameBuilder.cs" />
2627
</ItemGroup>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Microsoft.CodeAnalysis;
2+
3+
namespace Refit.Generator;
4+
5+
public class WellKnownTypes(Compilation compilation)
6+
{
7+
readonly Dictionary<string, INamedTypeSymbol?> cachedTypes = new();
8+
9+
public INamedTypeSymbol Get<T>() => Get(typeof(T));
10+
public INamedTypeSymbol Get(Type type)
11+
{
12+
return Get(type.FullName ?? throw new InvalidOperationException("Could not get name of type " + type));
13+
}
14+
15+
public INamedTypeSymbol? TryGet(string typeFullName)
16+
{
17+
if (cachedTypes.TryGetValue(typeFullName, out var typeSymbol))
18+
{
19+
return typeSymbol;
20+
}
21+
22+
typeSymbol = compilation.GetTypeByMetadataName(typeFullName);
23+
cachedTypes.Add(typeFullName, typeSymbol);
24+
25+
return typeSymbol;
26+
}
27+
28+
INamedTypeSymbol Get(string typeFullName) =>
29+
TryGet(typeFullName) ?? throw new InvalidOperationException("Could not get type " + typeFullName);
30+
}

InterfaceStubGenerator.Shared/Parser.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ CancellationToken cancellationToken
3232
if (compilation == null)
3333
throw new ArgumentNullException(nameof(compilation));
3434

35+
var wellKnownTypes = new WellKnownTypes(compilation);
36+
3537
refitInternalNamespace = $"{refitInternalNamespace ?? string.Empty}RefitInternalGenerated";
3638

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

44-
var disposableInterfaceSymbol = compilation.GetTypeByMetadataName("System.IDisposable")!;
45-
var httpMethodBaseAttributeSymbol = compilation.GetTypeByMetadataName(
46+
var disposableInterfaceSymbol = wellKnownTypes.Get(typeof(IDisposable));
47+
var httpMethodBaseAttributeSymbol = wellKnownTypes.TryGet(
4648
"Refit.HttpMethodAttribute"
4749
);
4850

0 commit comments

Comments
 (0)