Skip to content

Commit 80dfe2d

Browse files
committed
Adding new extension
1 parent 84f0e7f commit 80dfe2d

File tree

2 files changed

+88
-9
lines changed

2 files changed

+88
-9
lines changed

DataWarehouseAutomation/DataWarehouseAutomation/Utils/HandleBarsHelpers.cs

Lines changed: 87 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -434,27 +434,105 @@ public static void RegisterHandleBarsHelpers()
434434
}
435435
});
436436

437-
// lookupExtension allows a lookup of an extension value by key. Pass in the Extensions list and the string key value as parameters.
438-
Handlebars.RegisterHelper("lookupExtension", (writer, context, parameters) =>
437+
// Block helper that evalues is a certain classification exists in the list of classifications.
438+
Handlebars.RegisterHelper("hasClassification", (output, options, context, parameters) =>
439439
{
440440
// Check if the parameters are valid.
441441
if (parameters.Length != 2 || parameters[1] is not string)
442442
{
443-
throw new HandlebarsException("The {{lookupExtension}} helper expects two arguments: a List<Extension> and a string lookup key");
443+
throw new HandlebarsException("An issue was encountered. The {{hasClassification}} helper expects two arguments: a List<DataClassification> and a string lookup key.");
444444
}
445445

446446
try
447447
{
448-
var extensionList = JsonSerializer.Deserialize<List<Extension>>(parameters[0].ToString() ?? string.Empty);
449-
var key = (string)parameters[1];
450-
var result = extensionList?.Find(i => i.Key.Equals(key, StringComparison.OrdinalIgnoreCase))?.Value ?? "";
448+
if (parameters[0] == null || parameters[1] == null || string.IsNullOrEmpty(parameters[1].ToString()) || parameters[0].ToString() == "classifications" || parameters[0]?.ToString()?.Length == 0)
449+
{
450+
// Skip, it's really null.
451+
//Console.WriteLine("Something is NULL");
452+
}
453+
else
454+
{
455+
var classificationsParameter = parameters[0];
456+
var classificationName = (string)parameters[1];
457+
458+
//Console.WriteLine(classificationsParameter);
459+
460+
var classifications = JsonSerializer.Deserialize<List<DataClassification>>(classificationsParameter.ToString() ?? string.Empty);
451461

462+
var result = classifications?.Find(i => i.Classification?.ToString().Equals(classificationName, StringComparison.OrdinalIgnoreCase) == true)?.Classification;
452463

453-
writer.WriteSafeString($"{result}");
464+
if (!string.IsNullOrEmpty(result))
465+
{
466+
// Regular block, a classification has been found
467+
options.Template(output, context);
468+
}
469+
else
470+
{
471+
// Else block, no classification with the input name has been found.
472+
options.Inverse(output, context);
473+
}
474+
}
454475
}
455-
catch (Exception exception)
476+
catch (Exception ex)
477+
{
478+
throw new HandlebarsException($"The {{{{hasClassification}}}} function encountered an error. The list of classifications provided as the first argument could not be deserialized. The reported error is: '{ex.Message}'.");
479+
}
480+
});
481+
482+
// Block helper that evalues is a certain string exists in a list of string values.
483+
Handlebars.RegisterHelper("hasStringValue", (output, options, context, parameters) =>
484+
{
485+
// Check if the parameters are valid.
486+
if (parameters.Length != 2)
487+
{
488+
throw new HandlebarsException("An issue was encountered. The {{hasStringValue}} helper expects two arguments: a List<String> and a string lookup key.");
489+
}
490+
491+
try
492+
{
493+
if (parameters[0] == null || parameters[1] == null || string.IsNullOrEmpty(parameters[1].ToString()) || parameters[0]?.ToString()?.Length == 0)
494+
{
495+
// Skip, it's really null.
496+
}
497+
else
498+
{
499+
var stringListParameter = parameters[0];
500+
var lookupValue = parameters[1].ToString();
501+
502+
// Deserialize the JSON array
503+
JsonDocument doc = JsonDocument.Parse(stringListParameter.ToString());
504+
505+
JsonElement root = doc.RootElement;
506+
507+
bool valueExists = false;
508+
509+
// Iterate over the array elements
510+
foreach (JsonElement element in root.EnumerateArray())
511+
{
512+
// Check if the current element matches the value we're looking for
513+
if (element.ValueKind == JsonValueKind.String && element.GetString() == lookupValue)
514+
{
515+
// Value found
516+
valueExists = true;
517+
break;
518+
}
519+
}
520+
521+
if (valueExists)
522+
{
523+
// Regular block, a value has been found
524+
options.Template(output, context);
525+
}
526+
else
527+
{
528+
// Else block, no matching value has been found.
529+
options.Inverse(output, context);
530+
}
531+
}
532+
}
533+
catch (Exception ex)
456534
{
457-
throw new HandlebarsException($"{{lookupExtension}} encountered an error: the list of extensions provided as the first argument could not be deserialized. The reported error is :{exception.Message}");
535+
throw new HandlebarsException($"The {{{{hasStringValue}}}} function encountered an error. The list of strings provided as the first argument could not be deserialized. The reported error is: '{ex.Message}'.");
458536
}
459537
});
460538
}

DataWarehouseAutomation/Example_Project/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ private static void DisplayPatternResult(string patternFile, string jsonMetadata
5555
catch (Exception ex)
5656
{
5757
Console.WriteLine($"An issue was encountered: {ex}");
58+
Console.ReadKey();
5859
}
5960
}
6061
}

0 commit comments

Comments
 (0)