1
- using DataWarehouseAutomation . DwaModel ;
2
- using HandlebarsDotNet ;
1
+ namespace DataWarehouseAutomation . Utils ;
3
2
4
- namespace DataWarehouseAutomation . Utils ;
5
-
6
- public static class HandleBarsHelpers
3
+ public static class HandlebarsHelpers
7
4
{
8
5
/// <summary>
9
6
/// Generate a random integer value, capped by an input (maximum) value.
@@ -45,67 +42,67 @@ public static DateTime GetRandomDate(int startYear = 1995)
45
42
/// Convenience method to install all Handlebars extension in one go.
46
43
/// </summary>
47
44
/// <exception cref="HandlebarsException"></exception>
48
- public static void RegisterHandleBarsHelpers ( )
45
+ public static void RegisterHandlebarsHelpers ( )
49
46
{
50
47
// Extension to the Handlebars templating language. Shows the current date and time in the generated output.
51
48
// Usage: {{now}}
52
49
// 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 ) ) ;
54
51
55
52
// Generation random date, based on an integer input year value.
56
- Handlebars . RegisterHelper ( "randomDate" , ( output , context , arguments ) =>
53
+ Handlebars . RegisterHelper ( "randomDate" , ( output , _ , arguments ) =>
57
54
{
58
55
if ( arguments . Length > 1 )
59
56
{
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." ) ;
61
58
}
62
59
63
60
if ( arguments . Length == 1 )
64
61
{
65
62
bool evaluationResult = int . TryParse ( arguments [ 0 ] . ToString ( ) , out var localInteger ) ;
66
63
if ( evaluationResult == false )
67
64
{
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.") ;
69
66
}
70
67
71
68
output . WriteSafeString ( GetRandomDate ( localInteger ) . Date ) ;
72
69
}
73
70
} ) ;
74
71
75
72
// Generation random string, based on an integer input value cap.
76
- Handlebars . RegisterHelper ( "randomNumber" , ( output , context , arguments ) =>
73
+ Handlebars . RegisterHelper ( "randomNumber" , ( output , _ , arguments ) =>
77
74
{
78
75
if ( arguments . Length > 1 )
79
76
{
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." ) ;
81
78
}
82
79
83
80
if ( arguments . Length == 1 )
84
81
{
85
82
bool evaluationResult = int . TryParse ( arguments [ 0 ] . ToString ( ) , out var localInteger ) ;
86
- if ( evaluationResult == false )
83
+ if ( ! evaluationResult )
87
84
{
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.") ;
89
86
}
90
87
output . WriteSafeString ( GetRandomNumber ( localInteger ) ) ;
91
88
}
92
89
} ) ;
93
90
94
91
// Generation random string, based on an integer input value
95
- Handlebars . RegisterHelper ( "randomString" , ( output , context , arguments ) =>
92
+ Handlebars . RegisterHelper ( "randomString" , ( output , _ , arguments ) =>
96
93
{
97
94
if ( arguments . Length > 1 )
98
95
{
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." ) ;
100
97
}
101
98
102
99
if ( arguments . Length == 1 )
103
100
{
104
101
bool evaluationResult = int . TryParse ( arguments [ 0 ] . ToString ( ) , out var localInteger ) ;
105
102
106
- if ( evaluationResult == false )
103
+ if ( ! evaluationResult )
107
104
{
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.") ;
109
106
}
110
107
111
108
var array = new [ ]
@@ -121,9 +118,9 @@ public static void RegisterHandleBarsHelpers()
121
118
}
122
119
} ) ;
123
120
124
- Handlebars . RegisterHelper ( "stringWrap" , ( writer , context , args ) =>
121
+ Handlebars . RegisterHelper ( "stringWrap" , ( writer , _ , args ) =>
125
122
{
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." ) ;
127
124
128
125
if ( args [ 0 ] . GetType ( ) . Name != "UndefinedBindingResult" )
129
126
{
@@ -138,15 +135,15 @@ public static void RegisterHandleBarsHelpers()
138
135
}
139
136
} ) ;
140
137
141
- Handlebars . RegisterHelper ( "stringUpper" , ( writer , context , args ) =>
138
+ Handlebars . RegisterHelper ( "stringUpper" , ( writer , _ , args ) =>
142
139
{
143
140
if ( args . Length != 1 ) throw new HandlebarsException ( "The {{stringUpper}} function requires one, and only one, input string value." ) ;
144
141
145
- if ( args [ 0 ] . GetType ( ) . Name != "UndefinedBindingResult" )
142
+ if ( args [ 0 ] . GetType ( ) . Name != "UndefinedBindingResult" && args [ 0 ] is string theString )
146
143
{
147
144
try
148
145
{
149
- writer . Write ( args [ 0 ] . ToString ( ) . ToUpper ( ) ) ;
146
+ writer . Write ( theString . ToUpper ( ) ) ;
150
147
}
151
148
catch ( Exception ex )
152
149
{
@@ -155,15 +152,15 @@ public static void RegisterHandleBarsHelpers()
155
152
}
156
153
} ) ;
157
154
158
- Handlebars . RegisterHelper ( "stringLower" , ( writer , context , args ) =>
155
+ Handlebars . RegisterHelper ( "stringLower" , ( writer , _ , args ) =>
159
156
{
160
157
if ( args . Length != 1 ) throw new HandlebarsException ( "The {{stringLower}} function requires one, and only one, input string value." ) ;
161
158
162
- if ( args [ 0 ] . GetType ( ) . Name != "UndefinedBindingResult" )
159
+ if ( args [ 0 ] . GetType ( ) . Name != "UndefinedBindingResult" && args [ 0 ] is string theString )
163
160
{
164
161
try
165
162
{
166
- writer . Write ( args [ 0 ] . ToString ( ) . ToLower ( ) ) ;
163
+ writer . Write ( theString . ToLower ( ) ) ;
167
164
}
168
165
catch ( Exception ex )
169
166
{
@@ -173,8 +170,8 @@ public static void RegisterHandleBarsHelpers()
173
170
} ) ;
174
171
175
172
// 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 }}
178
175
Handlebars . RegisterHelper ( "stringCompare" , ( output , options , context , arguments ) =>
179
176
{
180
177
if ( arguments . Length != 2 ) throw new HandlebarsException ( "The {{stringCompare}} function requires exactly two arguments." ) ;
@@ -193,8 +190,8 @@ public static void RegisterHandleBarsHelpers()
193
190
} ) ;
194
191
195
192
// 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 }}
198
195
Handlebars . RegisterHelper ( "stringDiff" , ( output , options , context , arguments ) =>
199
196
{
200
197
if ( arguments . Length != 2 ) throw new HandlebarsException ( "The {{stringDiff}} functions requires exactly two arguments." ) ;
@@ -223,7 +220,7 @@ public static void RegisterHandleBarsHelpers()
223
220
{
224
221
bool evaluationResult = int . TryParse ( arguments [ 0 ] . ToString ( ) , out var localInteger ) ;
225
222
226
- if ( evaluationResult == false )
223
+ if ( ! evaluationResult )
227
224
{
228
225
throw new HandlebarsException ( $ "The {{replicate}} functions failed because { arguments [ 0 ] } could not be converted to an integer.") ;
229
226
}
@@ -253,7 +250,7 @@ public static void RegisterHandleBarsHelpers()
253
250
254
251
} ) ;
255
252
256
- Handlebars . RegisterHelper ( "stringReplace" , ( writer , context , args ) =>
253
+ Handlebars . RegisterHelper ( "stringReplace" , ( writer , _ , args ) =>
257
254
{
258
255
if ( args . Length < 3 ) throw new HandlebarsException ( "The {{StringReplace}} function requires at least three arguments." ) ;
259
256
@@ -267,12 +264,11 @@ public static void RegisterHandleBarsHelpers()
267
264
{
268
265
expression = value . GetString ( ) ;
269
266
}
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
+ }
276
272
}
277
273
catch ( Exception exception )
278
274
{
@@ -289,9 +285,9 @@ public static void RegisterHandleBarsHelpers()
289
285
try
290
286
{
291
287
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 ( ) ) ;
293
289
294
- var dataItemExists = dataObjectMapping . DataItemMappings . Where ( x => x . TargetDataItem . Name == searchString ) . FirstOrDefault ( ) ;
290
+ var dataItemExists = dataObjectMapping ? . DataItemMappings ? . FirstOrDefault ( x => x . TargetDataItem . Name == searchString ) ;
295
291
296
292
if ( dataItemExists != null )
297
293
{
@@ -306,7 +302,7 @@ public static void RegisterHandleBarsHelpers()
306
302
}
307
303
catch ( Exception exception )
308
304
{
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 ) ;
310
306
}
311
307
} ) ;
312
308
@@ -329,7 +325,6 @@ public static void RegisterHandleBarsHelpers()
329
325
330
326
try
331
327
{
332
-
333
328
var dataObjectMapping = JsonSerializer . Deserialize < DataObjectMapping > ( context . Value . ToString ( ) , DefaultJsonOptions . DeserializerOptions ) ;
334
329
335
330
var outcome = false ;
@@ -348,9 +343,9 @@ public static void RegisterHandleBarsHelpers()
348
343
349
344
if ( targetDataItemsWithClassifications != null )
350
345
{
351
- var dataItemClassifications = targetDataItemsWithClassifications . SelectMany ( x => x . TargetDataItem . Classifications ) . ToList ( ) ;
346
+ var dataItemClassifications = targetDataItemsWithClassifications . SelectMany ( x => x . TargetDataItem . Classifications ?? [ ] ) . ToList ( ) ;
352
347
353
- if ( dataItemClassifications != null && dataItemClassifications . Any ( ) )
348
+ if ( dataItemClassifications ? . Any ( ) == true )
354
349
{
355
350
foreach ( var classification in dataItemClassifications )
356
351
{
@@ -393,7 +388,7 @@ public static void RegisterHandleBarsHelpers()
393
388
}
394
389
} ) ;
395
390
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.
397
392
Handlebars . RegisterHelper ( "hasClassification" , ( output , options , context , parameters ) =>
398
393
{
399
394
// Check if the parameters are valid.
@@ -414,9 +409,9 @@ public static void RegisterHandleBarsHelpers()
414
409
{
415
410
var classifications = JsonSerializer . Deserialize < List < DataClassification > > ( classificationsParameter . ToString ( ) ?? string . Empty ) ;
416
411
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 ;
418
413
419
- if ( result != null && result != "" )
414
+ if ( ! string . IsNullOrEmpty ( result ) )
420
415
{
421
416
// Regular block, a classification has been found
422
417
options . Template ( output , context ) ;
@@ -434,7 +429,7 @@ public static void RegisterHandleBarsHelpers()
434
429
}
435
430
} ) ;
436
431
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.
438
433
Handlebars . RegisterHelper ( "hasClassification" , ( output , options , context , parameters ) =>
439
434
{
440
435
// Check if the parameters are valid.
@@ -479,7 +474,7 @@ public static void RegisterHandleBarsHelpers()
479
474
}
480
475
} ) ;
481
476
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.
483
478
Handlebars . RegisterHelper ( "hasStringValue" , ( output , options , context , parameters ) =>
484
479
{
485
480
// Check if the parameters are valid.
0 commit comments