Skip to content

Commit 5e86e1e

Browse files
committed
additional cleaning
notables: CardinalityRange as nullable removed reference to missing file in sln def rename HandleBars to Handlebars to align casing with src address potential nullable issues
1 parent d080a20 commit 5e86e1e

File tree

8 files changed

+365
-372
lines changed

8 files changed

+365
-372
lines changed

DataWarehouseAutomation/DataWarehouseAutomation.sln

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Sample Metadata", "Sample M
2727
Sample_Metadata\sampleCustomFunctions.json = Sample_Metadata\sampleCustomFunctions.json
2828
Sample_Metadata\sampleDataVaultHub.json = Sample_Metadata\sampleDataVaultHub.json
2929
Sample_Metadata\sampleFreeForm.json = Sample_Metadata\sampleFreeForm.json
30-
Sample_Metadata\sampleJsonStagingWithPsaDetails.json = Sample_Metadata\sampleJsonStagingWithPsaDetails.json
3130
Sample_Metadata\sampleMultipleDataItemMappings.json = Sample_Metadata\sampleMultipleDataItemMappings.json
3231
Sample_Metadata\sampleSimpleDDL.json = Sample_Metadata\sampleSimpleDDL.json
3332
Sample_Metadata\sampleSourceQuery.json = Sample_Metadata\sampleSourceQuery.json

DataWarehouseAutomation/DataWarehouseAutomation/DwaModel/Cardinality.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ public class Cardinality
1515
public string? Id { get; set; }
1616

1717
/// <summary>
18-
/// Optional information to name a certain cardinality construct. For example one-to-one, one-to-many, or many-to-many.
18+
/// Optional information to name a certain cardinality construct.
19+
/// For example one-to-one, one-to-many, or many-to-many.
1920
/// E.g. one-to-one could be defined as {"from": {"min": 1, "max": 1}, "to": {"min": 1, "max": 1}}.
2021
/// </summary>
2122
[JsonPropertyName("name")]
@@ -24,15 +25,15 @@ public class Cardinality
2425
/// <summary>
2526
/// The 'from' component in the cardinality, e.g. the '1' in 1 to many.
2627
/// </summary>
27-
public CardinalityRange FromRange { get; set; } = new CardinalityRange { Min = "1", Max = "1" };
28+
public CardinalityRange? FromRange { get; set; } = new CardinalityRange { Min = "1", Max = "1" };
2829

2930
/// <summary>
3031
/// The 'to' component in the cardinality, e.g. the 'many' in 1 to many.
3132
/// </summary>
32-
public CardinalityRange ToRange { get; set; } = new CardinalityRange { Min = "1", Max = "N" };
33+
public CardinalityRange? ToRange { get; set; } = new CardinalityRange { Min = "1", Max = "N" };
3334

3435
/// <summary>
35-
/// Free-form and optional classification for the Data Item for use in generation logic (evaluation).
36+
/// Free-form and optional classification for the Cardinality for use in generation logic (evaluation).
3637
/// </summary>
3738
[JsonPropertyName("classifications")]
3839
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
@@ -80,4 +81,4 @@ public override string ToString()
8081
return Name ?? string.Empty;
8182
}
8283
#endregion
83-
}
84+
}

DataWarehouseAutomation/DataWarehouseAutomation/Utils/HandleBarsHelpers.cs

Lines changed: 44 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
using DataWarehouseAutomation.DwaModel;
2-
using HandlebarsDotNet;
1+
namespace DataWarehouseAutomation.Utils;
32

4-
namespace DataWarehouseAutomation.Utils;
5-
6-
public static class HandleBarsHelpers
3+
public static class HandlebarsHelpers
74
{
85
/// <summary>
96
/// Generate a random integer value, capped by an input (maximum) value.
@@ -45,67 +42,67 @@ public static DateTime GetRandomDate(int startYear = 1995)
4542
/// Convenience method to install all Handlebars extension in one go.
4643
/// </summary>
4744
/// <exception cref="HandlebarsException"></exception>
48-
public static void RegisterHandleBarsHelpers()
45+
public static void RegisterHandlebarsHelpers()
4946
{
5047
// Extension to the Handlebars templating language. Shows the current date and time in the generated output.
5148
// Usage: {{now}}
5249
// Example: The time is {{now}}.
53-
Handlebars.RegisterHelper("now", (output, context, arguments) => { output.WriteSafeString(DateTime.Now); });
50+
Handlebars.RegisterHelper("now", (output, _, _) => output.WriteSafeString(DateTime.Now));
5451

5552
// Generation random date, based on an integer input year value.
56-
Handlebars.RegisterHelper("randomDate", (output, context, arguments) =>
53+
Handlebars.RegisterHelper("randomDate", (output, _, arguments) =>
5754
{
5855
if (arguments.Length > 1)
5956
{
60-
throw new HandlebarsException("The {{randomdate}} function requires a single integer (year e.g. 1995) value as input.");
57+
throw new HandlebarsException("The {{randomDate}} function requires a single integer (year e.g. 1995) value as input.");
6158
}
6259

6360
if (arguments.Length == 1)
6461
{
6562
bool evaluationResult = int.TryParse(arguments[0].ToString(), out var localInteger);
6663
if (evaluationResult == false)
6764
{
68-
throw new HandlebarsException($"The {{randomdate}} functions failed because {arguments[0]} could not be converted to an integer.");
65+
throw new HandlebarsException($"The {{randomDate}} functions failed because {arguments[0]} could not be converted to an integer.");
6966
}
7067

7168
output.WriteSafeString(GetRandomDate(localInteger).Date);
7269
}
7370
});
7471

7572
// Generation random string, based on an integer input value cap.
76-
Handlebars.RegisterHelper("randomNumber", (output, context, arguments) =>
73+
Handlebars.RegisterHelper("randomNumber", (output, _, arguments) =>
7774
{
7875
if (arguments.Length > 1)
7976
{
80-
throw new HandlebarsException("The {{randomnumber}} function requires a single integer value as input.");
77+
throw new HandlebarsException("The {{randomNumber}} function requires a single integer value as input.");
8178
}
8279

8380
if (arguments.Length == 1)
8481
{
8582
bool evaluationResult = int.TryParse(arguments[0].ToString(), out var localInteger);
86-
if (evaluationResult == false)
83+
if (!evaluationResult)
8784
{
88-
throw new HandlebarsException($"The {{randomnumber}} functions failed because {arguments[0]} could not be converted to an integer.");
85+
throw new HandlebarsException($"The {{randomNumber}} functions failed because {arguments[0]} could not be converted to an integer.");
8986
}
9087
output.WriteSafeString(GetRandomNumber(localInteger));
9188
}
9289
});
9390

9491
// Generation random string, based on an integer input value
95-
Handlebars.RegisterHelper("randomString", (output, context, arguments) =>
92+
Handlebars.RegisterHelper("randomString", (output, _, arguments) =>
9693
{
9794
if (arguments.Length > 1)
9895
{
99-
throw new HandlebarsException("The {{randomstring}} function requires a single integer value as input.");
96+
throw new HandlebarsException("The {{randomString}} function requires a single integer value as input.");
10097
}
10198

10299
if (arguments.Length == 1)
103100
{
104101
bool evaluationResult = int.TryParse(arguments[0].ToString(), out var localInteger);
105102

106-
if (evaluationResult == false)
103+
if (!evaluationResult)
107104
{
108-
throw new HandlebarsException($"The {{randomstring}} functions failed because {arguments[0]} could not be converted to an integer.");
105+
throw new HandlebarsException($"The {{randomString}} functions failed because {arguments[0]} could not be converted to an integer.");
109106
}
110107

111108
var array = new[]
@@ -121,9 +118,9 @@ public static void RegisterHandleBarsHelpers()
121118
}
122119
});
123120

124-
Handlebars.RegisterHelper("stringWrap", (writer, context, args) =>
121+
Handlebars.RegisterHelper("stringWrap", (writer, _, args) =>
125122
{
126-
if (args.Length != 3) throw new HandlebarsException("The {{stringwrap}} function requires exactly three arguments, an object and the string to wrap its value in.");
123+
if (args.Length != 3) throw new HandlebarsException("The {{stringWrap}} function requires exactly three arguments, an object and the string to wrap its value in.");
127124

128125
if (args[0].GetType().Name != "UndefinedBindingResult")
129126
{
@@ -138,15 +135,15 @@ public static void RegisterHandleBarsHelpers()
138135
}
139136
});
140137

141-
Handlebars.RegisterHelper("stringUpper", (writer, context, args) =>
138+
Handlebars.RegisterHelper("stringUpper", (writer, _, args) =>
142139
{
143140
if (args.Length != 1) throw new HandlebarsException("The {{stringUpper}} function requires one, and only one, input string value.");
144141

145-
if (args[0].GetType().Name != "UndefinedBindingResult")
142+
if (args[0].GetType().Name != "UndefinedBindingResult" && args[0] is string theString)
146143
{
147144
try
148145
{
149-
writer.Write(args[0].ToString().ToUpper());
146+
writer.Write(theString.ToUpper());
150147
}
151148
catch (Exception ex)
152149
{
@@ -155,15 +152,15 @@ public static void RegisterHandleBarsHelpers()
155152
}
156153
});
157154

158-
Handlebars.RegisterHelper("stringLower", (writer, context, args) =>
155+
Handlebars.RegisterHelper("stringLower", (writer, _, args) =>
159156
{
160157
if (args.Length != 1) throw new HandlebarsException("The {{stringLower}} function requires one, and only one, input string value.");
161158

162-
if (args[0].GetType().Name != "UndefinedBindingResult")
159+
if (args[0].GetType().Name != "UndefinedBindingResult" && args[0] is string theString)
163160
{
164161
try
165162
{
166-
writer.Write(args[0].ToString().ToLower());
163+
writer.Write(theString.ToLower());
167164
}
168165
catch (Exception ex)
169166
{
@@ -173,8 +170,8 @@ public static void RegisterHandleBarsHelpers()
173170
});
174171

175172
// Accept two values, and see if they are the same, use as block helper.
176-
// Usage {{#stringcompare string1 string2}} do something {{else}} do something else {{/stringcompare}}
177-
// Usage {{#stringcompare string1 string2}} do something {{/stringcompare}}
173+
// Usage {{#stringCompare string1 string2}} do something {{else}} do something else {{/stringCompare}}
174+
// Usage {{#stringCompare string1 string2}} do something {{/stringCompare}}
178175
Handlebars.RegisterHelper("stringCompare", (output, options, context, arguments) =>
179176
{
180177
if (arguments.Length != 2) throw new HandlebarsException("The {{stringCompare}} function requires exactly two arguments.");
@@ -193,8 +190,8 @@ public static void RegisterHandleBarsHelpers()
193190
});
194191

195192
// Accept two values, and do something if they are the different.
196-
// Usage {{#stringdiff string1 string2}} do something {{else}} do something else {{/stringdiff}}
197-
// Usage {{#stringdiff string1 string2}} do something {{/stringdiff}}
193+
// Usage {{#stringDiff string1 string2}} do something {{else}} do something else {{/stringDiff}}
194+
// Usage {{#stringDiff string1 string2}} do something {{/stringDiff}}
198195
Handlebars.RegisterHelper("stringDiff", (output, options, context, arguments) =>
199196
{
200197
if (arguments.Length != 2) throw new HandlebarsException("The {{stringDiff}} functions requires exactly two arguments.");
@@ -223,7 +220,7 @@ public static void RegisterHandleBarsHelpers()
223220
{
224221
bool evaluationResult = int.TryParse(arguments[0].ToString(), out var localInteger);
225222

226-
if (evaluationResult == false)
223+
if (!evaluationResult)
227224
{
228225
throw new HandlebarsException($"The {{replicate}} functions failed because {arguments[0]} could not be converted to an integer.");
229226
}
@@ -253,7 +250,7 @@ public static void RegisterHandleBarsHelpers()
253250

254251
});
255252

256-
Handlebars.RegisterHelper("stringReplace", (writer, context, args) =>
253+
Handlebars.RegisterHelper("stringReplace", (writer, _, args) =>
257254
{
258255
if (args.Length < 3) throw new HandlebarsException("The {{StringReplace}} function requires at least three arguments.");
259256

@@ -267,12 +264,11 @@ public static void RegisterHandleBarsHelpers()
267264
{
268265
expression = value.GetString();
269266
}
270-
271-
string pattern = args[1] as string;
272-
string replacement = args[2] as string;
273-
274-
expression = expression.ToString().Replace(pattern, replacement);
275-
writer.WriteSafeString(expression);
267+
if (args[1] is string pattern && args[2] is string replacement)
268+
{
269+
expression = expression?.ToString()?.Replace(pattern, replacement);
270+
writer.WriteSafeString(expression);
271+
}
276272
}
277273
catch (Exception exception)
278274
{
@@ -289,9 +285,9 @@ public static void RegisterHandleBarsHelpers()
289285
try
290286
{
291287
var searchString = arguments[0] == null ? "" : arguments[0].ToString();
292-
DataObjectMapping dataObjectMapping = JsonSerializer.Deserialize<DataObjectMapping>(context.Value.ToString());
288+
DataObjectMapping? dataObjectMapping = JsonSerializer.Deserialize<DataObjectMapping>(context.Value.ToString());
293289

294-
var dataItemExists = dataObjectMapping.DataItemMappings.Where(x => x.TargetDataItem.Name == searchString).FirstOrDefault();
290+
var dataItemExists = dataObjectMapping?.DataItemMappings?.FirstOrDefault(x => x.TargetDataItem.Name == searchString);
295291

296292
if (dataItemExists != null)
297293
{
@@ -306,7 +302,7 @@ public static void RegisterHandleBarsHelpers()
306302
}
307303
catch (Exception exception)
308304
{
309-
throw new HandlebarsException($"The {{targetDataItemExists}} helper reported a conversion error, and was unable to deserialize the context into a DataObjectMapping. The reported error is " + exception.Message);
305+
throw new HandlebarsException("The {{targetDataItemExists}} helper reported a conversion error, and was unable to deserialize the context into a DataObjectMapping. The reported error is " + exception.Message);
310306
}
311307
});
312308

@@ -329,7 +325,6 @@ public static void RegisterHandleBarsHelpers()
329325

330326
try
331327
{
332-
333328
var dataObjectMapping = JsonSerializer.Deserialize<DataObjectMapping>(context.Value.ToString(), DefaultJsonOptions.DeserializerOptions);
334329

335330
var outcome = false;
@@ -348,9 +343,9 @@ public static void RegisterHandleBarsHelpers()
348343

349344
if (targetDataItemsWithClassifications != null)
350345
{
351-
var dataItemClassifications = targetDataItemsWithClassifications.SelectMany(x => x.TargetDataItem.Classifications).ToList();
346+
var dataItemClassifications = targetDataItemsWithClassifications.SelectMany(x => x.TargetDataItem.Classifications ?? []).ToList();
352347

353-
if (dataItemClassifications != null && dataItemClassifications.Any())
348+
if (dataItemClassifications?.Any() == true)
354349
{
355350
foreach (var classification in dataItemClassifications)
356351
{
@@ -393,7 +388,7 @@ public static void RegisterHandleBarsHelpers()
393388
}
394389
});
395390

396-
// Block helper that evalues is a certain classification exists in the list of classifications.
391+
// Block helper that evaluates is a certain classification exists in the list of classifications.
397392
Handlebars.RegisterHelper("hasClassification", (output, options, context, parameters) =>
398393
{
399394
// Check if the parameters are valid.
@@ -414,9 +409,9 @@ public static void RegisterHandleBarsHelpers()
414409
{
415410
var classifications = JsonSerializer.Deserialize<List<DataClassification>>(classificationsParameter.ToString() ?? string.Empty);
416411
var classificationName = (string)parameters[1];
417-
var result = classifications?.Find(i => i.Classification.ToString().Equals(classificationName, StringComparison.OrdinalIgnoreCase)).Classification;
412+
var result = classifications?.Find(i => i.Classification.Equals(classificationName, StringComparison.OrdinalIgnoreCase))?.Classification;
418413

419-
if (result != null && result != "")
414+
if (!string.IsNullOrEmpty(result))
420415
{
421416
// Regular block, a classification has been found
422417
options.Template(output, context);
@@ -434,7 +429,7 @@ public static void RegisterHandleBarsHelpers()
434429
}
435430
});
436431

437-
// Block helper that evalues is a certain classification exists in the list of classifications.
432+
// Block helper that evaluates is a certain classification exists in the list of classifications.
438433
Handlebars.RegisterHelper("hasClassification", (output, options, context, parameters) =>
439434
{
440435
// Check if the parameters are valid.
@@ -479,7 +474,7 @@ public static void RegisterHandleBarsHelpers()
479474
}
480475
});
481476

482-
// Block helper that evalues is a certain string exists in a list of string values.
477+
// Block helper that evaluates is a certain string exists in a list of string values.
483478
Handlebars.RegisterHelper("hasStringValue", (output, options, context, parameters) =>
484479
{
485480
// Check if the parameters are valid.

0 commit comments

Comments
 (0)