Make the IntermediateSection and IntermediateSymbol less mutable
Rob Mensching committed
Mar 26, 2021 at 15:38 UTC
a0dd2bc561ee6aa6b7aebedcff76c8a11e14bc9f
6 files changed
+105
-39
src/WixToolset.Data/Intermediate.cs
+1
-1
@@ -47,7 +47,7 @@ namespace WixToolset.Data
47
public string Id { get; }
48
49
/// <summary>
50
- /// Get the id for the intermediate.
50
+ /// Get the level of the intermediate.
51
/// </summary>
52
public string Level { get; private set; }
53
src/WixToolset.Data/IntermediateSection.cs
+42
-7
@@ -11,18 +11,22 @@ namespace WixToolset.Data
11
/// </summary>
12
public class IntermediateSection
13
{
14
+ private readonly List<IntermediateSymbol> symbols;
15
+
16
/// <summary>
17
/// Creates a new section as part of an intermediate.
18
/// </summary>
19
/// <param name="id">Identifier for section.</param>
20
/// <param name="type">Type of section.</param>
21
/// <param name="codepage">Codepage for resulting database.</param>
20
- public IntermediateSection(string id, SectionType type, int codepage)
22
+ /// <param name="compilationId">Optional compilation identifier</param>
23
+ public IntermediateSection(string id, SectionType type, int codepage, string compilationId = null)
24
{
25
this.Id = id;
26
this.Type = type;
27
this.Codepage = codepage;
25
- this.Symbols = new List<IntermediateSymbol>();
28
+ this.CompilationId = compilationId;
29
+ this.symbols = new List<IntermediateSymbol>();
30
}
31
32
/// <summary>
@@ -41,22 +45,53 @@ namespace WixToolset.Data
45
/// Gets the codepage for the section.
46
/// </summary>
47
/// <value>Codepage for the section.</value>
44
- public int Codepage { get; set; }
48
+ public int Codepage { get; }
49
50
/// <summary>
51
/// Gets and sets the identifier of the compilation of the source file containing the section.
52
/// </summary>
49
- public string CompilationId { get; set; }
53
+ public string CompilationId { get; }
54
55
/// <summary>
56
/// Gets and sets the identifier of the library that combined the section.
57
/// </summary>
54
- public string LibraryId { get; set; }
58
+ public string LibraryId { get; private set; }
59
60
/// <summary>
61
/// Symbols in the section.
62
/// </summary>
59
- public IList<IntermediateSymbol> Symbols { get; }
63
+ public IReadOnlyCollection<IntermediateSymbol> Symbols => this.symbols;
64
+
65
+ /// <summary>
66
+ /// Adds a symbol to the section.
67
+ /// </summary>
68
+ /// <typeparam name="T">Type of IntermediateSymbol to add to the section.</typeparam>
69
+ /// <param name="symbol">Symbol to add to the section.</param>
70
+ /// <returns>Symbol added to the section.</returns>
71
+ public T AddSymbol<T>(T symbol) where T : IntermediateSymbol
72
+ {
73
+ this.symbols.Add(symbol);
74
+ return symbol;
75
+ }
76
+
77
+ /// <summary>
78
+ /// Assigns the section to a library.
79
+ /// </summary>
80
+ /// <param name="libraryId">Identifier of the library.</param>
81
+ public void AssignToLibrary(string libraryId)
82
+ {
83
+ this.LibraryId = libraryId;
84
+ }
85
+
86
+ /// <summary>
87
+ /// Removes a symbol from the section.
88
+ /// </summary>
89
+ /// <param name="symbol">Symbol to remove.</param>
90
+ /// <returns>True if the symbol was removed; otherwise false.</returns>
91
+ public bool RemoveSymbol(IntermediateSymbol symbol)
92
+ {
93
+ return this.symbols.Remove(symbol);
94
+ }
95
96
/// <summary>
97
/// Parse a section from the JSON data.
@@ -79,7 +114,7 @@ namespace WixToolset.Data
114
foreach (JsonObject symbolJson in symbolsJson)
115
{
116
var symbol = IntermediateSymbol.Deserialize(creator, baseUri, symbolJson);
82
- section.Symbols.Add(symbol);
117
+ section.symbols.Add(symbol);
118
}
119
120
return section;
src/WixToolset.Data/IntermediateSectionExtensions.cs
deleted
-14
@@ -1,14 +0,0 @@
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
-
3
-namespace WixToolset.Data
4
-{
5
- public static class IntermediateSectionExtensions
6
- {
7
- public static T AddSymbol<T>(this IntermediateSection section, T symbol)
8
- where T : IntermediateSymbol
9
- {
10
- section.Symbols.Add(symbol);
11
- return symbol;
12
- }
13
- }
14
-}
src/WixToolset.Data/IntermediateSymbol.cs
+49
-4
@@ -6,15 +6,28 @@ namespace WixToolset.Data
6
using System.Diagnostics;
7
using SimpleJson;
8
9
+ /// <summary>
10
+ /// Intermediate symbol.
11
+ /// </summary>
12
[DebuggerDisplay("{DebuggerDisplay,nq}")]
13
public class IntermediateSymbol
14
{
15
private object tags;
16
17
+ /// <summary>
18
+ /// Creates an intermediate symbol.
19
+ /// </summary>
20
+ /// <param name="definition">Symbol definition.</param>
21
public IntermediateSymbol(IntermediateSymbolDefinition definition) : this(definition, null, null)
22
{
23
}
24
25
+ /// <summary>
26
+ /// Creates an intermediate symbol with source line number and identifier.
27
+ /// </summary>
28
+ /// <param name="definition">Symbol definition.</param>
29
+ /// <param name="sourceLineNumber">Source line number.</param>
30
+ /// <param name="id">Symbol identifier.</param>
31
public IntermediateSymbol(IntermediateSymbolDefinition definition, SourceLineNumber sourceLineNumber, Identifier id = null)
32
{
33
this.Definition = definition;
@@ -23,18 +36,40 @@ namespace WixToolset.Data
36
this.Id = id;
37
}
38
39
+ /// <summary>
40
+ /// Gets the symbol's definition.
41
+ /// </summary>
42
public IntermediateSymbolDefinition Definition { get; }
43
44
+ /// <summary>
45
+ /// Gets the symbol's fields.
46
+ /// </summary>
47
public IntermediateField[] Fields { get; }
48
30
- public SourceLineNumber SourceLineNumbers { get; set; }
31
-
32
- public Identifier Id { get; set; }
33
-
49
+ /// <summary>
50
+ /// Gets the optional source line number of the symbol.
51
+ /// </summary>
52
+ public SourceLineNumber SourceLineNumbers { get; internal set; }
53
+
54
+ /// <summary>
55
+ /// Gets the optional identifier for the symbol.
56
+ /// </summary>
57
+ public Identifier Id { get; internal set; }
58
+
59
+ /// <summary>
60
+ /// Direct access by index to the symbol's fields.
61
+ /// </summary>
62
+ /// <param name="index">Index of the field to access.</param>
63
+ /// <returns>Symbol's field.</returns>
64
public IntermediateField this[int index] => this.Fields[index];
65
66
private string DebuggerDisplay => $"{this.Definition?.Name} {this.Id?.Id}";
67
68
+ /// <summary>
69
+ /// Add a custom tag to the symbol.
70
+ /// </summary>
71
+ /// <param name="add">String tag to add to the symbol.</param>
72
+ /// <returns>True if the tag was added; otherwise false if th tag was already present.</returns>
73
public bool AddTag(string add)
74
{
75
if (this.tags == null)
@@ -73,6 +108,11 @@ namespace WixToolset.Data
108
return true;
109
}
110
111
+ /// <summary>
112
+ /// Tests whether a symbol has a tag.
113
+ /// </summary>
114
+ /// <param name="has">String tag to find.</param>
115
+ /// <returns>True if the symbol has the tag; otherwise false.</returns>
116
public bool HasTag(string has)
117
{
118
if (this.tags == null)
@@ -97,6 +137,11 @@ namespace WixToolset.Data
137
return false;
138
}
139
140
+ /// <summary>
141
+ /// Removes a tag from the symbol.
142
+ /// </summary>
143
+ /// <param name="remove">String tag to remove.</param>
144
+ /// <returns>True if the tag was removed; otherwise false if the tag was not present.</returns>
145
public bool RemoveTag(string remove)
146
{
147
if (this.tags is string tag)
src/WixToolset.Data/IntermediateSymbolDefinition.cs
+4
-4
@@ -48,11 +48,11 @@ namespace WixToolset.Data
48
49
public IntermediateSymbol CreateSymbol(SourceLineNumber sourceLineNumber = null, Identifier id = null)
50
{
51
- var result = (this.StrongSymbolType == typeof(IntermediateSymbol)) ? (IntermediateSymbol)Activator.CreateInstance(this.StrongSymbolType, this) : (IntermediateSymbol)Activator.CreateInstance(this.StrongSymbolType);
52
- result.SourceLineNumbers = sourceLineNumber;
53
- result.Id = id;
51
+ var symbol = (this.StrongSymbolType == typeof(IntermediateSymbol)) ? (IntermediateSymbol)Activator.CreateInstance(this.StrongSymbolType, this) : (IntermediateSymbol)Activator.CreateInstance(this.StrongSymbolType);
52
+ symbol.SourceLineNumbers = sourceLineNumber;
53
+ symbol.Id = id;
54
55
- return result;
55
+ return symbol;
56
}
57
58
public bool AddTag(string add)
src/test/WixToolsetTest.Data/SerializeFixture.cs
+9
-9
@@ -22,7 +22,7 @@ namespace WixToolsetTest.Data
22
23
var section = new IntermediateSection("test", SectionType.Product, 65001);
24
25
- section.Symbols.Add(new ComponentSymbol(sln, new Identifier(AccessModifier.Global, "TestComponent"))
25
+ section.AddSymbol(new ComponentSymbol(sln, new Identifier(AccessModifier.Global, "TestComponent"))
26
{
27
ComponentId = new Guid(1, 0, 0, new byte[8]).ToString("B"),
28
DirectoryRef = "TestFolder",
@@ -64,7 +64,7 @@ namespace WixToolsetTest.Data
64
var sln = new SourceLineNumber("test.wxs", 1);
65
var section = new IntermediateSection("test", SectionType.Product, 65001);
66
67
- section.Symbols.Add(new ComponentSymbol(sln, new Identifier(AccessModifier.Global, "TestComponent"))
67
+ section.AddSymbol(new ComponentSymbol(sln, new Identifier(AccessModifier.Global, "TestComponent"))
68
{
69
ComponentId = new Guid(1, 0, 0, new byte[8]).ToString("B"),
70
DirectoryRef = "TestFolder",
@@ -91,7 +91,7 @@ namespace WixToolsetTest.Data
91
92
wixout.Reopen(writable: true);
93
94
- section.Symbols.Add(new ComponentSymbol(sln, new Identifier(AccessModifier.Global, "NewComponent"))
94
+ section.AddSymbol(new ComponentSymbol(sln, new Identifier(AccessModifier.Global, "NewComponent"))
95
{
96
ComponentId = new Guid(1, 0, 0, new byte[8]).ToString("B"),
97
});
@@ -135,7 +135,7 @@ namespace WixToolsetTest.Data
135
symbol.Set(1, 2);
136
symbol.Set(2, true);
137
138
- section.Symbols.Add(symbol);
138
+ section.AddSymbol(symbol);
139
140
var intermediate = new Intermediate("TestIntermediate", new[] { section }, null);
141
@@ -179,7 +179,7 @@ namespace WixToolsetTest.Data
179
symbol.Set(2, true);
180
181
var section = new IntermediateSection("test", SectionType.Product, 65001);
182
- section.Symbols.Add(symbol);
182
+ section.AddSymbol(symbol);
183
184
var intermediate1 = new Intermediate("TestIntermediate", new[] { section }, null);
185
@@ -201,7 +201,7 @@ namespace WixToolsetTest.Data
201
symbol2.Set(3, "baz");
202
203
var section2 = new IntermediateSection("test2", SectionType.Fragment, 65001);
204
- section2.Symbols.Add(symbol2);
204
+ section2.AddSymbol(symbol2);
205
206
var intermediate2 = new Intermediate("TestIntermediate2", new[] { section2 }, null);
207
@@ -262,7 +262,7 @@ namespace WixToolsetTest.Data
262
symbol.AddTag("symbol1tag");
263
264
var section = new IntermediateSection("test", SectionType.Product, 65001);
265
- section.Symbols.Add(symbol);
265
+ section.AddSymbol(symbol);
266
267
var intermediate1 = new Intermediate("TestIntermediate", new[] { section }, null);
268
@@ -290,7 +290,7 @@ namespace WixToolsetTest.Data
290
symbol2.AddTag("symbol2tag2");
291
292
var section2 = new IntermediateSection("test2", SectionType.Fragment, 65001);
293
- section2.Symbols.Add(symbol2);
293
+ section2.AddSymbol(symbol2);
294
295
var intermediate2 = new Intermediate("TestIntermediate2", new[] { section2 }, null);
296
@@ -356,7 +356,7 @@ namespace WixToolsetTest.Data
356
357
var section = new IntermediateSection("test", SectionType.Product, 65001);
358
359
- section.Symbols.Add(new ComponentSymbol(sln, new Identifier(AccessModifier.Global, "TestComponent"))
359
+ section.AddSymbol(new ComponentSymbol(sln, new Identifier(AccessModifier.Global, "TestComponent"))
360
{
361
ComponentId = new Guid(1, 0, 0, new byte[8]).ToString("B"),
362
DirectoryRef = "TestFolder",