diff options
Diffstat (limited to 'src/WixToolset.Data/IntermediateTupleDefinition.cs')
| -rw-r--r-- | src/WixToolset.Data/IntermediateTupleDefinition.cs | 163 |
1 files changed, 162 insertions, 1 deletions
diff --git a/src/WixToolset.Data/IntermediateTupleDefinition.cs b/src/WixToolset.Data/IntermediateTupleDefinition.cs index bc49bc7b..eb05c28b 100644 --- a/src/WixToolset.Data/IntermediateTupleDefinition.cs +++ b/src/WixToolset.Data/IntermediateTupleDefinition.cs | |||
| @@ -7,6 +7,8 @@ namespace WixToolset.Data | |||
| 7 | 7 | ||
| 8 | public class IntermediateTupleDefinition | 8 | public class IntermediateTupleDefinition |
| 9 | { | 9 | { |
| 10 | private object tags; | ||
| 11 | |||
| 10 | public IntermediateTupleDefinition(string name, IntermediateFieldDefinition[] fieldDefinitions, Type strongTupleType) | 12 | public IntermediateTupleDefinition(string name, IntermediateFieldDefinition[] fieldDefinitions, Type strongTupleType) |
| 11 | : this(TupleDefinitionType.MustBeFromAnExtension, name, 0, fieldDefinitions, strongTupleType) | 13 | : this(TupleDefinitionType.MustBeFromAnExtension, name, 0, fieldDefinitions, strongTupleType) |
| 12 | { | 14 | { |
| @@ -53,11 +55,131 @@ namespace WixToolset.Data | |||
| 53 | return result; | 55 | return result; |
| 54 | } | 56 | } |
| 55 | 57 | ||
| 58 | public bool AddTag(string add) | ||
| 59 | { | ||
| 60 | if (this.tags == null) | ||
| 61 | { | ||
| 62 | this.tags = add; | ||
| 63 | } | ||
| 64 | else if (this.tags is string tag) | ||
| 65 | { | ||
| 66 | if (tag == add) | ||
| 67 | { | ||
| 68 | return false; | ||
| 69 | } | ||
| 70 | |||
| 71 | this.tags = new[] { tag, add }; | ||
| 72 | } | ||
| 73 | else | ||
| 74 | { | ||
| 75 | var tagsArray = (string[])this.tags; | ||
| 76 | var array = new string[tagsArray.Length + 1]; | ||
| 77 | |||
| 78 | for (var i = 0; i < tagsArray.Length; ++i) | ||
| 79 | { | ||
| 80 | if (tagsArray[i] == add) | ||
| 81 | { | ||
| 82 | return false; | ||
| 83 | } | ||
| 84 | |||
| 85 | array[i] = tagsArray[i]; | ||
| 86 | } | ||
| 87 | |||
| 88 | array[tagsArray.Length] = add; | ||
| 89 | |||
| 90 | this.tags = array; | ||
| 91 | } | ||
| 92 | |||
| 93 | return true; | ||
| 94 | } | ||
| 95 | |||
| 96 | public bool HasTag(string has) | ||
| 97 | { | ||
| 98 | if (this.tags == null) | ||
| 99 | { | ||
| 100 | return false; | ||
| 101 | } | ||
| 102 | else if (this.tags is string tag) | ||
| 103 | { | ||
| 104 | return tag == has; | ||
| 105 | } | ||
| 106 | else | ||
| 107 | { | ||
| 108 | foreach (var element in (string[])this.tags) | ||
| 109 | { | ||
| 110 | if (element == has) | ||
| 111 | { | ||
| 112 | return true; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | return false; | ||
| 118 | } | ||
| 119 | |||
| 120 | public bool RemoveTag(string remove) | ||
| 121 | { | ||
| 122 | if (this.tags is string tag) | ||
| 123 | { | ||
| 124 | if (tag == remove) | ||
| 125 | { | ||
| 126 | this.tags = null; | ||
| 127 | return true; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | else if (this.tags is string[] tagsArray) | ||
| 131 | { | ||
| 132 | if (tagsArray.Length == 2) | ||
| 133 | { | ||
| 134 | if (tagsArray[0] == remove) | ||
| 135 | { | ||
| 136 | this.tags = tagsArray[1]; | ||
| 137 | return true; | ||
| 138 | } | ||
| 139 | else if (tagsArray[1] == remove) | ||
| 140 | { | ||
| 141 | this.tags = tagsArray[0]; | ||
| 142 | return true; | ||
| 143 | } | ||
| 144 | } | ||
| 145 | else | ||
| 146 | { | ||
| 147 | var array = new string[tagsArray.Length - 1]; | ||
| 148 | var arrayIndex = 0; | ||
| 149 | var found = false; | ||
| 150 | |||
| 151 | for (var i = 0; i < tagsArray.Length; ++i) | ||
| 152 | { | ||
| 153 | if (tagsArray[i] == remove) | ||
| 154 | { | ||
| 155 | found = true; | ||
| 156 | continue; | ||
| 157 | } | ||
| 158 | else if (arrayIndex == array.Length) | ||
| 159 | { | ||
| 160 | break; | ||
| 161 | } | ||
| 162 | |||
| 163 | array[arrayIndex++] = tagsArray[i]; | ||
| 164 | } | ||
| 165 | |||
| 166 | if (found) | ||
| 167 | { | ||
| 168 | this.tags = array; | ||
| 169 | return true; | ||
| 170 | } | ||
| 171 | } | ||
| 172 | } | ||
| 173 | |||
| 174 | return false; | ||
| 175 | } | ||
| 176 | |||
| 56 | internal static IntermediateTupleDefinition Deserialize(JsonObject jsonObject) | 177 | internal static IntermediateTupleDefinition Deserialize(JsonObject jsonObject) |
| 57 | { | 178 | { |
| 58 | var name = jsonObject.GetValueOrDefault<string>("name"); | 179 | var name = jsonObject.GetValueOrDefault<string>("name"); |
| 59 | var revision = jsonObject.GetValueOrDefault("rev", 0); | 180 | var revision = jsonObject.GetValueOrDefault("rev", 0); |
| 60 | var definitionsJson = jsonObject.GetValueOrDefault<JsonArray>("fields"); | 181 | var definitionsJson = jsonObject.GetValueOrDefault<JsonArray>("fields"); |
| 182 | var tagsJson = jsonObject.GetValueOrDefault<JsonArray>("tags"); | ||
| 61 | 183 | ||
| 62 | var fieldDefinitions = new IntermediateFieldDefinition[definitionsJson.Count]; | 184 | var fieldDefinitions = new IntermediateFieldDefinition[definitionsJson.Count]; |
| 63 | 185 | ||
| @@ -69,7 +191,28 @@ namespace WixToolset.Data | |||
| 69 | fieldDefinitions[i] = new IntermediateFieldDefinition(fieldName, fieldType); | 191 | fieldDefinitions[i] = new IntermediateFieldDefinition(fieldName, fieldType); |
| 70 | } | 192 | } |
| 71 | 193 | ||
| 72 | return new IntermediateTupleDefinition(name, revision, fieldDefinitions, null); | 194 | var definition = new IntermediateTupleDefinition(name, revision, fieldDefinitions, null); |
| 195 | |||
| 196 | if (tagsJson == null || tagsJson.Count == 0) | ||
| 197 | { | ||
| 198 | } | ||
| 199 | else if (tagsJson.Count == 1) | ||
| 200 | { | ||
| 201 | definition.tags = (string)tagsJson[0]; | ||
| 202 | } | ||
| 203 | else | ||
| 204 | { | ||
| 205 | var tags = new string[tagsJson.Count]; | ||
| 206 | |||
| 207 | for (var i = 0; i < tagsJson.Count; ++i) | ||
| 208 | { | ||
| 209 | tags[i] = (string)tagsJson[i]; | ||
| 210 | } | ||
| 211 | |||
| 212 | definition.tags = tags; | ||
| 213 | } | ||
| 214 | |||
| 215 | return definition; | ||
| 73 | } | 216 | } |
| 74 | 217 | ||
| 75 | internal JsonObject Serialize() | 218 | internal JsonObject Serialize() |
| @@ -103,6 +246,24 @@ namespace WixToolset.Data | |||
| 103 | 246 | ||
| 104 | jsonObject.Add("fields", fieldsJson); | 247 | jsonObject.Add("fields", fieldsJson); |
| 105 | 248 | ||
| 249 | if (this.tags is string || this.tags is string[]) | ||
| 250 | { | ||
| 251 | JsonArray tagsJson; | ||
| 252 | |||
| 253 | if (this.tags is string tag) | ||
| 254 | { | ||
| 255 | tagsJson = new JsonArray(1) { tag }; | ||
| 256 | } | ||
| 257 | else | ||
| 258 | { | ||
| 259 | var array = (string[])this.tags; | ||
| 260 | tagsJson = new JsonArray(array.Length); | ||
| 261 | tagsJson.AddRange(array); | ||
| 262 | } | ||
| 263 | |||
| 264 | jsonObject.Add("tags", tagsJson); | ||
| 265 | } | ||
| 266 | |||
| 106 | return jsonObject; | 267 | return jsonObject; |
| 107 | } | 268 | } |
| 108 | } | 269 | } |
