forked from CirrusRedOrg/EntityFrameworkCore.Jet
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathExpressionVisitorExtensions.cs
More file actions
130 lines (117 loc) · 4.37 KB
/
Copy pathExpressionVisitorExtensions.cs
File metadata and controls
130 lines (117 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using Microsoft.EntityFrameworkCore.Diagnostics;
using System.Collections.Generic;
using System.Diagnostics;
// ReSharper disable once CheckNamespace
using System.Runtime.CompilerServices;
namespace System.Linq.Expressions;
#nullable enable
[DebuggerStepThrough]
internal static class ExpressionVisitorExtensions
{
/// <summary>
/// Dispatches the list of expressions to one of the more specialized visit methods in this class.
/// </summary>
/// <param name="visitor">The expression visitor.</param>
/// <param name="nodes">The expressions to visit.</param>
/// <returns>
/// The modified expression list, if any of the elements were modified; otherwise, returns the original expression list.
/// </returns>
public static IReadOnlyList<Expression> Visit(this ExpressionVisitor visitor, IReadOnlyList<Expression> nodes)
{
Expression[]? newNodes = null;
for (int i = 0, n = nodes.Count; i < n; i++)
{
var node = visitor.Visit(nodes[i]);
if (newNodes is not null)
{
newNodes[i] = node;
}
else if (!ReferenceEquals(node, nodes[i]))
{
newNodes = new Expression[n];
for (var j = 0; j < i; j++)
{
newNodes[j] = nodes[j];
}
newNodes[i] = node;
}
}
return newNodes ?? nodes;
}
/// <summary>
/// Visits an expression, casting the result back to the original expression type.
/// </summary>
/// <typeparam name="T">The type of the expression.</typeparam>
/// <param name="visitor">The expression visitor.</param>
/// <param name="nodes">The expression to visit.</param>
/// <param name="callerName">The name of the calling method; used to report to report a better error message.</param>
/// <returns>
/// The modified expression, if it or any subexpression was modified; otherwise, returns the original expression.
/// </returns>
/// <exception cref="InvalidOperationException">The visit method for this node returned a different type.</exception>
public static IReadOnlyList<T> VisitAndConvert<T>(
this ExpressionVisitor visitor,
IReadOnlyList<T> nodes,
[CallerMemberName] string? callerName = null)
where T : Expression
{
T[]? newNodes = null;
for (int i = 0, n = nodes.Count; i < n; i++)
{
if (visitor.Visit(nodes[i]) is not T node)
{
throw new InvalidOperationException(CoreStrings.MustRewriteToSameNode(callerName, typeof(T).Name));
}
if (newNodes is not null)
{
newNodes[i] = node;
}
else if (!ReferenceEquals(node, nodes[i]))
{
newNodes = new T[n];
for (var j = 0; j < i; j++)
{
newNodes[j] = nodes[j];
}
newNodes[i] = node;
}
}
return newNodes ?? nodes;
}
/// <summary>
/// Visits all nodes in the collection using a specified element visitor.
/// </summary>
/// <typeparam name="T">The type of the nodes.</typeparam>
/// <param name="visitor">The expression visitor.</param>
/// <param name="nodes">The nodes to visit.</param>
/// <param name="elementVisitor">
/// A delegate that visits a single element,
/// optionally replacing it with a new element.
/// </param>
/// <returns>
/// The modified node list, if any of the elements were modified;
/// otherwise, returns the original node list.
/// </returns>
public static IReadOnlyList<T> Visit<T>(this ExpressionVisitor visitor, IReadOnlyList<T> nodes, Func<T, T> elementVisitor)
{
T[]? newNodes = null;
for (int i = 0, n = nodes.Count; i < n; i++)
{
var node = elementVisitor(nodes[i]);
if (newNodes is not null)
{
newNodes[i] = node;
}
else if (!ReferenceEquals(node, nodes[i]))
{
newNodes = new T[n];
for (var j = 0; j < i; j++)
{
newNodes[j] = nodes[j];
}
newNodes[i] = node;
}
}
return newNodes ?? nodes;
}
}