ValueSetExtensions.cs (2557B)
1 // ----------------------------------------------------------------------------- 2 // <copyright file="ValueSetExtensions.cs" company="Microsoft Corporation"> 3 // Copyright (c) Microsoft Corporation. Licensed under the MIT License. 4 // </copyright> 5 // ----------------------------------------------------------------------------- 6 7 namespace Microsoft.Management.Configuration.UnitTests.Helpers 8 { 9 using System; 10 using System.Text; 11 using Windows.Foundation.Collections; 12 13 /// <summary> 14 /// Extensions for ValueSet. 15 /// </summary> 16 internal static class ValueSetExtensions 17 { 18 /// <summary> 19 /// Converts the value set to YAML like output. 20 /// </summary> 21 /// <param name="set">The set to output.</param> 22 /// <returns>The string.</returns> 23 public static string ToYaml(this ValueSet set) 24 { 25 StringBuilder sb = new StringBuilder(); 26 ToYaml(set, sb); 27 return sb.ToString(); 28 } 29 30 private static void ToYaml(ValueSet set, StringBuilder sb, int indentation = 0) 31 { 32 foreach (var keyValuePair in set) 33 { 34 bool addLine = true; 35 36 sb.Append(' ', indentation); 37 sb.Append(keyValuePair.Key); 38 sb.Append(": "); 39 40 if (keyValuePair.Value == null) 41 { 42 sb.Append("null"); 43 } 44 else 45 { 46 switch (keyValuePair.Value) 47 { 48 case int i: 49 sb.Append(i); 50 break; 51 case long l: 52 sb.Append(l); 53 break; 54 case string s: 55 sb.Append(s); 56 break; 57 case bool b: 58 sb.Append(b); 59 break; 60 case ValueSet v: 61 sb.AppendLine(); 62 ToYaml(v, sb, indentation + 2); 63 addLine = false; 64 break; 65 default: 66 throw new NotImplementedException($"Add ToYaml type `{keyValuePair.Value.GetType().Name}`"); 67 } 68 } 69 70 if (addLine) 71 { 72 sb.AppendLine(); 73 } 74 } 75 } 76 } 77 }