| | | 1 | | using System.Linq.Expressions; |
| | | 2 | | using NGql.Core.Extensions; |
| | | 3 | | using NGql.Core.Features; |
| | | 4 | | |
| | | 5 | | namespace NGql.Core.Builders; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Builds a derived <see cref="QueryBuilder"/> that contains only a chosen subset of an |
| | | 9 | | /// existing query's fields. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <remarks> |
| | | 12 | | /// Use <see cref="Create(QueryBuilder)"/> to start, accumulate paths via <see cref="Preserve(string[])"/> |
| | | 13 | | /// or <see cref="PreserveFromExpression{T}(Expression{Func{T, bool}}, string?)"/>, then call |
| | | 14 | | /// <see cref="Build"/> to materialize the trimmed query. The source builder is never mutated. |
| | | 15 | | /// <para> |
| | | 16 | | /// Typical use: role-based field filtering — start from a "full" query and emit a smaller |
| | | 17 | | /// projection per caller without re-building from scratch. |
| | | 18 | | /// </para> |
| | | 19 | | /// </remarks> |
| | | 20 | | public sealed class PreservationBuilder |
| | | 21 | | { |
| | | 22 | | private readonly QueryBuilder _sourceQuery; |
| | | 23 | | private readonly HashSet<string> _pathsToPreserve; |
| | | 24 | | |
| | 303 | 25 | | private PreservationBuilder(QueryBuilder sourceQuery) |
| | | 26 | | { |
| | 303 | 27 | | _sourceQuery = sourceQuery ?? throw new ArgumentNullException(nameof(sourceQuery)); |
| | 300 | 28 | | _pathsToPreserve = new HashSet<string>(StringComparer.OrdinalIgnoreCase); |
| | 300 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <summary>Creates a new <see cref="PreservationBuilder"/> over <paramref name="query"/>.</summary> |
| | | 32 | | /// <param name="query">The source query to project from. Not mutated.</param> |
| | | 33 | | /// <exception cref="ArgumentNullException"><paramref name="query"/> is null.</exception> |
| | 303 | 34 | | public static PreservationBuilder Create(QueryBuilder query) => new(query); |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Adds dotted field paths to the preservation set. When a more-specific child path is |
| | | 38 | | /// added, any previously-added parent paths are dropped automatically (you don't end up |
| | | 39 | | /// preserving both <c>user</c> and <c>user.name</c> — only the more specific wins). |
| | | 40 | | /// </summary> |
| | | 41 | | /// <param name="fieldPaths">Dot-separated field paths to preserve. Null/whitespace entries are skipped.</param> |
| | | 42 | | /// <returns>This builder, for chaining.</returns> |
| | | 43 | | public PreservationBuilder Preserve(params string[]? fieldPaths) |
| | | 44 | | { |
| | 447 | 45 | | if (fieldPaths == null || fieldPaths.Length == 0) return this; |
| | | 46 | | |
| | 2289 | 47 | | foreach (var path in fieldPaths.Where(p => !string.IsNullOrWhiteSpace(p))) |
| | | 48 | | { |
| | | 49 | | // Remove parent paths when adding more specific child |
| | 474 | 50 | | _pathsToPreserve.RemoveWhere(existing => |
| | 807 | 51 | | path.StartsWith(existing + ".", StringComparison.OrdinalIgnoreCase)); |
| | | 52 | | |
| | 474 | 53 | | _pathsToPreserve.Add(path); |
| | | 54 | | } |
| | | 55 | | |
| | 429 | 56 | | return this; |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Preserves <paramref name="fieldPath"/> within the subtree at <paramref name="nodePath"/>. |
| | | 61 | | /// Useful when the same field name appears multiple times in the tree and you want to |
| | | 62 | | /// scope the preservation to one specific parent. |
| | | 63 | | /// </summary> |
| | | 64 | | /// <param name="fieldPath">Field name (or dotted relative path) inside the node.</param> |
| | | 65 | | /// <param name="nodePath">Dotted path identifying which parent node to scope to.</param> |
| | | 66 | | /// <returns>This builder, for chaining.</returns> |
| | | 67 | | public PreservationBuilder PreserveAtPath(string fieldPath, string nodePath) |
| | | 68 | | { |
| | 27 | 69 | | var lastIndex = nodePath.LastIndexOf('.'); |
| | 27 | 70 | | var lastSegment = lastIndex == -1 ? nodePath : nodePath.Substring(lastIndex + 1); |
| | 27 | 71 | | var fieldPathHasDot = fieldPath.Contains('.'); |
| | | 72 | | |
| | 114 | 73 | | foreach (var rootField in _sourceQuery.Definition.Fields.Values) |
| | | 74 | | { |
| | 30 | 75 | | PreserveAtPathForRoot(rootField.Alias ?? rootField.Name, fieldPath, nodePath, lastSegment, fieldPathHasDot); |
| | | 76 | | } |
| | 27 | 77 | | return this; |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | private void PreserveAtPathForRoot(string rootName, string fieldPath, string nodePath, string lastSegment, bool fiel |
| | | 81 | | { |
| | 30 | 82 | | var pathToNode = _sourceQuery.GetPathTo(rootName, nodePath); |
| | 30 | 83 | | if (pathToNode.Length == 0) return; |
| | | 84 | | |
| | 30 | 85 | | var fullNodePath = JoinPath(pathToNode, lastSegment); |
| | 30 | 86 | | var nodeField = QueryDefinitionExtensions.NavigatePath(_sourceQuery.Definition.Fields, fullNodePath.AsSpan(), ou |
| | 33 | 87 | | if (nodeField is null) return; |
| | 33 | 88 | | if (!nodeField.HasFields) return; |
| | | 89 | | |
| | 21 | 90 | | if (fieldPathHasDot) |
| | | 91 | | { |
| | 6 | 92 | | PreserveResolvedNestedPath(nodeField.Fields, fieldPath, fullNodePath); |
| | | 93 | | } |
| | | 94 | | else |
| | | 95 | | { |
| | 15 | 96 | | PreserveDirectMatch(nodeField.Fields, fieldPath, fullNodePath); |
| | | 97 | | } |
| | 15 | 98 | | } |
| | | 99 | | |
| | | 100 | | private void PreserveResolvedNestedPath(IReadOnlyDictionary<string, Abstractions.FieldDefinition> nodeFields, string |
| | | 101 | | { |
| | 6 | 102 | | if (QueryDefinitionExtensions.NavigatePath(nodeFields, fieldPath.AsSpan(), out var resolved, fullNodePath) != nu |
| | 6 | 103 | | && resolved is not null) |
| | | 104 | | { |
| | 3 | 105 | | Preserve(resolved); |
| | | 106 | | } |
| | 6 | 107 | | } |
| | | 108 | | |
| | | 109 | | private void PreserveDirectMatch(IReadOnlyDictionary<string, Abstractions.FieldDefinition> nodeFields, string fieldP |
| | | 110 | | { |
| | 15 | 111 | | var match = PreserveExtensions.FindFieldByNameOrAlias(nodeFields, fieldPath.AsSpan()); |
| | 15 | 112 | | if (match.HasValue) |
| | | 113 | | { |
| | 9 | 114 | | Preserve(string.Concat(fullNodePath, ".", match.Value.Key)); |
| | | 115 | | } |
| | 15 | 116 | | } |
| | | 117 | | |
| | | 118 | | /// <summary> |
| | | 119 | | /// Builds <c>{segments[0]}.{segments[1]}...{segments[N-1]}.{tail}</c> in a single allocation — |
| | | 120 | | /// replaces the prior <c>string.Join + "." + tail</c> two-step. |
| | | 121 | | /// </summary> |
| | | 122 | | private static string JoinPath(string[] segments, string tail) |
| | | 123 | | { |
| | 30 | 124 | | var totalLength = tail.Length + segments.Length; // dots between segments + before tail |
| | 204 | 125 | | for (int i = 0; i < segments.Length; i++) totalLength += segments[i].Length; |
| | | 126 | | |
| | 30 | 127 | | return string.Create(totalLength, (segments, tail), static (span, st) => |
| | 30 | 128 | | { |
| | 30 | 129 | | var pos = 0; |
| | 156 | 130 | | for (int i = 0; i < st.segments.Length; i++) |
| | 30 | 131 | | { |
| | 48 | 132 | | var seg = st.segments[i].AsSpan(); |
| | 48 | 133 | | seg.CopyTo(span[pos..]); |
| | 48 | 134 | | pos += seg.Length; |
| | 48 | 135 | | span[pos++] = '.'; |
| | 30 | 136 | | } |
| | 30 | 137 | | st.tail.AsSpan().CopyTo(span[pos..]); |
| | 60 | 138 | | }); |
| | | 139 | | } |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// Preserves every field touched by the predicate <paramref name="expression"/>. The |
| | | 143 | | /// expression is walked (not executed) to collect member access chains; comparisons, |
| | | 144 | | /// logical operators, ternaries, null-coalescing, and LINQ method calls (<c>Any</c>, |
| | | 145 | | /// <c>Where</c>, <c>First</c>) are all supported. |
| | | 146 | | /// </summary> |
| | | 147 | | /// <typeparam name="T">CLR type whose property hierarchy mirrors the query tree.</typeparam> |
| | | 148 | | /// <param name="expression">Predicate expression whose member access chains identify fields to preserve.</param> |
| | | 149 | | /// <param name="nodePath">Optional dotted path to scope preservation to a specific subtree.</param> |
| | | 150 | | /// <returns>This builder, for chaining.</returns> |
| | | 151 | | public PreservationBuilder PreserveFromExpression<T>(Expression<Func<T, bool>> expression, string? nodePath = null) |
| | 78 | 152 | | => PreserveFromExpressionCore(expression, nodePath, null, typeof(T), null); |
| | | 153 | | |
| | | 154 | | /// <summary> |
| | | 155 | | /// Same as <see cref="PreserveFromExpression{T}(Expression{Func{T, bool}}, string?)"/> |
| | | 156 | | /// but adds a <paramref name="localMap"/> that translates expression parameter names |
| | | 157 | | /// to relative dotted paths inside <paramref name="nodePath"/>'s subtree. Useful when |
| | | 158 | | /// the predicate references multiple sibling fields. |
| | | 159 | | /// </summary> |
| | | 160 | | /// <param name="expression">Predicate expression.</param> |
| | | 161 | | /// <param name="nodePath">Dotted path to scope preservation to.</param> |
| | | 162 | | /// <param name="localMap">Map of expression-parameter name to relative path segments.</param> |
| | | 163 | | /// <returns>This builder, for chaining.</returns> |
| | | 164 | | public PreservationBuilder PreserveFromExpression<T>(Expression<Func<T, bool>> expression, string nodePath, Dictiona |
| | 3 | 165 | | => PreserveFromExpressionCore(expression, nodePath, localMap, typeof(T), null); |
| | | 166 | | |
| | | 167 | | /// <summary> |
| | | 168 | | /// Untyped overload of <see cref="PreserveFromExpression{T}(Expression{Func{T, bool}}, string?)"/>. |
| | | 169 | | /// Useful when the predicate is constructed dynamically (e.g. via DynamicExpresso) and |
| | | 170 | | /// you don't have a compile-time <c>T</c>. |
| | | 171 | | /// </summary> |
| | | 172 | | /// <param name="expression">Lambda expression whose body is walked for member-access chains.</param> |
| | | 173 | | /// <param name="nodePath">Optional dotted path to scope preservation to.</param> |
| | | 174 | | /// <returns>This builder, for chaining.</returns> |
| | | 175 | | public PreservationBuilder PreserveFromExpression(Expression expression, string? nodePath = null) |
| | 9 | 176 | | => PreserveFromExpressionCore(expression, nodePath, null, InferTypeFromLambda(expression), null); |
| | | 177 | | |
| | | 178 | | /// <summary>Untyped overload accepting a parameter-name-to-path <paramref name="localMap"/>.</summary> |
| | | 179 | | /// <param name="expression">Lambda expression.</param> |
| | | 180 | | /// <param name="nodePath">Dotted path to scope preservation to.</param> |
| | | 181 | | /// <param name="localMap">Map of expression-parameter name to relative path segments.</param> |
| | | 182 | | /// <returns>This builder, for chaining.</returns> |
| | | 183 | | public PreservationBuilder PreserveFromExpression(Expression expression, string nodePath, Dictionary<string, string[ |
| | 15 | 184 | | => PreserveFromExpressionCore(expression, nodePath, localMap, InferTypeFromLambda(expression), null); |
| | | 185 | | |
| | | 186 | | /// <summary> |
| | | 187 | | /// Untyped overload that, in addition to expression-derived paths, unconditionally |
| | | 188 | | /// preserves <paramref name="alwaysPreserveFields"/> at <paramref name="nodePath"/> — |
| | | 189 | | /// useful for IDs and tracking columns that callers always want regardless of predicate. |
| | | 190 | | /// </summary> |
| | | 191 | | /// <param name="expression">Lambda expression.</param> |
| | | 192 | | /// <param name="nodePath">Dotted path to scope preservation to.</param> |
| | | 193 | | /// <param name="localMap">Map of expression-parameter name to relative path segments.</param> |
| | | 194 | | /// <param name="alwaysPreserveFields">Field names always added under <paramref name="nodePath"/>.</param> |
| | | 195 | | /// <returns>This builder, for chaining.</returns> |
| | | 196 | | public PreservationBuilder PreserveFromExpression(Expression expression, string nodePath, Dictionary<string, string[ |
| | 27 | 197 | | => PreserveFromExpressionCore(expression, nodePath, localMap, InferTypeFromLambda(expression), alwaysPreserveFie |
| | | 198 | | |
| | | 199 | | /// <summary> |
| | | 200 | | /// Materializes a new <see cref="QueryBuilder"/> containing only the accumulated |
| | | 201 | | /// preserved paths. Returns the source builder unchanged when no paths were added. |
| | | 202 | | /// The source builder is never mutated. |
| | | 203 | | /// </summary> |
| | | 204 | | /// <returns>A new builder containing the preserved subset, or the source if no paths were added.</returns> |
| | | 205 | | public QueryBuilder Build() |
| | 285 | 206 | | => _pathsToPreserve.Count == 0 |
| | 285 | 207 | | ? _sourceQuery |
| | 285 | 208 | | : _sourceQuery.Preserve(_pathsToPreserve.ToArray()); |
| | | 209 | | |
| | | 210 | | private static Type? InferTypeFromLambda(Expression expression) |
| | 51 | 211 | | => expression is LambdaExpression lambda && lambda.Parameters.Count > 0 |
| | 51 | 212 | | ? lambda.Parameters[0].Type |
| | 51 | 213 | | : null; |
| | | 214 | | |
| | | 215 | | private PreservationBuilder PreserveFromExpressionCore( |
| | | 216 | | Expression expression, |
| | | 217 | | string? nodePath, |
| | | 218 | | Dictionary<string, string[]>? localMap, |
| | | 219 | | Type? parameterType, |
| | | 220 | | string[]? alwaysPreserveFields) |
| | | 221 | | { |
| | 363 | 222 | | var processor = new ExpressionPreservationProcessor(_sourceQuery, path => Preserve(path)); |
| | 132 | 223 | | processor.ProcessExpression(expression, nodePath, localMap, parameterType, alwaysPreserveFields); |
| | 132 | 224 | | return this; |
| | | 225 | | } |
| | | 226 | | } |