-
Notifications
You must be signed in to change notification settings - Fork 161
/
ODataPathExtensions.cs
328 lines (281 loc) · 11 KB
/
ODataPathExtensions.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
//-----------------------------------------------------------------------------
// <copyright file="ODataPathExtensions.cs" company=".NET Foundation">
// Copyright (c) .NET Foundation and Contributors. All rights reserved.
// See License.txt in the project root for license information.
// </copyright>
//------------------------------------------------------------------------------
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.OData.Edm;
using Microsoft.OData.Edm;
using Microsoft.OData.UriParser;
namespace Microsoft.AspNetCore.OData.Routing;
/// <summary>
/// Extension methods for <see cref="ODataPath"/>.
/// </summary>
public static class ODataPathExtensions
{
/// <summary>
/// Gets a boolean value indicating whether the given path is a stream property path.
/// </summary>
/// <param name="path">The given odata path.</param>
/// <returns>true/false</returns>
public static bool IsStreamPropertyPath(this ODataPath path)
{
if (path == null)
{
return false;
}
PropertySegment propertySegment = path.LastSegment as PropertySegment;
if (propertySegment == null)
{
return false;
}
IEdmTypeReference propertyType = propertySegment.Property.Type;
// Edm.Stream, or a type definition whose underlying type is Edm.Stream,
// cannot be used in collections or for non-binding parameters to functions or actions.
// So, we don't need to test it but leave the codes here for awareness.
//if (propertyType.IsCollection())
//{
// propertyType = propertyType.AsCollection().ElementType();
//}
return propertyType.IsStream();
}
/// <summary>
/// Computes the <see cref="IEdmType"/> of the resource identified by this <see cref="ODataPath"/>.
/// </summary>
/// <param name="path">Path to compute the type for.</param>
/// <returns>The <see cref="IEdmType"/> of the resource, or null if the path does not identify a resource with a type.</returns>
public static IEdmType GetEdmType(this ODataPath path)
{
if (path == null)
{
throw Error.ArgumentNull(nameof(path));
}
ODataPathSegment lastSegment = path.LastSegment;
return lastSegment.EdmType;
}
/// <summary>
/// Computes the <see cref="IEdmNavigationSource"/> of the resource identified by this <see cref="ODataPath"/>.
/// </summary>
/// <param name="path">Path to compute the set for.</param>
/// <returns>The <see cref="IEdmNavigationSource"/> of the resource, or null if the path does not identify a resource that is part of a set.</returns>
public static IEdmNavigationSource GetNavigationSource(this ODataPath path)
{
if (path == null)
{
throw Error.ArgumentNull(nameof(path));
}
for (int i = path.Count - 1; i >= 0; --i)
{
ODataPathSegment segment = path[i];
if (segment is EntitySetSegment entitySetSegment)
{
return entitySetSegment.EntitySet;
}
if (segment is KeySegment keySegment)
{
return keySegment.NavigationSource;
}
if (segment is NavigationPropertyLinkSegment navigationPropertyLinkSegment)
{
return navigationPropertyLinkSegment.NavigationSource;
}
if (segment is NavigationPropertySegment navigationPropertySegment)
{
return navigationPropertySegment.NavigationSource;
}
if (segment is OperationImportSegment operationImportSegment)
{
return operationImportSegment.EntitySet;
}
if (segment is OperationSegment operationSegment)
{
return operationSegment.EntitySet;
}
if (segment is SingletonSegment singleton)
{
return singleton.Singleton;
}
if (segment is TypeSegment typeSegment)
{
return typeSegment.NavigationSource;
}
if (segment is PropertySegment)
{
continue;
}
return null;
}
return null;
}
/// <summary>
/// Get the string representation of <see cref="ODataPath"/> mainly translate Context Url path.
/// </summary>
/// <param name="path">Path to compute the set for.</param>
/// <returns>The string representation of the Context Url path.</returns>
public static string GetPathString(this ODataPath path)
{
if (path == null)
{
throw Error.ArgumentNull(nameof(path));
}
ODataPathSegmentHandler handler = new ODataPathSegmentHandler();
foreach (var segment in path)
{
segment.HandleWith(handler);
}
return handler.PathLiteral;
}
/// <summary>
/// Get the string representation of <see cref="ODataPath"/> mainly translate Context Url path.
/// </summary>
/// <param name="segments">The path segments.</param>
/// <returns>The string representation of the Context Url path.</returns>
public static string GetPathString(this IList<ODataPathSegment> segments)
{
if (segments == null)
{
throw Error.ArgumentNull(nameof(segments));
}
ODataPathSegmentHandler handler = new ODataPathSegmentHandler();
foreach (var segment in segments)
{
segment.HandleWith(handler);
}
return handler.PathLiteral;
}
internal static bool IsUntypedPropertyPath(this ODataPath path)
{
if (path == null)
{
return false;
}
// TODO: do we need take the type cast into consideration?
if (path.LastSegment is PropertySegment propertySegment)
{
return propertySegment.Property.Type.IsUntypedOrCollectionUntyped();
}
// TODO, Shall we take the dynamic property path segment into consideration?
return false;
}
/// <summary>
/// Gets the property and structured type from <see cref="ODataPath"/>.
/// TODO: The logic implenetation is not good and do need refactor it later.
/// </summary>
/// <param name="path">The OData path.</param>
/// <returns>The property, structured type and the name.</returns>
internal static (IEdmProperty, IEdmStructuredType, string) GetPropertyAndStructuredTypeFromPath(this ODataPath path)
{
if (path == null)
{
return (null, null, string.Empty);
}
IEdmStructuredType structuredType = null;
string typeCast = string.Empty;
IEnumerable<ODataPathSegment> reverseSegments = path.Reverse();
foreach (var segment in reverseSegments)
{
if (segment is NavigationPropertySegment navigationPathSegment)
{
IEdmProperty property = navigationPathSegment.NavigationProperty;
if (structuredType == null)
{
structuredType = navigationPathSegment.NavigationProperty.ToEntityType();
}
string name = navigationPathSegment.NavigationProperty.Name + typeCast;
return (property, structuredType, name);
}
if (segment is OperationSegment operationSegment)
{
if (structuredType == null)
{
structuredType = operationSegment.EdmType.AsElementType() as IEdmStructuredType;
}
string name = operationSegment.Operations.First().FullName() + typeCast;
return (null, structuredType, name);
}
if (segment is PropertySegment propertyAccessPathSegment)
{
IEdmProperty property = propertyAccessPathSegment.Property;
if (structuredType == null)
{
structuredType = property.Type.GetElementType() as IEdmStructuredType;
}
string name = property.Name + typeCast;
return (property, structuredType, name);
}
if (segment is EntitySetSegment entitySetSegment)
{
if (structuredType == null)
{
structuredType = entitySetSegment.EntitySet.EntityType;
}
string name = entitySetSegment.EntitySet.Name + typeCast;
return (null, structuredType, name);
}
if (segment is SingletonSegment singletonSegment)
{
if (structuredType == null)
{
structuredType = singletonSegment.Singleton.EntityType;
}
string name = singletonSegment.Singleton.Name + typeCast;
return (null, structuredType, name);
}
if (segment is TypeSegment typeSegment)
{
structuredType = typeSegment.EdmType.AsElementType() as IEdmStructuredType;
typeCast = "/" + structuredType;
}
else if (segment is KeySegment || segment is CountSegment)
{
// do nothing, just go to next segment, what about if meet OperationSegment?
}
else
{
// if we meet any other segments, just return (null, null, string.Empty);
break;
}
}
return (null, null, string.Empty);
}
#region BACKUP
#if false
/// <summary>
///
/// </summary>
/// <param name="pathTemplatesegment"></param>
/// <param name="value"></param>
/// <returns></returns>
public static string TranslatePathTemplateSegment(this PathTemplateSegment pathTemplatesegment, out string value)
{
if (pathTemplatesegment == null)
{
throw Error.ArgumentNull("pathTemplatesegment");
}
string pathTemplateSegmentLiteralText = pathTemplatesegment.LiteralText;
if (pathTemplateSegmentLiteralText == null)
{
throw new ODataException(Error.Format(SRResources.InvalidAttributeRoutingTemplateSegment, string.Empty));
}
if (pathTemplateSegmentLiteralText.StartsWith("{", StringComparison.Ordinal)
&& pathTemplateSegmentLiteralText.EndsWith("}", StringComparison.Ordinal))
{
string[] keyValuePair = pathTemplateSegmentLiteralText.Substring(1,
pathTemplateSegmentLiteralText.Length - 2).Split(':');
if (keyValuePair.Length != 2)
{
throw new ODataException(Error.Format(
SRResources.InvalidAttributeRoutingTemplateSegment,
pathTemplateSegmentLiteralText));
}
value = "{" + keyValuePair[0] + "}";
return keyValuePair[1];
}
value = string.Empty;
return string.Empty;
}
#endif
#endregion
}