| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. See License.txt in the project root for license information. |
| | 3 | |
|
| | 4 | | namespace Microsoft.Azure.Batch |
| | 5 | | { |
| | 6 | | using System; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using System.IO; |
| | 9 | | using System.Linq; |
| | 10 | | using System.Text; |
| | 11 | | using System.Threading.Tasks; |
| | 12 | | using System.Threading; |
| | 13 | | using Common; |
| | 14 | | using Utils; |
| | 15 | |
|
| | 16 | | internal static class UtilitiesInternal |
| | 17 | | { |
| | 18 | | internal static TTo MapEnum<TFrom, TTo>(TFrom otherEnum) |
| | 19 | | where TTo : struct |
| | 20 | | where TFrom : struct |
| | 21 | | { |
| 86495 | 22 | | TTo result = (TTo)Enum.Parse(typeof(TTo), otherEnum.ToString(), ignoreCase: true); |
| 86495 | 23 | | return result; |
| | 24 | | } |
| | 25 | |
|
| | 26 | | internal static TTo? MapNullableEnum<TFrom, TTo>(TFrom? otherEnum) |
| | 27 | | where TTo : struct |
| | 28 | | where TFrom : struct |
| | 29 | | { |
| 50362 | 30 | | if (otherEnum == null) |
| | 31 | | { |
| 21150 | 32 | | return null; |
| | 33 | | } |
| | 34 | |
|
| 29212 | 35 | | return UtilitiesInternal.MapEnum<TFrom, TTo>(otherEnum.Value); |
| | 36 | | } |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// Convert an enum of type CertificateVisibility to a List of Protocol.Models.CertificateVisibility. |
| | 40 | | /// </summary> |
| | 41 | | /// <param name='value'> |
| | 42 | | /// The value to convert. |
| | 43 | | /// </param> |
| | 44 | | /// <returns> |
| | 45 | | /// The enum value to convert into list format. |
| | 46 | | /// </returns> |
| | 47 | | internal static IList<Protocol.Models.CertificateVisibility> CertificateVisibilityToList(Common.CertificateVisib |
| | 48 | | { |
| 4399 | 49 | | List<Protocol.Models.CertificateVisibility> result = new List<Protocol.Models.CertificateVisibility>(); |
| 4399 | 50 | | if (value.HasValue) |
| | 51 | | { |
| 3969 | 52 | | IList<Common.CertificateVisibility> enumValues = new List<Common.CertificateVisibility>( |
| 3969 | 53 | | (Common.CertificateVisibility[])Enum.GetValues(typeof(Common.CertificateVisibility))); |
| | 54 | |
|
| 3969 | 55 | | enumValues.Remove(Common.CertificateVisibility.None); //None is an artifact of the OM so skip it |
| | 56 | |
|
| 31752 | 57 | | foreach (Common.CertificateVisibility enumValue in enumValues) |
| | 58 | | { |
| 11907 | 59 | | if (value.Value.HasFlag(enumValue)) |
| | 60 | | { |
| 5651 | 61 | | Protocol.Models.CertificateVisibility protoEnumValue = UtilitiesInternal.MapEnum<Common.Certific |
| 5651 | 62 | | result.Add(protoEnumValue); |
| | 63 | | } |
| | 64 | | } |
| | 65 | | } |
| | 66 | |
|
| 4399 | 67 | | return result; |
| | 68 | | } |
| | 69 | |
|
| | 70 | | /// <summary> |
| | 71 | | /// Convert an enum of type AccessScope to a List of Protocol.Models.AccessScope. |
| | 72 | | /// </summary> |
| | 73 | | /// <param name='value'> |
| | 74 | | /// The value to convert. |
| | 75 | | /// </param> |
| | 76 | | /// <returns> |
| | 77 | | /// The enum value to convert into list format. |
| | 78 | | /// </returns> |
| | 79 | | internal static IList<Protocol.Models.AccessScope> AccessScopeToList(Common.AccessScope value) |
| | 80 | | { |
| 681 | 81 | | List<Protocol.Models.AccessScope> result = new List<Protocol.Models.AccessScope>(); |
| | 82 | |
|
| 681 | 83 | | IList<Common.AccessScope> enumValues = new List<Common.AccessScope>((Common.AccessScope[])Enum.GetValues(typ |
| | 84 | |
|
| 681 | 85 | | enumValues.Remove(Common.AccessScope.None); //None is an artifact of the OM so skip it |
| | 86 | |
|
| 2724 | 87 | | foreach (Common.AccessScope enumValue in enumValues) |
| | 88 | | { |
| 681 | 89 | | if (value.HasFlag(enumValue)) |
| | 90 | | { |
| 261 | 91 | | Protocol.Models.AccessScope protoEnumValue = UtilitiesInternal.MapEnum<Common.AccessScope, Protocol. |
| 261 | 92 | | result.Add(protoEnumValue); |
| | 93 | | } |
| | 94 | | } |
| | 95 | |
|
| 681 | 96 | | return result; |
| | 97 | | } |
| | 98 | |
|
| | 99 | | /// <summary> |
| | 100 | | /// Parse enum values for type CertificateVisibility. |
| | 101 | | /// </summary> |
| | 102 | | /// <param name='value'> |
| | 103 | | /// The value to parse. |
| | 104 | | /// </param> |
| | 105 | | /// <returns> |
| | 106 | | /// The enum value. |
| | 107 | | /// </returns> |
| | 108 | | internal static Common.CertificateVisibility? ParseCertificateVisibility(IList<Protocol.Models.CertificateVisibi |
| | 109 | | { |
| 10408 | 110 | | if (value == null) |
| | 111 | | { |
| 1277 | 112 | | return null; |
| | 113 | | } |
| | 114 | |
|
| 9131 | 115 | | Common.CertificateVisibility flags = CertificateVisibility.None; |
| | 116 | |
|
| 71634 | 117 | | foreach (Protocol.Models.CertificateVisibility visibility in value) |
| | 118 | | { |
| 26686 | 119 | | Common.CertificateVisibility convertedEnum = UtilitiesInternal.MapEnum<Protocol.Models.CertificateVisibi |
| | 120 | |
|
| 26686 | 121 | | flags |= convertedEnum; |
| | 122 | | } |
| | 123 | |
|
| 9131 | 124 | | return flags; |
| | 125 | | } |
| | 126 | |
|
| | 127 | |
|
| | 128 | | /// <summary> |
| | 129 | | /// Parse enum values for type AccessScope. |
| | 130 | | /// </summary> |
| | 131 | | /// <param name='value'> |
| | 132 | | /// The value to parse. |
| | 133 | | /// </param> |
| | 134 | | /// <returns> |
| | 135 | | /// The enum value. |
| | 136 | | /// </returns> |
| | 137 | | internal static Common.AccessScope ParseAccessScope(IList<Protocol.Models.AccessScope> value) |
| | 138 | | { |
| 1986 | 139 | | if (value == null) |
| | 140 | | { |
| 458 | 141 | | return Common.AccessScope.None; |
| | 142 | | } |
| | 143 | |
|
| 1528 | 144 | | Common.AccessScope flags = AccessScope.None; |
| | 145 | |
|
| 11440 | 146 | | foreach (Protocol.Models.AccessScope visibility in value) |
| | 147 | | { |
| 4192 | 148 | | Common.AccessScope convertedEnum = UtilitiesInternal.MapEnum<Protocol.Models.AccessScope, Common.AccessS |
| | 149 | |
|
| 4192 | 150 | | flags |= convertedEnum; |
| | 151 | | } |
| | 152 | |
|
| 1528 | 153 | | return flags; |
| | 154 | | } |
| | 155 | |
|
| | 156 | |
|
| | 157 | | internal static void ThrowOnUnbound(BindingState bindingState) |
| | 158 | | { |
| 37 | 159 | | if (BindingState.Unbound == bindingState) |
| | 160 | | { |
| 4 | 161 | | throw OperationForbiddenOnUnboundObjects; |
| | 162 | | } |
| 33 | 163 | | } |
| | 164 | |
|
| | 165 | | internal static TWrappedItem CreateObjectWithNullCheck<TItem, TWrappedItem>(TItem item, Func<TItem, TWrappedItem |
| | 166 | | where TItem : class |
| | 167 | | where TWrappedItem : class |
| | 168 | | { |
| 159030 | 169 | | if (item == null) |
| | 170 | | { |
| 76782 | 171 | | if (nullFactory != null) |
| | 172 | | { |
| 0 | 173 | | return nullFactory(); |
| | 174 | | } |
| | 175 | | else |
| | 176 | | { |
| 76782 | 177 | | return null; |
| | 178 | | } |
| | 179 | | } |
| | 180 | | else |
| | 181 | | { |
| 82248 | 182 | | return factory(item); |
| | 183 | | } |
| | 184 | | } |
| | 185 | |
|
| | 186 | | #region Collection conversions |
| | 187 | |
|
| | 188 | | /// <summary> |
| | 189 | | /// Converts a collection of object model items into a corresponding collection of transport layer items. |
| | 190 | | /// If the <paramref name="items"/> collection is null, null is returned. |
| | 191 | | /// </summary> |
| | 192 | | /// <returns> |
| | 193 | | /// A collection of type <typeparam name="T"/> generated by calling |
| | 194 | | /// <see cref="ITransportObjectProvider{T}.GetTransportObject"/> on each element of the input collection. |
| | 195 | | /// </returns> |
| | 196 | | internal static IList<T> ConvertToProtocolCollection<T>(IEnumerable<ITransportObjectProvider<T>> items) |
| | 197 | | { |
| 10762 | 198 | | if (null == items) |
| | 199 | | { |
| 6593 | 200 | | return null; |
| | 201 | | } |
| | 202 | |
|
| 4169 | 203 | | List<T> protocolItems = new List<T>(); |
| | 204 | |
|
| 45658 | 205 | | foreach (ITransportObjectProvider<T> item in items) |
| | 206 | | { |
| 18660 | 207 | | T protocolItem = item.GetTransportObject(); |
| | 208 | |
|
| | 209 | | // add the protocol object to the return collection |
| 18660 | 210 | | protocolItems.Add(protocolItem); |
| | 211 | | } |
| | 212 | |
|
| 4169 | 213 | | return protocolItems; |
| | 214 | | } |
| | 215 | |
|
| | 216 | | /// <summary> |
| | 217 | | /// Converts a collection of object model items into a corresponding array of transport layer items. |
| | 218 | | /// If the <paramref name="items"/> collection is null, null is returned. |
| | 219 | | /// </summary> |
| | 220 | | /// <returns> |
| | 221 | | /// A collection of type <typeparam name="T"/> generated by calling |
| | 222 | | /// <see cref="ITransportObjectProvider{T}.GetTransportObject"/> on each element of the input collection. |
| | 223 | | /// </returns> |
| | 224 | | internal static T[] ConvertToProtocolArray<T>(IEnumerable<ITransportObjectProvider<T>> items) |
| | 225 | | { |
| 6 | 226 | | IEnumerable<T> protocolCollection = ConvertToProtocolCollection(items); |
| | 227 | |
|
| 6 | 228 | | if (protocolCollection != null) |
| | 229 | | { |
| 4 | 230 | | return protocolCollection.ToArray(); |
| | 231 | | } |
| | 232 | | else |
| | 233 | | { |
| 2 | 234 | | return null; |
| | 235 | | } |
| | 236 | | } |
| | 237 | |
|
| | 238 | | internal static IList<TTo> ConvertEnumCollection<TFrom, TTo>(IEnumerable<TFrom> items) |
| | 239 | | where TFrom : struct |
| | 240 | | where TTo : struct |
| | 241 | | { |
| 518 | 242 | | return items?.Select(MapEnum<TFrom, TTo>).ToList(); |
| | 243 | | } |
| | 244 | |
|
| | 245 | | internal static IList<TTo?> ConvertEnumCollection<TFrom, TTo>(IEnumerable<TFrom?> items) |
| | 246 | | where TFrom : struct |
| | 247 | | where TTo : struct |
| | 248 | | { |
| 0 | 249 | | return items?.Select(MapNullableEnum<TFrom, TTo>).ToList(); |
| | 250 | | } |
| | 251 | |
|
| | 252 | | private static TList ConvertCollection<TIn, TOut, TList>( |
| | 253 | | IEnumerable<TIn> items, |
| | 254 | | Func<TIn, TOut> objectCreationFunc, |
| | 255 | | Func<IEnumerable<TOut>, TList> listCreationFunc) |
| | 256 | | where TList : class, IList<TOut> |
| | 257 | | where TIn : class |
| | 258 | | where TOut : class |
| | 259 | | { |
| 39834 | 260 | | if (null == items) |
| | 261 | | { |
| 20166 | 262 | | return null; |
| | 263 | | } |
| | 264 | |
|
| 108362 | 265 | | TList result = listCreationFunc(items.Select(item => item == null ? null : objectCreationFunc(item))); |
| | 266 | |
|
| 19668 | 267 | | return result; |
| | 268 | | } |
| | 269 | |
|
| | 270 | | /// <summary> |
| | 271 | | /// Applies the <paramref name="objectCreationFunc"/> to each item in <paramref name="items"/> and returns a non |
| | 272 | | /// </summary> |
| | 273 | | /// <typeparam name="TIn">The type of the input collection.</typeparam> |
| | 274 | | /// <typeparam name="TOut">The type of the output collection.</typeparam> |
| | 275 | | /// <param name="items">The collection to convert.</param> |
| | 276 | | /// <param name="objectCreationFunc">The function used to created each <typeparamref name="TOut"/> type object.< |
| | 277 | | /// <returns>A non-threadsafe collection containing the results of the conversion, or null if <paramref name="it |
| | 278 | | internal static List<TOut> CollectionToNonThreadSafeCollection<TIn, TOut>( |
| | 279 | | IEnumerable<TIn> items, |
| | 280 | | Func<TIn, TOut> objectCreationFunc) |
| | 281 | | where TIn : class |
| | 282 | | where TOut : class |
| | 283 | | { |
| 13322 | 284 | | List<TOut> result = UtilitiesInternal.ConvertCollection( |
| 13322 | 285 | | items, |
| 13322 | 286 | | objectCreationFunc, |
| 19922 | 287 | | (convertedItemsEnumerable) => new List<TOut>(convertedItemsEnumerable)); |
| | 288 | |
|
| 13322 | 289 | | return result; |
| | 290 | | } |
| | 291 | |
|
| | 292 | | /// <summary> |
| | 293 | | /// Applies the <paramref name="objectCreationFunc"/> to each item in <paramref name="items"/> and returns a non |
| | 294 | | /// </summary> |
| | 295 | | /// <typeparam name="TIn">The type of the input collection.</typeparam> |
| | 296 | | /// <typeparam name="TOut">The type of the output collection.</typeparam> |
| | 297 | | /// <param name="items">The collection to convert.</param> |
| | 298 | | /// <param name="objectCreationFunc">The function used to created each <typeparamref name="TOut"/> type object.< |
| | 299 | | /// <returns>A non-threadsafe collection containing the results of the conversion, or null if <paramref name="it |
| | 300 | | internal static IList<TOut> CollectionToThreadSafeCollection<TIn, TOut>( |
| | 301 | | IEnumerable<TIn> items, |
| | 302 | | Func<TIn, TOut> objectCreationFunc) |
| | 303 | | where TIn : class |
| | 304 | | where TOut : class |
| | 305 | | { |
| 1830 | 306 | | ConcurrentChangeTrackedList<TOut> result = UtilitiesInternal.ConvertCollection( |
| 1830 | 307 | | items, |
| 1830 | 308 | | objectCreationFunc, |
| 2723 | 309 | | (convertedItemsEnumerable) => new ConcurrentChangeTrackedList<TOut>(convertedItemsEnumerable)); |
| | 310 | |
|
| 1830 | 311 | | return result; |
| | 312 | | } |
| | 313 | |
|
| | 314 | | /// <summary> |
| | 315 | | /// Applies the <paramref name="objectCreationFunc"/> to each item in <paramref name="items"/> and returns a thr |
| | 316 | | /// </summary> |
| | 317 | | /// <typeparam name="TIn">The type of the input collection.</typeparam> |
| | 318 | | /// <typeparam name="TOut">The type of the output collection.</typeparam> |
| | 319 | | /// <param name="items">The collection to convert.</param> |
| | 320 | | /// <param name="objectCreationFunc">The function used to created each <typeparamref name="TOut"/> type object.< |
| | 321 | | /// <returns>A threadsafe collection containing the results of the conversion, or null if <paramref name="items" |
| | 322 | | internal static ConcurrentChangeTrackedModifiableList<TOut> CollectionToThreadSafeCollectionIModifiable<TIn, TOu |
| | 323 | | IEnumerable<TIn> items, |
| | 324 | | Func<TIn, TOut> objectCreationFunc) |
| | 325 | | where TIn : class |
| | 326 | | where TOut : class, IPropertyMetadata |
| | 327 | | { |
| 24682 | 328 | | ConcurrentChangeTrackedModifiableList<TOut> result = UtilitiesInternal.ConvertCollection( |
| 24682 | 329 | | items, |
| 24682 | 330 | | objectCreationFunc, |
| 36857 | 331 | | (convertedItemsEnumerable) => new ConcurrentChangeTrackedModifiableList<TOut>(convertedItemsEnumerable)) |
| | 332 | |
|
| 24682 | 333 | | return result; |
| | 334 | | } |
| | 335 | |
|
| | 336 | | #endregion |
| | 337 | |
|
| | 338 | | internal static Exception OperationForbiddenOnUnboundObjects |
| | 339 | | { |
| | 340 | | get |
| | 341 | | { |
| 4 | 342 | | return new InvalidOperationException(BatchErrorMessages.OperationForbiddenOnUnboundObjects); |
| | 343 | | } |
| | 344 | | } |
| | 345 | |
|
| | 346 | | internal static Exception OperationForbiddenOnBoundObjects |
| | 347 | | { |
| | 348 | | get |
| | 349 | | { |
| 0 | 350 | | return new InvalidOperationException(BatchErrorMessages.OperationForbiddenOnBoundObjects); |
| | 351 | | } |
| | 352 | | } |
| | 353 | |
|
| | 354 | | internal static Exception MonitorRequiresConsistentHierarchyChain |
| | 355 | | { |
| | 356 | | get |
| | 357 | | { |
| 0 | 358 | | return new InvalidOperationException(BatchErrorMessages.MonitorInstancesMustHaveSameServerSideParent); |
| | 359 | | } |
| | 360 | | } |
| | 361 | |
|
| | 362 | | internal static Exception IncorrectTypeReturned |
| | 363 | | { |
| | 364 | | get |
| | 365 | | { |
| 0 | 366 | | return new InvalidOperationException(BatchErrorMessages.IncorrectTypeReturned); |
| | 367 | | } |
| | 368 | | } |
| | 369 | |
|
| | 370 | | /// <summary> |
| | 371 | | /// Reads entire Stream into a string. When null encoding is provided, UTF8 is used. |
| | 372 | | /// </summary> |
| | 373 | | /// <param name="s"></param> |
| | 374 | | /// <param name="encoding"></param> |
| | 375 | | /// <returns></returns> |
| | 376 | | internal static string StreamToString(Stream s, Encoding encoding) |
| | 377 | | { |
| 6 | 378 | | if (null == encoding) |
| | 379 | | { |
| 2 | 380 | | encoding = Encoding.UTF8; |
| | 381 | | } |
| | 382 | |
|
| | 383 | | //Avoid disposing of the underlying stream -- the streams lifetime is not owned by this method |
| | 384 | | //and disposing the reader will dispose the underlying stream. |
| | 385 | |
|
| | 386 | | //Note: detectEncodingFromByteOrderMarks default is true, and bufferSize default is 1024, we have to pass th |
| | 387 | | //get access to the leaveOpen parameter. |
| 6 | 388 | | using (StreamReader reader = new StreamReader(s, encoding, detectEncodingFromByteOrderMarks: true, bufferSiz |
| | 389 | | { |
| 6 | 390 | | string txt = reader.ReadToEnd(); |
| | 391 | |
|
| 6 | 392 | | return txt; |
| | 393 | | } |
| 6 | 394 | | } |
| | 395 | |
|
| | 396 | | /// <summary> |
| | 397 | | /// Enumerates an enumerable asyncronously if required. If the enumerable is of type IPagedCollection(T) then |
| | 398 | | /// ToListAsync is called on it. Otherwise the enumerable is returned as is. |
| | 399 | | /// </summary> |
| | 400 | | internal static async Task<IEnumerable<T>> EnumerateIfNeededAsync<T>(IEnumerable<T> enumerable, CancellationToke |
| | 401 | | { |
| | 402 | | IEnumerable<T> result; |
| | 403 | |
|
| 9 | 404 | | IPagedEnumerable<T> pagedEnumerable = enumerable as IPagedEnumerable<T>; |
| | 405 | |
|
| 9 | 406 | | if (pagedEnumerable != null) |
| | 407 | | { |
| 0 | 408 | | result = await pagedEnumerable.ToListAsync(cancellationToken).ConfigureAwait(false); |
| | 409 | | } |
| | 410 | | else |
| | 411 | | { |
| 9 | 412 | | result = enumerable; |
| | 413 | | } |
| | 414 | |
|
| 9 | 415 | | return result; |
| 9 | 416 | | } |
| | 417 | |
|
| | 418 | | internal static void WaitAndUnaggregateException(this Task task) |
| | 419 | | { |
| 74 | 420 | | task.GetAwaiter().GetResult(); |
| 68 | 421 | | } |
| | 422 | |
|
| | 423 | | internal static T WaitAndUnaggregateException<T>(this Task<T> task) |
| | 424 | | { |
| 57 | 425 | | return task.GetAwaiter().GetResult(); |
| | 426 | | } |
| | 427 | |
|
| | 428 | | internal static void WaitAndUnaggregateException(this Task task, IEnumerable<BatchClientBehavior> rootBehaviors, |
| | 429 | | { |
| 34 | 430 | | BehaviorManager bhMgr = new BehaviorManager(rootBehaviors, additionalBehaviors); |
| 34 | 431 | | SynchronousMethodExceptionBehavior exceptionBehavior = bhMgr.GetBehaviors<SynchronousMethodExceptionBehavior |
| 34 | 432 | | if (exceptionBehavior != null) |
| | 433 | | { |
| 0 | 434 | | exceptionBehavior.Wait(task); |
| | 435 | | } |
| | 436 | | else |
| | 437 | | { |
| 34 | 438 | | task.WaitAndUnaggregateException(); |
| | 439 | | } |
| 28 | 440 | | } |
| | 441 | |
|
| | 442 | | internal static T WaitAndUnaggregateException<T>(this Task<T> task, IEnumerable<BatchClientBehavior> rootBehavio |
| | 443 | | { |
| 41 | 444 | | BehaviorManager bhMgr = new BehaviorManager(rootBehaviors, additionalBehaviors); |
| 41 | 445 | | SynchronousMethodExceptionBehavior exceptionBehavior = bhMgr.GetBehaviors<SynchronousMethodExceptionBehavior |
| | 446 | | T result; |
| 41 | 447 | | if (exceptionBehavior != null) |
| | 448 | | { |
| 2 | 449 | | exceptionBehavior.Wait(task); |
| 0 | 450 | | result = task.Result; |
| | 451 | | } |
| | 452 | | else |
| | 453 | | { |
| 39 | 454 | | result = task.WaitAndUnaggregateException(); |
| | 455 | | } |
| | 456 | |
|
| 37 | 457 | | return result; |
| | 458 | | } |
| | 459 | |
|
| | 460 | | /// <summary> |
| | 461 | | /// Gets the object if the property has been changed, otherwise returns null. |
| | 462 | | /// </summary> |
| | 463 | | /// <exception cref="InvalidOperationException">When the property was changed to null, since the patch REST verb |
| | 464 | | internal static T? GetIfChangedOrNull<T>(this PropertyAccessor<T?> property) |
| | 465 | | where T : struct |
| | 466 | | { |
| 6 | 467 | | if (property.HasBeenModified) |
| | 468 | | { |
| 1 | 469 | | if (property.Value == null) |
| | 470 | | { |
| 0 | 471 | | throw new InvalidOperationException(BatchErrorMessages.CannotPatchNullValue); |
| | 472 | | } |
| 1 | 473 | | return property.Value; |
| | 474 | | } |
| | 475 | | else |
| | 476 | | { |
| 5 | 477 | | return null; |
| | 478 | | } |
| | 479 | | } |
| | 480 | |
|
| | 481 | | /// <summary> |
| | 482 | | /// Gets the transport object if the property has been changed, otherwise returns null. |
| | 483 | | /// </summary> |
| | 484 | | /// <exception cref="InvalidOperationException">When the property was changed to null, since the patch REST verb |
| | 485 | | internal static TProtocol GetTransportObjectIfChanged<TObjectModel, TProtocol>(this PropertyAccessor<TObjectMode |
| | 486 | | where TObjectModel : class, ITransportObjectProvider<TProtocol> |
| | 487 | | where TProtocol : class |
| | 488 | | { |
| 31 | 489 | | if (property.HasBeenModified) |
| | 490 | | { |
| 9 | 491 | | if (property.Value == null) |
| | 492 | | { |
| 3 | 493 | | throw new InvalidOperationException(BatchErrorMessages.CannotPatchNullValue); |
| | 494 | | } |
| 12 | 495 | | return CreateObjectWithNullCheck(property.Value, item => item.GetTransportObject()); |
| | 496 | | } |
| | 497 | | else |
| | 498 | | { |
| 22 | 499 | | return null; |
| | 500 | | } |
| | 501 | | } |
| | 502 | |
|
| | 503 | | /// <summary> |
| | 504 | | /// Gets the transport object if the property has been changed, otherwise returns null. |
| | 505 | | /// </summary> |
| | 506 | | /// <exception cref="InvalidOperationException">When the property was changed to null, since the patch REST verb |
| | 507 | | internal static TProtocol[] GetTransportObjectIfChanged<TObjectModel, TProtocol>(this PropertyAccessor<IList<TOb |
| | 508 | | where TObjectModel : class, ITransportObjectProvider<TProtocol> |
| | 509 | | where TProtocol : class |
| | 510 | | { |
| 30 | 511 | | if (property.HasBeenModified) |
| | 512 | | { |
| 3 | 513 | | if (property.Value == null) |
| | 514 | | { |
| 0 | 515 | | throw new InvalidOperationException(BatchErrorMessages.CannotPatchNullValue); |
| | 516 | | } |
| 3 | 517 | | return ConvertToProtocolArray(property.Value); |
| | 518 | | } |
| | 519 | | else |
| | 520 | | { |
| 27 | 521 | | return null; |
| | 522 | | } |
| | 523 | | } |
| | 524 | |
|
| | 525 | | /// <summary> |
| | 526 | | /// Begins asynchronous call to return the contents of the file as a string. |
| | 527 | | /// </summary> |
| | 528 | | /// <param name="copyStreamFunc">The stream copy function.</param> |
| | 529 | | /// <param name="encoding">The encoding used to interpret the file data. If no value or null is specified, UTF8 |
| | 530 | | /// <param name="byteRange">The file byte range to retrieve. If null, the entire file is retrieved.</param> |
| | 531 | | /// <param name="additionalBehaviors">A collection of BatchClientBehavior instances that are applied after the C |
| | 532 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> for controlling the lifetime of the asynch |
| | 533 | | /// <returns>A <see cref="System.Threading.Tasks.Task"/> object that represents the asynchronous operation.</ret |
| | 534 | | internal static async Task<string> ReadNodeFileAsStringAsync( |
| | 535 | | Func<Stream, GetFileRequestByteRange, IEnumerable<BatchClientBehavior>, CancellationToken, Task> copyStreamF |
| | 536 | | Encoding encoding, |
| | 537 | | GetFileRequestByteRange byteRange, |
| | 538 | | IEnumerable<BatchClientBehavior> additionalBehaviors, |
| | 539 | | CancellationToken cancellationToken) |
| | 540 | | { |
| 4 | 541 | | using (Stream streamToUse = new MemoryStream()) |
| | 542 | | { |
| | 543 | | // get the data |
| 4 | 544 | | Task asyncTask = copyStreamFunc(streamToUse, byteRange, additionalBehaviors, cancellationToken); |
| | 545 | |
|
| | 546 | | // wait for completion |
| 4 | 547 | | await asyncTask.ConfigureAwait(continueOnCapturedContext: false); |
| | 548 | |
|
| 2 | 549 | | streamToUse.Seek(0, SeekOrigin.Begin); //We just wrote to this stream, have to seek to the beginning |
| | 550 | | // convert to string |
| 2 | 551 | | return StreamToString(streamToUse, encoding); |
| | 552 | | } |
| 2 | 553 | | } |
| | 554 | | } |
| | 555 | |
|
| | 556 | | } |