| | | 1 | | using System.Reflection; |
| | | 2 | | using NGql.Core.Caching; |
| | | 3 | | |
| | | 4 | | namespace NGql.Core.Builders; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Handles expansion of navigation properties (getter-only computed properties) |
| | | 8 | | /// to their underlying settable properties. |
| | | 9 | | /// </summary> |
| | | 10 | | internal static class NavigationPropertyExpander |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Expands a field name if it's a navigation property (getter-only computed property). |
| | | 14 | | /// For navigation properties, returns all settable properties from the parameter type. |
| | | 15 | | /// Handles nested paths like "profile.name" by only checking the first segment. |
| | | 16 | | /// </summary> |
| | | 17 | | public static HashSet<string> ExpandNavigationProperty(string fieldName, Type? parameterType) |
| | | 18 | | { |
| | 450 | 19 | | var result = new HashSet<string>(StringComparer.OrdinalIgnoreCase); |
| | | 20 | | |
| | 450 | 21 | | if (parameterType == null) |
| | | 22 | | { |
| | 30 | 23 | | result.Add(fieldName); |
| | 30 | 24 | | return result; |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | try |
| | | 28 | | { |
| | | 29 | | // Handle nested paths - only check the first segment for navigation properties |
| | 420 | 30 | | var (firstSegment, remainingPath) = SplitPath(fieldName); |
| | | 31 | | |
| | | 32 | | // Get the property from the type (only first segment) via cached metadata |
| | 420 | 33 | | var property = TypeMetadataCache.GetNavigationProperties(parameterType).GetProperty(firstSegment); |
| | 414 | 34 | | if (property == null) |
| | | 35 | | { |
| | 117 | 36 | | result.Add(fieldName); |
| | 117 | 37 | | return result; |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | // Check if this is a navigation property (getter-only, no setter) |
| | 297 | 41 | | if (IsNavigationProperty(property)) |
| | | 42 | | { |
| | 54 | 43 | | ExpandNavigationPropertyFields(parameterType, remainingPath, result); |
| | | 44 | | } |
| | | 45 | | else |
| | | 46 | | { |
| | 243 | 47 | | HandleRegularProperty(fieldName, firstSegment, remainingPath, property, result); |
| | | 48 | | } |
| | 297 | 49 | | } |
| | 6 | 50 | | catch (AmbiguousMatchException) |
| | | 51 | | { |
| | | 52 | | // The first segment names a property shadowed by a `new` declaration, so there is no |
| | | 53 | | // single property to expand from — fall back to the original field name. |
| | 6 | 54 | | result.Add(fieldName); |
| | 6 | 55 | | } |
| | | 56 | | |
| | 303 | 57 | | return result; |
| | 117 | 58 | | } |
| | | 59 | | |
| | | 60 | | private static (string FirstSegment, string? RemainingPath) SplitPath(string fieldName) |
| | | 61 | | { |
| | 420 | 62 | | var dotIndex = fieldName.IndexOf('.'); |
| | 420 | 63 | | if (dotIndex > 0) |
| | | 64 | | { |
| | 96 | 65 | | return (fieldName.Substring(0, dotIndex), fieldName.Substring(dotIndex + 1)); |
| | | 66 | | } |
| | 324 | 67 | | return (fieldName, null); |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | private static bool IsNavigationProperty(PropertyInfo property) |
| | | 71 | | { |
| | | 72 | | // Properties surfaced by GetProperty(BindingFlags.Public | BindingFlags.Instance) |
| | | 73 | | // always have a public getter, so GetGetMethod() never returns null in this scope. |
| | 297 | 74 | | return property.SetMethod == null && property.GetGetMethod()!.IsPublic; |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | private static void ExpandNavigationPropertyFields(Type parameterType, string? remainingPath, HashSet<string> result |
| | | 78 | | { |
| | | 79 | | // This is a navigation property - get all SETTABLE properties |
| | 444 | 80 | | foreach (var prop in TypeMetadataCache.GetNavigationProperties(parameterType).Properties) |
| | | 81 | | { |
| | | 82 | | // Skip navigation properties themselves (getter-only) |
| | 168 | 83 | | if (prop.SetMethod != null) |
| | | 84 | | { |
| | | 85 | | // If there was a remaining path, append it to each expanded property |
| | 114 | 86 | | if (remainingPath != null) |
| | | 87 | | { |
| | 18 | 88 | | result.Add($"{prop.Name}.{remainingPath}"); |
| | | 89 | | } |
| | | 90 | | else |
| | | 91 | | { |
| | 96 | 92 | | result.Add(prop.Name); |
| | | 93 | | } |
| | | 94 | | } |
| | | 95 | | } |
| | 54 | 96 | | } |
| | | 97 | | |
| | | 98 | | private static void HandleRegularProperty( |
| | | 99 | | string fieldName, |
| | | 100 | | string firstSegment, |
| | | 101 | | string? remainingPath, |
| | | 102 | | PropertyInfo property, |
| | | 103 | | HashSet<string> result) |
| | | 104 | | { |
| | | 105 | | // Not a navigation property - check if we need to recurse for nested path |
| | 243 | 106 | | if (remainingPath != null && property.PropertyType != null) |
| | | 107 | | { |
| | | 108 | | // Recurse on the remaining path with the property's type |
| | 60 | 109 | | var nestedExpanded = ExpandNavigationProperty(remainingPath, property.PropertyType); |
| | 300 | 110 | | foreach (var nestedField in nestedExpanded) |
| | | 111 | | { |
| | 90 | 112 | | result.Add($"{firstSegment}.{nestedField}"); |
| | | 113 | | } |
| | | 114 | | } |
| | | 115 | | else |
| | | 116 | | { |
| | | 117 | | // Just return the field name |
| | 183 | 118 | | result.Add(fieldName); |
| | | 119 | | } |
| | 243 | 120 | | } |
| | | 121 | | } |