aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Data/IntermediateFieldExtensions.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/WixToolset.Data/IntermediateFieldExtensions.cs')
-rw-r--r--src/WixToolset.Data/IntermediateFieldExtensions.cs206
1 files changed, 206 insertions, 0 deletions
diff --git a/src/WixToolset.Data/IntermediateFieldExtensions.cs b/src/WixToolset.Data/IntermediateFieldExtensions.cs
new file mode 100644
index 00000000..c551b455
--- /dev/null
+++ b/src/WixToolset.Data/IntermediateFieldExtensions.cs
@@ -0,0 +1,206 @@
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
3namespace WixToolset.Data
4{
5 using System;
6
7 public static class IntermediateFieldExtensions
8 {
9 [ThreadStatic]
10 internal static string valueContext;
11
12 public static IntermediateField Set(this IntermediateField field, object value)
13 {
14 if (field == null)
15 {
16 throw new ArgumentNullException(nameof(field));
17 }
18 else if (value == null)
19 {
20 // Null is always allowed.
21 }
22 else if (field.Type == IntermediateFieldType.Bool && !(value is bool))
23 {
24 throw new ArgumentException(nameof(value));
25 }
26 else if (field.Type == IntermediateFieldType.Number && !(value is int))
27 {
28 throw new ArgumentException(nameof(value));
29 }
30 else if (field.Type == IntermediateFieldType.String && !(value is string))
31 {
32 throw new ArgumentException(nameof(value));
33 }
34 else if (field.Type == IntermediateFieldType.Path && !(value is IntermediateFieldPathValue || value is string))
35 {
36 throw new ArgumentException(nameof(value));
37 }
38
39 if (field.Type == IntermediateFieldType.Path && value != null && value is string)
40 {
41 value = new IntermediateFieldPathValue { Path = (string)value };
42 }
43
44 field.Value = new IntermediateFieldValue
45 {
46 Context = valueContext,
47 Data = value,
48 PreviousValue = field.Value
49 };
50
51 return field;
52 }
53
54 public static IntermediateField Set(this IntermediateField field, IntermediateFieldDefinition definition, object value)
55 {
56 if (field == null)
57 {
58 field = new IntermediateField(definition);
59 }
60
61 return field.Set(value);
62 }
63
64 public static bool AsBool(this IntermediateField field)
65 {
66 if (field == null || field.Value == null || field.Value.Data == null)
67 {
68 return false;
69 }
70
71 switch (field.Definition.Type)
72 {
73 case IntermediateFieldType.Bool:
74 return field.Value.AsBool();
75
76 case IntermediateFieldType.Number:
77 return field.Value.AsNumber() != 0;
78
79 case IntermediateFieldType.String:
80 return !String.IsNullOrEmpty(field.Value.AsString());
81
82 case IntermediateFieldType.Path:
83 return !String.IsNullOrEmpty(field.Value.AsPath()?.Path);
84
85 default:
86 throw new InvalidCastException($"Cannot convert field {field.Name} with type {field.Type} to boolean");
87 }
88 }
89
90 public static bool? AsNullableBool(this IntermediateField field)
91 {
92 if (field == null || field.Value == null || field.Value.Data == null)
93 {
94 return null;
95 }
96
97 switch (field.Definition.Type)
98 {
99 case IntermediateFieldType.Bool:
100 return field.Value.AsBool();
101
102 case IntermediateFieldType.Number:
103 return field.Value.AsNumber() != 0;
104
105 case IntermediateFieldType.String:
106 return !System.String.IsNullOrEmpty(field.Value.AsString());
107
108 default:
109 throw new InvalidCastException($"Cannot convert field {field.Name} with type {field.Type} to boolean");
110 }
111 }
112
113
114 public static int AsNumber(this IntermediateField field)
115 {
116 if (field == null || field.Value == null || field.Value.Data == null)
117 {
118 return 0;
119 }
120
121 switch (field.Definition.Type)
122 {
123 case IntermediateFieldType.Bool:
124 return field.Value.AsBool() ? 1 : 0;
125
126 case IntermediateFieldType.Number:
127 return field.Value.AsNumber();
128
129 case IntermediateFieldType.String:
130 return Convert.ToInt32(field.Value.AsString());
131
132 default:
133 throw new InvalidCastException($"Cannot convert field {field.Name} with type {field.Type} to number");
134 }
135 }
136
137 public static int? AsNullableNumber(this IntermediateField field)
138 {
139 if (field == null || field.Value == null || field.Value.Data == null)
140 {
141 return null;
142 }
143
144 switch (field.Definition.Type)
145 {
146 case IntermediateFieldType.Bool:
147 return field.Value.AsBool() ? 1 : 0;
148
149 case IntermediateFieldType.Number:
150 return field.Value.AsNumber();
151
152 case IntermediateFieldType.String:
153 return Convert.ToInt32(field.Value.AsString());
154
155 default:
156 throw new InvalidCastException($"Cannot convert field {field.Name} with type {field.Type} to number");
157 }
158 }
159
160 public static IntermediateFieldPathValue AsPath(this IntermediateField field)
161 {
162 if (field == null || field.Value == null || field.Value.Data == null)
163 {
164 return null;
165 }
166
167 switch (field.Definition.Type)
168 {
169 case IntermediateFieldType.String:
170 return new IntermediateFieldPathValue { Path = field.Value.AsString() };
171
172 case IntermediateFieldType.Path:
173 return field.Value.AsPath();
174
175 default:
176 throw new InvalidCastException($"Cannot convert field {field.Name} with type {field.Type} to string");
177 }
178 }
179
180 public static string AsString(this IntermediateField field)
181 {
182 if (field == null || field.Value == null || field.Value.Data == null)
183 {
184 return null;
185 }
186
187 switch (field.Definition.Type)
188 {
189 case IntermediateFieldType.Bool:
190 return field.Value.AsBool() ? "true" : "false";
191
192 case IntermediateFieldType.Number:
193 return field.Value.AsNumber().ToString();
194
195 case IntermediateFieldType.String:
196 return field.Value.AsString();
197
198 case IntermediateFieldType.Path:
199 return field.Value.AsPath()?.Path;
200
201 default:
202 throw new InvalidCastException($"Cannot convert field {field.Name} with type {field.Type} to string");
203 }
204 }
205 }
206}