aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Data/IntermediateTuple.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/WixToolset.Data/IntermediateTuple.cs')
-rw-r--r--src/WixToolset.Data/IntermediateTuple.cs159
1 files changed, 159 insertions, 0 deletions
diff --git a/src/WixToolset.Data/IntermediateTuple.cs b/src/WixToolset.Data/IntermediateTuple.cs
index bc46e3b4..8a5858ee 100644
--- a/src/WixToolset.Data/IntermediateTuple.cs
+++ b/src/WixToolset.Data/IntermediateTuple.cs
@@ -9,6 +9,8 @@ namespace WixToolset.Data
9 [DebuggerDisplay("{DebuggerDisplay,nq}")] 9 [DebuggerDisplay("{DebuggerDisplay,nq}")]
10 public class IntermediateTuple 10 public class IntermediateTuple
11 { 11 {
12 private object tags;
13
12 public IntermediateTuple(IntermediateTupleDefinition definition) : this(definition, null, null) 14 public IntermediateTuple(IntermediateTupleDefinition definition) : this(definition, null, null)
13 { 15 {
14 } 16 }
@@ -33,12 +35,132 @@ namespace WixToolset.Data
33 35
34 private string DebuggerDisplay => $"{this.Definition?.Name} {this.Id?.Id}"; 36 private string DebuggerDisplay => $"{this.Definition?.Name} {this.Id?.Id}";
35 37
38 public bool AddTag(string add)
39 {
40 if (this.tags == null)
41 {
42 this.tags = add;
43 }
44 else if (this.tags is string tag)
45 {
46 if (tag == add)
47 {
48 return false;
49 }
50
51 this.tags = new[] { tag, add };
52 }
53 else
54 {
55 var tagsArray = (string[])this.tags;
56 var array = new string[tagsArray.Length + 1];
57
58 for (var i = 0; i < tagsArray.Length; ++i)
59 {
60 if (tagsArray[i] == add)
61 {
62 return false;
63 }
64
65 array[i] = tagsArray[i];
66 }
67
68 array[tagsArray.Length] = add;
69
70 this.tags = array;
71 }
72
73 return true;
74 }
75
76 public bool HasTag(string has)
77 {
78 if (this.tags == null)
79 {
80 return false;
81 }
82 else if (this.tags is string tag)
83 {
84 return tag == has;
85 }
86 else
87 {
88 foreach (var element in (string[])this.tags)
89 {
90 if (element == has)
91 {
92 return true;
93 }
94 }
95 }
96
97 return false;
98 }
99
100 public bool RemoveTag(string remove)
101 {
102 if (this.tags is string tag)
103 {
104 if (tag == remove)
105 {
106 this.tags = null;
107 return true;
108 }
109 }
110 else if (this.tags is string[] tagsArray)
111 {
112 if (tagsArray.Length == 2)
113 {
114 if (tagsArray[0] == remove)
115 {
116 this.tags = tagsArray[1];
117 return true;
118 }
119 else if (tagsArray[1] == remove)
120 {
121 this.tags = tagsArray[0];
122 return true;
123 }
124 }
125 else
126 {
127 var array = new string[tagsArray.Length - 1];
128 var arrayIndex = 0;
129 var found = false;
130
131 for (var i = 0; i < tagsArray.Length; ++i)
132 {
133 if (tagsArray[i] == remove)
134 {
135 found = true;
136 continue;
137 }
138 else if (arrayIndex == array.Length)
139 {
140 break;
141 }
142
143 array[arrayIndex++] = tagsArray[i];
144 }
145
146 if (found)
147 {
148 this.tags = array;
149 return true;
150 }
151 }
152 }
153
154 return false;
155 }
156
36 internal static IntermediateTuple Deserialize(ITupleDefinitionCreator creator, Uri baseUri, JsonObject jsonObject) 157 internal static IntermediateTuple Deserialize(ITupleDefinitionCreator creator, Uri baseUri, JsonObject jsonObject)
37 { 158 {
38 var definitionName = jsonObject.GetValueOrDefault<string>("type"); 159 var definitionName = jsonObject.GetValueOrDefault<string>("type");
39 var idJson = jsonObject.GetValueOrDefault<JsonObject>("id"); 160 var idJson = jsonObject.GetValueOrDefault<JsonObject>("id");
40 var sourceLineNumbersJson = jsonObject.GetValueOrDefault<JsonObject>("ln"); 161 var sourceLineNumbersJson = jsonObject.GetValueOrDefault<JsonObject>("ln");
41 var fieldsJson = jsonObject.GetValueOrDefault<JsonArray>("fields"); 162 var fieldsJson = jsonObject.GetValueOrDefault<JsonArray>("fields");
163 var tagsJson = jsonObject.GetValueOrDefault<JsonArray>("tags");
42 164
43 var id = (idJson == null) ? null : Identifier.Deserialize(idJson); 165 var id = (idJson == null) ? null : Identifier.Deserialize(idJson);
44 var sourceLineNumbers = (sourceLineNumbersJson == null) ? null : SourceLineNumber.Deserialize(sourceLineNumbersJson); 166 var sourceLineNumbers = (sourceLineNumbersJson == null) ? null : SourceLineNumber.Deserialize(sourceLineNumbersJson);
@@ -54,6 +176,25 @@ namespace WixToolset.Data
54 } 176 }
55 } 177 }
56 178
179 if (tagsJson == null || tagsJson.Count == 0)
180 {
181 }
182 else if (tagsJson.Count == 1)
183 {
184 tuple.tags = (string)tagsJson[0];
185 }
186 else
187 {
188 var tags = new string[tagsJson.Count];
189
190 for (var i = 0; i < tagsJson.Count; ++i)
191 {
192 tags[i] = (string)tagsJson[i];
193 }
194
195 tuple.tags = tags;
196 }
197
57 return tuple; 198 return tuple;
58 } 199 }
59 200
@@ -86,6 +227,24 @@ namespace WixToolset.Data
86 227
87 jsonObject.Add("fields", fieldsJson); 228 jsonObject.Add("fields", fieldsJson);
88 229
230 if (this.tags is string || this.tags is string[])
231 {
232 JsonArray tagsJson;
233
234 if (this.tags is string tag)
235 {
236 tagsJson = new JsonArray(1) { tag };
237 }
238 else
239 {
240 var array = (string[])this.tags;
241 tagsJson = new JsonArray(array.Length);
242 tagsJson.AddRange(array);
243 }
244
245 jsonObject.Add("tags", tagsJson);
246 }
247
89 return jsonObject; 248 return jsonObject;
90 } 249 }
91 } 250 }