< Summary

Information
Class: NGql.Core.Extensions.TypeExtensions
Assembly: NGql.Core
File(s): /home/runner/work/NGql/NGql/src/Core/Extensions/TypeExtensions.cs
Line coverage
100%
Covered lines: 45
Uncovered lines: 0
Coverable lines: 45
Total lines: 142
Line coverage: 100%
Branch coverage
100%
Covered branches: 52
Total branches: 52
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ShouldConvertToObjectType(...)100%1010100%
HasArrayOrNullableMarker(...)100%88100%
.cctor()100%11100%
IsPrimitiveTypeName(...)100%11100%
GetCoreTypeName(...)100%44100%
TryHandleStandaloneMarker(...)100%88100%
ResolveDecoratedType(...)100%1010100%
GetBaseTypeName(...)100%11100%
IsPureTypeMarker(...)100%44100%
IsArrayType(...)100%44100%
IsNullableType(...)100%44100%

File(s)

/home/runner/work/NGql/NGql/src/Core/Extensions/TypeExtensions.cs

#LineLine coverage
 1using NGql.Core.Abstractions;
 2
 3namespace NGql.Core.Extensions;
 4
 5/// <summary>
 6/// Extensions for working with field types.
 7/// </summary>
 8internal static class TypeExtensions
 9{
 10    /// <summary>
 11    /// Determines if a field type should be converted to an object type when nested fields are added.
 12    /// </summary>
 13    /// <param name="fieldDefinition">The field definition to check.</param>
 14    /// <returns>True if the type should be converted to an object, false if it should preserve its type.</returns>
 15    public static bool ShouldConvertToObjectType(this FieldDefinition fieldDefinition)
 16    {
 1998617        ArgumentNullException.ThrowIfNull(fieldDefinition);
 18
 1998619        var type = fieldDefinition._type;
 20
 21        // Already object — no conversion needed.
 3328522        if (type == Constants.ObjectFieldType) return false;
 23
 24        // Default String type or unset — convert.
 1319125        if (type == Constants.DefaultFieldType || string.IsNullOrWhiteSpace(type)) return true;
 26
 27        // Array marker stays as-is.
 20728        if (type == Constants.ArrayTypeMarker) return false;
 29
 30        // Array / nullable / type-decorated names retain their type.
 21031        if (HasArrayOrNullableMarker(fieldDefinition, type)) return false;
 32
 33        // Primitive scalars promote to object when subfields are added.
 10834        return IsPrimitiveTypeName(type);
 35    }
 36
 37    private static bool HasArrayOrNullableMarker(FieldDefinition fieldDefinition, string type)
 15938        => fieldDefinition.IsArray
 15939        || fieldDefinition.IsNullable
 15940        || type.Contains('[')
 15941        || type.Contains(']')
 15942        || type.EndsWith('?');
 43
 344    private static readonly HashSet<string> PrimitiveTypeNames = new(StringComparer.OrdinalIgnoreCase)
 345    {
 346        "int", "integer", "string", "boolean", "bool", "float", "double", "decimal",
 347    };
 48
 10849    private static bool IsPrimitiveTypeName(string type) => PrimitiveTypeNames.Contains(type);
 50
 51    /// <summary>
 52    /// Gets the core type name.
 53    /// </summary>
 54    /// <param name="fieldDefinition">The field definition.</param>
 55    /// <param name="stripArrayMarker">Whether to strip the array marker.</param>
 56    /// <param name="stripNullableMarker">Whether to strip the nullable marker.</param>
 57    /// <returns>The processed type name according to the parameters.</returns>
 58    public static string GetCoreTypeName(this FieldDefinition fieldDefinition, bool stripArrayMarker = false, bool strip
 59    {
 9960        ArgumentNullException.ThrowIfNull(fieldDefinition);
 61
 9962        if (string.IsNullOrEmpty(fieldDefinition.Type)) return Constants.DefaultFieldType;
 63
 9364        var type = fieldDefinition.Type;
 9365        if (TryHandleStandaloneMarker(type, stripArrayMarker, stripNullableMarker, out var standalone))
 3666            return standalone;
 67
 5768        return ResolveDecoratedType(type, stripArrayMarker, stripNullableMarker);
 69    }
 70
 71    private static bool TryHandleStandaloneMarker(string type, bool stripArrayMarker, bool stripNullableMarker, out stri
 72    {
 9373        if (type == Constants.ArrayTypeMarker)
 74        {
 1875            result = stripArrayMarker ? Constants.DefaultFieldType : Constants.DefaultFieldType + Constants.ArrayTypeMar
 1876            return true;
 77        }
 7578        if (type == Constants.NullableTypeMarker)
 79        {
 1880            result = stripNullableMarker ? Constants.DefaultFieldType : Constants.DefaultFieldType + Constants.NullableT
 1881            return true;
 82        }
 5783        result = string.Empty;
 5784        return false;
 85    }
 86
 87    private static string ResolveDecoratedType(string type, bool stripArrayMarker, bool stripNullableMarker)
 88    {
 5789        var typeSpan = type.AsSpan();
 90
 5791        var arrayStart = typeSpan.IndexOf('[');
 5792        if (arrayStart > 0)
 93        {
 2194            return stripArrayMarker ? typeSpan[..arrayStart].ToString() : type;
 95        }
 96
 3697        if (typeSpan.EndsWith(Constants.NullableTypeMarkerSpan) && typeSpan.Length > 1)
 98        {
 1899            return stripNullableMarker ? typeSpan[..^1].ToString() : type;
 100        }
 101
 18102        return type;
 103    }
 104
 105    /// <summary>
 106    /// Gets just the base type name without any markers.
 107    /// </summary>
 108    /// <param name="fieldDefinition">The field definition.</param>
 109    /// <returns>The base type name without an array or nullable markers.</returns>
 110    public static string GetBaseTypeName(this FieldDefinition fieldDefinition)
 24111        => GetCoreTypeName(fieldDefinition, true, true);
 112
 113    /// <summary>
 114    /// Determines if the type is a pure type marker ([] or ?)
 115    /// </summary>
 116    /// <param name="fieldDefinition">The field definition to check.</param>
 117    /// <returns>True if the type is a pure marker, false otherwise.</returns>
 118    public static bool IsPureTypeMarker(this FieldDefinition fieldDefinition)
 119    {
 18120        ArgumentNullException.ThrowIfNull(fieldDefinition);
 121
 18122        return fieldDefinition.Type
 18123            is Constants.ArrayTypeMarker
 18124            or Constants.NullableTypeMarker;
 125    }
 126
 127    /// <summary>
 128    /// Determines if a type string represents an array type.
 129    /// </summary>
 130    /// <param name="type">The type string to check.</param>
 131    /// <returns>True if the type is an array type, false otherwise.</returns>
 132    public static bool IsArrayType(this string? type)
 120133        => type != null && (type == Constants.ArrayTypeMarker || type.Contains('['));
 134
 135    /// <summary>
 136    /// Determines if a type string represents a nullable type.
 137    /// </summary>
 138    /// <param name="type">The type string to check.</param>
 139    /// <returns>True if the type is nullable, false otherwise.</returns>
 140    public static bool IsNullableType(this string? type)
 96141        => type != null && (type == Constants.NullableTypeMarker || type.EndsWith('?'));
 142}