aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Data/IntermediateSymbolDefinition.cs
blob: 102a9f5c5b8834f3be4db14ad67d5f7f198e2974 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// 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.

namespace WixToolset.Data
{
    using System;
    using SimpleJson;

    public class IntermediateSymbolDefinition
    {
        private object tags;

        public IntermediateSymbolDefinition(string name, IntermediateFieldDefinition[] fieldDefinitions, Type strongSymbolType)
            : this(SymbolDefinitionType.MustBeFromAnExtension, name, 0, fieldDefinitions, strongSymbolType)
        {
        }

        public IntermediateSymbolDefinition(string name, int revision, IntermediateFieldDefinition[] fieldDefinitions, Type strongSymbolType)
            : this(SymbolDefinitionType.MustBeFromAnExtension, name, revision, fieldDefinitions, strongSymbolType)
        {
        }

        internal IntermediateSymbolDefinition(SymbolDefinitionType type, IntermediateFieldDefinition[] fieldDefinitions, Type strongSymbolType)
            : this(type, type.ToString(), 0, fieldDefinitions, strongSymbolType)
        {
        }

        private IntermediateSymbolDefinition(SymbolDefinitionType type, string name, int revision, IntermediateFieldDefinition[] fieldDefinitions, Type strongSymbolType)
        {
            this.Type = type;
            this.Name = name;
            this.Revision = revision;
            this.FieldDefinitions = fieldDefinitions;
            this.StrongSymbolType = strongSymbolType ?? typeof(IntermediateSymbol);
#if DEBUG
            if (this.StrongSymbolType != typeof(IntermediateSymbol) && !this.StrongSymbolType.IsSubclassOf(typeof(IntermediateSymbol))) { throw new ArgumentException(nameof(strongSymbolType)); }
#endif
        }

        public int Revision { get; }

        public SymbolDefinitionType Type { get; }

        public string Name { get; }

        public IntermediateFieldDefinition[] FieldDefinitions { get; }

        private Type StrongSymbolType { get; }

        public IntermediateSymbol CreateSymbol(SourceLineNumber sourceLineNumber = null, Identifier id = null)
        {
            var result = (this.StrongSymbolType == typeof(IntermediateSymbol)) ? (IntermediateSymbol)Activator.CreateInstance(this.StrongSymbolType, this) : (IntermediateSymbol)Activator.CreateInstance(this.StrongSymbolType);
            result.SourceLineNumbers = sourceLineNumber;
            result.Id = id;

            return result;
        }

        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 IntermediateSymbolDefinition Deserialize(JsonObject jsonObject)
        {
            var name = jsonObject.GetValueOrDefault<string>("name");
            var revision = jsonObject.GetValueOrDefault("rev", 0);
            var definitionsJson = jsonObject.GetValueOrDefault<JsonArray>("fields");
            var tagsJson = jsonObject.GetValueOrDefault<JsonArray>("tags");

            var fieldDefinitions = new IntermediateFieldDefinition[definitionsJson.Count];

            for (var i = 0; i < definitionsJson.Count; ++i)
            {
                var definitionJson = (JsonObject)definitionsJson[i];
                var fieldName = definitionJson.GetValueOrDefault<string>("name");
                var fieldType = definitionJson.GetEnumOrDefault("type", IntermediateFieldType.String);
                fieldDefinitions[i] = new IntermediateFieldDefinition(fieldName, fieldType);
            }

            var definition = new IntermediateSymbolDefinition(name, revision, fieldDefinitions, null);

            if (tagsJson == null || tagsJson.Count == 0)
            {
            }
            else if (tagsJson.Count == 1)
            {
                definition.tags = (string)tagsJson[0];
            }
            else
            {
                var tags = new string[tagsJson.Count];

                for (var i = 0; i < tagsJson.Count; ++i)
                {
                    tags[i] = (string)tagsJson[i];
                }

                definition.tags = tags;
            }

            return definition;
        }

        internal JsonObject Serialize()
        {
            var jsonObject = new JsonObject
            {
                { "name", this.Name }
            };

            if (this.Revision > 0)
            {
                jsonObject.Add("rev", this.Revision);
            }

            var fieldsJson = new JsonArray(this.FieldDefinitions.Length);

            foreach (var fieldDefinition in this.FieldDefinitions)
            {
                var fieldJson = new JsonObject
                {
                    { "name", fieldDefinition.Name },
                };

                if (fieldDefinition.Type != IntermediateFieldType.String)
                {
                    fieldJson.Add("type", fieldDefinition.Type.ToString().ToLowerInvariant());
                }

                fieldsJson.Add(fieldJson);
            }

            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;
        }
    }
}