Skip to content

Commit d87c7af

Browse files
committed
Use try pattern for base type resolution
1 parent 5f63071 commit d87c7af

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

  • packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/ModelProvider.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,7 @@ protected override string BuildNamespace() => string.IsNullOrEmpty(_inputModel.N
254254
return currentBase;
255255
}
256256

257-
var resolvedPreviousBase = ResolveTypeInCurrentBuild(previousBase);
258-
if (resolvedPreviousBase is null)
257+
if (!TryResolveTypeInCurrentBuild(previousBase, out var resolvedPreviousBase))
259258
{
260259
CodeModelGenerator.Instance.Emitter.ReportDiagnostic(
261260
DiagnosticCodes.UnavailableBackcompatType,
@@ -331,13 +330,14 @@ private static bool IsInBaseTypeHierarchy(CSharpType? currentBase, CSharpType pr
331330
return false;
332331
}
333332

334-
private CSharpType? ResolveTypeInCurrentBuild(CSharpType type)
333+
private bool TryResolveTypeInCurrentBuild(CSharpType type, [NotNullWhen(true)] out CSharpType? resolvedType)
335334
{
336335
foreach (var provider in CodeModelGenerator.Instance.TypeFactory.CSharpTypeMap.Values)
337336
{
338337
if (provider is not null && provider.Type.AreNamesEqual(type))
339338
{
340-
return provider.Type;
339+
resolvedType = provider.Type;
340+
return true;
341341
}
342342
}
343343

@@ -352,7 +352,8 @@ private static bool IsInBaseTypeHierarchy(CSharpType? currentBase, CSharpType pr
352352
{
353353
if (provider is not null && provider.Type.AreNamesEqual(type))
354354
{
355-
return provider.Type;
355+
resolvedType = provider.Type;
356+
return true;
356357
}
357358
}
358359

@@ -363,11 +364,13 @@ private static bool IsInBaseTypeHierarchy(CSharpType? currentBase, CSharpType pr
363364
includeReferencedAssemblies: true);
364365
if (currentProvider is null)
365366
{
366-
return null;
367+
resolvedType = null;
368+
return false;
367369
}
368370

369371
CodeModelGenerator.Instance.TypeFactory.CSharpTypeMap[currentProvider.Type] = currentProvider;
370-
return currentProvider.Type;
372+
resolvedType = currentProvider.Type;
373+
return true;
371374
}
372375

373376
protected override TypeProvider[] BuildSerializationProviders()

0 commit comments

Comments
 (0)