< Summary

Class:Azure.Core.Optional
Assembly:Azure.Security.KeyVault.Administration
File(s):C:\Git\azure-sdk-for-net\sdk\core\Azure.Core\src\Shared\AutoRest\Optional.cs
Covered lines:5
Uncovered lines:17
Coverable lines:22
Total lines:105
Line coverage:22.7% (5 of 22)
Covered branches:2
Total branches:16
Branch coverage:12.5% (2 of 16)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
IsCollectionDefined(...)-0%0%
IsCollectionDefined(...)-0%0%
IsCollectionDefined(...)-0%0%
IsDefined(...)-0%100%
IsDefined(...)-0%100%
IsDefined(...)-0%100%
ToDictionary(...)-0%0%
ToDictionary(...)-0%0%
ToList(...)-66.67%50%
ToList(...)-0%0%
ToNullable(...)-66.67%50%
ToNullable(...)-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\core\Azure.Core\src\Shared\AutoRest\Optional.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4#nullable disable
 5
 6using System.Collections.Generic;
 7using System.Linq;
 8
 9namespace Azure.Core
 10{
 11    internal static class Optional
 12    {
 13        public static bool IsCollectionDefined<T>(IEnumerable<T> collection)
 14        {
 015            return !(collection is ChangeTrackingList<T> changeTrackingList && changeTrackingList.IsUndefined);
 16        }
 17
 18        public static bool IsCollectionDefined<TKey, TValue>(IReadOnlyDictionary<TKey, TValue> collection)
 19        {
 020            return !(collection is ChangeTrackingDictionary<TKey, TValue> changeTrackingList && changeTrackingList.IsUnd
 21        }
 22
 23        public static bool IsCollectionDefined<TKey, TValue>(IDictionary<TKey, TValue> collection)
 24        {
 025            return !(collection is ChangeTrackingDictionary<TKey, TValue> changeTrackingList && changeTrackingList.IsUnd
 26        }
 27
 28        public static bool IsDefined<T>(T? value) where T: struct
 29        {
 030            return value.HasValue;
 31        }
 32        public static bool IsDefined(object value)
 33        {
 034            return value != null;
 35        }
 36        public static bool IsDefined(string value)
 37        {
 038            return value != null;
 39        }
 40
 41        public static IReadOnlyDictionary<TKey, TValue> ToDictionary<TKey, TValue>(Optional<IReadOnlyDictionary<TKey, TV
 42        {
 043            if (optional.HasValue)
 44            {
 045                return optional.Value;
 46            }
 047            return new ChangeTrackingDictionary<TKey, TValue>(optional);
 48        }
 49
 50        public static IDictionary<TKey, TValue> ToDictionary<TKey, TValue>(Optional<IDictionary<TKey, TValue>> optional)
 51        {
 052            if (optional.HasValue)
 53            {
 054                return optional.Value;
 55            }
 056            return new ChangeTrackingDictionary<TKey, TValue>(optional);
 57        }
 58        public static IReadOnlyList<T> ToList<T>(Optional<IReadOnlyList<T>> optional)
 59        {
 126460            if (optional.HasValue)
 61            {
 126462                return optional.Value;
 63            }
 064            return new ChangeTrackingList<T>(optional);
 65        }
 66
 67        public static IList<T> ToList<T>(Optional<IList<T>> optional)
 68        {
 069            if (optional.HasValue)
 70            {
 071                return optional.Value;
 72            }
 073            return new ChangeTrackingList<T>(optional);
 74        }
 75
 76        public static T? ToNullable<T>(Optional<T> optional) where T: struct
 77        {
 2278            if (optional.HasValue)
 79            {
 2280                return optional.Value;
 81            }
 082            return default;
 83        }
 84
 85        public static T? ToNullable<T>(Optional<T?> optional) where T: struct
 86        {
 2287            return optional.Value;
 88        }
 89    }
 90
 91    internal readonly partial struct Optional<T>
 92    {
 93        public Optional(T value) : this()
 94        {
 95            Value = value;
 96            HasValue = true;
 97        }
 98
 99        public T Value { get; }
 100        public bool HasValue { get; }
 101
 102        public static implicit operator Optional<T>(T value) => new Optional<T>(value);
 103        public static implicit operator T(Optional<T> optional) => optional.Value;
 104    }
 105}