| | | 1 | | using System.Linq.Expressions; |
| | | 2 | | using System.Reflection; |
| | | 3 | | using System.Runtime.CompilerServices; |
| | | 4 | | using NGql.Core.Abstractions; |
| | | 5 | | using NGql.Core.Extensions; |
| | | 6 | | using NGql.Core.Features; |
| | | 7 | | |
| | | 8 | | namespace NGql.Core.Builders; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Processes expressions to extract and preserve field paths from lambda expressions. |
| | | 12 | | /// Optimized for hotpath usage with minimal allocations. |
| | | 13 | | /// </summary> |
| | 177 | 14 | | internal sealed class ExpressionPreservationProcessor(QueryBuilder sourceQuery, Action<string> preserveCallback) |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Processes an expression and preserves the referenced fields. |
| | | 18 | | /// </summary> |
| | | 19 | | public void ProcessExpression( |
| | | 20 | | Expression expression, |
| | | 21 | | string? nodePath, |
| | | 22 | | Dictionary<string, string[]>? localMap, |
| | | 23 | | Type? parameterType, |
| | | 24 | | string[]? alwaysPreserveFields) |
| | | 25 | | { |
| | 177 | 26 | | var extractedPaths = ExpressionFieldExtractor.ExtractFieldPaths(expression); |
| | | 27 | | |
| | | 28 | | // Fast path: no nodePath means preserve directly |
| | 177 | 29 | | if (string.IsNullOrWhiteSpace(nodePath)) |
| | | 30 | | { |
| | 108 | 31 | | foreach (var path in extractedPaths) |
| | | 32 | | { |
| | 21 | 33 | | preserveCallback(path); |
| | | 34 | | } |
| | 33 | 35 | | return; |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | // Get parameter info once |
| | 144 | 39 | | var parameterNames = GetParameterNames(expression); |
| | 144 | 40 | | var parameterTypes = GetParameterTypes(expression); |
| | | 41 | | |
| | | 42 | | // Choose strategy based on localMap availability |
| | 144 | 43 | | if (localMap != null && parameterNames != null) |
| | | 44 | | { |
| | 72 | 45 | | PreserveWithLocalMap(extractedPaths, parameterNames, nodePath, localMap, parameterTypes, alwaysPreserveField |
| | | 46 | | } |
| | | 47 | | else |
| | | 48 | | { |
| | 72 | 49 | | PreserveWithGetPathTo(extractedPaths, parameterNames?.FirstOrDefault(), nodePath, parameterType); |
| | | 50 | | } |
| | 72 | 51 | | } |
| | | 52 | | |
| | | 53 | | private void PreserveWithLocalMap( |
| | | 54 | | HashSet<string> extractedPaths, |
| | | 55 | | string[] parameterNames, |
| | | 56 | | string nodePath, |
| | | 57 | | Dictionary<string, string[]> localMap, |
| | | 58 | | Dictionary<string, Type> parameterTypes, |
| | | 59 | | string[]? alwaysPreserveFields) |
| | | 60 | | { |
| | 72 | 61 | | var basePathCache = new Dictionary<object, string>(); |
| | 72 | 62 | | var paramsByBasePath = GroupParametersByBasePath(parameterNames, localMap, basePathCache); |
| | 72 | 63 | | EnsureAllBasePathsForAlwaysPreserve(localMap, alwaysPreserveFields, basePathCache, paramsByBasePath); |
| | | 64 | | |
| | 90 | 65 | | if (paramsByBasePath.Count == 0) return; |
| | | 66 | | |
| | 258 | 67 | | foreach (var (basePathKey, paramsForPath) in paramsByBasePath) |
| | | 68 | | { |
| | 75 | 69 | | ProcessBasePath(extractedPaths, basePathKey, paramsForPath, nodePath, parameterTypes, alwaysPreserveFields); |
| | | 70 | | } |
| | 54 | 71 | | } |
| | | 72 | | |
| | | 73 | | private static Dictionary<string, List<string>> GroupParametersByBasePath( |
| | | 74 | | string[] parameterNames, |
| | | 75 | | Dictionary<string, string[]> localMap, |
| | | 76 | | Dictionary<object, string> basePathCache) |
| | | 77 | | { |
| | 72 | 78 | | var paramsByBasePath = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase); |
| | 360 | 79 | | foreach (var paramName in parameterNames) |
| | | 80 | | { |
| | 108 | 81 | | if (!localMap.TryGetValue(paramName, out var basePath)) continue; |
| | 87 | 82 | | var basePathKey = GetOrAddBasePathKey(basePathCache, basePath); |
| | 87 | 83 | | GetOrAddList(paramsByBasePath, basePathKey).Add(paramName); |
| | | 84 | | } |
| | 72 | 85 | | return paramsByBasePath; |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | private static void EnsureAllBasePathsForAlwaysPreserve( |
| | | 89 | | Dictionary<string, string[]> localMap, |
| | | 90 | | string[]? alwaysPreserveFields, |
| | | 91 | | Dictionary<object, string> basePathCache, |
| | | 92 | | Dictionary<string, List<string>> paramsByBasePath) |
| | | 93 | | { |
| | 114 | 94 | | if (alwaysPreserveFields is not { Length: > 0 }) return; |
| | | 95 | | |
| | 162 | 96 | | foreach (var (_, basePath) in localMap) |
| | | 97 | | { |
| | 51 | 98 | | var basePathKey = GetOrAddBasePathKey(basePathCache, basePath); |
| | 51 | 99 | | if (!paramsByBasePath.ContainsKey(basePathKey)) |
| | | 100 | | { |
| | 3 | 101 | | paramsByBasePath[basePathKey] = new List<string>(); |
| | | 102 | | } |
| | | 103 | | } |
| | 30 | 104 | | } |
| | | 105 | | |
| | | 106 | | private static string GetOrAddBasePathKey(Dictionary<object, string> cache, string[] basePath) |
| | | 107 | | { |
| | 138 | 108 | | if (!cache.TryGetValue(basePath, out var key)) |
| | | 109 | | { |
| | 93 | 110 | | key = string.Join(".", basePath); |
| | 93 | 111 | | cache[basePath] = key; |
| | | 112 | | } |
| | 138 | 113 | | return key; |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | private static List<string> GetOrAddList(Dictionary<string, List<string>> map, string key) |
| | | 117 | | { |
| | 87 | 118 | | if (!map.TryGetValue(key, out var list)) |
| | | 119 | | { |
| | 72 | 120 | | list = new List<string>(); |
| | 72 | 121 | | map[key] = list; |
| | | 122 | | } |
| | 87 | 123 | | return list; |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | private void ProcessBasePath( |
| | | 127 | | HashSet<string> extractedPaths, |
| | | 128 | | string basePathKey, |
| | | 129 | | List<string> paramsForPath, |
| | | 130 | | string nodePath, |
| | | 131 | | Dictionary<string, Type> parameterTypes, |
| | | 132 | | string[]? alwaysPreserveFields) |
| | | 133 | | { |
| | 75 | 134 | | var fullPath = $"{basePathKey}.{nodePath}"; |
| | 75 | 135 | | var nodeField = QueryDefinitionExtensions.NavigatePath(sourceQuery.Definition._fields, fullPath.AsSpan(), out _) |
| | 99 | 136 | | if (nodeField is not { HasFields: true }) return; |
| | | 137 | | |
| | 51 | 138 | | var fieldsToPreserve = CollectFields(extractedPaths, paramsForPath, parameterTypes, alwaysPreserveFields); |
| | 51 | 139 | | PreserveFields(nodeField._children!, fieldsToPreserve, fullPath); |
| | 51 | 140 | | } |
| | | 141 | | |
| | | 142 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 143 | | private HashSet<string> CollectFields( |
| | | 144 | | HashSet<string> extractedPaths, |
| | | 145 | | List<string> paramsForPath, |
| | | 146 | | Dictionary<string, Type> parameterTypes, |
| | | 147 | | string[]? alwaysPreserveFields) |
| | | 148 | | { |
| | 51 | 149 | | var result = new HashSet<string>(StringComparer.OrdinalIgnoreCase); |
| | | 150 | | |
| | 51 | 151 | | if (ShouldExpandWithoutPrefixes(extractedPaths, paramsForPath)) |
| | | 152 | | { |
| | 6 | 153 | | ExpandPerParameterType(extractedPaths, paramsForPath, parameterTypes, result); |
| | | 154 | | } |
| | | 155 | | else |
| | | 156 | | { |
| | 45 | 157 | | FilterPerParameter(extractedPaths, paramsForPath, parameterTypes, result); |
| | | 158 | | } |
| | | 159 | | |
| | 51 | 160 | | AppendAlwaysPreserveFields(alwaysPreserveFields, result); |
| | 51 | 161 | | return result; |
| | | 162 | | } |
| | | 163 | | |
| | | 164 | | /// <summary> |
| | | 165 | | /// True when the extracted paths carry no parameter-name prefix AND there are multiple |
| | | 166 | | /// parameters in scope — falls into the "expand per type" branch instead of per-parameter |
| | | 167 | | /// filtering. |
| | | 168 | | /// </summary> |
| | | 169 | | private static bool ShouldExpandWithoutPrefixes(HashSet<string> extractedPaths, List<string> paramsForPath) |
| | | 170 | | { |
| | 90 | 171 | | if (paramsForPath.Count <= 1) return false; |
| | | 172 | | |
| | | 173 | | // Manual short-circuit loop — LINQ allocations would be heavier on the hot path. |
| | | 174 | | #pragma warning disable S3267 |
| | 54 | 175 | | foreach (var p in extractedPaths) |
| | | 176 | | { |
| | 90 | 177 | | foreach (var param in paramsForPath) |
| | | 178 | | { |
| | 30 | 179 | | if (p.StartsWith(param + ".", StringComparison.OrdinalIgnoreCase)) |
| | | 180 | | { |
| | 6 | 181 | | return false; |
| | | 182 | | } |
| | | 183 | | } |
| | | 184 | | } |
| | | 185 | | #pragma warning restore S3267 |
| | 6 | 186 | | return true; |
| | 6 | 187 | | } |
| | | 188 | | |
| | | 189 | | private static void ExpandPerParameterType( |
| | | 190 | | HashSet<string> extractedPaths, |
| | | 191 | | List<string> paramsForPath, |
| | | 192 | | Dictionary<string, Type> parameterTypes, |
| | | 193 | | HashSet<string> result) |
| | | 194 | | { |
| | 36 | 195 | | foreach (var paramName in paramsForPath) |
| | | 196 | | { |
| | 12 | 197 | | parameterTypes.TryGetValue(paramName, out var specificParameterType); |
| | 12 | 198 | | AddFieldsToPreserve(extractedPaths, null, specificParameterType, result); |
| | | 199 | | } |
| | 6 | 200 | | } |
| | | 201 | | |
| | | 202 | | private static void FilterPerParameter( |
| | | 203 | | HashSet<string> extractedPaths, |
| | | 204 | | List<string> paramsForPath, |
| | | 205 | | Dictionary<string, Type> parameterTypes, |
| | | 206 | | HashSet<string> result) |
| | | 207 | | { |
| | 198 | 208 | | foreach (var paramName in paramsForPath) |
| | | 209 | | { |
| | 54 | 210 | | var parameterPaths = SelectPathsForParameter(extractedPaths, paramName, paramsForPath.Count == 1); |
| | 54 | 211 | | if (parameterPaths == null) continue; |
| | | 212 | | |
| | 54 | 213 | | parameterTypes.TryGetValue(paramName, out var specificParameterType); |
| | 54 | 214 | | AddFieldsToPreserve(parameterPaths, paramName, specificParameterType, result); |
| | | 215 | | } |
| | 45 | 216 | | } |
| | | 217 | | |
| | | 218 | | /// <summary> |
| | | 219 | | /// Returns the subset of <paramref name="extractedPaths"/> that target <paramref name="paramName"/>. |
| | | 220 | | /// Falls back to the full set when no prefixed paths match AND this is a single-parameter lambda |
| | | 221 | | /// (so an unprefixed expression like <c>x => x.field</c> still preserves something). |
| | | 222 | | /// </summary> |
| | | 223 | | private static HashSet<string>? SelectPathsForParameter( |
| | | 224 | | HashSet<string> extractedPaths, |
| | | 225 | | string paramName, |
| | | 226 | | bool singleParameterFallback) |
| | | 227 | | { |
| | 54 | 228 | | var parameterPaths = CollectParameterPrefixedPaths(extractedPaths, paramName); |
| | 54 | 229 | | if ((parameterPaths is null || parameterPaths.Count == 0) |
| | 54 | 230 | | && extractedPaths.Count > 0 |
| | 54 | 231 | | && singleParameterFallback) |
| | | 232 | | { |
| | 27 | 233 | | return extractedPaths; |
| | | 234 | | } |
| | 27 | 235 | | return parameterPaths; |
| | | 236 | | } |
| | | 237 | | |
| | | 238 | | // Manual loop with lazy init beats LINQ Where().ToHashSet() when no paths match. |
| | | 239 | | #pragma warning disable S3267 |
| | | 240 | | private static HashSet<string>? CollectParameterPrefixedPaths(HashSet<string> extractedPaths, string paramName) |
| | | 241 | | { |
| | 54 | 242 | | HashSet<string>? parameterPaths = null; |
| | 54 | 243 | | var prefix = paramName + "."; |
| | 318 | 244 | | foreach (var p in extractedPaths) |
| | | 245 | | { |
| | 105 | 246 | | if (!IsParameterMatch(p, paramName, prefix)) continue; |
| | 27 | 247 | | parameterPaths ??= new HashSet<string>(StringComparer.OrdinalIgnoreCase); |
| | 27 | 248 | | parameterPaths.Add(p); |
| | | 249 | | } |
| | 54 | 250 | | return parameterPaths; |
| | | 251 | | } |
| | | 252 | | |
| | | 253 | | private static bool IsParameterMatch(string path, string paramName, string prefix) |
| | | 254 | | { |
| | 105 | 255 | | if (string.Equals(path, paramName, StringComparison.OrdinalIgnoreCase)) return true; |
| | 105 | 256 | | return path.StartsWith(prefix, StringComparison.OrdinalIgnoreCase); |
| | | 257 | | } |
| | | 258 | | #pragma warning restore S3267 |
| | | 259 | | |
| | | 260 | | private static void AppendAlwaysPreserveFields(string[]? alwaysPreserveFields, HashSet<string> result) |
| | | 261 | | { |
| | 72 | 262 | | if (alwaysPreserveFields == null) return; |
| | 120 | 263 | | foreach (var field in alwaysPreserveFields) |
| | | 264 | | { |
| | 30 | 265 | | result.Add(field); |
| | | 266 | | } |
| | 30 | 267 | | } |
| | | 268 | | |
| | | 269 | | private static void AddFieldsToPreserve( |
| | | 270 | | HashSet<string> extractedPaths, |
| | | 271 | | string? paramName, |
| | | 272 | | Type? parameterType, |
| | | 273 | | HashSet<string> result) |
| | | 274 | | { |
| | 138 | 275 | | var hasOnlyParameterName = ExpandPathsIntoResult(extractedPaths, paramName, parameterType, result); |
| | | 276 | | |
| | 138 | 277 | | if (result.Count > 1) |
| | | 278 | | { |
| | 48 | 279 | | ApplyMostSpecificWins(result); |
| | | 280 | | } |
| | | 281 | | |
| | 138 | 282 | | if (hasOnlyParameterName && result.Count == 0 && parameterType != null) |
| | | 283 | | { |
| | 6 | 284 | | AddAllPropertiesAsFallback(parameterType, result); |
| | | 285 | | } |
| | 138 | 286 | | } |
| | | 287 | | |
| | | 288 | | /// <summary> |
| | | 289 | | /// Walks <paramref name="extractedPaths"/> and adds the (parameter-prefix-stripped, navigation-expanded) |
| | | 290 | | /// fields to <paramref name="result"/>. Returns true when at least one path was just the parameter |
| | | 291 | | /// name itself, signalling the "greedy preserve everything" fallback below. |
| | | 292 | | /// </summary> |
| | | 293 | | private static bool ExpandPathsIntoResult( |
| | | 294 | | HashSet<string> extractedPaths, |
| | | 295 | | string? paramName, |
| | | 296 | | Type? parameterType, |
| | | 297 | | HashSet<string> result) |
| | | 298 | | { |
| | 138 | 299 | | var hasOnlyParameterName = false; |
| | 624 | 300 | | foreach (var path in extractedPaths) |
| | | 301 | | { |
| | 174 | 302 | | if (IsBareParameterReference(path, paramName)) |
| | | 303 | | { |
| | 6 | 304 | | hasOnlyParameterName = true; |
| | 6 | 305 | | continue; |
| | | 306 | | } |
| | | 307 | | |
| | 168 | 308 | | var field = StripParameterPrefix(path, paramName); |
| | 168 | 309 | | if (string.IsNullOrEmpty(field)) continue; |
| | | 310 | | |
| | 732 | 311 | | foreach (var expandedField in NavigationPropertyExpander.ExpandNavigationProperty(field, parameterType)) |
| | | 312 | | { |
| | 198 | 313 | | result.Add(expandedField); |
| | | 314 | | } |
| | | 315 | | } |
| | 138 | 316 | | return hasOnlyParameterName; |
| | | 317 | | } |
| | | 318 | | |
| | | 319 | | private static bool IsBareParameterReference(string path, string? paramName) |
| | 174 | 320 | | => !string.IsNullOrEmpty(paramName) |
| | 174 | 321 | | && string.Equals(path, paramName, StringComparison.OrdinalIgnoreCase); |
| | | 322 | | |
| | | 323 | | private static string StripParameterPrefix(string path, string? paramName) |
| | 168 | 324 | | => !string.IsNullOrEmpty(paramName) |
| | 168 | 325 | | && path.StartsWith(paramName + ".", StringComparison.OrdinalIgnoreCase) |
| | 168 | 326 | | ? path.Substring(paramName.Length + 1) |
| | 168 | 327 | | : path; |
| | | 328 | | |
| | | 329 | | /// <summary>Removes broader paths from <paramref name="result"/> when a more-specific child path exists.</summary> |
| | | 330 | | private static void ApplyMostSpecificWins(HashSet<string> result) |
| | | 331 | | { |
| | 177 | 332 | | result.RemoveWhere(path => HasMoreSpecificDescendant(path, result)); |
| | 48 | 333 | | } |
| | | 334 | | |
| | | 335 | | private static bool HasMoreSpecificDescendant(string path, HashSet<string> all) |
| | | 336 | | { |
| | 129 | 337 | | var prefix = path + "."; |
| | 1002 | 338 | | foreach (var other in all) |
| | | 339 | | { |
| | 375 | 340 | | if (other.Length > path.Length && other.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)) |
| | 6 | 341 | | return true; |
| | | 342 | | } |
| | 123 | 343 | | return false; |
| | 6 | 344 | | } |
| | | 345 | | |
| | | 346 | | private static void AddAllPropertiesAsFallback(Type parameterType, HashSet<string> result) |
| | | 347 | | { |
| | 54 | 348 | | foreach (var prop in parameterType.GetProperties(BindingFlags.Public | BindingFlags.Instance)) |
| | | 349 | | { |
| | 21 | 350 | | result.Add(prop.Name); |
| | | 351 | | } |
| | 6 | 352 | | } |
| | | 353 | | |
| | | 354 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 355 | | private void PreserveFields( |
| | | 356 | | IReadOnlyDictionary<string, FieldDefinition> nodeFields, |
| | | 357 | | HashSet<string> fieldsToPreserve, |
| | | 358 | | string basePathStr) |
| | | 359 | | { |
| | 342 | 360 | | foreach (var field in fieldsToPreserve) |
| | | 361 | | { |
| | | 362 | | // Fast path: nested field |
| | 120 | 363 | | if (field.Contains('.')) |
| | | 364 | | { |
| | 36 | 365 | | PreserveNestedField(nodeFields, field, basePathStr); |
| | 36 | 366 | | continue; |
| | | 367 | | } |
| | | 368 | | |
| | | 369 | | // Fast path: direct match |
| | 84 | 370 | | var match = PreserveExtensions.FindFieldByNameOrAlias(nodeFields, field.AsSpan()); |
| | 84 | 371 | | if (match.HasValue) |
| | | 372 | | { |
| | 66 | 373 | | PreserveMatchedField(match.Value, basePathStr); |
| | 66 | 374 | | continue; |
| | | 375 | | } |
| | | 376 | | |
| | | 377 | | // Slow path: recursive search |
| | 18 | 378 | | PreserveRecursiveMatches(nodeFields, field, basePathStr); |
| | | 379 | | } |
| | 51 | 380 | | } |
| | | 381 | | |
| | | 382 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 383 | | private void PreserveNestedField(IReadOnlyDictionary<string, FieldDefinition> nodeFields, string field, string baseP |
| | | 384 | | { |
| | 36 | 385 | | if (QueryDefinitionExtensions.NavigatePath(nodeFields, field.AsSpan(), out var resolvedPath, basePathStr) != nul |
| | | 386 | | { |
| | 33 | 387 | | preserveCallback(resolvedPath!); |
| | | 388 | | } |
| | 36 | 389 | | } |
| | | 390 | | |
| | | 391 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 392 | | private void PreserveMatchedField(KeyValuePair<string, FieldDefinition> match, string basePathStr) |
| | | 393 | | { |
| | 66 | 394 | | var matchingKey = match.Key; |
| | | 395 | | |
| | | 396 | | // Fast path: leaf field |
| | 66 | 397 | | if (!match.Value.HasFields) |
| | | 398 | | { |
| | 63 | 399 | | preserveCallback($"{basePathStr}.{matchingKey}"); |
| | 63 | 400 | | return; |
| | | 401 | | } |
| | | 402 | | |
| | | 403 | | // Object field: preserve all sub-fields using fast span iteration |
| | 3 | 404 | | var objectPath = $"{basePathStr}.{matchingKey}"; |
| | 18 | 405 | | foreach (var child in match.Value._children!.AsSpan()) |
| | | 406 | | { |
| | 6 | 407 | | preserveCallback($"{objectPath}.{child.Name}"); |
| | | 408 | | } |
| | 3 | 409 | | } |
| | | 410 | | |
| | | 411 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 412 | | private void PreserveRecursiveMatches(IReadOnlyDictionary<string, FieldDefinition> nodeFields, string field, string |
| | | 413 | | { |
| | 18 | 414 | | var recursiveMatches = QueryDefinitionExtensions.FindFieldRecursively(nodeFields, field, ""); |
| | 72 | 415 | | foreach (var recursivePath in recursiveMatches) |
| | | 416 | | { |
| | 18 | 417 | | preserveCallback($"{basePathStr}.{recursivePath}"); |
| | | 418 | | } |
| | 18 | 419 | | } |
| | | 420 | | |
| | | 421 | | private void PreserveWithGetPathTo( |
| | | 422 | | HashSet<string> extractedPaths, |
| | | 423 | | string? paramName, |
| | | 424 | | string nodePath, |
| | | 425 | | Type? parameterType) |
| | | 426 | | { |
| | 72 | 427 | | var fieldsToPreserve = new HashSet<string>(StringComparer.OrdinalIgnoreCase); |
| | 72 | 428 | | AddFieldsToPreserve(extractedPaths, paramName, parameterType, fieldsToPreserve); |
| | | 429 | | |
| | 72 | 430 | | var lastSegment = LastSegmentOf(nodePath); |
| | 276 | 431 | | foreach (var rootField in sourceQuery.Definition.Fields.Values) |
| | | 432 | | { |
| | 66 | 433 | | PreserveFromRoot(rootField, nodePath, lastSegment, fieldsToPreserve); |
| | | 434 | | } |
| | 72 | 435 | | } |
| | | 436 | | |
| | | 437 | | private static string LastSegmentOf(string nodePath) |
| | | 438 | | { |
| | 72 | 439 | | var nodePathSpan = nodePath.AsSpan(); |
| | 72 | 440 | | var lastSegmentStart = nodePathSpan.LastIndexOf('.'); |
| | 72 | 441 | | return (lastSegmentStart >= 0 ? nodePathSpan[(lastSegmentStart + 1)..] : nodePathSpan).ToString(); |
| | | 442 | | } |
| | | 443 | | |
| | | 444 | | private void PreserveFromRoot(FieldDefinition rootField, string nodePath, string lastSegment, HashSet<string> fields |
| | | 445 | | { |
| | 66 | 446 | | var pathToNode = sourceQuery.GetPathTo(rootField.Alias ?? rootField.Name, nodePath); |
| | 66 | 447 | | if (pathToNode.Length == 0) return; |
| | | 448 | | |
| | 66 | 449 | | var fullNodePath = $"{string.Join(".", pathToNode)}.{lastSegment}"; |
| | 66 | 450 | | var nodeField = QueryDefinitionExtensions.NavigatePath(sourceQuery.Definition._fields, fullNodePath.AsSpan(), ou |
| | 66 | 451 | | if (nodeField is null) return; |
| | 66 | 452 | | if (!nodeField.HasFields) return; |
| | | 453 | | |
| | 348 | 454 | | foreach (var field in fieldsToPreserve) |
| | | 455 | | { |
| | 108 | 456 | | if (QueryDefinitionExtensions.NavigatePath(nodeField.Fields, field.AsSpan(), out var resolvedPath, fullNodeP |
| | | 457 | | { |
| | 96 | 458 | | preserveCallback(resolvedPath!); |
| | | 459 | | } |
| | | 460 | | } |
| | 66 | 461 | | } |
| | | 462 | | |
| | | 463 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 464 | | private static string[]? GetParameterNames(Expression expression) |
| | | 465 | | { |
| | 144 | 466 | | if (expression is not LambdaExpression { Parameters.Count: > 0 } lambda) |
| | 3 | 467 | | return null; |
| | | 468 | | |
| | | 469 | | // Lambdas authored in C# always carry parameter names. The BCL allows null names via |
| | | 470 | | // Expression.Parameter(type, null) but those don't reach here through the public API. |
| | | 471 | | #pragma warning disable S3267 |
| | 141 | 472 | | var names = new string[lambda.Parameters.Count]; |
| | 636 | 473 | | for (int i = 0; i < lambda.Parameters.Count; i++) |
| | | 474 | | { |
| | 177 | 475 | | names[i] = lambda.Parameters[i].Name!; |
| | | 476 | | } |
| | | 477 | | #pragma warning restore S3267 |
| | 141 | 478 | | return names; |
| | | 479 | | } |
| | | 480 | | |
| | | 481 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 482 | | private static Dictionary<string, Type> GetParameterTypes(Expression expression) |
| | | 483 | | { |
| | 144 | 484 | | if (expression is not LambdaExpression lambda || lambda.Parameters.Count == 0) |
| | 3 | 485 | | return new Dictionary<string, Type>(StringComparer.OrdinalIgnoreCase); |
| | | 486 | | |
| | 141 | 487 | | var dict = new Dictionary<string, Type>(lambda.Parameters.Count, StringComparer.OrdinalIgnoreCase); |
| | 636 | 488 | | foreach (var p in lambda.Parameters) |
| | | 489 | | { |
| | 177 | 490 | | if (p.Name != null) |
| | 177 | 491 | | dict[p.Name] = p.Type; |
| | | 492 | | } |
| | | 493 | | |
| | 141 | 494 | | return dict; |
| | | 495 | | } |
| | | 496 | | } |