Skip to content

Commit 478b90d

Browse files
committed
Added exists block helper in handlebars.
1 parent 778999d commit 478b90d

File tree

5 files changed

+88
-10
lines changed

5 files changed

+88
-10
lines changed

ClassLibrary/DataWarehouseAutomation/DataWarehouseAutomation/DataWarehouseAutomation.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>net7.0</TargetFramework>
5-
<Version>1.3.3</Version>
5+
<Version>1.3.4</Version>
66
<Authors>Roelant Vos</Authors>
77
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
88
<Title>Data Warehouse Automation</Title>

ClassLibrary/DataWarehouseAutomation/DataWarehouseAutomation/HandleBarsHelpers.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Security.Cryptography;
45
using System.Text;
@@ -248,6 +249,7 @@ public static void RegisterHandleBarsHelpers()
248249
}
249250
});
250251

252+
// Run at data object mapping level.
251253
Handlebars.RegisterHelper("targetDataItemExists", (output, options, context, arguments) =>
252254
{
253255
if (arguments.Length != 1) throw new HandlebarsException("The {{targetDataItemExists}} function requires only one argument.");
@@ -275,5 +277,73 @@ public static void RegisterHandleBarsHelpers()
275277
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);
276278
}
277279
});
280+
281+
// Run at data object mapping level.
282+
Handlebars.RegisterHelper("exists", (output, options, context, arguments) =>
283+
{
284+
if (arguments.Length != 1) throw new HandlebarsException("The {{exists}} function must have a single argument, which must be a property of a data object mapping.");
285+
286+
var property = arguments[0] == null ? "" : arguments[0].ToString();
287+
288+
// Supported:
289+
// - targetDataItemClassification
290+
// - targetDataItem
291+
292+
try
293+
{
294+
DataObjectMapping dataObjectMapping = JsonSerializer.Deserialize<DataObjectMapping>(context.Value.ToString());
295+
296+
var outcome = false;
297+
298+
if (property == "multiActiveKey")
299+
{
300+
var targetDataItemsWithClassifications = dataObjectMapping?.DataItemMappings?.Where(x => x.TargetDataItem.DataItemClassification != null).ToList();
301+
302+
if (targetDataItemsWithClassifications != null)
303+
{
304+
var dataItemClassifications = targetDataItemsWithClassifications.SelectMany(x => x.TargetDataItem.DataItemClassification).ToList();
305+
306+
if (dataItemClassifications != null && dataItemClassifications.Any())
307+
{
308+
foreach (var classification in dataItemClassifications)
309+
{
310+
if (classification.Classification == "MultiActiveKey")
311+
{
312+
outcome = true;
313+
}
314+
}
315+
}
316+
}
317+
}
318+
else if (property == "targetDataItem")
319+
{
320+
var targetDataItem = dataObjectMapping?.DataItemMappings?.Where(x => x.TargetDataItem != null).FirstOrDefault();
321+
322+
if (targetDataItem != null)
323+
{
324+
outcome = true;
325+
}
326+
}
327+
else
328+
{
329+
throw new HandlebarsException($"The {{{property}}} used is not supported by the exist function. Only multiActiveKey and targetDataItem are currently supported.");
330+
}
331+
332+
if (outcome)
333+
{
334+
// Regular block
335+
options.Template(output, context);
336+
}
337+
else
338+
{
339+
// Else block
340+
options.Inverse(output, context);
341+
}
342+
}
343+
catch (Exception exception)
344+
{
345+
throw new HandlebarsException($"The exists helper using the property {{{property}}} reported a conversion error, and was unable to deserialize the context into a DataObjectMapping. The reported error is " + exception.Message);
346+
}
347+
});
278348
}
279349
}

ClassLibrary/DataWarehouseAutomation/Example_Project/Program.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ static void Main(string[] args)
1515
var sampleTemplateDirectory = AppDomain.CurrentDomain.BaseDirectory+@"..\..\..\..\Sample_Templates\";
1616
var sampleMetadataDirectory = AppDomain.CurrentDomain.BaseDirectory + @"..\..\..\..\Sample_Metadata\";
1717

18-
DisplayPatternResult(sampleTemplateDirectory + @"TemplateSampleBasic.handlebars", sampleMetadataDirectory + @"sampleBasic.json");
19-
DisplayPatternResult(sampleTemplateDirectory + @"TemplateSampleBasicWithExtensions.handlebars", sampleMetadataDirectory + @"sampleBasicWithExtensions.json");
20-
DisplayPatternResult(sampleTemplateDirectory + @"TemplateSampleMultipleDataItemMappings.handlebars", sampleMetadataDirectory + @"sampleMultipleDataItemMappings.json");
21-
DisplayPatternResult(sampleTemplateDirectory + @"TemplateSampleSourceQuery.handlebars", sampleMetadataDirectory + @"sampleSourceQuery.json");
22-
DisplayPatternResult(sampleTemplateDirectory + @"TemplateSampleSimpleDDL.handlebars", sampleMetadataDirectory + @"sampleSimpleDDL.json");
23-
DisplayPatternResult(sampleTemplateDirectory + @"TemplateSampleCalculation.handlebars", sampleMetadataDirectory + @"sampleCalculation.json");
24-
DisplayPatternResult(sampleTemplateDirectory + @"TemplateSatelliteView.handlebars", sampleMetadataDirectory + @"sampleVDW_Sat_Customer_v161.json");
25-
DisplayPatternResult(sampleTemplateDirectory + @"TemplateSampleFreeForm.handlebars", sampleMetadataDirectory + @"sampleFreeForm.json");
18+
//DisplayPatternResult(sampleTemplateDirectory + @"TemplateSampleBasic.handlebars", sampleMetadataDirectory + @"sampleBasic.json");
19+
//DisplayPatternResult(sampleTemplateDirectory + @"TemplateSampleBasicWithExtensions.handlebars", sampleMetadataDirectory + @"sampleBasicWithExtensions.json");
20+
//DisplayPatternResult(sampleTemplateDirectory + @"TemplateSampleMultipleDataItemMappings.handlebars", sampleMetadataDirectory + @"sampleMultipleDataItemMappings.json");
21+
//DisplayPatternResult(sampleTemplateDirectory + @"TemplateSampleSourceQuery.handlebars", sampleMetadataDirectory + @"sampleSourceQuery.json");
22+
//DisplayPatternResult(sampleTemplateDirectory + @"TemplateSampleSimpleDDL.handlebars", sampleMetadataDirectory + @"sampleSimpleDDL.json");
23+
//DisplayPatternResult(sampleTemplateDirectory + @"TemplateSampleCalculation.handlebars", sampleMetadataDirectory + @"sampleCalculation.json");
24+
//DisplayPatternResult(sampleTemplateDirectory + @"TemplateSatelliteView.handlebars", sampleMetadataDirectory + @"sampleVDW_Sat_Customer_v161.json");
25+
//DisplayPatternResult(sampleTemplateDirectory + @"TemplateSampleFreeForm.handlebars", sampleMetadataDirectory + @"sampleFreeForm.json");
2626
DisplayPatternResult(sampleTemplateDirectory + @"TemplateSampleCustomFunctions.handlebars", sampleMetadataDirectory + @"sampleCustomFunctions.json");
2727

2828
Console.ReadKey();

ClassLibrary/DataWarehouseAutomation/Sample_Metadata/sampleCustomFunctions.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@
3838
}
3939
],
4040
"targetDataItem": {
41-
"name": "DATE_OF_BIRTH"
41+
"name": "DATE_OF_BIRTH",
42+
"dataItemClassification": [
43+
{
44+
"classification": "MultiActiveKey"
45+
}
46+
]
4247
}
4348
}
4449
],

ClassLibrary/DataWarehouseAutomation/Sample_Templates/TemplateSampleCustomFunctions.Handlebars

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ Working on mapping {{mappingName}}
2323
Found a data item mapping from {{sourceDataItems.0.name}} to {{targetDataItem.name}}
2424
{{/each}}
2525

26+
{{#exists multiActiveKey}}There is a multi-active key!{{else}}No multi-active key in this data object mapping.{{/exists}}
27+
{{#exists targetDataItem}}There is a target data item in this mapping!{{else}}No target data items are defined in this mapping.{{/exists}}
28+
2629
{{#targetDataItemExists "FIRST_NAME"}}FIRST_NAME exists{{else}}FIRST_NAME does not exist{{/targetDataItemExists}}
2730
{{#targetDataItemExists "NAME"}}NAME exists{{else}}NAME does not exist{{/targetDataItemExists}}
2831
{{#targetDataItemExists "DATE_OF_BIRTH"}}NAME exists{{else}}NAME does not exist{{/targetDataItemExists}}

0 commit comments

Comments
 (0)