-
-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathParsingConfig.cs
More file actions
315 lines (282 loc) · 11.7 KB
/
ParsingConfig.cs
File metadata and controls
315 lines (282 loc) · 11.7 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq.Dynamic.Core.Config;
using System.Linq.Dynamic.Core.CustomTypeProviders;
using System.Linq.Dynamic.Core.Parser;
using System.Linq.Dynamic.Core.Util.Cache;
namespace System.Linq.Dynamic.Core;
/// <summary>
/// Configuration class for System.Linq.Dynamic.Core.
/// </summary>
public class ParsingConfig
{
private IDynamicLinqCustomTypeProvider? _customTypeProvider;
private IExpressionPromoter? _expressionPromoter;
private IQueryableAnalyzer? _queryableAnalyzer;
/// <summary>
/// Default ParsingConfig
/// </summary>
public static ParsingConfig Default { get; } = new();
/// <summary>
/// Default ParsingConfig for EntityFramework Core 2.1 and higher
/// </summary>
public static ParsingConfig DefaultEFCore21 { get; } = new()
{
EvaluateGroupByAtDatabase = true
};
/// <summary>
/// Default ParsingConfig for CosmosDb
/// </summary>
public static ParsingConfig DefaultCosmosDb { get; } = new()
{
RenameEmptyParameterExpressionNames = true
};
/// <summary>
/// Defines if the resolution should be case-sensitive for:
/// - fields and properties
/// - (extension) methods
/// - constant expressions ("null", "true", "false")
/// - keywords ("it", "parent", "root")
/// - functions ("as", "cast", "iif", "is", "isnull", "new", "np")
/// - operator aliases ("eq", "equal", "ne", "notequal", "neq", "lt", "LessThan", "le", "LessThanEqual", "gt", "GreaterThan", "ge", "GreaterThanEqual", "and", "AndAlso", "or", "OrElse", "not", "mod")
///
/// Default value is <c>false</c>.
/// </summary>
public bool IsCaseSensitive { get; set; }
/// <summary>
/// Gets or sets the <see cref="IDynamicLinqCustomTypeProvider"/>.
/// </summary>
public IDynamicLinqCustomTypeProvider? CustomTypeProvider
{
get
{
#if !(UAP10_0 || NETSTANDARD)
// Only use DefaultDynamicLinqCustomTypeProvider for full .NET Framework and .NET Core App 2.x and higher.
return _customTypeProvider ??= new DefaultDynamicLinqCustomTypeProvider(this);
#else
return _customTypeProvider;
#endif
}
set
{
_customTypeProvider = value;
}
}
/// <summary>
/// Sets the CustomTypeProvider to <see cref="DefaultDynamicLinqCustomTypeProvider"/>.
/// </summary>
/// <param name="cacheCustomTypes">Defines whether to cache the CustomTypes (including extension methods) which are found in the Application Domain. Default set to <c>true</c>.</param>
public void UseDefaultDynamicLinqCustomTypeProvider(bool cacheCustomTypes = true)
{
_customTypeProvider = new DefaultDynamicLinqCustomTypeProvider(this, cacheCustomTypes);
}
/// <summary>
/// Sets the CustomTypeProvider to <see cref="DefaultDynamicLinqCustomTypeProvider"/>.
/// </summary>
/// <param name="cacheCustomTypes">Defines whether to cache the CustomTypes (including extension methods) which are found in the Application Domain. Default set to <c>true</c>.</param>
/// <param name="additionalTypes">A list of additional types (without the DynamicLinqTypeAttribute annotation) which should also be resolved.</param>
public void UseDefaultDynamicLinqCustomTypeProvider(IList<Type> additionalTypes, bool cacheCustomTypes = true)
{
_customTypeProvider = new DefaultDynamicLinqCustomTypeProvider(this, additionalTypes, cacheCustomTypes);
}
/// <summary>
/// Load additional assemblies from the current domain base directory.
/// Note: only used when full .NET Framework and .NET Core App 2.x and higher.
///
/// Default value is <c>false</c>.
/// </summary>
public bool LoadAdditionalAssembliesFromCurrentDomainBaseDirectory { get; set; }
/// <summary>
/// Gets or sets the <see cref="IExpressionPromoter"/>.
/// </summary>
public IExpressionPromoter ExpressionPromoter
{
get => _expressionPromoter ??= new ExpressionPromoter(this);
set
{
// ReSharper disable once RedundantCheckBeforeAssignment
if (_expressionPromoter != value)
{
_expressionPromoter = value;
}
}
}
/// <summary>
/// Gets or sets the <see cref="IQueryableAnalyzer"/>.
/// </summary>
public IQueryableAnalyzer QueryableAnalyzer
{
get
{
return _queryableAnalyzer ??= new DefaultQueryableAnalyzer();
}
set
{
// ReSharper disable once RedundantCheckBeforeAssignment
if (_queryableAnalyzer != value)
{
_queryableAnalyzer = value;
}
}
}
/// <summary>
/// Determines if the context keywords (it, parent, and root) are valid and usable inside a Dynamic Linq string expression.
/// Does not affect the usability of the equivalent context symbols ($, ^ and ~).
///
/// Default value is <c>false</c>.
/// </summary>
public bool AreContextKeywordsEnabled { get; set; } = true;
/// <summary>
/// Gets or sets a value indicating whether the EntityFramework version supports evaluating GroupBy at database level.
/// See https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-2.1#linq-groupby-translation
/// Remark: when this setting is set to <c>true</c>, make sure to supply this ParsingConfig as first parameter on the extension methods.
///
/// Default value is <c>false</c>.
/// </summary>
public bool EvaluateGroupByAtDatabase { get; set; }
/// <summary>
/// Use Parameterized Names in generated dynamic SQL query.
/// See https://github.com/graeme-hill/gblog/blob/master/source_content/articles/2014.139_entity-framework-dynamic-queries-and-parameterization.mkd
///
/// Default value is <c>false</c>.
/// </summary>
public bool UseParameterizedNamesInDynamicQuery { get; set; }
/// <summary>
/// Allows the New() keyword to evaluate any available Type.
///
/// Default value is <c>false</c>.
/// </summary>
public bool AllowNewToEvaluateAnyType { get; set; }
/// <summary>
/// Renames the (Typed)ParameterExpression empty Name to the correct supplied name from `it`.
///
/// Default value is <c>false</c>.
/// </summary>
public bool RenameParameterExpression { get; set; }
/// <summary>
/// Prevents any System.Linq.Expressions.ParameterExpression.Name value from being empty by substituting a random 16 character word.
///
/// Default value is <c>false</c>.
/// </summary>
public bool RenameEmptyParameterExpressionNames { get; set; }
/// <summary>
/// By default, when a member is not found in a type and the type has a string based index accessor it will be parsed as an index accessor.
/// Use this flag to disable this behaviour and have parsing fail when parsing an expression where a member access on a non-existing member happens.
///
/// Default value is <c>false</c>.
/// </summary>
public bool DisableMemberAccessToIndexAccessorFallback { get; set; }
/// <summary>
/// By default, finding types by a simple name is not supported.
/// Use this flag to use the CustomTypeProvider to resolve types by a simple name like "Employee" instead of "MyDatabase.Entities.Employee".
/// Note that a first matching type is returned and this functionality needs to scan all types from all assemblies, so use with caution.
///
/// Default value is <c>false</c>.
/// </summary>
public bool ResolveTypesBySimpleName { get; set; }
/// <summary>
/// Support enumeration-types from the System namespace in mscorlib. An example could be "StringComparison".
///
/// Default value is <c>true</c>.
/// </summary>
public bool SupportEnumerationsFromSystemNamespace { get; set; } = true;
/// <summary>
/// By default, a DateTime (like 'Fri, 10 May 2019 11:03:17 GMT') is parsed as local time.
/// Use this flag to parse all DateTime strings as UTC.
///
/// Default value is <c>false</c>.
/// </summary>
public bool DateTimeIsParsedAsUTC { get; set; }
/// <summary>
/// The number parsing culture.
///
/// Default value is CultureInfo.InvariantCulture
/// </summary>
public CultureInfo? NumberParseCulture { get; set; } = CultureInfo.InvariantCulture;
/// <summary>
/// Additional TypeConverters
/// </summary>
public IDictionary<Type, TypeConverter>? TypeConverters { get; set; }
/// <summary>
/// When using the NullPropagating function np(...), use a "default value" for non-nullable value types instead of "null value".
///
/// Default value is <c>false</c>.
/// </summary>
public bool NullPropagatingUseDefaultValueForNonNullableValueTypes { get; set; }
/// <summary>
/// Support casting to a full qualified type using a string (double-quoted value).
/// <code>
/// var result = queryable.Select($"\"System.DateTime\"(LastUpdate)");
/// </code>
///
/// Default value is <c>true</c>.
/// </summary>
public bool SupportCastingToFullyQualifiedTypeAsString { get; set; } = true;
/// <summary>
/// When the type and property have the same name the parser takes the property instead of type when this setting is set to <c>true</c>.
/// This setting is also used for calling ExtensionMethods.
///
/// Default value is <c>true</c>.
/// </summary>
public bool PrioritizePropertyOrFieldOverTheType { get; set; } = true;
/// <summary>
/// Support a "." in a property-name. Used in the 'new (a.b as a.b)' syntax.
///
/// Default value is <c>false</c>.
/// </summary>
public bool SupportDotInPropertyNames { get; set; }
/// <summary>
/// Disallows the New() keyword to be used to construct a class.
///
/// Default value is <c>false</c>.
/// </summary>
public bool DisallowNewKeyword { get; set; }
/// <summary>
/// Caches constant expressions to enhance performance. Periodic cleanup is performed to manage cache size, governed by this configuration.
/// </summary>
public CacheConfig? ConstantExpressionCacheConfig { get; set; }
/// <summary>
/// Converts typeof(object) to the correct type to allow comparison (Equal, NotEqual, GreaterThan, GreaterThanEqual, LessThan and LessThanEqual).
///
/// Default value is <c>false</c>.
///
/// When set to <c>true</c>, the following code will work correct:
/// <example>
/// <code>
/// <![CDATA[
/// class Person
/// {
/// public string Name { get; set; }
/// public object Age { get; set; }
/// }
///
/// var persons = new[]
/// {
/// new Person { Name = "Foo", Age = 99 },
/// new Person { Name = "Bar", Age = 33 }
/// }.AsQueryable();
///
/// var config = new ParsingConfig
/// {
/// ConvertObjectToSupportComparison = true
/// };
///
/// var results = persons.Where(config, "Age > 50").ToList();
/// ]]>
/// </code>
/// </example>
/// </summary>
public bool ConvertObjectToSupportComparison { get; set; }
/// <summary>
/// Defines the type of string literal parsing that will be performed.
/// Default value is <c>StringLiteralParsingType.Default</c>.
/// </summary>
public StringLiteralParsingType StringLiteralParsing { get; set; } = StringLiteralParsingType.Default;
/// <summary>
/// When set to <c>true</c>, the parser will restrict the OrderBy method to only allow properties or fields.
///
/// Default value is <c>false</c>.
/// </summary>
public bool RestrictOrderByToPropertyOrField { get; set; }
}