-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
08ef0e8
commit 59edee0
Showing
8 changed files
with
59 additions
and
115 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using Microsoft.CodeAnalysis; | ||
|
||
using System.Diagnostics; | ||
|
||
namespace MapperGenerator; | ||
|
||
public static partial class CodeAnalysisExtensions | ||
{ | ||
public static bool IsNullable(this ITypeSymbol typeSymbol) => | ||
typeSymbol.NullableAnnotation == NullableAnnotation.Annotated; | ||
|
||
public static bool IsNullableValueType(this ITypeSymbol typeSymbol) => | ||
typeSymbol.IsValueType && typeSymbol.IsNullable(); | ||
|
||
public static bool TryGetNullableValueUnderlyingType(this ITypeSymbol typeSymbol, out ITypeSymbol? underlyingType) | ||
{ | ||
if (typeSymbol is INamedTypeSymbol namedType && typeSymbol.IsNullableValueType() && namedType.IsGenericType) | ||
{ | ||
var typeParameters = namedType.TypeArguments; | ||
// Assert the generic is named System.Nullable<T> as expected. | ||
Debug.Assert(namedType.ConstructUnboundGenericType() is { } genericType && genericType.Name == "Nullable" && genericType.ContainingNamespace.Name == "System" && genericType.TypeArguments.Length == 1); | ||
Debug.Assert(typeParameters.Length == 1); | ||
underlyingType = typeParameters[0]; | ||
// TODO: decide what to return when the underlying type is not declared due to some compilation error. | ||
// TypeKind.Error indicats a compilation error, specifically a nullable type where the underlying type was not found. | ||
// I have observed that IsValueType will be true in such cases even though it is actually unknown whether the missing type is a value type | ||
// I chose to return false but you may prefer something else. | ||
return underlyingType.TypeKind == TypeKind.Error ? false : true; | ||
} | ||
underlyingType = null; | ||
return false; | ||
} | ||
|
||
public static bool IsEnum(this ITypeSymbol typeSymbol) => | ||
typeSymbol is INamedTypeSymbol namedType && namedType.EnumUnderlyingType != null; | ||
|
||
public static bool IsNullableEnumType(this ITypeSymbol typeSymbol) => | ||
typeSymbol.TryGetNullableValueUnderlyingType(out var underlyingType) == true && underlyingType.IsEnum(); | ||
} |
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