| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | using NGql.Core.Abstractions; |
| | | 3 | | using NGql.Core.Extensions; |
| | | 4 | | using NGql.Core.Pooling; |
| | | 5 | | |
| | | 6 | | namespace NGql.Core.Builders; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Factory class for creating and processing FieldDefinition instances. |
| | | 10 | | /// Handles complex field creation logic, including dotted paths, type parsing, and field merging. |
| | | 11 | | /// </summary> |
| | | 12 | | internal static class FieldFactory |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Gets or adds a field to the collection, handling all field path complexities. |
| | | 16 | | /// This overload operates on the root-level <see cref="QueryDefinition.Fields"/> dictionary. |
| | | 17 | | /// </summary> |
| | | 18 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 19 | | internal static FieldDefinition GetOrAddField(Dictionary<string, FieldDefinition> fieldDefinitions, ReadOnlySpan<cha |
| | | 20 | | { |
| | 84351 | 21 | | var fieldType = type.IsEmpty ? Constants.DefaultFieldTypeSpan : type; |
| | | 22 | | |
| | | 23 | | // FAST PATH: Simple field name |
| | 84351 | 24 | | if (fieldPath.IsSimpleField()) |
| | | 25 | | { |
| | 8484 | 26 | | return fieldDefinitions.GetOrAddSimpleField(fieldPath, fieldType, arguments, parentPath, metadata); |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | // MEDIUM PATH: Dotted field |
| | 75867 | 30 | | if (fieldPath.IsDottedField()) |
| | | 31 | | { |
| | 74139 | 32 | | return GetOrAddDottedField(fieldDefinitions, fieldPath, fieldType, arguments, parentPath, metadata); |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | // SLOW PATH: Complex field processing |
| | 1728 | 36 | | return GetOrAddComplexField(fieldDefinitions, fieldPath, fieldType, arguments, parentPath, metadata); |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Gets or adds a field as a child of the given parent node, handling all field path complexities. |
| | | 41 | | /// This overload is for per-node child access (not root-level dictionary access). |
| | | 42 | | /// </summary> |
| | | 43 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 44 | | internal static FieldDefinition GetOrAddField(FieldDefinition parent, ReadOnlySpan<char> fieldPath, ReadOnlySpan<cha |
| | | 45 | | { |
| | 4188 | 46 | | var fieldType = type.IsEmpty ? Constants.DefaultFieldTypeSpan : type; |
| | 4188 | 47 | | var children = parent._children ??= new FieldChildren(); |
| | | 48 | | |
| | | 49 | | // parent's memoized deep fingerprint (if any) summarizes its entire subtree, including |
| | | 50 | | // whatever descendant fieldPath resolves to. Only an argument-bearing call can change the |
| | | 51 | | // merge-relevant subtree, so only those need to invalidate — but they must invalidate here, |
| | | 52 | | // at the single choke point every simple/dotted/complex descendant mutation funnels through, |
| | | 53 | | // since parent itself is never revisited by the per-segment walks below. |
| | 4188 | 54 | | if (arguments is { Count: > 0 }) |
| | | 55 | | { |
| | 267 | 56 | | parent.ClearMergeMemo(); |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | // FAST PATH: Simple field name |
| | 4188 | 60 | | if (fieldPath.IsSimpleField()) |
| | | 61 | | { |
| | 3378 | 62 | | return children.GetOrAddSimpleField(fieldPath, fieldType, arguments, parentPath, metadata); |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | // MEDIUM PATH: Dotted field |
| | 810 | 66 | | if (fieldPath.IsDottedField()) |
| | | 67 | | { |
| | 435 | 68 | | return GetOrAddDottedField(parent, fieldPath, fieldType, arguments, parentPath, metadata); |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | // SLOW PATH: Complex field processing |
| | 375 | 72 | | return GetOrAddComplexField(parent, fieldPath, fieldType, arguments, parentPath, metadata); |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Gets or adds a dotted field (contains dots for nested access) — root-level variant. |
| | | 77 | | /// </summary> |
| | | 78 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 79 | | private static FieldDefinition GetOrAddDottedField(Dictionary<string, FieldDefinition> fieldDefinitions, ReadOnlySpa |
| | | 80 | | { |
| | 74139 | 81 | | var hasNoArguments = arguments == null; |
| | 74139 | 82 | | var hasNoMetadata = metadata == null; |
| | | 83 | | |
| | | 84 | | // FAST PATH: No arguments/metadata - use optimized processing |
| | 74139 | 85 | | if (hasNoArguments && hasNoMetadata) |
| | | 86 | | { |
| | 72090 | 87 | | return ProcessDottedFieldFastPath(fieldDefinitions, fieldPath, fieldType); |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | // SLOW PATH: With arguments/metadata |
| | 2049 | 91 | | return ProcessDottedFieldWithMetadata(fieldDefinitions, fieldPath, fieldType, arguments, parentPath, metadata); |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Gets or adds a dotted field (contains dots for nested access) — per-node variant. |
| | | 96 | | /// </summary> |
| | | 97 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 98 | | private static FieldDefinition GetOrAddDottedField(FieldDefinition rootParent, ReadOnlySpan<char> fieldPath, ReadOnl |
| | | 99 | | { |
| | 435 | 100 | | var hasNoArguments = arguments == null; |
| | 435 | 101 | | var hasNoMetadata = metadata == null; |
| | | 102 | | |
| | 435 | 103 | | if (hasNoArguments && hasNoMetadata) |
| | | 104 | | { |
| | 312 | 105 | | return ProcessDottedFieldFastPath(rootParent, fieldPath, fieldType); |
| | | 106 | | } |
| | | 107 | | |
| | 123 | 108 | | return ProcessDottedFieldWithMetadata(rootParent, fieldPath, fieldType, arguments, parentPath, metadata); |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | /// <summary> |
| | | 112 | | /// Processes dotted fields without arguments or metadata for optimal performance — root-level variant. |
| | | 113 | | /// The first segment uses the root dictionary; subsequent segments use <see cref="FieldDefinition._children"/>. |
| | | 114 | | /// </summary> |
| | | 115 | | // Callers reach here only via IsDottedField() which guarantees fieldPath contains '.', |
| | | 116 | | // so the loop runs at least twice and parentField is non-null on exit. |
| | | 117 | | private static FieldDefinition ProcessDottedFieldFastPath(Dictionary<string, FieldDefinition> rootFields, ReadOnlySp |
| | | 118 | | { |
| | 72090 | 119 | | FieldDefinition? parentField = null; |
| | 72090 | 120 | | var pathStart = 0; |
| | | 121 | | |
| | 287775 | 122 | | while (pathStart < fieldPath.Length) |
| | | 123 | | { |
| | 215685 | 124 | | ExtractDottedSegment(fieldPath, pathStart, out var spanSegment, out var nextStart); |
| | 215685 | 125 | | if (spanSegment.Name.IsWhiteSpace()) |
| | | 126 | | { |
| | 48 | 127 | | pathStart = nextStart; |
| | 48 | 128 | | continue; |
| | | 129 | | } |
| | 215637 | 130 | | parentField = parentField is null |
| | 215637 | 131 | | ? GetOrCreateRootSegment(rootFields, spanSegment, fieldPath, pathStart, fieldType) |
| | 215637 | 132 | | : GetOrCreateChildSegment(parentField, spanSegment, fieldPath, pathStart, fieldType); |
| | 215637 | 133 | | pathStart = nextStart; |
| | | 134 | | } |
| | | 135 | | |
| | | 136 | | // A dotted path made up entirely of empty/whitespace segments (e.g. "." or "..") yields |
| | | 137 | | // no field. Reject it rather than dereferencing a null result. |
| | 72090 | 138 | | return parentField ?? throw EmptyDottedPath(); |
| | | 139 | | } |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// Processes dotted fields without arguments or metadata — per-node variant. |
| | | 143 | | /// All segments use <see cref="FieldDefinition._children"/>. |
| | | 144 | | /// </summary> |
| | | 145 | | private static FieldDefinition ProcessDottedFieldFastPath(FieldDefinition rootParent, ReadOnlySpan<char> fieldPath, |
| | | 146 | | { |
| | 312 | 147 | | var currentParent = rootParent; |
| | 312 | 148 | | var pathStart = 0; |
| | | 149 | | |
| | 1506 | 150 | | while (pathStart < fieldPath.Length) |
| | | 151 | | { |
| | 1194 | 152 | | ExtractDottedSegment(fieldPath, pathStart, out var spanSegment, out var nextStart); |
| | 1194 | 153 | | if (spanSegment.Name.IsWhiteSpace()) |
| | | 154 | | { |
| | 9 | 155 | | pathStart = nextStart; |
| | 9 | 156 | | continue; |
| | | 157 | | } |
| | 1185 | 158 | | currentParent = GetOrCreateChildSegment(currentParent, spanSegment, fieldPath, pathStart, fieldType); |
| | 1185 | 159 | | pathStart = nextStart; |
| | | 160 | | } |
| | | 161 | | |
| | 312 | 162 | | return currentParent; |
| | | 163 | | } |
| | | 164 | | |
| | | 165 | | private static FieldDefinition GetOrCreateRootSegment(Dictionary<string, FieldDefinition> rootFields, SpanSegment sp |
| | | 166 | | { |
| | 72084 | 167 | | if (!TryGetRootFieldBySpan(rootFields, spanSegment.Name, out var field, out var segmentName)) |
| | | 168 | | { |
| | 58299 | 169 | | field = CreateDottedFieldSegment(spanSegment.Name, fieldPath, pathStart + spanSegment.Name.Length, spanSegme |
| | 58299 | 170 | | rootFields[segmentName] = field; |
| | 58299 | 171 | | return field; |
| | | 172 | | } |
| | 13785 | 173 | | PromoteToObjectIfNeeded(field, spanSegment.IsLastFragment); |
| | 13785 | 174 | | return field; |
| | | 175 | | } |
| | | 176 | | |
| | | 177 | | /// <summary> |
| | | 178 | | /// Probes <paramref name="rootFields"/> (keyed <see cref="StringComparer.OrdinalIgnoreCase"/>) by |
| | | 179 | | /// span without allocating on .NET 9+, via |
| | | 180 | | /// <c>Dictionary<TKey,TValue>.GetAlternateLookup<ReadOnlySpan<char>>()</c>. On a |
| | | 181 | | /// miss (or on net8.0, where the alternate-lookup API does not exist), materializes |
| | | 182 | | /// <paramref name="name"/> exactly once and hands it back via <paramref name="key"/> so the |
| | | 183 | | /// caller's subsequent write on the not-found path reuses it instead of calling |
| | | 184 | | /// <see cref="ReadOnlySpan{T}.ToString"/> again. |
| | | 185 | | /// </summary> |
| | | 186 | | private static bool TryGetRootFieldBySpan(Dictionary<string, FieldDefinition> rootFields, ReadOnlySpan<char> name, [ |
| | | 187 | | { |
| | | 188 | | #if NET9_0_OR_GREATER |
| | 49202 | 189 | | if (rootFields.GetAlternateLookup<ReadOnlySpan<char>>().TryGetValue(name, out field)) |
| | | 190 | | { |
| | 9804 | 191 | | key = null!; |
| | 9804 | 192 | | return true; |
| | | 193 | | } |
| | 39398 | 194 | | key = name.ToString(); |
| | 39398 | 195 | | return false; |
| | | 196 | | #else |
| | 24601 | 197 | | key = name.ToString(); |
| | 24601 | 198 | | return rootFields.TryGetValue(key, out field); |
| | | 199 | | #endif |
| | | 200 | | } |
| | | 201 | | |
| | | 202 | | private static FieldDefinition GetOrCreateChildSegment(FieldDefinition parentField, SpanSegment spanSegment, ReadOnl |
| | | 203 | | { |
| | 144738 | 204 | | var children = parentField._children ??= new FieldChildren(); |
| | 144738 | 205 | | if (!children.TryGetValue(spanSegment.Name, out var field) || field is null) |
| | | 206 | | { |
| | 139566 | 207 | | field = CreateDottedFieldSegment(spanSegment.Name, fieldPath, pathStart + spanSegment.Name.Length, spanSegme |
| | 139566 | 208 | | children.Append(field); |
| | 139566 | 209 | | return field; |
| | | 210 | | } |
| | 5172 | 211 | | PromoteToObjectIfNeeded(field, spanSegment.IsLastFragment); |
| | 5172 | 212 | | return field; |
| | | 213 | | } |
| | | 214 | | |
| | | 215 | | private static void PromoteToObjectIfNeeded(FieldDefinition field, bool isLastFragment) |
| | | 216 | | { |
| | 18957 | 217 | | if (!isLastFragment && field.ShouldConvertToObjectType()) |
| | | 218 | | { |
| | 6126 | 219 | | field._type = Constants.ObjectFieldType; |
| | | 220 | | } |
| | 18957 | 221 | | } |
| | | 222 | | |
| | | 223 | | /// <summary> |
| | | 224 | | /// Processes dotted fields with arguments and metadata — root-level variant. |
| | | 225 | | /// </summary> |
| | | 226 | | private static FieldDefinition ProcessDottedFieldWithMetadata(Dictionary<string, FieldDefinition> fieldDefinitions, |
| | | 227 | | { |
| | 2049 | 228 | | var parentPathSpan = parentPath.AsSpan(); |
| | | 229 | | |
| | | 230 | | // Use stack allocation for small paths, pooled resources for larger ones |
| | 2049 | 231 | | var estimatedPathLength = parentPathSpan.Length + fieldPath.Length + 10; // Extra space for dots |
| | | 232 | | |
| | 2049 | 233 | | if (estimatedPathLength <= 512) |
| | | 234 | | { |
| | 2043 | 235 | | Span<char> pathBuffer = stackalloc char[512]; |
| | 2043 | 236 | | var pathBuilder = new SpanPathBuilder(pathBuffer); |
| | | 237 | | |
| | 2043 | 238 | | if (!parentPathSpan.IsEmpty) |
| | | 239 | | { |
| | 6 | 240 | | pathBuilder.Append(parentPathSpan); |
| | | 241 | | } |
| | | 242 | | |
| | 2043 | 243 | | return ProcessDottedFieldSegments(fieldDefinitions, fieldPath, fieldType, arguments, metadata, ref pathBuild |
| | | 244 | | } |
| | | 245 | | else |
| | | 246 | | { |
| | | 247 | | // Use pooled resources for very long paths |
| | 6 | 248 | | using var pooledArray = CharArrayPool.GetPooled(estimatedPathLength); |
| | 6 | 249 | | var pathBuilder = new SpanPathBuilder(pooledArray.AsSpan()); |
| | | 250 | | |
| | 6 | 251 | | if (!parentPathSpan.IsEmpty) |
| | | 252 | | { |
| | 3 | 253 | | pathBuilder.Append(parentPathSpan); |
| | | 254 | | } |
| | | 255 | | |
| | 6 | 256 | | return ProcessDottedFieldSegments(fieldDefinitions, fieldPath, fieldType, arguments, metadata, ref pathBuild |
| | | 257 | | } |
| | 6 | 258 | | } |
| | | 259 | | |
| | | 260 | | /// <summary> |
| | | 261 | | /// Processes dotted fields with arguments and metadata — per-node variant. |
| | | 262 | | /// </summary> |
| | | 263 | | private static FieldDefinition ProcessDottedFieldWithMetadata(FieldDefinition rootParent, ReadOnlySpan<char> fieldPa |
| | | 264 | | { |
| | 123 | 265 | | var parentPathSpan = parentPath.AsSpan(); |
| | 123 | 266 | | var estimatedPathLength = parentPathSpan.Length + fieldPath.Length + 10; |
| | | 267 | | |
| | 123 | 268 | | if (estimatedPathLength <= 512) |
| | | 269 | | { |
| | 120 | 270 | | Span<char> pathBuffer = stackalloc char[512]; |
| | 120 | 271 | | var pathBuilder = new SpanPathBuilder(pathBuffer); |
| | 231 | 272 | | if (!parentPathSpan.IsEmpty) pathBuilder.Append(parentPathSpan); |
| | 120 | 273 | | return ProcessDottedFieldSegments(rootParent, fieldPath, fieldType, arguments, metadata, ref pathBuilder); |
| | | 274 | | } |
| | | 275 | | else |
| | | 276 | | { |
| | 3 | 277 | | using var pooledArray = CharArrayPool.GetPooled(estimatedPathLength); |
| | 3 | 278 | | var pathBuilder = new SpanPathBuilder(pooledArray.AsSpan()); |
| | 6 | 279 | | if (!parentPathSpan.IsEmpty) pathBuilder.Append(parentPathSpan); |
| | 3 | 280 | | return ProcessDottedFieldSegments(rootParent, fieldPath, fieldType, arguments, metadata, ref pathBuilder); |
| | | 281 | | } |
| | 3 | 282 | | } |
| | | 283 | | |
| | | 284 | | /// <summary> |
| | | 285 | | /// Processes individual segments of a dotted field path — root-level variant. |
| | | 286 | | /// </summary> |
| | | 287 | | // Callers reach here only via IsDottedField() which guarantees fieldPath contains '.', so the |
| | | 288 | | // loop runs at least once and result is non-null on exit. |
| | | 289 | | private static FieldDefinition ProcessDottedFieldSegments(Dictionary<string, FieldDefinition> rootFields, ReadOnlySp |
| | | 290 | | { |
| | 2049 | 291 | | FieldDefinition? parentField = null; |
| | 2049 | 292 | | FieldDefinition? result = null; |
| | | 293 | | |
| | 10149 | 294 | | while (fieldPath.Length > 0) |
| | | 295 | | { |
| | 8100 | 296 | | ExtractDottedSegmentWithPath(fieldPath, out var spanSegment, out var remainingPath); |
| | | 297 | | |
| | 8100 | 298 | | if (spanSegment.Name.IsWhiteSpace()) |
| | | 299 | | { |
| | 9 | 300 | | fieldPath = remainingPath; |
| | 9 | 301 | | continue; |
| | | 302 | | } |
| | | 303 | | |
| | 8091 | 304 | | pathBuilder.Append(spanSegment.Name); |
| | | 305 | | |
| | 8091 | 306 | | if (parentField == null) |
| | | 307 | | { |
| | | 308 | | // Root level — use the root Dictionary. |
| | 2046 | 309 | | result = ProcessDottedSegment(rootFields, spanSegment.Name, spanSegment.IsLastFragment, fieldType, argum |
| | | 310 | | } |
| | | 311 | | else |
| | | 312 | | { |
| | | 313 | | // Nested level — use FieldChildren. parentField is an ancestor of whatever this |
| | | 314 | | // segment (or a later one) mutates, so its memoized fingerprint must not survive |
| | | 315 | | // an argument-bearing walk past it. |
| | 6045 | 316 | | if (arguments is { Count: > 0 }) |
| | | 317 | | { |
| | 5556 | 318 | | parentField.ClearMergeMemo(); |
| | | 319 | | } |
| | 6045 | 320 | | var children = parentField._children ??= new FieldChildren(); |
| | 6045 | 321 | | result = ProcessDottedSegment(children, spanSegment.Name, spanSegment.IsLastFragment, fieldType, argumen |
| | | 322 | | } |
| | | 323 | | |
| | 8091 | 324 | | parentField = result; |
| | 8091 | 325 | | fieldPath = remainingPath; |
| | | 326 | | } |
| | | 327 | | |
| | 2049 | 328 | | return result ?? throw EmptyDottedPath(); |
| | | 329 | | } |
| | | 330 | | |
| | | 331 | | /// <summary> |
| | | 332 | | /// Processes individual segments of a dotted field path — per-node variant. |
| | | 333 | | /// </summary> |
| | | 334 | | private static FieldDefinition ProcessDottedFieldSegments(FieldDefinition rootParent, ReadOnlySpan<char> fieldPath, |
| | | 335 | | { |
| | 123 | 336 | | var currentParent = rootParent; |
| | 123 | 337 | | FieldDefinition? result = null; |
| | | 338 | | |
| | 1158 | 339 | | while (fieldPath.Length > 0) |
| | | 340 | | { |
| | 1035 | 341 | | ExtractDottedSegmentWithPath(fieldPath, out var spanSegment, out var remainingPath); |
| | | 342 | | |
| | 1035 | 343 | | if (spanSegment.Name.IsWhiteSpace()) |
| | | 344 | | { |
| | 15 | 345 | | fieldPath = remainingPath; |
| | 15 | 346 | | continue; |
| | | 347 | | } |
| | | 348 | | |
| | 1020 | 349 | | pathBuilder.Append(spanSegment.Name); |
| | | 350 | | // currentParent is an ancestor of whatever this segment (or a later one) mutates — |
| | | 351 | | // see the root-level variant above for the full invalidation rationale. |
| | 1020 | 352 | | if (arguments is { Count: > 0 }) |
| | | 353 | | { |
| | 522 | 354 | | currentParent.ClearMergeMemo(); |
| | | 355 | | } |
| | 1020 | 356 | | var children = currentParent._children ??= new FieldChildren(); |
| | 1020 | 357 | | result = ProcessDottedSegment(children, spanSegment.Name, spanSegment.IsLastFragment, fieldType, arguments, |
| | 1020 | 358 | | currentParent = result; |
| | 1020 | 359 | | fieldPath = remainingPath; |
| | | 360 | | } |
| | | 361 | | |
| | | 362 | | // Matches the per-node fast-path variant: an all-empty dotted path leaves the parent |
| | | 363 | | // unchanged rather than throwing. |
| | 123 | 364 | | return result ?? rootParent; |
| | | 365 | | } |
| | | 366 | | |
| | | 367 | | /// <summary> |
| | | 368 | | /// Creates an exception for a dotted path that contains no non-empty segments. |
| | | 369 | | /// Mirrors the null/empty guard used at the public AddField boundary. |
| | | 370 | | /// </summary> |
| | | 371 | | private static ArgumentException EmptyDottedPath() |
| | 9 | 372 | | => new("Field cannot be null or empty"); |
| | | 373 | | |
| | | 374 | | /// <summary> |
| | | 375 | | /// Creates a field segment for dotted field processing. |
| | | 376 | | /// </summary> |
| | | 377 | | private static FieldDefinition CreateDottedFieldSegment(ReadOnlySpan<char> segment, ReadOnlySpan<char> fullPath, int |
| | | 378 | | { |
| | 197865 | 379 | | var segmentType = isLastSegment ? fieldType : Constants.ObjectFieldTypeSpan; |
| | 197865 | 380 | | var segmentPath = fullPath.Slice(0, segmentEnd); |
| | | 381 | | |
| | 197865 | 382 | | return Helpers.CreateFieldDefinition(segment, segmentType, ReadOnlySpan<char>.Empty, null, segmentPath, null); |
| | | 383 | | } |
| | | 384 | | |
| | | 385 | | /// <summary> |
| | | 386 | | /// Processes a single dotted segment with arguments and metadata — root-Dict variant. |
| | | 387 | | /// </summary> |
| | | 388 | | private static FieldDefinition ProcessDottedSegment(Dictionary<string, FieldDefinition> currentFields, ReadOnlySpan< |
| | | 389 | | { |
| | 2046 | 390 | | if (!currentFields.TryGetValue(segment, out var field)) |
| | | 391 | | { |
| | 1362 | 392 | | field = CreateDottedSegmentField(segment, isLastSegment, fieldType, arguments, metadata, segmentPath); |
| | 1362 | 393 | | currentFields.SetValue(segment, field); |
| | 1362 | 394 | | return field; |
| | | 395 | | } |
| | | 396 | | |
| | | 397 | | // This Dictionary variant only processes the FIRST segment of dotted paths, so isLastSegment |
| | | 398 | | // is necessarily false (single-segment paths route through GetOrAddSimpleField instead). |
| | 684 | 399 | | var existing = field!; |
| | 684 | 400 | | if (existing.ShouldConvertToObjectType()) |
| | | 401 | | { |
| | 309 | 402 | | existing = existing with { Type = Constants.ObjectFieldType }; |
| | 309 | 403 | | currentFields.SetValue(segment, existing); |
| | | 404 | | } |
| | 684 | 405 | | return existing; |
| | | 406 | | } |
| | | 407 | | |
| | | 408 | | /// <summary> |
| | | 409 | | /// Processes a single dotted segment with arguments and metadata — FieldChildren variant. |
| | | 410 | | /// </summary> |
| | | 411 | | private static FieldDefinition ProcessDottedSegment(FieldChildren children, ReadOnlySpan<char> segment, bool isLastS |
| | | 412 | | { |
| | 7065 | 413 | | if (!children.TryGetValue(segment, out var field)) |
| | | 414 | | { |
| | 6606 | 415 | | field = CreateDottedSegmentField(segment, isLastSegment, fieldType, arguments, metadata, segmentPath); |
| | 6606 | 416 | | children.Append(field); |
| | 6606 | 417 | | return field; |
| | | 418 | | } |
| | | 419 | | |
| | 561 | 420 | | if (!isLastSegment) return PromoteIntermediateChildToObject(field!); |
| | | 421 | | // FieldBuilder normalizes empty argument dictionaries to null upstream, so a |
| | | 422 | | // non-null `arguments` here always has Count > 0. |
| | 357 | 423 | | return arguments is null ? field! : MergeArgumentsIntoExistingChild(children, segment, field!, arguments); |
| | | 424 | | } |
| | | 425 | | |
| | | 426 | | // ProcessDottedFieldFastPath handles the args-null case before reaching here, and |
| | | 427 | | // FieldBuilder.Create normalizes empty argument dictionaries to null upstream — so by |
| | | 428 | | // the time we get here arguments is always a non-null dictionary with Count > 0. |
| | | 429 | | private static FieldDefinition MergeArgumentsIntoExistingChild(FieldChildren children, ReadOnlySpan<char> segment, F |
| | | 430 | | { |
| | 354 | 431 | | var merged = existing.MergeFieldArguments(arguments); |
| | 354 | 432 | | children.Set(segment, merged); |
| | 354 | 433 | | return merged; |
| | | 434 | | } |
| | | 435 | | |
| | | 436 | | private static FieldDefinition PromoteIntermediateChildToObject(FieldDefinition existing) |
| | | 437 | | { |
| | 102 | 438 | | if (existing.ShouldConvertToObjectType()) |
| | | 439 | | { |
| | 6 | 440 | | existing._type = Constants.ObjectFieldType; |
| | | 441 | | } |
| | 102 | 442 | | return existing; |
| | | 443 | | } |
| | | 444 | | |
| | | 445 | | private static FieldDefinition CreateDottedSegmentField(ReadOnlySpan<char> segment, bool isLastSegment, ReadOnlySpan |
| | | 446 | | { |
| | 7968 | 447 | | var segmentArgs = isLastSegment ? arguments : null; |
| | 7968 | 448 | | var segmentType = isLastSegment ? fieldType : Constants.ObjectFieldTypeSpan; |
| | 7968 | 449 | | var segmentMetadata = isLastSegment ? metadata : null; |
| | 7968 | 450 | | return Helpers.CreateFieldDefinition(segment, segmentType, ReadOnlySpan<char>.Empty, segmentArgs, segmentPath, s |
| | | 451 | | } |
| | | 452 | | |
| | | 453 | | /// <summary> |
| | | 454 | | /// Gets or adds a complex field with type parsing and alias handling — root-level variant. |
| | | 455 | | /// The Dictionary variant is the entry point from QueryBuilder.AddField; callers never pass a |
| | | 456 | | /// non-empty parentPath (that's used only by the per-node FieldChildren overload below). The |
| | | 457 | | /// parameter exists to share a signature with the FieldChildren variant via the public dispatch. |
| | | 458 | | /// </summary> |
| | | 459 | | // AddFieldCore rejects null/whitespace fieldPath at the public-API boundary, so by the time |
| | | 460 | | // we reach here at least one non-whitespace segment exists and result is non-null on exit. |
| | | 461 | | #pragma warning disable S1172 // parentPath unused — kept for signature symmetry with the FieldChildren variant. |
| | | 462 | | private static FieldDefinition GetOrAddComplexField(Dictionary<string, FieldDefinition> fieldDefinitions, ReadOnlySp |
| | | 463 | | #pragma warning restore S1172 |
| | | 464 | | { |
| | | 465 | | // Type/alias parsing only ever shrinks the accumulated path, so fieldPath length plus slack |
| | | 466 | | // for separators is a safe upper bound. Mirror the dotted path: stackalloc for the common |
| | | 467 | | // short case, pooled fallback for long paths so we never overflow SpanPathBuilder. |
| | 1728 | 468 | | var estimatedPathLength = fieldPath.Length + 10; |
| | | 469 | | |
| | 1728 | 470 | | if (estimatedPathLength <= 512) |
| | | 471 | | { |
| | 1719 | 472 | | Span<char> pathBuffer = stackalloc char[512]; |
| | 1719 | 473 | | var pathBuilder = new SpanPathBuilder(pathBuffer); |
| | 1719 | 474 | | return ProcessComplexFieldSegments(fieldDefinitions, fieldPath, fieldType, arguments, metadata, ref pathBuil |
| | | 475 | | } |
| | | 476 | | else |
| | | 477 | | { |
| | 9 | 478 | | using var pooledArray = CharArrayPool.GetPooled(estimatedPathLength); |
| | 9 | 479 | | var pathBuilder = new SpanPathBuilder(pooledArray.AsSpan()); |
| | 9 | 480 | | return ProcessComplexFieldSegments(fieldDefinitions, fieldPath, fieldType, arguments, metadata, ref pathBuil |
| | | 481 | | } |
| | 9 | 482 | | } |
| | | 483 | | |
| | | 484 | | /// <summary> |
| | | 485 | | /// Gets or adds a complex field with type parsing and alias handling — per-node variant. |
| | | 486 | | /// </summary> |
| | | 487 | | private static FieldDefinition GetOrAddComplexField(FieldDefinition rootParent, ReadOnlySpan<char> fieldPath, ReadOn |
| | | 488 | | { |
| | 375 | 489 | | var parentPathSpan = parentPath.AsSpan(); |
| | | 490 | | |
| | | 491 | | // Type/alias parsing only shrinks the segment path, so parent + fieldPath + slack is a safe |
| | | 492 | | // upper bound. Mirror the dotted path: stackalloc for short paths, pooled fallback for long |
| | | 493 | | // ones so we never overflow SpanPathBuilder. |
| | 375 | 494 | | var estimatedPathLength = parentPathSpan.Length + fieldPath.Length + 10; |
| | | 495 | | |
| | 375 | 496 | | if (estimatedPathLength <= 512) |
| | | 497 | | { |
| | 369 | 498 | | Span<char> pathBuffer = stackalloc char[512]; |
| | 369 | 499 | | var pathBuilder = new SpanPathBuilder(pathBuffer); |
| | 735 | 500 | | if (!parentPathSpan.IsEmpty) pathBuilder.Append(parentPathSpan); |
| | 369 | 501 | | return ProcessComplexFieldSegments(rootParent, fieldPath, fieldType, arguments, metadata, ref pathBuilder); |
| | | 502 | | } |
| | | 503 | | else |
| | | 504 | | { |
| | 6 | 505 | | using var pooledArray = CharArrayPool.GetPooled(estimatedPathLength); |
| | 6 | 506 | | var pathBuilder = new SpanPathBuilder(pooledArray.AsSpan()); |
| | 12 | 507 | | if (!parentPathSpan.IsEmpty) pathBuilder.Append(parentPathSpan); |
| | 6 | 508 | | return ProcessComplexFieldSegments(rootParent, fieldPath, fieldType, arguments, metadata, ref pathBuilder); |
| | | 509 | | } |
| | 6 | 510 | | } |
| | | 511 | | |
| | | 512 | | private static FieldDefinition ProcessComplexFieldSegments(Dictionary<string, FieldDefinition> fieldDefinitions, Rea |
| | | 513 | | { |
| | 1728 | 514 | | fieldPath = Helpers.ParseFieldTypeFromPath(fieldPath, fieldType, out var parsedFieldType); |
| | | 515 | | |
| | 1728 | 516 | | FieldDefinition? parentField = null; |
| | 1728 | 517 | | FieldDefinition? result = null; |
| | | 518 | | |
| | 7149 | 519 | | while (fieldPath.Length > 0) |
| | | 520 | | { |
| | 5430 | 521 | | ExtractNextSegment(fieldPath, out var segment, out var remaining); |
| | | 522 | | |
| | 5421 | 523 | | if (!segment.Name.IsWhiteSpace()) |
| | | 524 | | { |
| | 5418 | 525 | | pathBuilder.Append(segment.Name); |
| | 5418 | 526 | | var typeToUse = !segment.ParsedType.IsEmpty ? segment.ParsedType : parsedFieldType; |
| | | 527 | | |
| | 5418 | 528 | | if (parentField == null) |
| | | 529 | | { |
| | | 530 | | // Root level |
| | 1719 | 531 | | result = ProcessFieldSegment(fieldDefinitions, segment, arguments, typeToUse, pathBuilder.AsSpan(), |
| | | 532 | | } |
| | | 533 | | else |
| | | 534 | | { |
| | | 535 | | // Nested level. parentField is an ancestor of whatever this segment (or a |
| | | 536 | | // later one) mutates — only the last fragment ever carries arguments, but |
| | | 537 | | // every field visited on the way there must lose its memoized fingerprint. |
| | 3699 | 538 | | if (arguments is { Count: > 0 }) |
| | | 539 | | { |
| | 84 | 540 | | parentField.ClearMergeMemo(); |
| | | 541 | | } |
| | 3699 | 542 | | var children = parentField._children ??= new FieldChildren(); |
| | 3699 | 543 | | result = ProcessFieldSegment(children, segment, arguments, typeToUse, pathBuilder.AsSpan(), metadata |
| | | 544 | | } |
| | | 545 | | |
| | 5418 | 546 | | parentField = result; |
| | | 547 | | } |
| | | 548 | | |
| | 5421 | 549 | | fieldPath = remaining; |
| | | 550 | | } |
| | | 551 | | |
| | 1719 | 552 | | return result!; |
| | | 553 | | } |
| | | 554 | | |
| | | 555 | | private static FieldDefinition ProcessComplexFieldSegments(FieldDefinition rootParent, ReadOnlySpan<char> fieldPath, |
| | | 556 | | { |
| | 375 | 557 | | fieldPath = Helpers.ParseFieldTypeFromPath(fieldPath, fieldType, out var parsedFieldType); |
| | | 558 | | |
| | 375 | 559 | | var currentParent = rootParent; |
| | 375 | 560 | | FieldDefinition? result = null; |
| | | 561 | | |
| | 852 | 562 | | while (fieldPath.Length > 0) |
| | | 563 | | { |
| | 477 | 564 | | ExtractNextSegment(fieldPath, out var segment, out var remaining); |
| | | 565 | | |
| | 477 | 566 | | if (!segment.Name.IsWhiteSpace()) |
| | | 567 | | { |
| | 474 | 568 | | pathBuilder.Append(segment.Name); |
| | 474 | 569 | | var typeToUse = !segment.ParsedType.IsEmpty ? segment.ParsedType : parsedFieldType; |
| | | 570 | | // currentParent is an ancestor of whatever this segment (or a later one) mutates — |
| | | 571 | | // see the root-Dict variant above for the full invalidation rationale. |
| | 474 | 572 | | if (arguments is { Count: > 0 }) |
| | | 573 | | { |
| | 3 | 574 | | currentParent.ClearMergeMemo(); |
| | | 575 | | } |
| | 474 | 576 | | var children = currentParent._children ??= new FieldChildren(); |
| | 474 | 577 | | result = ProcessFieldSegment(children, segment, arguments, typeToUse, pathBuilder.AsSpan(), metadata); |
| | 474 | 578 | | currentParent = result; |
| | | 579 | | } |
| | | 580 | | |
| | 477 | 581 | | fieldPath = remaining; |
| | | 582 | | } |
| | | 583 | | |
| | 375 | 584 | | return result!; |
| | | 585 | | } |
| | | 586 | | |
| | | 587 | | /// <summary> |
| | | 588 | | /// Processes a field segment for complex field creation — root-Dict variant. |
| | | 589 | | /// </summary> |
| | | 590 | | private static FieldDefinition ProcessFieldSegment(Dictionary<string, FieldDefinition> currentFields, SpanSegment se |
| | | 591 | | { |
| | 1719 | 592 | | if (!TryGetRootFieldBySpan(currentFields, segment.Name, out var field, out var segmentKey)) |
| | | 593 | | { |
| | 798 | 594 | | return CreateNewField(currentFields, segmentKey, segment, arguments, parsedFieldType, fullPath, metadata); |
| | | 595 | | } |
| | | 596 | | |
| | 921 | 597 | | return UpdateExistingField(currentFields, segment, field, arguments, parsedFieldType); |
| | | 598 | | } |
| | | 599 | | |
| | | 600 | | /// <summary> |
| | | 601 | | /// Processes a field segment for complex field creation — FieldChildren variant. |
| | | 602 | | /// </summary> |
| | | 603 | | private static FieldDefinition ProcessFieldSegment(FieldChildren children, SpanSegment segment, IDictionary<string, |
| | | 604 | | { |
| | 4173 | 605 | | var field = FindExistingSegmentChild(children, segment); |
| | 4173 | 606 | | if (field is null) |
| | | 607 | | { |
| | 2844 | 608 | | return CreateNewField(children, segment, arguments, parsedFieldType, fullPath, metadata); |
| | | 609 | | } |
| | | 610 | | |
| | 1329 | 611 | | return UpdateExistingField(children, segment, field, arguments, parsedFieldType); |
| | | 612 | | } |
| | | 613 | | |
| | | 614 | | /// <summary> |
| | | 615 | | /// Finds a child matching <paramref name="segment"/>. For the LAST fragment of a complex path, |
| | | 616 | | /// identity is (Name, alias) — not Name alone — because two children may legitimately share a |
| | | 617 | | /// Name while differing by alias (distinct response keys), e.g. adding <c>"aliasA:id"</c> then |
| | | 618 | | /// <c>"aliasB:id"</c> under the same parent must produce two siblings, not collapse the second |
| | | 619 | | /// into the first. Intermediate (non-last) segments route purely by Name, exactly like dotted |
| | | 620 | | /// paths: a path addresses an intermediate node structurally by name, and an alias set on it |
| | | 621 | | /// (e.g. <c>"alias:profile.displayName:name"</c>) is rendering metadata, not routing identity — |
| | | 622 | | /// a LATER reference to the same node via its bare name (e.g. <c>"profile.userEmail:email"</c>) |
| | | 623 | | /// must still resolve to that one node, never fork a duplicate. |
| | | 624 | | /// </summary> |
| | | 625 | | private static FieldDefinition? FindExistingSegmentChild(FieldChildren children, SpanSegment segment) |
| | | 626 | | { |
| | 4173 | 627 | | var candidate = children.Find(segment.Name); |
| | 6990 | 628 | | if (candidate is null) return null; |
| | 2652 | 629 | | if (!segment.IsLastFragment) return candidate; |
| | | 630 | | |
| | 90 | 631 | | if (SegmentAliasMatches(segment, candidate)) return candidate; |
| | | 632 | | |
| | 135 | 633 | | foreach (var f in children.AsSpan()) |
| | | 634 | | { |
| | 39 | 635 | | if (f.Name.AsSpan().Equals(segment.Name, StringComparison.OrdinalIgnoreCase) && SegmentAliasMatches(segment, |
| | 3 | 636 | | return f; |
| | | 637 | | } |
| | 27 | 638 | | return null; |
| | | 639 | | } |
| | | 640 | | |
| | | 641 | | // An existing child with NO alias yet always matches — it hasn't picked a response-key identity |
| | | 642 | | // yet, so this segment either confirms the unaliased identity or (via ApplyIntermediateUpdates) |
| | | 643 | | // adopts an incoming alias onto it, exactly like the FIRST time any alias is set on a field. |
| | | 644 | | // Once a child HAS an alias, a segment only matches it when the aliases agree — a same-named |
| | | 645 | | // segment carrying a DIFFERENT (or absent) alias is a distinct sibling, not the same field. |
| | | 646 | | private static bool SegmentAliasMatches(SpanSegment segment, FieldDefinition field) |
| | 99 | 647 | | => field._alias is null |
| | 99 | 648 | | || (segment.HasAlias |
| | 99 | 649 | | ? segment.Alias.Equals(field._alias.AsSpan(), StringComparison.Ordinal) |
| | 99 | 650 | | : string.IsNullOrEmpty(field._alias)); |
| | | 651 | | |
| | | 652 | | /// <summary> |
| | | 653 | | /// Creates a new field for complex field processing — root-Dict variant. <paramref name="segmentKey"/> |
| | | 654 | | /// is the already-materialized dictionary key for <c>segment.Name</c> (computed once by the |
| | | 655 | | /// caller's failed probe via <see cref="TryGetRootFieldBySpan"/>), reused here for the insert |
| | | 656 | | /// instead of calling <see cref="ReadOnlySpan{T}.ToString"/> a second time. |
| | | 657 | | /// </summary> |
| | | 658 | | private static FieldDefinition CreateNewField(Dictionary<string, FieldDefinition> currentFields, string segmentKey, |
| | | 659 | | { |
| | 798 | 660 | | var (fieldArgs, fieldMetadata) = ResolveSegmentArgsAndMetadata(segment, arguments, metadata); |
| | 798 | 661 | | var fieldType = ResolveSegmentFieldType(segment, parsedFieldType); |
| | 798 | 662 | | var field = Helpers.CreateFieldDefinition(segment.Name, fieldType, segment.Alias, fieldArgs, fullPath, fieldMeta |
| | 798 | 663 | | currentFields[segmentKey] = field; |
| | 798 | 664 | | return field; |
| | | 665 | | } |
| | | 666 | | |
| | | 667 | | /// <summary> |
| | | 668 | | /// Creates a new field for complex field processing — FieldChildren variant. |
| | | 669 | | /// </summary> |
| | | 670 | | private static FieldDefinition CreateNewField(FieldChildren children, SpanSegment segment, IDictionary<string, objec |
| | | 671 | | { |
| | 2844 | 672 | | var (fieldArgs, fieldMetadata) = ResolveSegmentArgsAndMetadata(segment, arguments, metadata); |
| | 2844 | 673 | | var fieldType = ResolveSegmentFieldType(segment, parsedFieldType); |
| | 2844 | 674 | | var field = Helpers.CreateFieldDefinition(segment.Name, fieldType, segment.Alias, fieldArgs, fullPath, fieldMeta |
| | 2844 | 675 | | children.Append(field); |
| | 2844 | 676 | | return field; |
| | | 677 | | } |
| | | 678 | | |
| | | 679 | | private static (IDictionary<string, object?>? args, Dictionary<string, object?>? meta) ResolveSegmentArgsAndMetadata |
| | 3642 | 680 | | => segment.IsLastFragment ? (arguments, metadata) : (null, null); |
| | | 681 | | |
| | | 682 | | private static ReadOnlySpan<char> ResolveSegmentFieldType(SpanSegment segment, ReadOnlySpan<char> parsedFieldType) |
| | | 683 | | { |
| | 5685 | 684 | | if (segment.IsLastFragment) return segment.HasParsedType ? segment.ParsedType : parsedFieldType; |
| | 1599 | 685 | | return segment.HasParsedType && segment.ParsedType.SequenceEqual(Constants.ArrayTypeMarkerSpan) |
| | 1599 | 686 | | ? Constants.ArrayTypeMarkerSpan |
| | 1599 | 687 | | : Constants.ObjectFieldTypeSpan; |
| | | 688 | | } |
| | | 689 | | |
| | | 690 | | /// <summary> |
| | | 691 | | /// Updates an existing field during complex field processing — root-Dict variant. |
| | | 692 | | /// </summary> |
| | | 693 | | private static FieldDefinition UpdateExistingField(Dictionary<string, FieldDefinition> currentFields, SpanSegment se |
| | | 694 | | { |
| | 1824 | 695 | | if (!ApplyIntermediateUpdates(segment, field)) return field; |
| | | 696 | | |
| | 18 | 697 | | if (arguments?.Count > 0) |
| | | 698 | | { |
| | | 699 | | // field was found via a case-insensitive lookup keyed by this exact string (set at |
| | | 700 | | // insertion time in CreateNewField), so reusing it here — rather than re-materializing |
| | | 701 | | // segment.Name — both avoids a second allocation and targets the correct existing slot. |
| | 3 | 702 | | var fieldKey = field.Name; |
| | 3 | 703 | | field = currentFields[fieldKey] = field.MergeFieldArguments(arguments); |
| | | 704 | | } |
| | 18 | 705 | | ApplyParsedFieldType(field, parsedFieldType); |
| | 18 | 706 | | return field; |
| | | 707 | | } |
| | | 708 | | |
| | | 709 | | private static FieldDefinition UpdateExistingField(FieldChildren children, SpanSegment segment, FieldDefinition fiel |
| | | 710 | | { |
| | 2625 | 711 | | if (!ApplyIntermediateUpdates(segment, field)) return field; |
| | | 712 | | |
| | 33 | 713 | | if (arguments?.Count > 0) |
| | | 714 | | { |
| | 9 | 715 | | var mergedField = field.MergeFieldArguments(arguments); |
| | | 716 | | // Replace by REFERENCE: field was matched by (Name, alias) in FindExistingSegmentChild, |
| | | 717 | | // and a name-keyed Set could instead overwrite a different same-named/different-alias sibling. |
| | 9 | 718 | | children.ReplaceReference(field, mergedField); |
| | 9 | 719 | | field = mergedField; |
| | | 720 | | } |
| | 33 | 721 | | ApplyParsedFieldType(field, parsedFieldType); |
| | 33 | 722 | | return field; |
| | | 723 | | } |
| | | 724 | | |
| | | 725 | | // Mutates intermediate-segment metadata (alias, object-promotion). Returns true when the |
| | | 726 | | // segment is the last fragment and the caller should continue with last-fragment updates. |
| | | 727 | | private static bool ApplyIntermediateUpdates(SpanSegment segment, FieldDefinition field) |
| | | 728 | | { |
| | | 729 | | // segment.HasAlias <=> !Alias.IsEmpty by SpanSegment's invariant. |
| | 2250 | 730 | | if (segment.HasAlias && field._alias is null) |
| | | 731 | | { |
| | 15 | 732 | | field._alias = segment.Alias.ToString(); |
| | | 733 | | } |
| | 2250 | 734 | | if (!segment.IsLastFragment && field.ShouldConvertToObjectType()) |
| | | 735 | | { |
| | 9 | 736 | | field._type = Constants.ObjectFieldType; |
| | | 737 | | } |
| | 2250 | 738 | | return segment.IsLastFragment; |
| | | 739 | | } |
| | | 740 | | |
| | | 741 | | private static void ApplyParsedFieldType(FieldDefinition field, ReadOnlySpan<char> parsedFieldType) |
| | | 742 | | { |
| | 51 | 743 | | if (!parsedFieldType.Equals(Constants.DefaultFieldTypeSpan, StringComparison.OrdinalIgnoreCase) |
| | 51 | 744 | | && !field._type.AsSpan().Equals(parsedFieldType, StringComparison.OrdinalIgnoreCase)) |
| | | 745 | | { |
| | 6 | 746 | | field._type = parsedFieldType.ToString(); |
| | | 747 | | } |
| | 51 | 748 | | } |
| | | 749 | | |
| | | 750 | | /// <summary> |
| | | 751 | | /// Creates or merges a field definition into the target collection — root-Dict variant. |
| | | 752 | | /// </summary> |
| | | 753 | | internal static FieldDefinition CreateOrMergeField(Dictionary<string, FieldDefinition> fields, FieldDefinition field |
| | | 754 | | { |
| | | 755 | | // Try to find existing field to merge with |
| | 282 | 756 | | var existingField = Helpers.FindExistingField(fields, fieldDefinition); |
| | 282 | 757 | | if (existingField != null) |
| | | 758 | | { |
| | 183 | 759 | | var mergedField = existingField.MergeFieldArguments(fieldDefinition._arguments); |
| | 183 | 760 | | fields[existingField.Name] = mergedField; |
| | 183 | 761 | | return mergedField; |
| | | 762 | | } |
| | | 763 | | |
| | 99 | 764 | | var newField = CloneFieldDefinitionForMerge(fieldDefinition); |
| | 99 | 765 | | fields[fieldDefinition.Name] = newField; |
| | 99 | 766 | | return newField; |
| | | 767 | | } |
| | | 768 | | |
| | | 769 | | /// <summary> |
| | | 770 | | /// Creates or merges a field definition into the target collection — FieldChildren variant. |
| | | 771 | | /// </summary> |
| | | 772 | | internal static FieldDefinition CreateOrMergeField(FieldChildren children, FieldDefinition fieldDefinition) |
| | | 773 | | { |
| | 411 | 774 | | var existingField = Helpers.FindExistingField(children, fieldDefinition); |
| | 411 | 775 | | if (existingField != null) |
| | | 776 | | { |
| | 57 | 777 | | var mergedField = existingField.MergeFieldArguments(fieldDefinition._arguments); |
| | | 778 | | // Replace by REFERENCE, not by name: existingField was matched by (Name, alias) above, |
| | | 779 | | // and a name-keyed replace could instead overwrite a different same-named sibling that |
| | | 780 | | // carries a different alias (see FieldChildren.ReplaceReference). |
| | 57 | 781 | | children.ReplaceReference(existingField, mergedField); |
| | 57 | 782 | | return mergedField; |
| | | 783 | | } |
| | | 784 | | |
| | 354 | 785 | | var newField = CloneFieldDefinitionForMerge(fieldDefinition); |
| | 354 | 786 | | children.Append(newField); |
| | 354 | 787 | | return newField; |
| | | 788 | | } |
| | | 789 | | |
| | | 790 | | private static FieldDefinition CloneFieldDefinitionForMerge(FieldDefinition fieldDefinition) |
| | | 791 | | { |
| | 453 | 792 | | return Helpers.CreateFieldDefinition( |
| | 453 | 793 | | fieldDefinition.Name, |
| | 453 | 794 | | fieldDefinition._type.AsSpan(), |
| | 453 | 795 | | fieldDefinition._alias, |
| | 453 | 796 | | fieldDefinition._arguments, |
| | 453 | 797 | | fieldDefinition.Path, |
| | 453 | 798 | | fieldDefinition._metadata); |
| | | 799 | | } |
| | | 800 | | |
| | | 801 | | private static void ExtractDottedSegment(ReadOnlySpan<char> fieldPath, int pathStart, out SpanSegment segment, out i |
| | | 802 | | { |
| | 216879 | 803 | | var dotIndex = fieldPath.Slice(pathStart).IndexOf('.'); |
| | 216879 | 804 | | var isLastSegment = dotIndex == -1; |
| | 216879 | 805 | | var segmentEnd = isLastSegment ? fieldPath.Length : pathStart + dotIndex; |
| | 216879 | 806 | | var segmentSpan = fieldPath.Slice(pathStart, segmentEnd - pathStart); |
| | 216879 | 807 | | nextStart = isLastSegment ? fieldPath.Length : segmentEnd + 1; |
| | | 808 | | |
| | 216879 | 809 | | segment = new SpanSegment(segmentSpan, ReadOnlySpan<char>.Empty, isLastSegment, ReadOnlySpan<char>.Empty); |
| | 216879 | 810 | | } |
| | | 811 | | |
| | | 812 | | private static void ExtractDottedSegmentWithPath(ReadOnlySpan<char> fieldPath, out SpanSegment segment, out ReadOnly |
| | | 813 | | { |
| | 9135 | 814 | | var dotIndex = fieldPath.IndexOf('.'); |
| | 9135 | 815 | | var isLastSegment = dotIndex == -1; |
| | 9135 | 816 | | var segmentSpan = isLastSegment ? fieldPath : fieldPath[..dotIndex]; |
| | 9135 | 817 | | remainingPath = isLastSegment ? ReadOnlySpan<char>.Empty : fieldPath[(dotIndex + 1)..]; |
| | | 818 | | |
| | 9135 | 819 | | segment = new SpanSegment(segmentSpan, ReadOnlySpan<char>.Empty, isLastSegment, ReadOnlySpan<char>.Empty); |
| | 9135 | 820 | | } |
| | | 821 | | |
| | | 822 | | private static void ExtractNextSegment(ReadOnlySpan<char> fieldPath, out SpanSegment segment, out ReadOnlySpan<char> |
| | | 823 | | { |
| | 5907 | 824 | | var nextDot = fieldPath.IndexOf('.'); |
| | 5907 | 825 | | var isLastFragment = nextDot == -1; |
| | 5907 | 826 | | var currentPart = isLastFragment ? fieldPath : fieldPath[..nextDot]; |
| | 5907 | 827 | | var trimmedPart = currentPart.Trim(); |
| | | 828 | | |
| | | 829 | | // Parse and remove type information from the segment |
| | 5907 | 830 | | var cleanedPath = Helpers.ParseFieldTypeFromPath(trimmedPart, Constants.DefaultFieldType, out var parsedType); |
| | | 831 | | |
| | | 832 | | // Parse field name and alias from cleanedPath: only exactly 2 non-empty trimmed |
| | | 833 | | // colon-separated parts mean alias:name. Span-based equivalent of |
| | | 834 | | // Split(':', TrimEntries | RemoveEmptyEntries) without the string/array allocations. |
| | 5907 | 835 | | ParseAliasAndName(cleanedPath, out var name, out var alias, out var malformed); |
| | | 836 | | |
| | | 837 | | // Only include parsed type if it's not the default |
| | 5907 | 838 | | var typeToInclude = parsedType.SequenceEqual(Constants.DefaultFieldTypeSpan) ? ReadOnlySpan<char>.Empty : parsed |
| | | 839 | | |
| | | 840 | | // Reject a colon that produced an empty alias/name part (e.g. "a : b" → ": b", or |
| | | 841 | | // "alias:"): folding the stray colon back into the field name emits invalid GraphQL such |
| | | 842 | | // as a field literally named ": b". Throw a clear error instead. |
| | 5907 | 843 | | if (malformed) |
| | | 844 | | { |
| | 9 | 845 | | throw MalformedComplexSegment(trimmedPart); |
| | | 846 | | } |
| | | 847 | | |
| | 5898 | 848 | | segment = new SpanSegment(name, alias, isLastFragment, typeToInclude); |
| | 5898 | 849 | | remaining = isLastFragment ? ReadOnlySpan<char>.Empty : fieldPath[(nextDot + 1)..]; |
| | 5898 | 850 | | } |
| | | 851 | | |
| | | 852 | | private static ArgumentException MalformedComplexSegment(ReadOnlySpan<char> segment) |
| | 9 | 853 | | => new($"Malformed field segment '{segment.ToString()}'. Use 'alias:name' (no spaces around ':') and put any typ |
| | | 854 | | |
| | | 855 | | /// <summary> |
| | | 856 | | /// Splits <paramref name="cleanedPath"/> on ':' into trimmed non-empty parts. Exactly two |
| | | 857 | | /// parts mean <c>alias:name</c>; a colon-free segment is the name with no alias. A colon that |
| | | 858 | | /// yields an empty alias or name part (e.g. <c>": name"</c> or <c>"alias:"</c>) sets |
| | | 859 | | /// <paramref name="malformed"/> so the caller can reject it rather than folding the stray |
| | | 860 | | /// colon back into a field name and emitting invalid GraphQL. Three or more parts still fall |
| | | 861 | | /// back to treating the whole segment as the name (unchanged from prior behavior). |
| | | 862 | | /// </summary> |
| | | 863 | | private static void ParseAliasAndName(ReadOnlySpan<char> cleanedPath, out ReadOnlySpan<char> name, out ReadOnlySpan< |
| | | 864 | | { |
| | 5907 | 865 | | malformed = false; |
| | | 866 | | |
| | 5907 | 867 | | if (cleanedPath.IndexOf(':') < 0) |
| | | 868 | | { |
| | 3513 | 869 | | name = cleanedPath.Trim(); |
| | 3513 | 870 | | alias = ReadOnlySpan<char>.Empty; |
| | 3513 | 871 | | return; |
| | | 872 | | } |
| | | 873 | | |
| | 4788 | 874 | | ReadOnlySpan<char> first = default, second = default; |
| | 2394 | 875 | | var partCount = 0; |
| | 2394 | 876 | | var emptyPartSeen = false; |
| | 2394 | 877 | | var rest = cleanedPath; |
| | 2415 | 878 | | while (true) |
| | | 879 | | { |
| | 4809 | 880 | | var colonIndex = rest.IndexOf(':'); |
| | 4809 | 881 | | var part = (colonIndex < 0 ? rest : rest[..colonIndex]).Trim(); |
| | 4809 | 882 | | if (part.IsEmpty) |
| | | 883 | | { |
| | 27 | 884 | | emptyPartSeen = true; |
| | | 885 | | } |
| | | 886 | | else |
| | | 887 | | { |
| | 7176 | 888 | | if (partCount == 0) first = part; |
| | 4773 | 889 | | else if (partCount == 1) second = part; |
| | 4782 | 890 | | partCount++; |
| | 4782 | 891 | | if (partCount > 2) break; |
| | | 892 | | } |
| | 4806 | 893 | | if (colonIndex < 0) break; |
| | 2415 | 894 | | rest = rest[(colonIndex + 1)..]; |
| | | 895 | | } |
| | | 896 | | |
| | 2394 | 897 | | if (partCount == 2) |
| | | 898 | | { |
| | 2382 | 899 | | alias = first; |
| | 2382 | 900 | | name = second; |
| | 2382 | 901 | | return; |
| | | 902 | | } |
| | | 903 | | |
| | | 904 | | // A colon that produced an empty alias/name part (e.g. ": name") is malformed: surface it |
| | | 905 | | // instead of folding the stray colon back into the name. Other shapes (three or more |
| | | 906 | | // parts) keep the historical whole-segment-as-name fallback. |
| | 12 | 907 | | name = cleanedPath.Trim(); |
| | 12 | 908 | | alias = ReadOnlySpan<char>.Empty; |
| | 12 | 909 | | malformed = emptyPartSeen && partCount < 2; |
| | 12 | 910 | | } |
| | | 911 | | } |