From 32b86b0cf42133b8d1f603b6f9d78270736b3054 Mon Sep 17 00:00:00 2001 From: Alberto Spelta Date: Thu, 5 Dec 2024 15:04:46 +0100 Subject: [PATCH] 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 --- src/Dax.Template.TestUI/ApplyDaxTemplate.cs | 20 +++---- src/Dax.Template.TestUI/BravoDaxTemplate.cs | 2 +- src/Dax.Template/Engine.cs | 36 ++++++------- .../Measures/MeasureTemplateBase.cs | 6 +-- src/Dax.Template/Measures/MeasuresTemplate.cs | 20 +++---- .../Tables/CalculatedTableTemplateBase.cs | 28 +++++----- .../Tables/Dates/BaseDateTemplate.cs | 8 +-- .../Tables/Dates/HolidaysDefinitionTable.cs | 2 +- .../Tables/Dates/HolidaysTable.cs | 4 +- .../Tables/ReferenceCalculatedTable.cs | 2 +- src/Dax.Template/Tables/TableTemplateBase.cs | 54 +++++++++---------- 11 files changed, 91 insertions(+), 91 deletions(-) diff --git a/src/Dax.Template.TestUI/ApplyDaxTemplate.cs b/src/Dax.Template.TestUI/ApplyDaxTemplate.cs index 026e990..f9984e8 100644 --- a/src/Dax.Template.TestUI/ApplyDaxTemplate.cs +++ b/src/Dax.Template.TestUI/ApplyDaxTemplate.cs @@ -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(); } @@ -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; @@ -91,7 +91,7 @@ private CalculatedTableTemplateBase CreateHolidaysTable(string dateTableName, TO } CalculatedTableTemplateBase template; template = new HolidaysTable(ReadConfig()); - template.ApplyTemplate(tableHolidays, null, hideTable); + template.ApplyTemplate(tableHolidays, hideTable); tableHolidays.RequestRefresh(TOM.RefreshType.Full); return template; @@ -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); @@ -157,11 +157,11 @@ private void GenerateDax_Click(object sender, EventArgs e) if (chkCustomTemplate.Checked) { var template = new CustomDateTable(ReadConfig(), ReadTemplateDefinition(), null); - result = template?.GetDaxTableExpression(null, null); + result = template?.GetDaxTableExpression(null); } else { var template = new SimpleDateTable(ReadConfig(),null); - result = template.GetDaxTableExpression(null, null); + result = template.GetDaxTableExpression(null); } txtDax.Text = result; } @@ -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; } @@ -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(); } @@ -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) @@ -370,7 +370,7 @@ private void ApplyTemplate(bool commitChanges) try { - templateEngine.ApplyTemplates(model, null); + templateEngine.ApplyTemplates(model); var modelChanges = Engine.GetModelChanges(model); if (commitChanges) diff --git a/src/Dax.Template.TestUI/BravoDaxTemplate.cs b/src/Dax.Template.TestUI/BravoDaxTemplate.cs index 6fcbf5e..07052e0 100644 --- a/src/Dax.Template.TestUI/BravoDaxTemplate.cs +++ b/src/Dax.Template.TestUI/BravoDaxTemplate.cs @@ -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) diff --git a/src/Dax.Template/Engine.cs b/src/Dax.Template/Engine.cs index 24e3e63..5cfbc1e 100644 --- a/src/Dax.Template/Engine.cs +++ b/src/Dax.Template/Engine.cs @@ -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 action)[] classes = new (string, Action)[] + (string className, Action action)[] classes = new (string, Action)[] { ( nameof(HolidaysDefinitionTable), ApplyHolidaysDefinitionTable ), ( nameof(HolidaysTable), ApplyHolidaysTable ), @@ -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); }); @@ -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) @@ -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(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) @@ -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)) { @@ -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( @@ -193,12 +193,12 @@ 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)) { @@ -206,16 +206,16 @@ void ApplyMeasuresTemplate(ITemplates.TemplateEntry templateEntry, CancellationT } var measuresTemplateDefinition = _package.ReadDefinition(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(localizationFile); translations.Translations = translations.Translations.Union(definitions.Translations).ToArray(); } @@ -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) @@ -256,7 +256,7 @@ private ReferenceCalculatedTable CreateDateTable( IsoFormat = isoFormat }; - template.ApplyTemplate(tableDate, cancellationToken, hideTable); + template.ApplyTemplate(tableDate, hideTable, cancellationToken); tableDate.RequestRefresh(RefreshType.Full); diff --git a/src/Dax.Template/Measures/MeasureTemplateBase.cs b/src/Dax.Template/Measures/MeasureTemplateBase.cs index 997600a..992afd5 100644 --- a/src/Dax.Template/Measures/MeasureTemplateBase.cs +++ b/src/Dax.Template/Measures/MeasureTemplateBase.cs @@ -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) @@ -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(); diff --git a/src/Dax.Template/Measures/MeasuresTemplate.cs b/src/Dax.Template/Measures/MeasuresTemplate.cs index 50b4e37..cca3a62 100644 --- a/src/Dax.Template/Measures/MeasuresTemplate.cs +++ b/src/Dax.Template/Measures/MeasuresTemplate.cs @@ -80,7 +80,7 @@ public string? DisplayFolderRuleSingleInstanceMeasures /// Returns the target measures for the template /// /// - private IEnumerable GetTargetMeasures(TabularModel model, CancellationToken? cancellationToken) + private IEnumerable GetTargetMeasures(TabularModel model, CancellationToken cancellationToken = default) { if (Config.TargetMeasures == null || Config.TargetMeasures.Length == 0) { @@ -94,7 +94,7 @@ from m in t.Measures IEnumerable result = Array.Empty(); foreach(var tm in Config.TargetMeasures) { - cancellationToken?.ThrowIfCancellationRequested(); + cancellationToken.ThrowIfCancellationRequested(); result = result.Union( from t in model.Tables from m in t.Measures @@ -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(); @@ -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); } @@ -197,7 +197,7 @@ where m.Annotations.Any(a => { foreach (var template in templateMeasures) { - cancellationToken?.ThrowIfCancellationRequested(); + cancellationToken.ThrowIfCancellationRequested(); ApplyMeasureTemplate(template, targetTable, referenceMeasure: target, cancellationToken); } } @@ -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) { @@ -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); } } @@ -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; @@ -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()) { diff --git a/src/Dax.Template/Tables/CalculatedTableTemplateBase.cs b/src/Dax.Template/Tables/CalculatedTableTemplateBase.cs index 31f7141..8576ddd 100644 --- a/src/Dax.Template/Tables/CalculatedTableTemplateBase.cs +++ b/src/Dax.Template/Tables/CalculatedTableTemplateBase.cs @@ -35,7 +35,7 @@ protected override bool RemoveExistingPartitions(Table dateTable) return removeExistingPartition; } - protected override void AddPartitions(Table dateTable, CancellationToken? cancellationToken) + protected override void AddPartitions(Table dateTable, CancellationToken cancellationToken = default) { // Add the new partition dateTable.Partitions.Add(new Partition @@ -53,7 +53,7 @@ protected override void AddPartitions(Table dateTable, CancellationToken? cancel private static readonly Regex regexGetIso = new(@"@@GETISO[ \r\n\t]*\([ \r\n\t]*\)", RegexOptions.Compiled); - protected virtual string? ProcessDaxExpression(string? expression, string lastStep, CancellationToken? cancellationToken, Microsoft.AnalysisServices.Tabular.Model? model = null) + protected virtual string? ProcessDaxExpression(string? expression, string lastStep, Microsoft.AnalysisServices.Tabular.Model? model = null, CancellationToken cancellationToken = default) { if (string.IsNullOrEmpty(expression)) return expression; @@ -104,7 +104,7 @@ protected static string GetComments(IDaxComment daxComment, string padding) static protected readonly string PadColumnAddColumnsExpression = new(' ', 12); static protected readonly string PadColumnAddColumnsDefinition = new(' ', 8); - public virtual string? GetDaxTableExpression(Microsoft.AnalysisServices.Tabular.Model? model, CancellationToken? cancellationToken) + public virtual string? GetDaxTableExpression(Microsoft.AnalysisServices.Tabular.Model? model, CancellationToken cancellationToken = default) { var listDependencies = Columns.GetDependencies(); @@ -121,7 +121,7 @@ orderby newLevel.Key var lastStepName = string.Empty; foreach (var level in groupElements) { - cancellationToken?.ThrowIfCancellationRequested(); + cancellationToken.ThrowIfCancellationRequested(); var daxSteps = GetLevelElements(level); lastStepName = daxSteps.LastOrDefault()?.Name ?? lastStepName; string previousStepToReference = (!string.IsNullOrEmpty(previousStepName) ? previousStepName : lastStepName); @@ -129,7 +129,7 @@ orderby newLevel.Key var daxElements = GetLevelElements(level).Except(daxSteps); var rowVars = GetLevelElements(level); // Skip columns without definition, such as Date assigned to a step - var columns = GetLevelElements(level).Where(c => !string.IsNullOrEmpty(ProcessDaxExpression(c.Expression, previousStepToReference, cancellationToken, model))); + var columns = GetLevelElements(level).Where(c => !string.IsNullOrEmpty(ProcessDaxExpression(c.Expression, previousStepToReference, model, cancellationToken))); var configurableGlobalVars = globalVars.Where(v => v.IsConfigurable); var internalGlobalVars = globalVars.Where(v => !v.IsConfigurable); @@ -138,11 +138,11 @@ orderby newLevel.Key result += CommentPrefixNewLine; result += $"{CommentPrefixNewLine} Configuration"; result += CommentPrefixNewLine; - result += string.Join(string.Empty, configurableGlobalVars.Select(e => $"\r\n{GetComments(e, PadGlobalVarDefinition)}VAR {e.Name} = {ProcessDaxExpression(e.Expression, previousStepToReference, cancellationToken, model)}")); + result += string.Join(string.Empty, configurableGlobalVars.Select(e => $"\r\n{GetComments(e, PadGlobalVarDefinition)}VAR {e.Name} = {ProcessDaxExpression(e.Expression, previousStepToReference, model, cancellationToken)}")); result += CommentLineSeparator; } - result += string.Join(string.Empty, internalGlobalVars.Select(e => $"\r\n{GetComments(e, PadGlobalVarDefinition)}VAR {e.Name} = {ProcessDaxExpression(e.Expression, previousStepToReference, cancellationToken, model)}")); - result += string.Join(string.Empty, daxSteps.Select(e => $"\r\n{GetComments(e, PadStepDefinition)}VAR {e.Name} = {ProcessDaxExpression(e.Expression, previousStepToReference, cancellationToken, model)}")); + result += string.Join(string.Empty, internalGlobalVars.Select(e => $"\r\n{GetComments(e, PadGlobalVarDefinition)}VAR {e.Name} = {ProcessDaxExpression(e.Expression, previousStepToReference, model, cancellationToken)}")); + result += string.Join(string.Empty, daxSteps.Select(e => $"\r\n{GetComments(e, PadStepDefinition)}VAR {e.Name} = {ProcessDaxExpression(e.Expression, previousStepToReference, model, cancellationToken)}")); if (rowVars.Any() || columns.Any()) { if (!columns.Any()) @@ -173,13 +173,13 @@ orderby newLevel.Key var daxElement = daxElements.FirstOrDefault() ?? defaultElement; var daxRowVars = (rowVars?.Any() == true) ? - string.Join("\r\n", rowVars.Select(e => $"{GetComments(e,PadRowVarDefinition)}{PadRowVarDefinition}VAR {e.Name} = {ProcessDaxExpression(e.Expression, previousStepToReference, cancellationToken, model)}")) + "\r\n RETURN " : + string.Join("\r\n", rowVars.Select(e => $"{GetComments(e,PadRowVarDefinition)}{PadRowVarDefinition}VAR {e.Name} = {ProcessDaxExpression(e.Expression, previousStepToReference, model, cancellationToken)}")) + "\r\n RETURN " : " "; - var columnsList = string.Join(",\r\n", columns.Select(c => $"{GetComments(c, PadColumnGenerateDefinition)}{PadColumnGenerateDefinition}\"{c.Name}\", {ProcessDaxExpression(c.Expression, previousStepToReference, cancellationToken, model)}")); + var columnsList = string.Join(",\r\n", columns.Select(c => $"{GetComments(c, PadColumnGenerateDefinition)}{PadColumnGenerateDefinition}\"{c.Name}\", {ProcessDaxExpression(c.Expression, previousStepToReference, model, cancellationToken)}")); var daxColumns = $@"ROW ( {columnsList} )"; - var x = ProcessDaxExpression(daxElement.Expression, previousStepToReference, cancellationToken, model)?.Replace("@@VARS@@", daxRowVars).Replace("@@COLUMNS@@", daxColumns); + var x = ProcessDaxExpression(daxElement.Expression, previousStepToReference, model, cancellationToken)?.Replace("@@VARS@@", daxRowVars).Replace("@@COLUMNS@@", daxColumns); result += x; } else @@ -192,13 +192,13 @@ orderby newLevel.Key )" }; var daxElement = daxElements.FirstOrDefault() ?? defaultElement; - var columnsList = string.Join(",\r\n", columns.Select(e => $"{PadColumnAddColumnsDefinition}\"{e.Name}\", {ProcessDaxExpression(e.Expression, previousStepToReference, cancellationToken, model)}")); + var columnsList = string.Join(",\r\n", columns.Select(e => $"{PadColumnAddColumnsDefinition}\"{e.Name}\", {ProcessDaxExpression(e.Expression, previousStepToReference, model, cancellationToken)}")); string? replacedExpression = daxElement.Expression?.Replace("@@COLUMNS@@", columnsList); if (replacedExpression == daxElement.Expression) { throw new TemplateException($"Missing columns list in DaxElement for {stepName}: columnsList: {columnsList}"); } - result += ProcessDaxExpression(replacedExpression, previousStepToReference, cancellationToken, model); + result += ProcessDaxExpression(replacedExpression, previousStepToReference, model, cancellationToken); } previousStepName = stepName; } @@ -224,7 +224,7 @@ from c in Columns { throw new TemplateException($"Missing columns list in final result - are all columns hidden?"); } - result += ProcessDaxExpression(replacedExpression, (!string.IsNullOrEmpty(previousStepName) ? previousStepName : lastStepName), cancellationToken, model); + result += ProcessDaxExpression(replacedExpression, (!string.IsNullOrEmpty(previousStepName) ? previousStepName : lastStepName), model, cancellationToken); previousStepName = stepName; } result += $"\r\nRETURN\r\n {previousStepName}"; diff --git a/src/Dax.Template/Tables/Dates/BaseDateTemplate.cs b/src/Dax.Template/Tables/Dates/BaseDateTemplate.cs index 87e23a4..aabbeab 100644 --- a/src/Dax.Template/Tables/Dates/BaseDateTemplate.cs +++ b/src/Dax.Template/Tables/Dates/BaseDateTemplate.cs @@ -40,7 +40,7 @@ protected override bool IsRelationshipToSaveAndRestore(SingleColumnRelationship && relationship.ToColumn.DataType == DataType.DateTime; } - public override void ApplyTemplate(Table dateTable, CancellationToken? cancellationToken) + public override void ApplyTemplate(Table dateTable, CancellationToken cancellationToken = default) { foreach (var column in Columns.Where(c => c is Model.DateColumn)) { @@ -75,10 +75,10 @@ public override void ApplyTemplate(Table dateTable, CancellationToken? cancellat /// /// /// - protected override string? ProcessDaxExpression(string? expression, string lastStep, CancellationToken? cancellationToken, TabularModel? model = null) + protected override string? ProcessDaxExpression(string? expression, string lastStep, TabularModel? model = null, CancellationToken cancellationToken = default) { - cancellationToken?.ThrowIfCancellationRequested(); - expression = base.ProcessDaxExpression(expression, lastStep, cancellationToken, model); + cancellationToken.ThrowIfCancellationRequested(); + expression = base.ProcessDaxExpression(expression, lastStep, model, cancellationToken); if (string.IsNullOrEmpty(expression)) return expression; expression = GetConfig(expression); diff --git a/src/Dax.Template/Tables/Dates/HolidaysDefinitionTable.cs b/src/Dax.Template/Tables/Dates/HolidaysDefinitionTable.cs index e7fd52d..fd9f440 100644 --- a/src/Dax.Template/Tables/Dates/HolidaysDefinitionTable.cs +++ b/src/Dax.Template/Tables/Dates/HolidaysDefinitionTable.cs @@ -172,7 +172,7 @@ public HolidaysDefinitionTable(HolidaysDefinitions holidaysDefinitions) Columns.AddRange(columns); } - public override string? GetDaxTableExpression(TabularModel? model, CancellationToken? cancellationToken) + public override string? GetDaxTableExpression(TabularModel? model, CancellationToken cancellationToken = default) { return __HolidaysDefinition.Expression; } diff --git a/src/Dax.Template/Tables/Dates/HolidaysTable.cs b/src/Dax.Template/Tables/Dates/HolidaysTable.cs index 9b682f5..179e884 100644 --- a/src/Dax.Template/Tables/Dates/HolidaysTable.cs +++ b/src/Dax.Template/Tables/Dates/HolidaysTable.cs @@ -360,9 +360,9 @@ RETURN _SubstituteWeekDay - _HolidayWeekDayStep1 Columns.AddRange(columns); } - public override string? GetDaxTableExpression(TabularModel? model, CancellationToken? cancellationToken) + public override string? GetDaxTableExpression(TabularModel? model, CancellationToken cancellationToken = default) { - return ProcessDaxExpression(__HolidaysTable.Expression, string.Empty, cancellationToken, model); + return ProcessDaxExpression(__HolidaysTable.Expression, string.Empty, model, cancellationToken); } } } diff --git a/src/Dax.Template/Tables/ReferenceCalculatedTable.cs b/src/Dax.Template/Tables/ReferenceCalculatedTable.cs index a043125..8da8dc2 100644 --- a/src/Dax.Template/Tables/ReferenceCalculatedTable.cs +++ b/src/Dax.Template/Tables/ReferenceCalculatedTable.cs @@ -7,7 +7,7 @@ public abstract class ReferenceCalculatedTable : CalculatedTableTemplateBase { public string? HiddenTable { get; init; } - public override string? GetDaxTableExpression(Microsoft.AnalysisServices.Tabular.Model? model, CancellationToken? cancellationToken) + public override string? GetDaxTableExpression(Microsoft.AnalysisServices.Tabular.Model? model, CancellationToken cancellationToken = default) { return QuotedHiddenTable ?? base.GetDaxTableExpression(model, cancellationToken); } diff --git a/src/Dax.Template/Tables/TableTemplateBase.cs b/src/Dax.Template/Tables/TableTemplateBase.cs index 62a99f7..c6ccca2 100644 --- a/src/Dax.Template/Tables/TableTemplateBase.cs +++ b/src/Dax.Template/Tables/TableTemplateBase.cs @@ -24,15 +24,15 @@ public abstract class TableTemplateBase public Translations? Translation { get; set; } private bool templateApplied = false; - private void ResetTabularReferences(CancellationToken? cancellationToken) + private void ResetTabularReferences(CancellationToken cancellationToken = default) { - cancellationToken?.ThrowIfCancellationRequested(); + cancellationToken.ThrowIfCancellationRequested(); foreach (var column in Columns) column.Reset(); foreach (var hierarchy in Hierarchies) { - cancellationToken?.ThrowIfCancellationRequested(); + cancellationToken.ThrowIfCancellationRequested(); hierarchy.Reset(); foreach (var level in hierarchy.Levels) { @@ -46,14 +46,14 @@ private void ResetTabularReferences(CancellationToken? cancellationToken) templateApplied = false; } - protected virtual void RenameWithTranslation(Table tabularTable, Translations.Language language, CancellationToken? cancellationToken) + protected virtual void RenameWithTranslation(Table tabularTable, Translations.Language language, CancellationToken cancellationToken = default) { // if (!string.IsNullOrEmpty(language.Table.Name)) tabularTable.Name = language.Table.Name; if (!string.IsNullOrEmpty(language.Table?.Description)) tabularTable.Description = language.Table.Description; foreach(var column in tabularTable.Columns) { - cancellationToken?.ThrowIfCancellationRequested(); + cancellationToken.ThrowIfCancellationRequested(); var columnTranslation = language.Columns.FirstOrDefault(c => c.OriginalName == column.Name); if (columnTranslation != null) { @@ -65,7 +65,7 @@ protected virtual void RenameWithTranslation(Table tabularTable, Translations.La } foreach (var measure in tabularTable.Measures) { - cancellationToken?.ThrowIfCancellationRequested(); + cancellationToken.ThrowIfCancellationRequested(); var measureTranslation = language.Measures.FirstOrDefault(c => c.OriginalName == measure.Name); if (measureTranslation != null) { @@ -77,7 +77,7 @@ protected virtual void RenameWithTranslation(Table tabularTable, Translations.La } foreach (var hierarchy in tabularTable.Hierarchies) { - cancellationToken?.ThrowIfCancellationRequested(); + cancellationToken.ThrowIfCancellationRequested(); var hierarchyTranslation = language.Hierarchies.FirstOrDefault(h => h.OriginalName == hierarchy.Name); if (hierarchyTranslation != null) { @@ -86,7 +86,7 @@ protected virtual void RenameWithTranslation(Table tabularTable, Translations.La if (hierarchyTranslation.DisplayFolders != null) hierarchy.DisplayFolder = hierarchyTranslation.DisplayFolders; foreach(var level in hierarchy.Levels) { - cancellationToken?.ThrowIfCancellationRequested(); + cancellationToken.ThrowIfCancellationRequested(); var levelTranslation = hierarchyTranslation.Levels.FirstOrDefault(l => l.OriginalName == level.Name); if (levelTranslation != null) { @@ -103,7 +103,7 @@ protected virtual void AddTranslation(Table tabularTable, Translations.Language throw new NotImplementedException(); } - protected virtual void ApplyTranslations(Table tabularTable, CancellationToken? cancellationToken) + protected virtual void ApplyTranslations(Table tabularTable, CancellationToken cancellationToken = default) { if (Translation == null) return; @@ -120,11 +120,11 @@ protected virtual void ApplyTranslations(Table tabularTable, CancellationToken? // Apply required translations Translation.GetTranslations().Where(t => Translation.ApplyAllIso || Translation.ApplyIso.Contains(t.Iso)).ToList().ForEach(t => { - cancellationToken?.ThrowIfCancellationRequested(); + cancellationToken.ThrowIfCancellationRequested(); AddTranslation(tabularTable, t); }); } - public void ApplyTemplate(Table tabularTable, CancellationToken? cancellationToken, bool hideTable = false) + public void ApplyTemplate(Table tabularTable, bool hideTable = false, CancellationToken cancellationToken = default) { ApplyTemplate(tabularTable, cancellationToken); @@ -152,7 +152,7 @@ protected virtual bool IsRelationshipToSaveAndRestore(SingleColumnRelationship r return true; } - public virtual void ApplyTemplate(Table tabularTable, CancellationToken? cancellationToken) + public virtual void ApplyTemplate(Table tabularTable, CancellationToken cancellationToken = default) { if (templateApplied) { @@ -175,9 +175,9 @@ public virtual void ApplyTemplate(Table tabularTable, CancellationToken? cancell RestoreAffectedRelationships(tabularTable, cancellationToken); } - private void SaveAffectedRelationships(Table tabularTable, CancellationToken? cancellationToken) + private void SaveAffectedRelationships(Table tabularTable, CancellationToken cancellationToken = default) { - cancellationToken?.ThrowIfCancellationRequested(); + cancellationToken.ThrowIfCancellationRequested(); if (tabularTable.Model != null) { @@ -201,7 +201,7 @@ where r is SingleColumnRelationship } } - private void RestoreAffectedRelationships(Table tabularTable, CancellationToken? cancellationToken) + private void RestoreAffectedRelationships(Table tabularTable, CancellationToken cancellationToken = default) { if (FixRelationshipsTo != null) { @@ -212,7 +212,7 @@ private void RestoreAffectedRelationships(Table tabularTable, CancellationToken? } } - cancellationToken?.ThrowIfCancellationRequested(); + cancellationToken.ThrowIfCancellationRequested(); if (FixRelationshipsFrom != null) { @@ -256,7 +256,7 @@ protected virtual string GetSourceColumnName(Column column) return null; } - protected virtual void AddColumns(Table dateTable, CancellationToken? cancellationToken) + protected virtual void AddColumns(Table dateTable, CancellationToken cancellationToken = default) { // Save existing columns (like calculated columns) var existingColumns = dateTable.Columns.Select(c => c.Clone()).ToList(); @@ -266,7 +266,7 @@ protected virtual void AddColumns(Table dateTable, CancellationToken? cancellati // Add the columns foreach (var column in Columns.Where(c=>!c.IsTemporary)) { - cancellationToken?.ThrowIfCancellationRequested(); + cancellationToken.ThrowIfCancellationRequested(); column.TabularColumn = new CalculatedTableColumn { Name = column.Name, @@ -337,13 +337,13 @@ protected virtual void AddColumns(Table dateTable, CancellationToken? cancellati } // TODO: this code is very similar to ApplyAnnotations in MeasuresTemplateBase.cs - evaluate whether we should consolidate the code in a single function - protected virtual void AddAnnotations(Table dateTable, CancellationToken? cancellationToken) + protected virtual void AddAnnotations(Table dateTable, CancellationToken cancellationToken = default) { - cancellationToken?.ThrowIfCancellationRequested(); + cancellationToken.ThrowIfCancellationRequested(); if (Annotations == null) return; foreach (var annotation in Annotations) { - cancellationToken?.ThrowIfCancellationRequested(); + cancellationToken.ThrowIfCancellationRequested(); var annotationName = annotation.Key; var annotationValue = annotation.Value.ToString(); @@ -361,13 +361,13 @@ protected virtual void AddAnnotations(Table dateTable, CancellationToken? cancel } } - protected virtual void AddHierarchies(Table dateTable, CancellationToken? cancellationToken) + protected virtual void AddHierarchies(Table dateTable, CancellationToken cancellationToken = default) { // Create Tabular level for hierarchies var levels = from h in Hierarchies from l in h.Levels select l; foreach (var level in levels) { - cancellationToken?.ThrowIfCancellationRequested(); + cancellationToken.ThrowIfCancellationRequested(); level.TabularLevel = new TabularLevel { Name = level.Name, @@ -380,7 +380,7 @@ protected virtual void AddHierarchies(Table dateTable, CancellationToken? cancel // Set the hierarchies foreach (var hierarchy in Hierarchies) { - cancellationToken?.ThrowIfCancellationRequested(); + cancellationToken.ThrowIfCancellationRequested(); var tabularHierarchy = new TabularHierarchy { Name = hierarchy.Name, @@ -406,9 +406,9 @@ protected virtual void AddHierarchies(Table dateTable, CancellationToken? cancel } } - protected virtual void RemoveExistingElements(Table dateTable, CancellationToken? cancellationToken) + protected virtual void RemoveExistingElements(Table dateTable, CancellationToken cancellationToken = default) { - cancellationToken?.ThrowIfCancellationRequested(); + cancellationToken.ThrowIfCancellationRequested(); if (RemoveExistingPartitions(dateTable)) { @@ -442,7 +442,7 @@ protected virtual void RemoveExistingColumns(Table dateTable) } protected abstract bool RemoveExistingPartitions(Table dateTable); - protected abstract void AddPartitions(Table dateTable, CancellationToken? cancellationToken); + protected abstract void AddPartitions(Table dateTable, CancellationToken cancellationToken = default); } }