| | | 1 | | using NGql.Core.Abstractions; |
| | | 2 | | |
| | | 3 | | namespace NGql.Core.Extensions; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Extensions for working with field types. |
| | | 7 | | /// </summary> |
| | | 8 | | internal 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 | | { |
| | 19986 | 17 | | ArgumentNullException.ThrowIfNull(fieldDefinition); |
| | | 18 | | |
| | 19986 | 19 | | var type = fieldDefinition._type; |
| | | 20 | | |
| | | 21 | | // Already object — no conversion needed. |
| | 33285 | 22 | | if (type == Constants.ObjectFieldType) return false; |
| | | 23 | | |
| | | 24 | | // Default String type or unset — convert. |
| | 13191 | 25 | | if (type == Constants.DefaultFieldType || string.IsNullOrWhiteSpace(type)) return true; |
| | | 26 | | |
| | | 27 | | // Array marker stays as-is. |
| | 207 | 28 | | if (type == Constants.ArrayTypeMarker) return false; |
| | | 29 | | |
| | | 30 | | // Array / nullable / type-decorated names retain their type. |
| | 210 | 31 | | if (HasArrayOrNullableMarker(fieldDefinition, type)) return false; |
| | | 32 | | |
| | | 33 | | // Primitive scalars promote to object when subfields are added. |
| | 108 | 34 | | return IsPrimitiveTypeName(type); |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | private static bool HasArrayOrNullableMarker(FieldDefinition fieldDefinition, string type) |
| | 159 | 38 | | => fieldDefinition.IsArray |
| | 159 | 39 | | || fieldDefinition.IsNullable |
| | 159 | 40 | | || type.Contains('[') |
| | 159 | 41 | | || type.Contains(']') |
| | 159 | 42 | | || type.EndsWith('?'); |
| | | 43 | | |
| | 3 | 44 | | private static readonly HashSet<string> PrimitiveTypeNames = new(StringComparer.OrdinalIgnoreCase) |
| | 3 | 45 | | { |
| | 3 | 46 | | "int", "integer", "string", "boolean", "bool", "float", "double", "decimal", |
| | 3 | 47 | | }; |
| | | 48 | | |
| | 108 | 49 | | 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 | | { |
| | 99 | 60 | | ArgumentNullException.ThrowIfNull(fieldDefinition); |
| | | 61 | | |
| | 99 | 62 | | if (string.IsNullOrEmpty(fieldDefinition.Type)) return Constants.DefaultFieldType; |
| | | 63 | | |
| | 93 | 64 | | var type = fieldDefinition.Type; |
| | 93 | 65 | | if (TryHandleStandaloneMarker(type, stripArrayMarker, stripNullableMarker, out var standalone)) |
| | 36 | 66 | | return standalone; |
| | | 67 | | |
| | 57 | 68 | | return ResolveDecoratedType(type, stripArrayMarker, stripNullableMarker); |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | private static bool TryHandleStandaloneMarker(string type, bool stripArrayMarker, bool stripNullableMarker, out stri |
| | | 72 | | { |
| | 93 | 73 | | if (type == Constants.ArrayTypeMarker) |
| | | 74 | | { |
| | 18 | 75 | | result = stripArrayMarker ? Constants.DefaultFieldType : Constants.DefaultFieldType + Constants.ArrayTypeMar |
| | 18 | 76 | | return true; |
| | | 77 | | } |
| | 75 | 78 | | if (type == Constants.NullableTypeMarker) |
| | | 79 | | { |
| | 18 | 80 | | result = stripNullableMarker ? Constants.DefaultFieldType : Constants.DefaultFieldType + Constants.NullableT |
| | 18 | 81 | | return true; |
| | | 82 | | } |
| | 57 | 83 | | result = string.Empty; |
| | 57 | 84 | | return false; |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | private static string ResolveDecoratedType(string type, bool stripArrayMarker, bool stripNullableMarker) |
| | | 88 | | { |
| | 57 | 89 | | var typeSpan = type.AsSpan(); |
| | | 90 | | |
| | 57 | 91 | | var arrayStart = typeSpan.IndexOf('['); |
| | 57 | 92 | | if (arrayStart > 0) |
| | | 93 | | { |
| | 21 | 94 | | return stripArrayMarker ? typeSpan[..arrayStart].ToString() : type; |
| | | 95 | | } |
| | | 96 | | |
| | 36 | 97 | | if (typeSpan.EndsWith(Constants.NullableTypeMarkerSpan) && typeSpan.Length > 1) |
| | | 98 | | { |
| | 18 | 99 | | return stripNullableMarker ? typeSpan[..^1].ToString() : type; |
| | | 100 | | } |
| | | 101 | | |
| | 18 | 102 | | 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) |
| | 24 | 111 | | => 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 | | { |
| | 18 | 120 | | ArgumentNullException.ThrowIfNull(fieldDefinition); |
| | | 121 | | |
| | 18 | 122 | | return fieldDefinition.Type |
| | 18 | 123 | | is Constants.ArrayTypeMarker |
| | 18 | 124 | | 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) |
| | 120 | 133 | | => 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) |
| | 96 | 141 | | => type != null && (type == Constants.NullableTypeMarker || type.EndsWith('?')); |
| | | 142 | | } |