Skip to content

Commit 32b86b0

Browse files
Refactor CancellationToken parameters to be non-nullable (#56)
* Refactor: replace nullable CancellationToken parameters Modified method signatures to replace nullable `CancellationToken?` parameters with non-nullable `CancellationToken` parameters, providing a default value of `CancellationToken.None` * Remove null-conditional operator from cancellationToken calls * Reorder cancellationToken parameter in method calls * Fix build errors in TestUI project
1 parent ec37a56 commit 32b86b0

11 files changed

Lines changed: 91 additions & 91 deletions

src/Dax.Template.TestUI/ApplyDaxTemplate.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private void Update_Click(object sender, EventArgs e)
4747
//File.WriteAllText(@"c:\temp\changes1.json", modelBim);
4848

4949
// Show DAX for debug purposes
50-
txtDax.Text = hiddenDateTemplate.GetDaxTableExpression(model,null);
50+
txtDax.Text = hiddenDateTemplate.GetDaxTableExpression(model);
5151

5252
model.SaveChanges();
5353
}
@@ -73,7 +73,7 @@ private CalculatedTableTemplateBase CreateHolidaysDefinitionTable(string dateTab
7373
}
7474
CalculatedTableTemplateBase template;
7575
template = new HolidaysDefinitionTable(ReadHolidaysDefinitionConfig());
76-
template.ApplyTemplate(tableHolidays, null, hideTable);
76+
template.ApplyTemplate(tableHolidays, hideTable);
7777
tableHolidays.RequestRefresh(TOM.RefreshType.Full);
7878

7979
return template;
@@ -91,7 +91,7 @@ private CalculatedTableTemplateBase CreateHolidaysTable(string dateTableName, TO
9191
}
9292
CalculatedTableTemplateBase template;
9393
template = new HolidaysTable(ReadConfig<TemplateConfiguration>());
94-
template.ApplyTemplate(tableHolidays, null, hideTable);
94+
template.ApplyTemplate(tableHolidays, hideTable);
9595
tableHolidays.RequestRefresh(TOM.RefreshType.Full);
9696

9797
return template;
@@ -130,7 +130,7 @@ private ReferenceCalculatedTable CreateDateTable(string dateTableName, TOM.Model
130130
};
131131
}
132132

133-
template.ApplyTemplate(tableDate, null, hideTable);
133+
template.ApplyTemplate(tableDate, hideTable);
134134

135135
tableDate.RequestRefresh(TOM.RefreshType.Full);
136136

@@ -157,11 +157,11 @@ private void GenerateDax_Click(object sender, EventArgs e)
157157
if (chkCustomTemplate.Checked)
158158
{
159159
var template = new CustomDateTable(ReadConfig<TemplateConfiguration>(), ReadTemplateDefinition(), null);
160-
result = template?.GetDaxTableExpression(null, null);
160+
result = template?.GetDaxTableExpression(null);
161161
}
162162
else {
163163
var template = new SimpleDateTable(ReadConfig<SimpleDateTemplateConfig>(),null);
164-
result = template.GetDaxTableExpression(null, null);
164+
result = template.GetDaxTableExpression(null);
165165
}
166166
txtDax.Text = result;
167167
}
@@ -220,7 +220,7 @@ private HolidaysDefinitionTable.HolidaysDefinitions ReadHolidaysDefinitionConfig
220220
private void GenerateHolidays_Click(object sender, EventArgs e)
221221
{
222222
var template = new HolidaysDefinitionTable(ReadHolidaysDefinitionConfig());
223-
var result = template.GetDaxTableExpression(null, null);
223+
var result = template.GetDaxTableExpression(null);
224224
txtDax.Text = result;
225225
}
226226

@@ -250,7 +250,7 @@ private void UpdateHolidays_Click(object sender, EventArgs e)
250250
File.WriteAllText(@"c:\temp\changes.json", modelBim);
251251

252252
// Show DAX for debug purposes
253-
txtDax.Text = hiddenHolidaysDefinitionTemplate.GetDaxTableExpression(model, null);
253+
txtDax.Text = hiddenHolidaysDefinitionTemplate.GetDaxTableExpression(model);
254254

255255
model.SaveChanges();
256256
}
@@ -280,7 +280,7 @@ private void MeasureTemplate_Click(object sender, EventArgs e)
280280

281281
try
282282
{
283-
template.ApplyTemplate(model, isEnabled: true, null);
283+
template.ApplyTemplate(model, isEnabled: true);
284284
model.SaveChanges();
285285
}
286286
catch (TemplateException ex)
@@ -370,7 +370,7 @@ private void ApplyTemplate(bool commitChanges)
370370

371371
try
372372
{
373-
templateEngine.ApplyTemplates(model, null);
373+
templateEngine.ApplyTemplates(model);
374374
var modelChanges = Engine.GetModelChanges(model);
375375

376376
if (commitChanges)

src/Dax.Template.TestUI/BravoDaxTemplate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public class BravoDaxTemplate
107107

108108
CopyConfiguration();
109109
Engine templateEngine = new(package);
110-
templateEngine.ApplyTemplates(model, null);
110+
templateEngine.ApplyTemplates(model);
111111
var modelChanges = Engine.GetModelChanges(model);
112112

113113
if (commitChanges)

src/Dax.Template/Engine.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ public static Model.ModelChanges GetModelChanges(TabularModel model, Cancellatio
6262
return modelChanges;
6363
}
6464

65-
public void ApplyTemplates(TabularModel model, CancellationToken? cancellationToken)
65+
public void ApplyTemplates(TabularModel model, CancellationToken cancellationToken = default)
6666
{
67-
(string className, Action<ITemplates.TemplateEntry, CancellationToken?> action)[] classes = new (string, Action<ITemplates.TemplateEntry, CancellationToken?>)[]
67+
(string className, Action<ITemplates.TemplateEntry, CancellationToken> action)[] classes = new (string, Action<ITemplates.TemplateEntry, CancellationToken>)[]
6868
{
6969
( nameof(HolidaysDefinitionTable), ApplyHolidaysDefinitionTable ),
7070
( nameof(HolidaysTable), ApplyHolidaysTable ),
@@ -76,7 +76,7 @@ public void ApplyTemplates(TabularModel model, CancellationToken? cancellationTo
7676
{
7777
Configuration.Templates.ToList().ForEach(template =>
7878
{
79-
cancellationToken?.ThrowIfCancellationRequested();
79+
cancellationToken.ThrowIfCancellationRequested();
8080
var (className, action) = classes.First(c => c.className == template.Class);
8181
action(template, cancellationToken);
8282
});
@@ -108,7 +108,7 @@ void RemoveOrphanTranslations()
108108
}
109109
}
110110

111-
void ApplyHolidaysDefinitionTable(ITemplates.TemplateEntry templateEntry, CancellationToken? cancellationToken)
111+
void ApplyHolidaysDefinitionTable(ITemplates.TemplateEntry templateEntry, CancellationToken cancellationToken = default)
112112
{
113113
Table tableHolidaysDefinition = model.Tables.Find(templateEntry.Table);
114114
if (!templateEntry.IsEnabled)
@@ -134,10 +134,10 @@ void ApplyHolidaysDefinitionTable(ITemplates.TemplateEntry templateEntry, Cancel
134134
throw new InvalidConfigurationException($"Undefined Template in class {templateEntry.Class} configuration");
135135
}
136136
template = new HolidaysDefinitionTable(_package.ReadDefinition<HolidaysDefinitionTable.HolidaysDefinitions>(templateEntry.Template));
137-
template.ApplyTemplate(tableHolidaysDefinition, cancellationToken, templateEntry.IsHidden);
137+
template.ApplyTemplate(tableHolidaysDefinition, templateEntry.IsHidden, cancellationToken);
138138
tableHolidaysDefinition.RequestRefresh(RefreshType.Full);
139139
}
140-
void ApplyHolidaysTable(ITemplates.TemplateEntry templateEntry, CancellationToken? cancellationToken)
140+
void ApplyHolidaysTable(ITemplates.TemplateEntry templateEntry, CancellationToken cancellationToken = default)
141141
{
142142
Table tableHolidays = model.Tables.Find(templateEntry.Table);
143143
if (!templateEntry.IsEnabled)
@@ -159,10 +159,10 @@ void ApplyHolidaysTable(ITemplates.TemplateEntry templateEntry, CancellationToke
159159
}
160160
CalculatedTableTemplateBase template;
161161
template = new HolidaysTable(Configuration);
162-
template.ApplyTemplate(tableHolidays, cancellationToken, templateEntry.IsHidden);
162+
template.ApplyTemplate(tableHolidays, templateEntry.IsHidden, cancellationToken);
163163
tableHolidays.RequestRefresh(RefreshType.Full);
164164
}
165-
void ApplyCustomDateTable(ITemplates.TemplateEntry templateEntry, CancellationToken? cancellationToken)
165+
void ApplyCustomDateTable(ITemplates.TemplateEntry templateEntry, CancellationToken cancellationToken = default)
166166
{
167167
if (string.IsNullOrWhiteSpace(templateEntry.Template))
168168
{
@@ -184,7 +184,7 @@ void ApplyCustomDateTable(ITemplates.TemplateEntry templateEntry, CancellationTo
184184
model,
185185
hideTable: true,
186186
isoFormat: Configuration.IsoFormat,
187-
cancellationToken);
187+
cancellationToken: cancellationToken);
188188
}
189189
bool translationsEnabled = !string.IsNullOrWhiteSpace(Configuration.IsoTranslation);
190190
ReferenceCalculatedTable visibleDateTemplate = CreateDateTable(
@@ -193,29 +193,29 @@ void ApplyCustomDateTable(ITemplates.TemplateEntry templateEntry, CancellationTo
193193
model,
194194
hideTable: templateEntry.IsHidden,
195195
isoFormat: Configuration.IsoFormat,
196-
cancellationToken,
197196
referenceTable: templateEntry.ReferenceTable,
198-
applyTranslations: translationsEnabled);
197+
applyTranslations: translationsEnabled,
198+
cancellationToken);
199199

200200
}
201-
void ApplyMeasuresTemplate(ITemplates.TemplateEntry templateEntry, CancellationToken? cancellationToken)
201+
void ApplyMeasuresTemplate(ITemplates.TemplateEntry templateEntry, CancellationToken cancellationToken = default)
202202
{
203203
if (string.IsNullOrWhiteSpace(templateEntry.Template))
204204
{
205205
throw new InvalidConfigurationException($"Undefined Template in class {templateEntry.Class} configuration");
206206
}
207207
var measuresTemplateDefinition = _package.ReadDefinition<MeasuresTemplateDefinition>(templateEntry.Template);
208208
var template = new MeasuresTemplate(Configuration, measuresTemplateDefinition, templateEntry.Properties);
209-
template.ApplyTemplate(model, templateEntry.IsEnabled, cancellationToken);
209+
template.ApplyTemplate(model, isEnabled: templateEntry.IsEnabled, cancellationToken: cancellationToken);
210210
}
211211
}
212212

213-
private Translations.Definitions ReadTranslations(CancellationToken? cancellationToken)
213+
private Translations.Definitions ReadTranslations(CancellationToken cancellationToken = default)
214214
{
215215
Translations.Definitions translations = new();
216216
foreach (var localizationFile in Configuration.LocalizationFiles!)
217217
{
218-
cancellationToken?.ThrowIfCancellationRequested();
218+
cancellationToken.ThrowIfCancellationRequested();
219219
Translations.Definitions definitions = _package.ReadDefinition<Translations.Definitions>(localizationFile);
220220
translations.Translations = translations.Translations.Union(definitions.Translations).ToArray();
221221
}
@@ -230,9 +230,9 @@ private ReferenceCalculatedTable CreateDateTable(
230230
TabularModel model,
231231
bool hideTable,
232232
string? isoFormat,
233-
CancellationToken? cancellationToken,
234233
string? referenceTable = null,
235-
bool applyTranslations = false)
234+
bool applyTranslations = false,
235+
CancellationToken cancellationToken = default)
236236
{
237237
Table tableDate = model.Tables.Find(dateTableName);
238238
if (tableDate == null)
@@ -256,7 +256,7 @@ private ReferenceCalculatedTable CreateDateTable(
256256
IsoFormat = isoFormat
257257
};
258258

259-
template.ApplyTemplate(tableDate, cancellationToken, hideTable);
259+
template.ApplyTemplate(tableDate, hideTable, cancellationToken);
260260

261261
tableDate.RequestRefresh(RefreshType.Full);
262262

src/Dax.Template/Measures/MeasureTemplateBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ string GetYearEndFromFirstMonthVariable(string expression)
145145
return expression;
146146
}
147147

148-
public virtual TabularMeasure ApplyTemplate(TabularModel model, Table targetTable, CancellationToken? cancellationToken, bool overrideExistingMeasure = true)
148+
public virtual TabularMeasure ApplyTemplate(TabularModel model, Table targetTable, bool overrideExistingMeasure = true, CancellationToken cancellationToken = default)
149149
{
150150
var measure = FindMeasure(model, Name);
151151
if (measure == null)
@@ -170,12 +170,12 @@ public virtual TabularMeasure ApplyTemplate(TabularModel model, Table targetTabl
170170

171171
return measure;
172172

173-
void ApplyAnnotations(TabularMeasure measure, CancellationToken? cancellationToken)
173+
void ApplyAnnotations(TabularMeasure measure, CancellationToken cancellationToken = default)
174174
{
175175
if (Annotations == null) return;
176176
foreach (var annotation in Annotations)
177177
{
178-
cancellationToken?.ThrowIfCancellationRequested();
178+
cancellationToken.ThrowIfCancellationRequested();
179179

180180
var annotationName = annotation.Key;
181181
var annotationValue = annotation.Value.ToString();

src/Dax.Template/Measures/MeasuresTemplate.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public string? DisplayFolderRuleSingleInstanceMeasures
8080
/// Returns the target measures for the template
8181
/// </summary>
8282
/// <returns></returns>
83-
private IEnumerable<Measure> GetTargetMeasures(TabularModel model, CancellationToken? cancellationToken)
83+
private IEnumerable<Measure> GetTargetMeasures(TabularModel model, CancellationToken cancellationToken = default)
8484
{
8585
if (Config.TargetMeasures == null || Config.TargetMeasures.Length == 0)
8686
{
@@ -94,7 +94,7 @@ from m in t.Measures
9494
IEnumerable<Measure> result = Array.Empty<Measure>();
9595
foreach(var tm in Config.TargetMeasures)
9696
{
97-
cancellationToken?.ThrowIfCancellationRequested();
97+
cancellationToken.ThrowIfCancellationRequested();
9898
result = result.Union(
9999
from t in model.Tables
100100
from m in t.Measures
@@ -157,7 +157,7 @@ protected internal virtual string GetTargetMeasureName(string templateName, stri
157157
return $"{prefix}{referenceMeasureName}{suffix}";
158158
}
159159

160-
public void ApplyTemplate(TabularModel model, bool isEnabled, CancellationToken? cancellationToken, bool overrideExistingMeasures = true)
160+
public void ApplyTemplate(TabularModel model, bool isEnabled, bool overrideExistingMeasures = true, CancellationToken cancellationToken = default)
161161
{
162162
// Retrieves the existing measures created by a previous execution of the same template type
163163
string? SqlbiTemplateValue = GetSqlbiTemplateValue();
@@ -188,7 +188,7 @@ where m.Annotations.Any(a =>
188188
// Create the individual measures of the template (not applied to single measures)
189189
foreach (var template in singleInstanceMeasures)
190190
{
191-
cancellationToken?.ThrowIfCancellationRequested();
191+
cancellationToken.ThrowIfCancellationRequested();
192192
ApplyMeasureTemplate(template, targetTableSingleInstanceMeasures, referenceMeasure: null, cancellationToken);
193193
}
194194

@@ -197,7 +197,7 @@ where m.Annotations.Any(a =>
197197
{
198198
foreach (var template in templateMeasures)
199199
{
200-
cancellationToken?.ThrowIfCancellationRequested();
200+
cancellationToken.ThrowIfCancellationRequested();
201201
ApplyMeasureTemplate(template, targetTable, referenceMeasure: target, cancellationToken);
202202
}
203203
}
@@ -209,12 +209,12 @@ where m.Annotations.Any(a =>
209209
existingMeasuresFromSameTemplate.RemoveAll(m => appliedMeasures.Any(am => am.Name.Equals(m.Name)));
210210
foreach (var removeMeasure in existingMeasuresFromSameTemplate)
211211
{
212-
cancellationToken?.ThrowIfCancellationRequested();
212+
cancellationToken.ThrowIfCancellationRequested();
213213
removeMeasure.Table.Measures.Remove(removeMeasure);
214214
}
215215
}
216216

217-
void ApplyMeasureTemplate(MeasuresTemplateDefinition.MeasureTemplate template, Table targetTable, Measure? referenceMeasure, CancellationToken? cancellationToken)
217+
void ApplyMeasureTemplate(MeasuresTemplateDefinition.MeasureTemplate template, Table targetTable, Measure? referenceMeasure, CancellationToken cancellationToken = default)
218218
{
219219
if (template.Name == null)
220220
{
@@ -234,7 +234,7 @@ void ApplyMeasureTemplate(MeasuresTemplateDefinition.MeasureTemplate template, T
234234
ReferenceMeasure = referenceMeasure,
235235
DefaultVariables = Config.DefaultVariables
236236
};
237-
var modelMeasure = measureTemplate.ApplyTemplate(model, referenceMeasure?.Parent as Table ?? targetTable, cancellationToken, overrideExistingMeasures);
237+
var modelMeasure = measureTemplate.ApplyTemplate(model, referenceMeasure?.Parent as Table ?? targetTable, overrideExistingMeasures, cancellationToken);
238238
appliedMeasures.Add(modelMeasure);
239239
}
240240
}
@@ -279,7 +279,7 @@ private string GetSqlbiTemplateValue()
279279
(from t in model.Tables
280280
select t).FirstOrDefault(t => t.Name == tableName);
281281
}
282-
private Table GetTargetTable(TabularModel model, CancellationToken? cancellationToken)
282+
private Table GetTargetTable(TabularModel model, CancellationToken cancellationToken = default)
283283
{
284284
Table? targetTable = null;
285285
var targetTableName = Template.TargetTable.FirstOrDefault(tt => tt.Key == "Name").Value;
@@ -291,7 +291,7 @@ private Table GetTargetTable(TabularModel model, CancellationToken? cancellation
291291
{
292292
foreach (var tt in Template.TargetTable)
293293
{
294-
cancellationToken?.ThrowIfCancellationRequested();
294+
cancellationToken.ThrowIfCancellationRequested();
295295
var tables = MeasureTemplateBase.GetTablesFromAnnotations(model, tt.Key, tt.Value);
296296
if (!tables.Any())
297297
{

0 commit comments

Comments
 (0)