-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPropertyHelper.cs
More file actions
829 lines (706 loc) · 34.2 KB
/
PropertyHelper.cs
File metadata and controls
829 lines (706 loc) · 34.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
// -------------------------------------------------------------------------------------------------
// <copyright file="PropertyHelper.cs" company="Starion Group S.A.">
//
// Copyright 2022-2025 Starion Group S.A.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// </copyright>
// ------------------------------------------------------------------------------------------------
namespace SysML2.NET.CodeGenerator.HandleBarHelpers
{
using System;
using System.Globalization;
using System.Linq;
using System.Text;
using HandlebarsDotNet;
using SysML2.NET.CodeGenerator.Extensions;
using uml4net.SimpleClassifiers;
using uml4net.Extensions;
using uml4net.Classification;
using uml4net.CommonStructure;
using uml4net.StructuredClassifiers;
/// <summary>
/// A handlebars block helper for the <see cref="IProperty"/> interface
/// </summary>
public static class PropertyHelper
{
/// <summary>
/// Registers the <see cref="PropertyHelper"/>
/// </summary>
/// <param name="handlebars">
/// The <see cref="IHandlebars"/> context with which the helper needs to be registered
/// </param>
public static void RegisterPropertyHelper(this IHandlebars handlebars)
{
handlebars.RegisterHelper("Property.WriteForDTOInterface", (writer, context, _) =>
{
if (context.Value is not IProperty property)
{
throw new ArgumentException("supposed to be IProperty");
}
var sb = new StringBuilder();
if (property.RedefinedProperty.Any(x => x.Name == property.Name))
{
sb.Append("new ");
}
if (property.Type is IDataType)
{
if (property.QueryIsEnumerable())
{
sb.Append($"List<{property.QueryCSharpTypeName()}>");
sb.Append(' ');
}
else
{
sb.Append($"{property.QueryCSharpTypeName()}");
if (uml4net.Extensions.PropertyExtensions.QueryIsNullableAndNotString(property))
{
sb.Append('?');
}
sb.Append(' ');
}
}
else
{
if (property.QueryIsEnumerable())
{
sb.Append("List<Guid>");
sb.Append(' ');
}
else
{
sb.Append("Guid");
if (uml4net.Extensions.PropertyExtensions.QueryIsNullableAndNotString(property))
{
sb.Append('?');
}
sb.Append(' ');
}
}
var propertyName = StringExtensions.CapitalizeFirstLetter(property.Name);
if (property.IsDerived || property.IsDerivedUnion)
{
propertyName = StringExtensions.LowerCaseFirstLetter(propertyName);
}
sb.Append(propertyName);
sb.Append(' ');
if (property.IsReadOnly || property.IsDerived || property.IsDerivedUnion)
{
sb.Append("{ get; }");
}
else
{
sb.Append("{ get; set; }");
}
writer.WriteSafeString(sb + Environment.NewLine);
});
handlebars.RegisterHelper("Property.WriteForDTOClass", (writer, _, parameters) =>
{
if (parameters.Length != 2)
{
throw new HandlebarsException("{{#Property.WriteForDTOClass}} helper must have exactly two arguments");
}
var property = (parameters[0] as IProperty)!;
var classContext = (parameters[1] as IClass)!;
var sb = new StringBuilder();
var propertyName = StringExtensions.CapitalizeFirstLetter(property.Name);
var isRedefinedPropertyInContext = property.TryQueryRedefinedByProperty(classContext, out var redefiningProperty);
if (!isRedefinedPropertyInContext)
{
sb.Append(property.Visibility.ToString().ToLower(CultureInfo.InvariantCulture));
sb.Append(' ');
}
if (property.Type is IDataType)
{
if (property.QueryIsEnumerable())
{
sb.Append($"List<{property.QueryCSharpTypeName()}>");
sb.Append(' ');
}
else
{
sb.Append($"{property.QueryCSharpTypeName()}");
if (uml4net.Extensions.PropertyExtensions.QueryIsNullableAndNotString(property))
{
sb.Append('?');
}
sb.Append(' ');
}
}
else
{
if (property.QueryIsEnumerable())
{
sb.Append("List<Guid>");
sb.Append(' ');
}
else
{
sb.Append("Guid");
if (uml4net.Extensions.PropertyExtensions.QueryIsNullableAndNotString(property))
{
sb.Append('?');
}
sb.Append(' ');
}
}
if (property.IsDerived || property.IsDerivedUnion)
{
propertyName = StringExtensions.LowerCaseFirstLetter(propertyName);
}
if (isRedefinedPropertyInContext)
{
var owner = (INamedElement)property.Owner;
propertyName = $"I{owner.Name}.{propertyName}";
var ownerNamespace = Extensions.NamedElementExtensions.QueryNamespace(owner);
if (ownerNamespace != Extensions.NamedElementExtensions.QueryNamespace(classContext))
{
propertyName = $"{ownerNamespace}.{propertyName}";
}
}
sb.Append(propertyName);
sb.Append(' ');
if (property.IsReadOnly || property.IsDerived || property.IsDerivedUnion)
{
if (isRedefinedPropertyInContext)
{
sb.Append($"=> {GetRedefinedPropertyGetterImplementationForDto(property, redefiningProperty, classContext)}");
}
else
{
sb.Append("{ get; internal set; }");
if (property.QueryIsEnumerable())
{
sb.Append(" = [];");
}
}
}
else
{
if (isRedefinedPropertyInContext)
{
sb.AppendLine("{");
sb.AppendLine($"\tget => {GetRedefinedPropertyGetterImplementationForDto(property, redefiningProperty, classContext)}");
var setterImplementation = GetRedefinedPropertySetterImplementationForDto(property, redefiningProperty, classContext);
if (string.IsNullOrWhiteSpace(setterImplementation))
{
sb.AppendLine("\tset { }");
}
else
{
sb.AppendLine("\tset ");
sb.AppendLine("{");
sb.AppendLine($"\t{setterImplementation}");
sb.Append('}');
}
sb.Append('}');
}
else
{
sb.Append("{ get; set; }");
if (property.QueryIsEnumerable())
{
sb.Append(" = [];");
}
}
}
if (!isRedefinedPropertyInContext)
{
if (property.QueryIsDefaultValueDifferentThanDefault())
{
if (property.QueryIsString())
{
sb.Append($" = \"{property.QueryDefaultValueAsString()}\";");
}
else
{
sb.Append($" = {property.QueryDefaultValueAsString()};");
}
}
else if (property.QueryIsEnumPropertyWithDefaultValue())
{
sb.Append($" = {StringExtensions.CapitalizeFirstLetter(property.Type.Name)}.{StringExtensions.CapitalizeFirstLetter(property.QueryDefaultValueAsString())};");
}
}
writer.WriteSafeString(sb + Environment.NewLine);
});
handlebars.RegisterHelper("Property.WriteForPOCOInterface", (writer, context, _) =>
{
if (context.Value is not IProperty property)
{
throw new ArgumentException("The #Property.WriteForPOCOInterface supposed to be IProperty");
}
var sb = new StringBuilder();
var typeName = property.QueryCSharpTypeName();
if (property.RedefinedProperty.Any(x => x.Name == property.Name))
{
sb.Append("new ");
}
if (property.Type is not IDataType)
{
typeName = $"I{typeName}";
}
if (property.QueryIsEnumerable())
{
sb.Append($"List<{typeName}> ");
}
else
{
if (uml4net.Extensions.PropertyExtensions.QueryIsNullableAndNotString(property) && !property.QueryIsReferenceProperty())
{
sb.Append($"{typeName}? ");
}
else
{
sb.Append($"{typeName} ");
}
}
var propertyName = StringExtensions.CapitalizeFirstLetter(property.Name);
if (property.IsDerived || property.IsDerivedUnion)
{
propertyName = StringExtensions.LowerCaseFirstLetter(propertyName);
}
sb.Append(propertyName);
if (property.IsReadOnly || property.IsDerived || property.IsDerivedUnion)
{
sb.Append(" { get; }");
}
else
{
sb.Append(" { get; set; }");
}
writer.WriteSafeString(sb + Environment.NewLine);
});
handlebars.RegisterHelper("Property.WriteForPOCOClass", (writer, context, arguments) =>
{
if (context.Value is not IProperty property)
{
throw new ArgumentException("The #Property.WriteForPOCOClass context supposed to be IProperty");
}
if (arguments.Length != 2)
{
throw new ArgumentException("The #Property.WriteForPOCOClass supposed to be have 2 arguments IProperty");
}
if (arguments[1] is not IClass classContext)
{
throw new ArgumentException("The #Property.WriteForPOCOClass supposed to be have an IClass as second argument");
}
var sb = new StringBuilder();
var isRedefinedPropertyInContext = property.TryQueryRedefinedByProperty(classContext, out var redefiningProperty);
var typeName = property.QueryCSharpTypeName();
var propertyName = StringExtensions.CapitalizeFirstLetter(property.Name);
if (!isRedefinedPropertyInContext)
{
sb.Append(property.Visibility.ToString().ToLower(CultureInfo.InvariantCulture));
sb.Append(' ');
}
if (property.Type is not IDataType)
{
typeName = $"I{typeName}";
}
if (property.QueryIsEnumerable())
{
sb.Append($"List<{typeName}> ");
}
else
{
if (uml4net.Extensions.PropertyExtensions.QueryIsNullableAndNotString(property) && !property.QueryIsReferenceProperty())
{
sb.Append($"{typeName}? ");
}
else
{
sb.Append($"{typeName} ");
}
}
if (property.IsDerived || property.IsDerivedUnion)
{
propertyName = StringExtensions.LowerCaseFirstLetter(propertyName);
}
if (isRedefinedPropertyInContext)
{
var owner = (INamedElement)property.Owner;
propertyName = $"I{owner.Name}.{propertyName}";
var ownerNamespace = Extensions.NamedElementExtensions.QueryNamespace(owner);
if (ownerNamespace != Extensions.NamedElementExtensions.QueryNamespace(classContext))
{
propertyName = $"{ownerNamespace}.{propertyName}";
}
}
sb.Append(propertyName);
if (property.IsDerived || property.IsDerivedUnion)
{
if (isRedefinedPropertyInContext)
{
sb.Append($"=> {GetRedefinedPropertyGetterImplementationForPoco(property, redefiningProperty, classContext)}");
}
else
{
sb.Append($"=> this.Compute{StringExtensions.CapitalizeFirstLetter(property.Name)}();");
}
}
else if (property.IsReadOnly)
{
if (isRedefinedPropertyInContext)
{
sb.Append($"=> {GetRedefinedPropertyGetterImplementationForPoco(property, redefiningProperty, classContext)}");
}
else
{
sb.Append(" { get; }");
}
}
else
{
if (isRedefinedPropertyInContext)
{
sb.AppendLine("{");
sb.AppendLine($"\tget => {GetRedefinedPropertyGetterImplementationForPoco(property, redefiningProperty, classContext)}");
var setterImplementation = GetRedefinedPropertySetterImplementationForPoco(property, redefiningProperty, classContext);
if (string.IsNullOrWhiteSpace(setterImplementation))
{
sb.AppendLine("\tset { }");
}
else
{
sb.AppendLine("\tset ");
sb.AppendLine("{");
sb.AppendLine($"\t{setterImplementation}");
sb.Append('}');
}
sb.Append('}');
}
else
{
sb.Append(" { get; set; }");
if (property.QueryIsEnumerable())
{
sb.Append(" = [];");
}
}
}
if ((!property.IsDerived || property.IsDerivedUnion) && !isRedefinedPropertyInContext)
{
if (property.QueryIsDefaultValueDifferentThanDefault())
{
if (property.QueryIsString())
{
sb.Append($" = \"{property.QueryDefaultValueAsString()}\";");
}
else
{
sb.Append($" = {property.QueryDefaultValueAsString()};");
}
}
else if (property.QueryIsEnumPropertyWithDefaultValue())
{
sb.Append($" = {StringExtensions.CapitalizeFirstLetter(property.Type.Name)}.{StringExtensions.CapitalizeFirstLetter(property.QueryDefaultValueAsString())};");
}
}
writer.WriteSafeString(sb + Environment.NewLine);
});
handlebars.RegisterHelper("Property.WriteTypeForExtendClass", (writer, context, _) =>
{
if (context.Value is not IProperty property)
{
throw new ArgumentException("The #Property.WriteTypeForExtendClass context supposed to be IProperty");
}
if (!property.IsDerived && !property.IsDerivedUnion)
{
throw new ArgumentException("The #Property.WriteTypeForExtendClass shall only be called on IsDerived or IsDerivedUnion properties");
}
var sb = new StringBuilder();
var typeName = property.QueryCSharpTypeName();
if (property.Type is not IDataType)
{
typeName = $"I{typeName}";
}
if (property.QueryIsEnumerable())
{
sb.Append($"List<{typeName}> ");
}
else
{
sb.Append($"{typeName} ");
}
writer.WriteSafeString(sb);
});
handlebars.RegisterHelper("Property.IsPropertyRedefinedInClass", (context, arguments) =>
{
if (context.Value is not IProperty property)
{
throw new ArgumentException("The #Property.IsPropertyRedefinedInClass context supposed to be IProperty");
}
if (arguments.Length != 2)
{
throw new ArgumentException("The #Property.IsPropertyRedefinedInClass supposed to be have 2 arguments IProperty");
}
if (arguments[1] is not IClass generatedClass)
{
throw new ArgumentException("The #Property.IsPropertyRedefinedInClass supposed to be have an IClass as second argument");
}
return property.TryQueryRedefinedByProperty(generatedClass, out _);
});
handlebars.RegisterHelper("Property.IsRedefinedOrRedifines", (context, arguments) =>
{
if (context.Value is not IProperty property)
{
throw new ArgumentException("The #Property.IsRedefinedOrRedifines context supposed to be IProperty");
}
if (arguments.Length != 2)
{
throw new ArgumentException("The #Property.IsRedefinedOrRedifines supposed to be have 2 arguments IProperty");
}
if (arguments[1] is not IClass generatedClass)
{
throw new ArgumentException("The #Property.IsRedefinedOrRedifines supposed to be have an IClass as second argument");
}
var allProperties = generatedClass.QueryAllProperties();
var propertiesWithSameName = allProperties.SkipWhile(x => x == property).Where(x => x.Name == property.Name);
return property.RedefinedProperty.Any(x => propertiesWithSameName.Contains(x)) || propertiesWithSameName.Any(x => x.RedefinedProperty.Contains(property));
});
handlebars.RegisterHelper("Property.IsRedefinedByPropertyWithSameName", (context, arguments) =>
{
if (context.Value is not IProperty property)
{
throw new ArgumentException("The #Property.IsRedefinedByPropertyWithSameName context supposed to be IProperty");
}
if (arguments.Length != 2)
{
throw new ArgumentException("The #Property.IsRedefinedByPropertyWithSameName supposed to be have 2 arguments");
}
if (arguments[1] is not IClass generatedClass)
{
throw new ArgumentException("The #Property.IsRedefinedByPropertyWithSameName supposed to be have an IClass as second argument");
}
if (!property.TryQueryRedefinedByProperty(generatedClass, out var redefinition))
{
return false;
}
return redefinition.Name == property.Name;
});
handlebars.RegisterHelper("Property.WritePropertyName", (writer, _, arguments) =>
{
if (arguments.Single() is not IProperty property)
{
throw new ArgumentException("The #Property.WritePropertyName context supposed to be IProperty");
}
writer.WriteSafeString(property.QueryPropertyNameBasedOnUmlProperties());
});
handlebars.RegisterHelper("Property.WritePropertyWithExplicitInterfaceIfRequiredForDTO", (writer, context, arguments) =>
{
if (context.Value is not IProperty property)
{
throw new ArgumentException("The #Property.WritePropertyWithExplicitInterfaceIfRequiredForDTO context supposed to be IProperty");
}
if (arguments.Length != 3)
{
throw new ArgumentException("The #Property.WritePropertyWithExplicitInterfaceIfRequiredForDTO supposed to be have 3 arguments");
}
if (arguments[1] is not IClass generatedClass)
{
throw new ArgumentException("The #Property.WritePropertyWithExplicitInterfaceIfRequiredForDTO supposed to be have an IClass as second argument");
}
var variableName = arguments[2].ToString();
var propertyName = property.IsDerived || property.IsDerivedUnion ? StringExtensions.LowerCaseFirstLetter(property.Name) : StringExtensions.CapitalizeFirstLetter(property.Name);
if (property.TryQueryRedefinedByProperty(generatedClass, out var redefinitionProperty) && redefinitionProperty.Name == property.Name)
{
var owner = (INamedElement)property.Owner;
writer.WriteSafeString($"((SysML2.NET.Core.DTO.{Extensions.NamedElementExtensions.QueryNamespace(owner)}.I{owner.Name}){variableName}).{propertyName}");
}
else
{
writer.WriteSafeString($"{variableName}.{propertyName}");
}
});
handlebars.RegisterHelper("Property.IsOfTypeBaseElement", (_, arguments) =>
{
if (arguments.Single() is not IProperty property)
{
throw new ArgumentException("The #Property.IsOfTypeBaseElement argument supposed to be IProperty");
}
return property.Type.Name == "Element";
});
}
/// <summary>
/// Gets the getter implementation for an <see cref="IProperty"/> that has been redefined, for DTO generation
/// </summary>
/// <param name="redefinedProperty">The redefined property</param>
/// <param name="redefinition">The property that redefines <paramref name="redefinedProperty"/></param>
/// <param name="context">Gets the <see cref="IClass"/> context</param>
/// <returns>The getter implementation</returns>
private static string GetRedefinedPropertyGetterImplementationForDto(IProperty redefinedProperty, IProperty redefinition, IClass context)
{
string redefinitionPropertyName;
if (redefinition.TryQueryRedefinedByProperty(context, out _))
{
var owner = (INamedElement)redefinition.Owner;
redefinitionPropertyName = $"((SysML2.NET.Core.DTO.{Extensions.NamedElementExtensions.QueryNamespace(owner)}.I{owner.Name})this).{redefinition.QueryPropertyNameBasedOnUmlProperties()}";
}
else
{
redefinitionPropertyName = $"this.{redefinition.QueryPropertyNameBasedOnUmlProperties()}";
}
if (redefinedProperty.QueryIsEnumerable() && redefinition.QueryIsEnumerable())
{
return $"[..{redefinitionPropertyName}];";
}
if (redefinedProperty.QueryIsEnumerable() && !redefinition.QueryIsEnumerable())
{
return uml4net.Extensions.PropertyExtensions.QueryIsNullableAndNotString(redefinition)
? $"{redefinitionPropertyName}.HasValue ? [{redefinitionPropertyName}.Value] : [];"
: $"[{redefinitionPropertyName}];";
}
return uml4net.Extensions.PropertyExtensions.QueryIsNullableAndNotString(redefinition)
? $"{redefinitionPropertyName}.HasValue ? {redefinitionPropertyName}.Value : {(redefinedProperty.QueryIsReferenceProperty() ? "Guid.Empty" : "default")};"
: $"{redefinitionPropertyName};";
}
/// <summary>
/// Gets the getter implementation for an <see cref="IProperty"/> that has been redefined, for POCO generation
/// </summary>
/// <param name="redefinedProperty">The redefined property</param>
/// <param name="redefinition">The property that redefines <paramref name="redefinedProperty"/></param>
/// <param name="context">Gets the <see cref="IClass"/> context</param>
/// <returns>The getter implementation</returns>
private static string GetRedefinedPropertyGetterImplementationForPoco(IProperty redefinedProperty, IProperty redefinition, IClass context)
{
string redefinitionPropertyName;
if (redefinition.TryQueryRedefinedByProperty(context, out _))
{
var owner = (INamedElement)redefinition.Owner;
redefinitionPropertyName = $"((SysML2.NET.Core.POCO.{Extensions.NamedElementExtensions.QueryNamespace(owner)}.I{owner.Name})this).{redefinition.QueryPropertyNameBasedOnUmlProperties()}";
}
else
{
redefinitionPropertyName = $"this.{redefinition.QueryPropertyNameBasedOnUmlProperties()}";
}
if (redefinedProperty.QueryIsEnumerable() && redefinition.QueryIsEnumerable())
{
return $"[..{redefinitionPropertyName}];";
}
if (redefinedProperty.QueryIsEnumerable() && !redefinition.QueryIsEnumerable())
{
if (redefinedProperty.QueryIsReferenceProperty())
{
return $"{redefinitionPropertyName} != null ? [{redefinitionPropertyName}] : [];";
}
if (redefinedProperty.QueryIsString())
{
return $"string.IsNullOrWhiteSpace({redefinitionPropertyName}) ? [{redefinitionPropertyName}] : [];";
}
return uml4net.Extensions.PropertyExtensions.QueryIsNullableAndNotString(redefinition)
? $"{redefinitionPropertyName}.HasValue ? [{redefinitionPropertyName}.Value] : [];"
: $"[{redefinitionPropertyName}];";
}
return uml4net.Extensions.PropertyExtensions.QueryIsNullableAndNotString(redefinition) && !redefinedProperty.QueryIsReferenceProperty()
? $"{redefinitionPropertyName}.HasValue ? {redefinitionPropertyName}.Value : default;"
: $"{redefinitionPropertyName};";
}
/// <summary>
/// Gets the setter implementation for an <see cref="IProperty"/> that has been redefined, for DTO generation
/// </summary>
/// <param name="redefinedProperty">The redefined property</param>
/// <param name="redefinition">The property that redefines <paramref name="redefinedProperty"/></param>
/// <param name="context">Gets the <see cref="IClass"/> context</param>
/// <returns>The setter imlementation</returns>
private static string GetRedefinedPropertySetterImplementationForDto(IProperty redefinedProperty, IProperty redefinition, IClass context)
{
if (redefinition.IsDerived || redefinition.IsDerivedUnion || redefinition.IsReadOnly)
{
return string.Empty;
}
string redefinitionPropertyName;
if (redefinition.TryQueryRedefinedByProperty(context, out _))
{
var owner = (INamedElement)redefinition.Owner;
redefinitionPropertyName = $"((SysML2.NET.Core.DTO.{Extensions.NamedElementExtensions.QueryNamespace(owner)}.I{owner.Name})this).{redefinition.QueryPropertyNameBasedOnUmlProperties()}";
}
else
{
redefinitionPropertyName = $"this.{redefinition.QueryPropertyNameBasedOnUmlProperties()}";
}
if (redefinedProperty.QueryIsEnumerable() == redefinition.QueryIsEnumerable())
{
return $"{redefinitionPropertyName} = value;";
}
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine("if(value.Count != 0)");
stringBuilder.AppendLine("{");
stringBuilder.AppendLine($"\t{redefinitionPropertyName} = value[0];");
stringBuilder.AppendLine("}");
return stringBuilder.ToString();
}
/// <summary>
/// Gets the setter implementation for an <see cref="IProperty"/> that has been redefined, for POCO generation
/// </summary>
/// <param name="redefinedProperty">The redefined property</param>
/// <param name="redefinition">The property that redefines <paramref name="redefinedProperty"/></param>
/// <param name="context">Gets the <see cref="IClass"/> context</param>
/// <returns>The setter imlementation</returns>
private static string GetRedefinedPropertySetterImplementationForPoco(IProperty redefinedProperty, IProperty redefinition, IClass context)
{
if (redefinition.IsDerived || redefinition.IsDerivedUnion || redefinition.IsReadOnly)
{
return string.Empty;
}
string redefinitionPropertyName;
if (redefinition.TryQueryRedefinedByProperty(context, out _))
{
var owner = (INamedElement)redefinition.Owner;
redefinitionPropertyName = $"((SysML2.NET.Core.POCO.{Extensions.NamedElementExtensions.QueryNamespace(owner)}.I{owner.Name})this).{redefinition.QueryPropertyNameBasedOnUmlProperties()}";
}
else
{
redefinitionPropertyName = $"this.{redefinition.QueryPropertyNameBasedOnUmlProperties()}";
}
var redefinitionPropertyTypeName = redefinition.QueryIsReferenceProperty() ? $"I{redefinition.Type.Name}" : redefinition.QueryCSharpTypeName();
if (redefinedProperty.QueryIsEnumerable() && redefinition.QueryIsEnumerable())
{
return redefinedProperty.Type == redefinition.Type ? $"{redefinitionPropertyName} = value;" : $"{redefinitionPropertyName} = [..value.OfType<{redefinitionPropertyTypeName}>()];";
}
var stringBuilder = new StringBuilder();
if (redefinedProperty.QueryIsEnumerable())
{
if (redefinedProperty.Type == redefinition.Type)
{
stringBuilder.AppendLine("if(value.Count != 0)");
stringBuilder.AppendLine("{");
stringBuilder.AppendLine($"\t{redefinitionPropertyName} = value[0];");
}
else
{
stringBuilder.AppendLine($"if(value.OfType<{redefinitionPropertyTypeName}>().FirstOrDefault() is {{ }} firstValue)");
stringBuilder.AppendLine("{");
stringBuilder.AppendLine($"\t{redefinitionPropertyName} = firstValue;");
}
stringBuilder.Append('}');
}
else
{
if (redefinedProperty.Type == redefinition.Type)
{
stringBuilder.Append($"{redefinitionPropertyName} = value;");
}
else
{
stringBuilder.AppendLine($"if(value is {redefinitionPropertyTypeName} castedValue)");
stringBuilder.AppendLine("{");
stringBuilder.AppendLine($"\t{redefinitionPropertyName} = castedValue;");
stringBuilder.Append('}');
}
}
return stringBuilder.ToString();
}
}
}