Skip to content

Commit

Permalink
Refactor CancellationToken parameters to be non-nullable (#56)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
albertospelta authored Dec 5, 2024
1 parent ec37a56 commit 32b86b0
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 91 deletions.
20 changes: 10 additions & 10 deletions src/Dax.Template.TestUI/ApplyDaxTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private void Update_Click(object sender, EventArgs e)
//File.WriteAllText(@"c:\temp\changes1.json", modelBim);

// Show DAX for debug purposes
txtDax.Text = hiddenDateTemplate.GetDaxTableExpression(model,null);
txtDax.Text = hiddenDateTemplate.GetDaxTableExpression(model);

model.SaveChanges();
}
Expand All @@ -73,7 +73,7 @@ private CalculatedTableTemplateBase CreateHolidaysDefinitionTable(string dateTab
}
CalculatedTableTemplateBase template;
template = new HolidaysDefinitionTable(ReadHolidaysDefinitionConfig());
template.ApplyTemplate(tableHolidays, null, hideTable);
template.ApplyTemplate(tableHolidays, hideTable);
tableHolidays.RequestRefresh(TOM.RefreshType.Full);

return template;
Expand All @@ -91,7 +91,7 @@ private CalculatedTableTemplateBase CreateHolidaysTable(string dateTableName, TO
}
CalculatedTableTemplateBase template;
template = new HolidaysTable(ReadConfig<TemplateConfiguration>());
template.ApplyTemplate(tableHolidays, null, hideTable);
template.ApplyTemplate(tableHolidays, hideTable);
tableHolidays.RequestRefresh(TOM.RefreshType.Full);

return template;
Expand Down Expand Up @@ -130,7 +130,7 @@ private ReferenceCalculatedTable CreateDateTable(string dateTableName, TOM.Model
};
}

template.ApplyTemplate(tableDate, null, hideTable);
template.ApplyTemplate(tableDate, hideTable);

tableDate.RequestRefresh(TOM.RefreshType.Full);

Expand All @@ -157,11 +157,11 @@ private void GenerateDax_Click(object sender, EventArgs e)
if (chkCustomTemplate.Checked)
{
var template = new CustomDateTable(ReadConfig<TemplateConfiguration>(), ReadTemplateDefinition(), null);
result = template?.GetDaxTableExpression(null, null);
result = template?.GetDaxTableExpression(null);
}
else {
var template = new SimpleDateTable(ReadConfig<SimpleDateTemplateConfig>(),null);
result = template.GetDaxTableExpression(null, null);
result = template.GetDaxTableExpression(null);
}
txtDax.Text = result;
}
Expand Down Expand Up @@ -220,7 +220,7 @@ private HolidaysDefinitionTable.HolidaysDefinitions ReadHolidaysDefinitionConfig
private void GenerateHolidays_Click(object sender, EventArgs e)
{
var template = new HolidaysDefinitionTable(ReadHolidaysDefinitionConfig());
var result = template.GetDaxTableExpression(null, null);
var result = template.GetDaxTableExpression(null);
txtDax.Text = result;
}

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

// Show DAX for debug purposes
txtDax.Text = hiddenHolidaysDefinitionTemplate.GetDaxTableExpression(model, null);
txtDax.Text = hiddenHolidaysDefinitionTemplate.GetDaxTableExpression(model);

model.SaveChanges();
}
Expand Down Expand Up @@ -280,7 +280,7 @@ private void MeasureTemplate_Click(object sender, EventArgs e)

try
{
template.ApplyTemplate(model, isEnabled: true, null);
template.ApplyTemplate(model, isEnabled: true);
model.SaveChanges();
}
catch (TemplateException ex)
Expand Down Expand Up @@ -370,7 +370,7 @@ private void ApplyTemplate(bool commitChanges)

try
{
templateEngine.ApplyTemplates(model, null);
templateEngine.ApplyTemplates(model);
var modelChanges = Engine.GetModelChanges(model);

if (commitChanges)
Expand Down
2 changes: 1 addition & 1 deletion src/Dax.Template.TestUI/BravoDaxTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public class BravoDaxTemplate

CopyConfiguration();
Engine templateEngine = new(package);
templateEngine.ApplyTemplates(model, null);
templateEngine.ApplyTemplates(model);
var modelChanges = Engine.GetModelChanges(model);

if (commitChanges)
Expand Down
36 changes: 18 additions & 18 deletions src/Dax.Template/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ public static Model.ModelChanges GetModelChanges(TabularModel model, Cancellatio
return modelChanges;
}

public void ApplyTemplates(TabularModel model, CancellationToken? cancellationToken)
public void ApplyTemplates(TabularModel model, CancellationToken cancellationToken = default)
{
(string className, Action<ITemplates.TemplateEntry, CancellationToken?> action)[] classes = new (string, Action<ITemplates.TemplateEntry, CancellationToken?>)[]
(string className, Action<ITemplates.TemplateEntry, CancellationToken> action)[] classes = new (string, Action<ITemplates.TemplateEntry, CancellationToken>)[]
{
( nameof(HolidaysDefinitionTable), ApplyHolidaysDefinitionTable ),
( nameof(HolidaysTable), ApplyHolidaysTable ),
Expand All @@ -76,7 +76,7 @@ public void ApplyTemplates(TabularModel model, CancellationToken? cancellationTo
{
Configuration.Templates.ToList().ForEach(template =>
{
cancellationToken?.ThrowIfCancellationRequested();
cancellationToken.ThrowIfCancellationRequested();
var (className, action) = classes.First(c => c.className == template.Class);
action(template, cancellationToken);
});
Expand Down Expand Up @@ -108,7 +108,7 @@ void RemoveOrphanTranslations()
}
}

void ApplyHolidaysDefinitionTable(ITemplates.TemplateEntry templateEntry, CancellationToken? cancellationToken)
void ApplyHolidaysDefinitionTable(ITemplates.TemplateEntry templateEntry, CancellationToken cancellationToken = default)
{
Table tableHolidaysDefinition = model.Tables.Find(templateEntry.Table);
if (!templateEntry.IsEnabled)
Expand All @@ -134,10 +134,10 @@ void ApplyHolidaysDefinitionTable(ITemplates.TemplateEntry templateEntry, Cancel
throw new InvalidConfigurationException($"Undefined Template in class {templateEntry.Class} configuration");
}
template = new HolidaysDefinitionTable(_package.ReadDefinition<HolidaysDefinitionTable.HolidaysDefinitions>(templateEntry.Template));
template.ApplyTemplate(tableHolidaysDefinition, cancellationToken, templateEntry.IsHidden);
template.ApplyTemplate(tableHolidaysDefinition, templateEntry.IsHidden, cancellationToken);
tableHolidaysDefinition.RequestRefresh(RefreshType.Full);
}
void ApplyHolidaysTable(ITemplates.TemplateEntry templateEntry, CancellationToken? cancellationToken)
void ApplyHolidaysTable(ITemplates.TemplateEntry templateEntry, CancellationToken cancellationToken = default)
{
Table tableHolidays = model.Tables.Find(templateEntry.Table);
if (!templateEntry.IsEnabled)
Expand All @@ -159,10 +159,10 @@ void ApplyHolidaysTable(ITemplates.TemplateEntry templateEntry, CancellationToke
}
CalculatedTableTemplateBase template;
template = new HolidaysTable(Configuration);
template.ApplyTemplate(tableHolidays, cancellationToken, templateEntry.IsHidden);
template.ApplyTemplate(tableHolidays, templateEntry.IsHidden, cancellationToken);
tableHolidays.RequestRefresh(RefreshType.Full);
}
void ApplyCustomDateTable(ITemplates.TemplateEntry templateEntry, CancellationToken? cancellationToken)
void ApplyCustomDateTable(ITemplates.TemplateEntry templateEntry, CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(templateEntry.Template))
{
Expand All @@ -184,7 +184,7 @@ void ApplyCustomDateTable(ITemplates.TemplateEntry templateEntry, CancellationTo
model,
hideTable: true,
isoFormat: Configuration.IsoFormat,
cancellationToken);
cancellationToken: cancellationToken);
}
bool translationsEnabled = !string.IsNullOrWhiteSpace(Configuration.IsoTranslation);
ReferenceCalculatedTable visibleDateTemplate = CreateDateTable(
Expand All @@ -193,29 +193,29 @@ void ApplyCustomDateTable(ITemplates.TemplateEntry templateEntry, CancellationTo
model,
hideTable: templateEntry.IsHidden,
isoFormat: Configuration.IsoFormat,
cancellationToken,
referenceTable: templateEntry.ReferenceTable,
applyTranslations: translationsEnabled);
applyTranslations: translationsEnabled,
cancellationToken);

}
void ApplyMeasuresTemplate(ITemplates.TemplateEntry templateEntry, CancellationToken? cancellationToken)
void ApplyMeasuresTemplate(ITemplates.TemplateEntry templateEntry, CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(templateEntry.Template))
{
throw new InvalidConfigurationException($"Undefined Template in class {templateEntry.Class} configuration");
}
var measuresTemplateDefinition = _package.ReadDefinition<MeasuresTemplateDefinition>(templateEntry.Template);
var template = new MeasuresTemplate(Configuration, measuresTemplateDefinition, templateEntry.Properties);
template.ApplyTemplate(model, templateEntry.IsEnabled, cancellationToken);
template.ApplyTemplate(model, isEnabled: templateEntry.IsEnabled, cancellationToken: cancellationToken);
}
}

private Translations.Definitions ReadTranslations(CancellationToken? cancellationToken)
private Translations.Definitions ReadTranslations(CancellationToken cancellationToken = default)
{
Translations.Definitions translations = new();
foreach (var localizationFile in Configuration.LocalizationFiles!)
{
cancellationToken?.ThrowIfCancellationRequested();
cancellationToken.ThrowIfCancellationRequested();
Translations.Definitions definitions = _package.ReadDefinition<Translations.Definitions>(localizationFile);
translations.Translations = translations.Translations.Union(definitions.Translations).ToArray();
}
Expand All @@ -230,9 +230,9 @@ private ReferenceCalculatedTable CreateDateTable(
TabularModel model,
bool hideTable,
string? isoFormat,
CancellationToken? cancellationToken,
string? referenceTable = null,
bool applyTranslations = false)
bool applyTranslations = false,
CancellationToken cancellationToken = default)
{
Table tableDate = model.Tables.Find(dateTableName);
if (tableDate == null)
Expand All @@ -256,7 +256,7 @@ private ReferenceCalculatedTable CreateDateTable(
IsoFormat = isoFormat
};

template.ApplyTemplate(tableDate, cancellationToken, hideTable);
template.ApplyTemplate(tableDate, hideTable, cancellationToken);

tableDate.RequestRefresh(RefreshType.Full);

Expand Down
6 changes: 3 additions & 3 deletions src/Dax.Template/Measures/MeasureTemplateBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ string GetYearEndFromFirstMonthVariable(string expression)
return expression;
}

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

return measure;

void ApplyAnnotations(TabularMeasure measure, CancellationToken? cancellationToken)
void ApplyAnnotations(TabularMeasure measure, CancellationToken cancellationToken = default)
{
if (Annotations == null) return;
foreach (var annotation in Annotations)
{
cancellationToken?.ThrowIfCancellationRequested();
cancellationToken.ThrowIfCancellationRequested();

var annotationName = annotation.Key;
var annotationValue = annotation.Value.ToString();
Expand Down
20 changes: 10 additions & 10 deletions src/Dax.Template/Measures/MeasuresTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public string? DisplayFolderRuleSingleInstanceMeasures
/// Returns the target measures for the template
/// </summary>
/// <returns></returns>
private IEnumerable<Measure> GetTargetMeasures(TabularModel model, CancellationToken? cancellationToken)
private IEnumerable<Measure> GetTargetMeasures(TabularModel model, CancellationToken cancellationToken = default)
{
if (Config.TargetMeasures == null || Config.TargetMeasures.Length == 0)
{
Expand All @@ -94,7 +94,7 @@ from m in t.Measures
IEnumerable<Measure> result = Array.Empty<Measure>();
foreach(var tm in Config.TargetMeasures)
{
cancellationToken?.ThrowIfCancellationRequested();
cancellationToken.ThrowIfCancellationRequested();
result = result.Union(
from t in model.Tables
from m in t.Measures
Expand Down Expand Up @@ -157,7 +157,7 @@ protected internal virtual string GetTargetMeasureName(string templateName, stri
return $"{prefix}{referenceMeasureName}{suffix}";
}

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

Expand All @@ -197,7 +197,7 @@ where m.Annotations.Any(a =>
{
foreach (var template in templateMeasures)
{
cancellationToken?.ThrowIfCancellationRequested();
cancellationToken.ThrowIfCancellationRequested();
ApplyMeasureTemplate(template, targetTable, referenceMeasure: target, cancellationToken);
}
}
Expand All @@ -209,12 +209,12 @@ where m.Annotations.Any(a =>
existingMeasuresFromSameTemplate.RemoveAll(m => appliedMeasures.Any(am => am.Name.Equals(m.Name)));
foreach (var removeMeasure in existingMeasuresFromSameTemplate)
{
cancellationToken?.ThrowIfCancellationRequested();
cancellationToken.ThrowIfCancellationRequested();
removeMeasure.Table.Measures.Remove(removeMeasure);
}
}

void ApplyMeasureTemplate(MeasuresTemplateDefinition.MeasureTemplate template, Table targetTable, Measure? referenceMeasure, CancellationToken? cancellationToken)
void ApplyMeasureTemplate(MeasuresTemplateDefinition.MeasureTemplate template, Table targetTable, Measure? referenceMeasure, CancellationToken cancellationToken = default)
{
if (template.Name == null)
{
Expand All @@ -234,7 +234,7 @@ void ApplyMeasureTemplate(MeasuresTemplateDefinition.MeasureTemplate template, T
ReferenceMeasure = referenceMeasure,
DefaultVariables = Config.DefaultVariables
};
var modelMeasure = measureTemplate.ApplyTemplate(model, referenceMeasure?.Parent as Table ?? targetTable, cancellationToken, overrideExistingMeasures);
var modelMeasure = measureTemplate.ApplyTemplate(model, referenceMeasure?.Parent as Table ?? targetTable, overrideExistingMeasures, cancellationToken);
appliedMeasures.Add(modelMeasure);
}
}
Expand Down Expand Up @@ -279,7 +279,7 @@ private string GetSqlbiTemplateValue()
(from t in model.Tables
select t).FirstOrDefault(t => t.Name == tableName);
}
private Table GetTargetTable(TabularModel model, CancellationToken? cancellationToken)
private Table GetTargetTable(TabularModel model, CancellationToken cancellationToken = default)
{
Table? targetTable = null;
var targetTableName = Template.TargetTable.FirstOrDefault(tt => tt.Key == "Name").Value;
Expand All @@ -291,7 +291,7 @@ private Table GetTargetTable(TabularModel model, CancellationToken? cancellation
{
foreach (var tt in Template.TargetTable)
{
cancellationToken?.ThrowIfCancellationRequested();
cancellationToken.ThrowIfCancellationRequested();
var tables = MeasureTemplateBase.GetTablesFromAnnotations(model, tt.Key, tt.Value);
if (!tables.Any())
{
Expand Down
Loading

0 comments on commit 32b86b0

Please sign in to comment.