From 7e87a6b75d0570c3ebfbf0e537fcf04e1cf84224 Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Mon, 7 Oct 2019 07:33:18 -0700 Subject: Support tagging tuples and definitions --- src/WixToolset.Data/IntermediateTuple.cs | 159 +++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) (limited to 'src/WixToolset.Data/IntermediateTuple.cs') 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 [DebuggerDisplay("{DebuggerDisplay,nq}")] public class IntermediateTuple { + private object tags; + public IntermediateTuple(IntermediateTupleDefinition definition) : this(definition, null, null) { } @@ -33,12 +35,132 @@ namespace WixToolset.Data private string DebuggerDisplay => $"{this.Definition?.Name} {this.Id?.Id}"; + public bool AddTag(string add) + { + if (this.tags == null) + { + this.tags = add; + } + else if (this.tags is string tag) + { + if (tag == add) + { + return false; + } + + this.tags = new[] { tag, add }; + } + else + { + var tagsArray = (string[])this.tags; + var array = new string[tagsArray.Length + 1]; + + for (var i = 0; i < tagsArray.Length; ++i) + { + if (tagsArray[i] == add) + { + return false; + } + + array[i] = tagsArray[i]; + } + + array[tagsArray.Length] = add; + + this.tags = array; + } + + return true; + } + + public bool HasTag(string has) + { + if (this.tags == null) + { + return false; + } + else if (this.tags is string tag) + { + return tag == has; + } + else + { + foreach (var element in (string[])this.tags) + { + if (element == has) + { + return true; + } + } + } + + return false; + } + + public bool RemoveTag(string remove) + { + if (this.tags is string tag) + { + if (tag == remove) + { + this.tags = null; + return true; + } + } + else if (this.tags is string[] tagsArray) + { + if (tagsArray.Length == 2) + { + if (tagsArray[0] == remove) + { + this.tags = tagsArray[1]; + return true; + } + else if (tagsArray[1] == remove) + { + this.tags = tagsArray[0]; + return true; + } + } + else + { + var array = new string[tagsArray.Length - 1]; + var arrayIndex = 0; + var found = false; + + for (var i = 0; i < tagsArray.Length; ++i) + { + if (tagsArray[i] == remove) + { + found = true; + continue; + } + else if (arrayIndex == array.Length) + { + break; + } + + array[arrayIndex++] = tagsArray[i]; + } + + if (found) + { + this.tags = array; + return true; + } + } + } + + return false; + } + internal static IntermediateTuple Deserialize(ITupleDefinitionCreator creator, Uri baseUri, JsonObject jsonObject) { var definitionName = jsonObject.GetValueOrDefault("type"); var idJson = jsonObject.GetValueOrDefault("id"); var sourceLineNumbersJson = jsonObject.GetValueOrDefault("ln"); var fieldsJson = jsonObject.GetValueOrDefault("fields"); + var tagsJson = jsonObject.GetValueOrDefault("tags"); var id = (idJson == null) ? null : Identifier.Deserialize(idJson); var sourceLineNumbers = (sourceLineNumbersJson == null) ? null : SourceLineNumber.Deserialize(sourceLineNumbersJson); @@ -54,6 +176,25 @@ namespace WixToolset.Data } } + if (tagsJson == null || tagsJson.Count == 0) + { + } + else if (tagsJson.Count == 1) + { + tuple.tags = (string)tagsJson[0]; + } + else + { + var tags = new string[tagsJson.Count]; + + for (var i = 0; i < tagsJson.Count; ++i) + { + tags[i] = (string)tagsJson[i]; + } + + tuple.tags = tags; + } + return tuple; } @@ -86,6 +227,24 @@ namespace WixToolset.Data jsonObject.Add("fields", fieldsJson); + if (this.tags is string || this.tags is string[]) + { + JsonArray tagsJson; + + if (this.tags is string tag) + { + tagsJson = new JsonArray(1) { tag }; + } + else + { + var array = (string[])this.tags; + tagsJson = new JsonArray(array.Length); + tagsJson.AddRange(array); + } + + jsonObject.Add("tags", tagsJson); + } + return jsonObject; } } -- cgit v1.2.3-55-g6feb