| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | using NGql.Core.Abstractions; |
| | | 3 | | using NGql.Core.Builders; |
| | | 4 | | using NGql.Core.Extensions; |
| | | 5 | | |
| | | 6 | | namespace NGql.Core.Features; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Extension methods for preserving specific QueryBuilder fields based on specified paths. |
| | | 10 | | /// </summary> |
| | | 11 | | internal static class PreserveExtensions |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Preserves only the fields that match or are descendants of the specified paths. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <param name="query">The query builder to preserve fields from.</param> |
| | | 17 | | /// <param name="fieldPaths">The field paths to preserve.</param> |
| | | 18 | | /// <returns>A new QueryBuilder instance with only the preserved fields.</returns> |
| | | 19 | | public static QueryBuilder Preserve(this QueryBuilder query, params string[]? fieldPaths) |
| | | 20 | | { |
| | 306 | 21 | | if (fieldPaths == null || fieldPaths.Length == 0) |
| | 6 | 22 | | return query; |
| | | 23 | | |
| | 300 | 24 | | var newQuery = QueryBuilder.CreateDefaultBuilder(query.Definition.Name); |
| | | 25 | | |
| | | 26 | | // Copy merging strategy and metadata |
| | 300 | 27 | | newQuery.Definition.MergingStrategy = query.Definition.MergingStrategy; |
| | 300 | 28 | | if (query.Definition._metadata != null) |
| | | 29 | | { |
| | 60 | 30 | | foreach (var metadata in query.Definition.Metadata) |
| | | 31 | | { |
| | 18 | 32 | | newQuery.Definition.Metadata[metadata.Key] = metadata.Value; |
| | | 33 | | } |
| | | 34 | | } |
| | | 35 | | |
| | 1632 | 36 | | foreach (var targetPath in fieldPaths) |
| | | 37 | | { |
| | 516 | 38 | | ExtractMatchingFields(query.Definition.Fields, newQuery.Definition.Fields, targetPath.AsSpan()); |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | // Extract variables from all preserved fields |
| | 300 | 42 | | ExtractVariablesFromFields(newQuery.Definition.Fields, newQuery.Definition.Variables); |
| | | 43 | | |
| | 300 | 44 | | return newQuery; |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | private static void ExtractMatchingFields(Dictionary<string, FieldDefinition> sourceFields, |
| | | 48 | | Dictionary<string, FieldDefinition> targetFields, ReadOnlySpan<char> path) |
| | | 49 | | { |
| | 516 | 50 | | var isLeafPath = SplitFirstSegment(path, out var currentSegment, out var remainingPath); |
| | | 51 | | |
| | 516 | 52 | | var match = FindFieldByNameOrAlias(sourceFields, currentSegment); |
| | 540 | 53 | | if (!match.HasValue) return; |
| | | 54 | | |
| | 492 | 55 | | var (fieldKey, fieldDef) = match.Value; |
| | | 56 | | |
| | 492 | 57 | | if (isLeafPath) |
| | | 58 | | { |
| | 51 | 59 | | targetFields[fieldKey] = fieldDef; |
| | 51 | 60 | | return; |
| | | 61 | | } |
| | | 62 | | |
| | 447 | 63 | | if (fieldDef._children is null) return; |
| | | 64 | | |
| | 435 | 65 | | var existing = GetOrAddTargetChild(targetFields, fieldKey, fieldDef); |
| | 435 | 66 | | ExtractMatchingFields(fieldDef._children, existing._children!, remainingPath); |
| | 435 | 67 | | } |
| | | 68 | | |
| | | 69 | | private static void ExtractMatchingFields(FieldChildren sourceFields, FieldChildren targetFields, ReadOnlySpan<char> |
| | | 70 | | { |
| | 1248 | 71 | | var isLeafPath = SplitFirstSegment(path, out var currentSegment, out var remainingPath); |
| | | 72 | | |
| | 1257 | 73 | | if (!TryFindByNameOrAlias(sourceFields, currentSegment, out var fieldKey, out var fieldDef)) return; |
| | | 74 | | |
| | 1239 | 75 | | if (isLeafPath) |
| | | 76 | | { |
| | 423 | 77 | | targetFields.Set(fieldKey, fieldDef); |
| | 423 | 78 | | return; |
| | | 79 | | } |
| | | 80 | | |
| | 819 | 81 | | if (fieldDef._children is null) return; |
| | | 82 | | |
| | 813 | 83 | | var targetChild = GetOrAddTargetChild(targetFields, fieldKey, fieldDef); |
| | 813 | 84 | | ExtractMatchingFields(fieldDef._children, targetChild._children!, remainingPath); |
| | 813 | 85 | | } |
| | | 86 | | |
| | | 87 | | private static bool SplitFirstSegment(ReadOnlySpan<char> path, out ReadOnlySpan<char> current, out ReadOnlySpan<char |
| | | 88 | | { |
| | 1764 | 89 | | var dot = path.IndexOf('.'); |
| | 1764 | 90 | | if (dot < 0) |
| | | 91 | | { |
| | 489 | 92 | | current = path; |
| | 489 | 93 | | remaining = ReadOnlySpan<char>.Empty; |
| | 489 | 94 | | return true; |
| | | 95 | | } |
| | 1275 | 96 | | current = path[..dot]; |
| | 1275 | 97 | | remaining = path[(dot + 1)..]; |
| | 1275 | 98 | | return false; |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | private static bool TryFindByNameOrAlias(FieldChildren children, ReadOnlySpan<char> nameOrAlias, out string key, out |
| | | 102 | | { |
| | 4617 | 103 | | foreach (var f in children.AsSpan()) |
| | | 104 | | { |
| | 1680 | 105 | | if (f.Name.AsSpan().Equals(nameOrAlias, StringComparison.OrdinalIgnoreCase) |
| | 1680 | 106 | | || (f.Alias is { Length: > 0 } alias && alias.AsSpan().Equals(nameOrAlias, StringComparison.OrdinalIgnor |
| | | 107 | | { |
| | 1239 | 108 | | key = f.Name; |
| | 1239 | 109 | | field = f; |
| | 1239 | 110 | | return true; |
| | | 111 | | } |
| | | 112 | | } |
| | 9 | 113 | | key = string.Empty; |
| | 9 | 114 | | field = null!; |
| | 9 | 115 | | return false; |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | private static FieldDefinition GetOrAddTargetChild(Dictionary<string, FieldDefinition> target, string fieldKey, Fiel |
| | | 119 | | { |
| | 435 | 120 | | if (!target.TryGetValue(fieldKey, out var existing)) |
| | | 121 | | { |
| | 249 | 122 | | existing = CloneFieldWithoutChildren(source); |
| | 249 | 123 | | target[fieldKey] = existing; |
| | | 124 | | } |
| | 435 | 125 | | existing._children ??= new FieldChildren(); |
| | 435 | 126 | | return existing; |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | private static FieldDefinition GetOrAddTargetChild(FieldChildren targetFields, string fieldKey, FieldDefinition sour |
| | | 130 | | { |
| | 813 | 131 | | if (!targetFields.TryGetValue(fieldKey, out var existing) || existing is null) |
| | | 132 | | { |
| | 447 | 133 | | existing = CloneFieldWithoutChildren(source); |
| | 447 | 134 | | targetFields.Set(fieldKey, existing); |
| | | 135 | | } |
| | 813 | 136 | | existing._children ??= new FieldChildren(); |
| | 813 | 137 | | return existing; |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | /// <summary> |
| | | 141 | | /// Creates a shallow clone of a field without its children, preserving arguments and metadata. |
| | | 142 | | /// </summary> |
| | | 143 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 144 | | private static FieldDefinition CloneFieldWithoutChildren(FieldDefinition source) |
| | | 145 | | { |
| | | 146 | | // FieldDefinition._type is always set by every constructor; the null-coalesce was |
| | | 147 | | // a defensive guard for the rare `with { Type = null }` path and never fires here. |
| | 696 | 148 | | var cloned = new FieldDefinition( |
| | 696 | 149 | | source.Name, |
| | 696 | 150 | | source._type!, |
| | 696 | 151 | | source.Alias, |
| | 696 | 152 | | source._arguments) |
| | 696 | 153 | | { |
| | 696 | 154 | | Path = source.Path |
| | 696 | 155 | | }; |
| | | 156 | | |
| | 696 | 157 | | cloned.IsNeverMerge = source.IsNeverMerge; |
| | | 158 | | |
| | 696 | 159 | | if (source._metadata != null) |
| | | 160 | | { |
| | 18 | 161 | | foreach (var meta in source._metadata) |
| | | 162 | | { |
| | 6 | 163 | | cloned.Metadata[meta.Key] = meta.Value; |
| | | 164 | | } |
| | | 165 | | } |
| | | 166 | | |
| | 696 | 167 | | return cloned; |
| | | 168 | | } |
| | | 169 | | |
| | | 170 | | // All call sites pre-check the fields collection for null (NavigatePath, PreserveAtPath, |
| | | 171 | | // QueryDefinitionExtensions.NavigatePath); the parameter could safely be tightened but |
| | | 172 | | // is kept nullable to preserve the existing internal API signature. |
| | | 173 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 174 | | internal static KeyValuePair<string, FieldDefinition>? FindFieldByNameOrAlias(IReadOnlyDictionary<string, FieldDefin |
| | | 175 | | { |
| | 4617 | 176 | | foreach (var kvp in fields!) |
| | | 177 | | { |
| | 1638 | 178 | | if (MatchesKeyNameOrAlias(kvp, nameOrAlias)) |
| | | 179 | | { |
| | 1191 | 180 | | return kvp; |
| | | 181 | | } |
| | | 182 | | } |
| | | 183 | | |
| | 75 | 184 | | return null; |
| | 1191 | 185 | | } |
| | | 186 | | |
| | | 187 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 188 | | private static bool MatchesKeyNameOrAlias(KeyValuePair<string, FieldDefinition> kvp, ReadOnlySpan<char> nameOrAlias) |
| | 1638 | 189 | | => nameOrAlias.Equals(kvp.Key.AsSpan(), StringComparison.OrdinalIgnoreCase) |
| | 1638 | 190 | | || nameOrAlias.Equals(kvp.Value.Name.AsSpan(), StringComparison.OrdinalIgnoreCase) |
| | 1638 | 191 | | || (!string.IsNullOrEmpty(kvp.Value.Alias) && nameOrAlias.Equals(kvp.Value.Alias.AsSpan(), StringComparison.Ordi |
| | | 192 | | |
| | | 193 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 194 | | private static void ExtractVariablesFromFields(FieldChildren children, SortedSet<Variable> variables) |
| | | 195 | | { |
| | 3582 | 196 | | foreach (var field in children.AsSpan()) |
| | | 197 | | { |
| | 1017 | 198 | | ExtractVariablesFromField(field, variables); |
| | | 199 | | } |
| | 774 | 200 | | } |
| | | 201 | | |
| | | 202 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 203 | | private static void ExtractVariablesFromFields(Dictionary<string, FieldDefinition> fields, SortedSet<Variable> varia |
| | | 204 | | { |
| | 1200 | 205 | | foreach (var field in fields.Values) |
| | | 206 | | { |
| | 300 | 207 | | ExtractVariablesFromField(field, variables); |
| | | 208 | | } |
| | 300 | 209 | | } |
| | | 210 | | |
| | | 211 | | private static void ExtractVariablesFromField(FieldDefinition field, SortedSet<Variable> variables) |
| | | 212 | | { |
| | 1317 | 213 | | if (field._arguments?.Count > 0) |
| | | 214 | | { |
| | 6 | 215 | | Helpers.ExtractVariablesFromValue(field._arguments, variables); |
| | | 216 | | } |
| | 1317 | 217 | | if (field._children is { Count: > 0 }) |
| | | 218 | | { |
| | 774 | 219 | | ExtractVariablesFromFields(field._children, variables); |
| | | 220 | | } |
| | 1317 | 221 | | } |
| | | 222 | | } |