-
Notifications
You must be signed in to change notification settings - Fork 352
/
Copy pathInBinder.cs
427 lines (377 loc) · 18.5 KB
/
InBinder.cs
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//---------------------------------------------------------------------
// <copyright file="InBinder.cs" company="Microsoft">
// Copyright (C) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
// </copyright>
//---------------------------------------------------------------------
namespace Microsoft.OData.UriParser
{
using System;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
using Microsoft.OData.Edm;
using Microsoft.OData.Core;
/// <summary>
/// Class that knows how to bind the In operator.
/// </summary>
internal sealed class InBinder
{
private const string NullLiteral = "null";
/// <summary>
/// Method to use for binding the parent node, if needed.
/// </summary>
private readonly Func<QueryToken, QueryNode> bindMethod;
/// <summary>
/// Resolver for parsing
/// </summary>
private readonly ODataUriResolver resolver;
/// <summary>
/// Constructs a InBinder with the given method to be used binding the parent token if needed.
/// </summary>
/// <param name="bindMethod">Method to use for binding the parent token, if needed.</param>
/// <param name="resolver">Resolver for parsing.</param>
internal InBinder(Func<QueryToken, QueryNode> bindMethod, ODataUriResolver resolver)
{
this.bindMethod = bindMethod;
this.resolver = resolver;
}
/// <summary>
/// Binds an In operator token.
/// </summary>
/// <param name="inToken">The In operator token to bind.</param>
/// <param name="state">State of the metadata binding.</param>
/// <returns>The bound In operator token.</returns>
internal QueryNode BindInOperator(InToken inToken, BindingState state)
{
ExceptionUtils.CheckArgumentNotNull(inToken, "inToken");
SingleValueNode left = this.GetSingleValueOperandFromToken(inToken.Left);
CollectionNode right = null;
if (left.TypeReference != null)
{
right = this.GetCollectionOperandFromToken(
inToken.Right, new EdmCollectionTypeReference(new EdmCollectionType(left.TypeReference)), state.Model);
}
else
{
right = this.GetCollectionOperandFromToken(
inToken.Right, new EdmCollectionTypeReference(new EdmCollectionType(EdmCoreModel.Instance.GetUntyped())), state.Model);
}
// If the left operand is either an integral or a string type and the right operand is a collection of enums,
// Calls the MetadataBindingUtils.ConvertToTypeIfNeeded() method to convert the left operand to the same enum type as the right operand.
if ((!(right is CollectionConstantNode) && right.ItemType.IsEnum()) && (left.TypeReference != null && (left.TypeReference.IsString() || left.TypeReference.IsIntegral())))
{
left = MetadataBindingUtils.ConvertToTypeIfNeeded(left, right.ItemType);
}
MetadataBindingUtils.VerifyCollectionNode(right, this.resolver.EnableCaseInsensitive);
return new InNode(left, right);
}
/// <summary>
/// Retrieve SingleValueNode bound with given query token.
/// </summary>
/// <param name="queryToken">The query token</param>
/// <returns>The corresponding SingleValueNode</returns>
private SingleValueNode GetSingleValueOperandFromToken(QueryToken queryToken)
{
SingleValueNode operand = this.bindMethod(queryToken) as SingleValueNode;
if (operand == null)
{
throw new ODataException(SRResources.MetadataBinder_LeftOperandNotSingleValue);
}
return operand;
}
/// <summary>
/// Retrieve CollectionNode bound with given query token.
/// </summary>
/// <param name="queryToken">The query token</param>
/// <param name="expectedType">The expected type that this collection holds</param>
/// <param name="model">The Edm model</param>
/// <returns>The corresponding CollectionNode</returns>
private CollectionNode GetCollectionOperandFromToken(QueryToken queryToken, IEdmTypeReference expectedType, IEdmModel model)
{
CollectionNode operand = null;
LiteralToken literalToken = queryToken as LiteralToken;
if (literalToken != null)
{
// Parentheses-based collections are not standard JSON but bracket-based ones are.
// Temporarily switch our collection to bracket-based so that the JSON reader will
// correctly parse the collection. Then pass the original literal text to the token.
string bracketLiteralText = literalToken.OriginalText;
if (bracketLiteralText[0] == '(' || bracketLiteralText[0] == '[')
{
Debug.Assert((bracketLiteralText[0] == '(' && bracketLiteralText[^1] == ')') || (bracketLiteralText[0] == '[' && bracketLiteralText[^1] == ']'),
$"Collection with opening '{bracketLiteralText[0]}' should have corresponding '{(bracketLiteralText[0] == '(' ? ')' : ']')}'");
if (bracketLiteralText[0] == '(' && bracketLiteralText[^1] == ')')
{
bracketLiteralText = string.Create(bracketLiteralText.Length, bracketLiteralText, (span, state) =>
{
state.AsSpan().CopyTo(span);
span[0] = '[';
span[^1] = ']';
});
}
Debug.Assert(expectedType.IsCollection());
string expectedTypeFullName = expectedType.Definition.AsElementType().FullTypeName();
if (expectedTypeFullName.Equals("Edm.String", StringComparison.Ordinal) || expectedTypeFullName.Equals("Edm.Untyped", StringComparison.Ordinal))
{
// For collection of strings, need to convert single-quoted string to double-quoted string,
// and also, per ABNF, a single quote within a string literal is "encoded" as two consecutive single quotes in either
// literal or percent - encoded representation.
// Sample: ['a''bc','''def','xyz'''] ==> ["a'bc","'def","xyz'"], which is legitimate Json format.
bracketLiteralText = NormalizeStringCollectionItems(bracketLiteralText);
}
else if (expectedTypeFullName.Equals("Edm.Guid", StringComparison.Ordinal))
{
// For collection of Guids, need to convert the Guid literals to single-quoted form, so that it is compatible
// with the Json reader used for deserialization.
// Sample: [D01663CF-EB21-4A0E-88E0-361C10ACE7FD, 492CF54A-84C9-490C-A7A4-B5010FAD8104]
// ==> ['D01663CF-EB21-4A0E-88E0-361C10ACE7FD', '492CF54A-84C9-490C-A7A4-B5010FAD8104']
bracketLiteralText = NormalizeGuidCollectionItems(bracketLiteralText);
}
else if (expectedTypeFullName.Equals("Edm.DateTimeOffset", StringComparison.Ordinal) ||
expectedTypeFullName.Equals("Edm.Date", StringComparison.Ordinal) ||
expectedTypeFullName.Equals("Edm.TimeOfDay", StringComparison.Ordinal) ||
expectedTypeFullName.Equals("Edm.Duration", StringComparison.Ordinal))
{
// For collection of Date/Time/Duration items, need to convert the Date/Time/Duration literals to single-quoted form, so that it is compatible
// with the Json reader used for deserialization.
// Sample: [1970-01-01T00:00:00Z, 1980-01-01T01:01:01+01:00]
// ==> ['1970-01-01T00:00:00Z', '1980-01-01T01:01:01+01:00']
bracketLiteralText = NormalizeDateTimeCollectionItems(bracketLiteralText);
}
}
object collection = ODataUriConversionUtils.ConvertFromCollectionValue(bracketLiteralText, model, expectedType);
LiteralToken collectionLiteralToken = new LiteralToken(collection, literalToken.OriginalText, expectedType);
operand = this.bindMethod(collectionLiteralToken) as CollectionConstantNode;
}
else
{
var node = this.bindMethod(queryToken);
if (node is SingleValueOpenPropertyAccessNode openNode)
{
operand = new CollectionOpenPropertyAccessNode(openNode.Source, openNode.Name, expectedType as IEdmCollectionTypeReference);
}
else
{
operand = node as CollectionNode;
}
}
if (operand == null)
{
throw new ODataException(SRResources.MetadataBinder_RightOperandNotCollectionValue);
}
return operand;
}
private static string NormalizeStringCollectionItems(string literalText)
{
// a comma-separated list of primitive values, enclosed in parentheses, or a single expression that resolves to a collection
// However, for String collection, we should process:
// 1) comma could be part of the string value
// 2) single quote could not be part of string value
// 3) double quote could be part of string value, double quote also could be the starting and ending character.
// remove the '[' and ']'
string normalizedText = literalText.Substring(1, literalText.Length - 2).Trim();
int length = normalizedText.Length;
StringBuilder sb = new StringBuilder(length + 2);
sb.Append('[');
for (int i = 0; i < length; i++)
{
char ch = normalizedText[i];
switch (ch)
{
case '"':
i = ProcessDoubleQuotedStringItem(i, normalizedText, sb);
break;
case '\'':
i = ProcessSingleQuotedStringItem(i, normalizedText, sb);
break;
case ' ':
// ignore all whitespaces between items
break;
case ',':
// for multiple comma(s) between items, for example ('abc',,,'xyz'),
// We let it go and let the next layer to identify the problem by design.
sb.Append(',');
break;
case 'n':
// it maybe null
int index = normalizedText.IndexOf(',', i + 1);
string subStr;
if (index < 0)
{
subStr = normalizedText.Substring(i).TrimEnd(' ');
i = length - 1;
}
else
{
subStr = normalizedText.Substring(i, index - i).TrimEnd(' ');
i = index - 1;
}
if (subStr == NullLiteral)
{
sb.Append(NullLiteral);
}
else
{
throw new ODataException(Error.Format(SRResources.StringItemShouldBeQuoted, subStr));
}
break;
default:
// any other character between items is not valid.
throw new ODataException(Error.Format(SRResources.StringItemShouldBeQuoted, ch));
}
}
sb.Append(']');
return sb.ToString();
}
private static int ProcessDoubleQuotedStringItem(int start, string input, StringBuilder sb)
{
Debug.Assert(input[start] == '"');
int length = input.Length;
int k = start + 1;
// no matter it's single quote or not, just starting it as double quote (JSON).
sb.Append('"');
for (; k < length; k++)
{
char next = input[k];
if (next == '"')
{
// If prev and next are both double quotes, then it's an empty string.
if (input[k - 1] == '"')
{
// We append \"\" so as to return "\"\"" instead of "".
// This is to avoid passing an empty string to the ConstantNode.
sb.Append("\\\"\\\"");
}
break;
}
else if (next == '\\')
{
sb.Append('\\');
if (k + 1 >= length)
{
// if end of string, stop it.
break;
}
else
{
// otherwise, append "\x" into
sb.Append(input[k + 1]);
k++;
}
}
else
{
sb.Append(next);
}
}
// no matter it's single quote or not, just ending it as double quote.
sb.Append('"');
return k;
}
private static int ProcessSingleQuotedStringItem(int start, string input, StringBuilder sb)
{
Debug.Assert(input[start] == '\'');
int length = input.Length;
int k = start + 1;
// no matter it's single quote or not, just starting it as double quote (JSON).
sb.Append('"');
for (; k < length; k++)
{
char next = input[k];
if (next == '\'')
{
if (k + 1 >= length || input[k + 1] != '\'')
{
// If prev and next are both single quotes, then it's an empty string.
if (input[k - 1] == '\'')
{
if(k > 2 && input[k - 2] == '\'')
{
// We have 3 single quotes e.g 'ghi'''
// It means we need to unescape the double single quotes
// and escape double quote to return the result "ghi'" and process next items
sb.Append('"');
return k;
}
// We append \"\" so as to return "\"\"" instead of "".
// This is to avoid passing an empty string to the ConstantNode.
sb.Append("\\\"\\\"");
}
// match with single quote ('), stop it.
break;
}
else
{
// Unescape the double single quotes as one single quote, and continue
sb.Append('\'');
k++;
}
}
else if (next == '"')
{
sb.Append('\\');
sb.Append('"');
}
else if (next == '\\')
{
sb.Append("\\\\");
}
else
{
sb.Append(next);
}
}
// no matter it's single quote or not, just ending it as double quote.
sb.Append('"');
return k;
}
private static string NormalizeGuidCollectionItems(string bracketLiteralText)
{
string normalizedText = bracketLiteralText.Substring(1, bracketLiteralText.Length - 2).Trim();
// If we have empty brackets ()
if (normalizedText.Length == 0)
{
return "[]";
}
string[] items = normalizedText.Split(',')
.Select(s => s.Trim()).ToArray();
for (int i = 0; i < items.Length; i++)
{
if (items[i] != NullLiteral && items[i][0] != '\'' && items[i][0] != '"')
{
items[i] = String.Format(CultureInfo.InvariantCulture, "'{0}'", items[i]);
}
}
return "[" + String.Join(",", items) + "]";
}
private static string NormalizeDateTimeCollectionItems(string bracketLiteralText)
{
string normalizedText = bracketLiteralText.Substring(1, bracketLiteralText.Length - 2).Trim();
// If we have empty brackets ()
if (normalizedText.Length == 0)
{
return "[]";
}
string[] items = normalizedText.Split(',')
.Select(s => s.Trim()).ToArray();
for (int i = 0; i < items.Length; i++)
{
const string durationPrefix = "duration";
if (items[i] == NullLiteral)
{
continue;
}
if (items[i].StartsWith(durationPrefix, StringComparison.Ordinal))
{
items[i] = items[i].Remove(0, durationPrefix.Length);
}
if (items[i][0] != '\'' && items[i][0] != '"')
{
items[i] = String.Format(CultureInfo.InvariantCulture, "'{0}'", items[i]);
}
}
return "[" + String.Join(",", items) + "]";
}
}
}