| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. |
| 2 | |
| 3 | namespace WixToolset.Data |
| 4 | { |
| 5 | using System; |
| 6 | |
| 7 | public static class IntermediateFieldValueExtensions |
| 8 | { |
| 9 | public static bool AsBool(this IntermediateFieldValue value) |
| 10 | { |
| 11 | if (value.Data is bool b) |
| 12 | { |
| 13 | return b; |
| 14 | } |
| 15 | else if (value.Data is int n) |
| 16 | { |
| 17 | return n != 0; |
| 18 | } |
| 19 | else if (value.Data is long l) |
| 20 | { |
| 21 | return l != 0; |
| 22 | } |
| 23 | else if (value.Data is string s) |
| 24 | { |
| 25 | if (s.Equals("yes", StringComparison.OrdinalIgnoreCase) || s.Equals("true", StringComparison.OrdinalIgnoreCase)) |
| 26 | { |
| 27 | return true; |
| 28 | } |
| 29 | else if (s.Equals("no", StringComparison.OrdinalIgnoreCase) || s.Equals("false", StringComparison.OrdinalIgnoreCase)) |
| 30 | { |
| 31 | return false; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | return (bool)value.Data; |
| 36 | } |
| 37 | |
| 38 | public static bool? AsNullableBool(this IntermediateFieldValue value) |
| 39 | { |
| 40 | if (value?.Data == null) |
| 41 | { |
| 42 | return null; |
| 43 | } |
| 44 | |
| 45 | return value.AsBool(); |
| 46 | } |
| 47 | |
| 48 | public static long AsLargeNumber(this IntermediateFieldValue value) |
| 49 | { |
| 50 | if (value.Data is long l) |
| 51 | { |
| 52 | return l; |
| 53 | } |
| 54 | else if (value.Data is int n) |
| 55 | { |
| 56 | return n; |
| 57 | } |
| 58 | else if (value.Data is bool b) |
| 59 | { |
| 60 | return b ? 1 : 0; |
| 61 | } |
| 62 | else if (value.Data is string s) |
| 63 | { |
| 64 | try |
| 65 | { |
| 66 | return Convert.ToInt32(s); |
| 67 | } |
| 68 | catch (FormatException) |
| 69 | { |
| 70 | throw new WixException(ErrorMessages.UnableToConvertFieldToNumber(s)); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return (long)value.Data; |
| 75 | } |
| 76 | |
| 77 | public static long? AsNullableLargeNumber(this IntermediateFieldValue value) |
| 78 | { |
| 79 | if (value?.Data == null) |
| 80 | { |
| 81 | return null; |
| 82 | } |
| 83 | |
| 84 | return value.AsLargeNumber(); |
| 85 | } |
| 86 | |
| 87 | public static int AsNumber(this IntermediateFieldValue value) |
| 88 | { |
| 89 | if (value.Data is int n) |
| 90 | { |
| 91 | return n; |
| 92 | } |
| 93 | else if (value.Data is long l) |
| 94 | { |
| 95 | return (int)l; |
| 96 | } |
| 97 | else if (value.Data is bool b) |
| 98 | { |
| 99 | return b ? 1 : 0; |
| 100 | } |
| 101 | else if (value.Data is string s) |
| 102 | { |
| 103 | try |
| 104 | { |
| 105 | return Convert.ToInt32(s); |
| 106 | } |
| 107 | catch (FormatException) |
| 108 | { |
| 109 | throw new WixException(ErrorMessages.UnableToConvertFieldToNumber(s)); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return (int)value.Data; |
| 114 | } |
| 115 | |
| 116 | public static int? AsNullableNumber(this IntermediateFieldValue value) |
| 117 | { |
| 118 | if (value?.Data == null) |
| 119 | { |
| 120 | return null; |
| 121 | } |
| 122 | |
| 123 | return value.AsNumber(); |
| 124 | } |
| 125 | |
| 126 | public static IntermediateFieldPathValue AsPath(this IntermediateFieldValue value) |
| 127 | { |
| 128 | return (IntermediateFieldPathValue)value?.Data; |
| 129 | } |
| 130 | |
| 131 | public static string AsString(this IntermediateFieldValue value) |
| 132 | { |
| 133 | if (value?.Data == null) |
| 134 | { |
| 135 | return null; |
| 136 | } |
| 137 | else if (value.Data is string s) |
| 138 | { |
| 139 | return s; |
| 140 | } |
| 141 | else if (value.Data is int n) |
| 142 | { |
| 143 | return n.ToString(); |
| 144 | } |
| 145 | else if (value.Data is long l) |
| 146 | { |
| 147 | return l.ToString(); |
| 148 | } |
| 149 | else if (value.Data is bool b) |
| 150 | { |
| 151 | return b ? "true" : "false"; |
| 152 | } |
| 153 | else if (value.Data is IntermediateFieldPathValue p) |
| 154 | { |
| 155 | return p.Path; |
| 156 | } |
| 157 | |
| 158 | return (string)value.Data; |
| 159 | } |
| 160 | } |
| 161 | } |