From 13d3f7d9d94ab25dd0d513603cb9d3c4b240c735 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Sun, 2 Jan 2022 14:31:42 +0100 Subject: [PATCH] feat: add extensions for checking type nullability --- .../Extensions/TypeSymbolExtensions.cs | 36 +++++++++++++++++++ .../Extensions/TypeSyntaxExtensions.cs | 25 +++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 Core/NosSmooth.PacketSerializersGenerator/Extensions/TypeSymbolExtensions.cs create mode 100644 Core/NosSmooth.PacketSerializersGenerator/Extensions/TypeSyntaxExtensions.cs diff --git a/Core/NosSmooth.PacketSerializersGenerator/Extensions/TypeSymbolExtensions.cs b/Core/NosSmooth.PacketSerializersGenerator/Extensions/TypeSymbolExtensions.cs new file mode 100644 index 0000000..f05c7a1 --- /dev/null +++ b/Core/NosSmooth.PacketSerializersGenerator/Extensions/TypeSymbolExtensions.cs @@ -0,0 +1,36 @@ +// +// TypeSymbolExtensions.cs +// +// Copyright (c) František Boháček. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using Microsoft.CodeAnalysis; + +namespace NosSmooth.PacketSerializersGenerator.Extensions; + +/// +/// Extension methods for . +/// +public static class TypeSymbolExtensions +{ + /// + /// Gets whether the type symbol is nullable. + /// + /// The type symbol. + /// Whether the type symbol is nullable. Returns null if not possible to determine. + public static bool? IsNullable(this ITypeSymbol typeSymbol) + { + if (typeSymbol.NullableAnnotation == NullableAnnotation.Annotated || typeSymbol.Name == "Nullable") + { + return true; + } + + if (typeSymbol.IsValueType) + { + return false; + } + + // cannot determine if not nullable from reference type. + return null; + } +} \ No newline at end of file diff --git a/Core/NosSmooth.PacketSerializersGenerator/Extensions/TypeSyntaxExtensions.cs b/Core/NosSmooth.PacketSerializersGenerator/Extensions/TypeSyntaxExtensions.cs new file mode 100644 index 0000000..d95bbbc --- /dev/null +++ b/Core/NosSmooth.PacketSerializersGenerator/Extensions/TypeSyntaxExtensions.cs @@ -0,0 +1,25 @@ +// +// TypeSyntaxExtensions.cs +// +// Copyright (c) František Boháček. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using Microsoft.CodeAnalysis.CSharp.Syntax; + +namespace NosSmooth.PacketSerializersGenerator.Extensions; + +/// +/// Extension methods for . +/// +public static class TypeSyntaxExtensions +{ + /// + /// Gets whether the type syntax is nullable. + /// + /// The type syntax. + /// Whether the type syntax is nullable. + public static bool IsNullable(this TypeSyntax typeSyntax) + { + return typeSyntax is NullableTypeSyntax; + } +} \ No newline at end of file -- 2.49.0