~ruther/NosSmooth

13d3f7d9d94ab25dd0d513603cb9d3c4b240c735 — František Boháček 3 years ago ed1280e
feat: add extensions for checking type nullability
A Core/NosSmooth.PacketSerializersGenerator/Extensions/TypeSymbolExtensions.cs => Core/NosSmooth.PacketSerializersGenerator/Extensions/TypeSymbolExtensions.cs +36 -0
@@ 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;

/// <summary>
/// Extension methods for <see cref="ITypeSymbol"/>.
/// </summary>
public static class TypeSymbolExtensions
{
    /// <summary>
    /// Gets whether the type symbol is nullable.
    /// </summary>
    /// <param name="typeSymbol">The type symbol.</param>
    /// <returns>Whether the type symbol is nullable. Returns null if not possible to determine.</returns>
    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

A Core/NosSmooth.PacketSerializersGenerator/Extensions/TypeSyntaxExtensions.cs => Core/NosSmooth.PacketSerializersGenerator/Extensions/TypeSyntaxExtensions.cs +25 -0
@@ 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;

/// <summary>
/// Extension methods for <see cref="TypeSyntax"/>.
/// </summary>
public static class TypeSyntaxExtensions
{
    /// <summary>
    /// Gets whether the type syntax is nullable.
    /// </summary>
    /// <param name="typeSyntax">The type syntax.</param>
    /// <returns>Whether the type syntax is nullable.</returns>
    public static bool IsNullable(this TypeSyntax typeSyntax)
    {
        return typeSyntax is NullableTypeSyntax;
    }
}
\ No newline at end of file

Do not follow this link