|
13 | 13 | // ----------------------------------------------------------------------------------
|
14 | 14 |
|
15 | 15 | using System;
|
| 16 | +using System.Collections; |
| 17 | +using System.Collections.Generic; |
| 18 | +using System.Linq; |
16 | 19 | using ClientModel = Microsoft.Azure.Management.BackupServices.Models;
|
17 | 20 | using CmdletModel = Microsoft.Azure.Commands.AzureBackup.Models;
|
18 | 21 |
|
@@ -50,5 +53,76 @@ public static string GetResourceGroup(string vaultId)
|
50 | 53 | string[] tokens = vaultId.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
|
51 | 54 | return tokens[3];
|
52 | 55 | }
|
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// Extension to convert enumerable Hashtable into a dictionary |
| 59 | + /// </summary> |
| 60 | + /// <param name="tags"></param> |
| 61 | + /// <returns></returns> |
| 62 | + public static Dictionary<string, string> ConvertToDictionary(this Hashtable[] tags) |
| 63 | + { |
| 64 | + return tags == null |
| 65 | + ? null |
| 66 | + : tags |
| 67 | + .CoalesceEnumerable() |
| 68 | + .Select(hashTable => hashTable.OfType<DictionaryEntry>() |
| 69 | + .ToDictionary(kvp => kvp.Key.ToString(), kvp => kvp.Value)) |
| 70 | + .Where(tagDictionary => tagDictionary.ContainsKey("Name")) |
| 71 | + .Select(tagDictionary => Tuple |
| 72 | + .Create( |
| 73 | + tagDictionary["Name"].ToString(), |
| 74 | + tagDictionary.ContainsKey("Value") ? tagDictionary["Value"].ToString() : string.Empty)) |
| 75 | + .Distinct(kvp => kvp.Item1) |
| 76 | + .ToDictionary(kvp => kvp.Item1, kvp => kvp.Item2); |
| 77 | + } |
| 78 | + |
| 79 | + /// <summary> |
| 80 | + /// Extension to coalesce enumerable |
| 81 | + /// </summary> |
| 82 | + /// <typeparam name="TSource">Enumerable type</typeparam> |
| 83 | + /// <param name="source">Enumerable</param> |
| 84 | + /// <returns></returns> |
| 85 | + public static IEnumerable<TSource> CoalesceEnumerable<TSource>(this IEnumerable<TSource> source) |
| 86 | + { |
| 87 | + return source ?? Enumerable.Empty<TSource>(); |
| 88 | + } |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// Extension to remove duplicates from enumerable based on a provided key selector |
| 92 | + /// </summary> |
| 93 | + /// <typeparam name="TSource">Enumerable type</typeparam> |
| 94 | + /// <typeparam name="TKeyType">Type of key</typeparam> |
| 95 | + /// <param name="source">Input enumerable to remove duplicates from</param> |
| 96 | + /// <param name="keySelector">Lambda to select key</param> |
| 97 | + /// <returns></returns> |
| 98 | + public static IEnumerable<TSource> Distinct<TSource, TKeyType>(this IEnumerable<TSource> source, Func<TSource, TKeyType> keySelector) |
| 99 | + { |
| 100 | + var set = new Dictionary<TKeyType, TSource>(EqualityComparer<TKeyType>.Default); |
| 101 | + foreach (TSource element in source) |
| 102 | + { |
| 103 | + TSource value; |
| 104 | + var key = keySelector(element); |
| 105 | + if (!set.TryGetValue(key, out value)) |
| 106 | + { |
| 107 | + yield return element; |
| 108 | + } |
| 109 | + else |
| 110 | + { |
| 111 | + set[key] = value; |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + /// <summary> |
| 117 | + /// Extension to convert dictionary to hashtable enumerable |
| 118 | + /// </summary> |
| 119 | + /// <param name="tags"></param> |
| 120 | + /// <returns></returns> |
| 121 | + public static Hashtable[] GetTagsHashtables(this IDictionary<string, string> tags) |
| 122 | + { |
| 123 | + return tags == null |
| 124 | + ? null |
| 125 | + : tags.Select(kvp => new Hashtable { { "Name", kvp.Key }, { "Value", kvp.Value } }).ToArray(); |
| 126 | + } |
53 | 127 | }
|
54 | 128 | }
|
0 commit comments