Skip to content

Commit 1799651

Browse files
[Refactor] XmiReaders to use generated enum providers to parse enum values
1 parent f05a5b7 commit 1799651

200 files changed

Lines changed: 5292 additions & 1446 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

SysML2.NET.CodeGenerator.Tests/Expected/UML/Core/AutoGenEnumProvider/TransitionFeatureKindProvider.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,48 @@ public static TransitionFeatureKind Parse(ReadOnlySpan<char> value)
7373
throw new ArgumentException($"'{new string(value)}' is not a valid TransitionFeatureKind", nameof(value));
7474
}
7575

76+
/// <summary>
77+
/// Tries to parse the <see cref="ReadOnlySpan{Char}"/> to a <see cref="TransitionFeatureKind"/>
78+
/// </summary>
79+
/// <param name="value">
80+
/// The <see cref="ReadOnlySpan{Char}"/> that is to be parsed
81+
/// </param>
82+
/// <param name="result">
83+
/// When this method returns, contains the <see cref="TransitionFeatureKind"/> value equivalent
84+
/// to the span, if the conversion succeeded, or <c>default</c> if the conversion failed.
85+
/// </param>
86+
/// <returns>
87+
/// <c>true</c> if <paramref name="value"/> was converted successfully; otherwise, <c>false</c>.
88+
/// </returns>
89+
/// <remarks>
90+
/// This method is suited for string parsing
91+
/// There are zero allocations, no boxing, Fast short-circuit evaluation
92+
/// JIT friendly
93+
/// </remarks>
94+
public static bool TryParse(ReadOnlySpan<char> value, out TransitionFeatureKind result)
95+
{
96+
if (value.Length == 7 && value.Equals("trigger".AsSpan(), StringComparison.OrdinalIgnoreCase))
97+
{
98+
result = TransitionFeatureKind.Trigger;
99+
return true;
100+
}
101+
102+
if (value.Length == 5 && value.Equals("guard".AsSpan(), StringComparison.OrdinalIgnoreCase))
103+
{
104+
result = TransitionFeatureKind.Guard;
105+
return true;
106+
}
107+
108+
if (value.Length == 6 && value.Equals("effect".AsSpan(), StringComparison.OrdinalIgnoreCase))
109+
{
110+
result = TransitionFeatureKind.Effect;
111+
return true;
112+
}
113+
114+
result = default;
115+
return false;
116+
}
117+
76118
/// <summary>
77119
/// Parses the <see cref="ReadOnlySpan{Byte}"/> to a <see cref="TransitionFeatureKind"/>
78120
/// </summary>

SysML2.NET.CodeGenerator.Tests/Expected/UML/Core/AutoGenEnumProvider/VisibilityKindProvider.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,48 @@ public static VisibilityKind Parse(ReadOnlySpan<char> value)
7373
throw new ArgumentException($"'{new string(value)}' is not a valid VisibilityKind", nameof(value));
7474
}
7575

76+
/// <summary>
77+
/// Tries to parse the <see cref="ReadOnlySpan{Char}"/> to a <see cref="VisibilityKind"/>
78+
/// </summary>
79+
/// <param name="value">
80+
/// The <see cref="ReadOnlySpan{Char}"/> that is to be parsed
81+
/// </param>
82+
/// <param name="result">
83+
/// When this method returns, contains the <see cref="VisibilityKind"/> value equivalent
84+
/// to the span, if the conversion succeeded, or <c>default</c> if the conversion failed.
85+
/// </param>
86+
/// <returns>
87+
/// <c>true</c> if <paramref name="value"/> was converted successfully; otherwise, <c>false</c>.
88+
/// </returns>
89+
/// <remarks>
90+
/// This method is suited for string parsing
91+
/// There are zero allocations, no boxing, Fast short-circuit evaluation
92+
/// JIT friendly
93+
/// </remarks>
94+
public static bool TryParse(ReadOnlySpan<char> value, out VisibilityKind result)
95+
{
96+
if (value.Length == 7 && value.Equals("private".AsSpan(), StringComparison.OrdinalIgnoreCase))
97+
{
98+
result = VisibilityKind.Private;
99+
return true;
100+
}
101+
102+
if (value.Length == 9 && value.Equals("protected".AsSpan(), StringComparison.OrdinalIgnoreCase))
103+
{
104+
result = VisibilityKind.Protected;
105+
return true;
106+
}
107+
108+
if (value.Length == 6 && value.Equals("public".AsSpan(), StringComparison.OrdinalIgnoreCase))
109+
{
110+
result = VisibilityKind.Public;
111+
return true;
112+
}
113+
114+
result = default;
115+
return false;
116+
}
117+
76118
/// <summary>
77119
/// Parses the <see cref="ReadOnlySpan{Byte}"/> to a <see cref="VisibilityKind"/>
78120
/// </summary>

SysML2.NET.CodeGenerator.Tests/Expected/UML/Core/AutoGenReaders/AnnotatingElementReader.cs

Lines changed: 73 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ namespace SysML2.NET.Serializer.Xmi.Readers
3333
using Microsoft.Extensions.Logging.Abstractions;
3434

3535
using SysML2.NET.Common;
36+
using SysML2.NET.Extensions.Core;
3637
using SysML2.NET.Core.POCO.Root.Elements;
3738
using SysML2.NET.Core.POCO.Root.Namespaces;
3839
using SysML2.NET.Core.POCO.Root.Annotations;
@@ -163,6 +164,10 @@ public override IAnnotatingElement Read(XmlReader xmiReader, Uri currentLocation
163164
{
164165
ownedRelationshipXmlAttributeReferences.Add(ownedRelationshipXmlAttributeReference);
165166
}
167+
else
168+
{
169+
this.logger.LogWarning("Failed to parse GUID reference value '{Value}' for property 'ownedRelationship' on element {ElementId}", ownedRelationshipXmlAttributeValue, poco.Id);
170+
}
166171
}
167172

168173
if (ownedRelationshipXmlAttributeReferences.Count != 0)
@@ -179,6 +184,10 @@ public override IAnnotatingElement Read(XmlReader xmiReader, Uri currentLocation
179184
{
180185
this.Cache.AddSingleValueReferencePropertyIdentifier(poco.Id, "owningRelationship", owningRelationshipXmlAttributeReference);
181186
}
187+
else
188+
{
189+
this.logger.LogWarning("Failed to parse GUID reference value '{Value}' for property 'owningRelationship' on element {ElementId}", owningRelationshipXmlAttribute, poco.Id);
190+
}
182191
}
183192

184193
while (xmiReader.Read())
@@ -241,7 +250,14 @@ public override IAnnotatingElement Read(XmlReader xmiReader, Uri currentLocation
241250

242251
if (!string.IsNullOrWhiteSpace(isImpliedIncludedValue))
243252
{
244-
poco.IsImpliedIncluded = bool.Parse(isImpliedIncludedValue);
253+
if (bool.TryParse(isImpliedIncludedValue, out var isImpliedIncludedValueAsBool))
254+
{
255+
poco.IsImpliedIncluded = isImpliedIncludedValueAsBool;
256+
}
257+
else
258+
{
259+
this.logger.LogWarning("Failed to parse bool value '{Value}' for property 'isImpliedIncluded' on element {ElementId}", isImpliedIncludedValue, poco.Id);
260+
}
245261
}
246262

247263
break;
@@ -255,8 +271,14 @@ public override IAnnotatingElement Read(XmlReader xmiReader, Uri currentLocation
255271
{
256272
var hrefSplit = hrefAttribute.Split('#');
257273
this.ExternalReferenceService.AddExternalReferenceToProcess(currentLocation, hrefSplit[0]);
258-
var ownedRelationshipId = Guid.Parse(hrefSplit[1]);
259-
this.Cache.AddMultipleValueReferencePropertyIdentifiers(poco.Id, "ownedRelationship", ownedRelationshipId);
274+
if (Guid.TryParse(hrefSplit[1], out var ownedRelationshipId))
275+
{
276+
this.Cache.AddMultipleValueReferencePropertyIdentifiers(poco.Id, "ownedRelationship", ownedRelationshipId);
277+
}
278+
else
279+
{
280+
this.logger.LogWarning("Failed to parse href GUID value '{HrefValue}' for property 'ownedRelationship' on element {ElementId}", hrefSplit[1], poco.Id);
281+
}
260282
}
261283
else
262284
{
@@ -276,8 +298,14 @@ public override IAnnotatingElement Read(XmlReader xmiReader, Uri currentLocation
276298
{
277299
var hrefSplit = hrefAttribute.Split('#');
278300
this.ExternalReferenceService.AddExternalReferenceToProcess(currentLocation, hrefSplit[0]);
279-
var owningRelationshipId = Guid.Parse(hrefSplit[1]);
280-
this.Cache.AddSingleValueReferencePropertyIdentifier(poco.Id, "owningRelationship", owningRelationshipId);
301+
if (Guid.TryParse(hrefSplit[1], out var owningRelationshipId))
302+
{
303+
this.Cache.AddSingleValueReferencePropertyIdentifier(poco.Id, "owningRelationship", owningRelationshipId);
304+
}
305+
else
306+
{
307+
this.logger.LogWarning("Failed to parse href GUID value '{HrefValue}' for property 'owningRelationship' on element {ElementId}", hrefSplit[1], poco.Id);
308+
}
281309
}
282310
else
283311
{
@@ -289,6 +317,10 @@ public override IAnnotatingElement Read(XmlReader xmiReader, Uri currentLocation
289317
break;
290318
}
291319

320+
default:
321+
this.logger.LogWarning("Unexpected element '{LocalName}' encountered while reading AnnotatingElement at line:position {LineNumber}:{LinePosition}", xmiReader.LocalName, xmlLineInfo?.LineNumber, xmlLineInfo?.LinePosition);
322+
xmiReader.Skip();
323+
break;
292324
}
293325
}
294326
}
@@ -393,6 +425,10 @@ public override async Task<IAnnotatingElement> ReadAsync(XmlReader xmiReader, Ur
393425
{
394426
ownedRelationshipXmlAttributeReferences.Add(ownedRelationshipXmlAttributeReference);
395427
}
428+
else
429+
{
430+
this.logger.LogWarning("Failed to parse GUID reference value '{Value}' for property 'ownedRelationship' on element {ElementId}", ownedRelationshipXmlAttributeValue, poco.Id);
431+
}
396432
}
397433

398434
if (ownedRelationshipXmlAttributeReferences.Count != 0)
@@ -409,6 +445,10 @@ public override async Task<IAnnotatingElement> ReadAsync(XmlReader xmiReader, Ur
409445
{
410446
this.Cache.AddSingleValueReferencePropertyIdentifier(poco.Id, "owningRelationship", owningRelationshipXmlAttributeReference);
411447
}
448+
else
449+
{
450+
this.logger.LogWarning("Failed to parse GUID reference value '{Value}' for property 'owningRelationship' on element {ElementId}", owningRelationshipXmlAttribute, poco.Id);
451+
}
412452
}
413453

414454
while (await xmiReader.ReadAsync())
@@ -471,7 +511,14 @@ public override async Task<IAnnotatingElement> ReadAsync(XmlReader xmiReader, Ur
471511

472512
if (!string.IsNullOrWhiteSpace(isImpliedIncludedValue))
473513
{
474-
poco.IsImpliedIncluded = bool.Parse(isImpliedIncludedValue);
514+
if (bool.TryParse(isImpliedIncludedValue, out var isImpliedIncludedValueAsBool))
515+
{
516+
poco.IsImpliedIncluded = isImpliedIncludedValueAsBool;
517+
}
518+
else
519+
{
520+
this.logger.LogWarning("Failed to parse bool value '{Value}' for property 'isImpliedIncluded' on element {ElementId}", isImpliedIncludedValue, poco.Id);
521+
}
475522
}
476523

477524
break;
@@ -485,8 +532,14 @@ public override async Task<IAnnotatingElement> ReadAsync(XmlReader xmiReader, Ur
485532
{
486533
var hrefSplit = hrefAttribute.Split('#');
487534
this.ExternalReferenceService.AddExternalReferenceToProcess(currentLocation, hrefSplit[0]);
488-
var ownedRelationshipId = Guid.Parse(hrefSplit[1]);
489-
this.Cache.AddMultipleValueReferencePropertyIdentifiers(poco.Id, "ownedRelationship", ownedRelationshipId);
535+
if (Guid.TryParse(hrefSplit[1], out var ownedRelationshipId))
536+
{
537+
this.Cache.AddMultipleValueReferencePropertyIdentifiers(poco.Id, "ownedRelationship", ownedRelationshipId);
538+
}
539+
else
540+
{
541+
this.logger.LogWarning("Failed to parse href GUID value '{HrefValue}' for property 'ownedRelationship' on element {ElementId}", hrefSplit[1], poco.Id);
542+
}
490543
}
491544
else
492545
{
@@ -506,8 +559,14 @@ public override async Task<IAnnotatingElement> ReadAsync(XmlReader xmiReader, Ur
506559
{
507560
var hrefSplit = hrefAttribute.Split('#');
508561
this.ExternalReferenceService.AddExternalReferenceToProcess(currentLocation, hrefSplit[0]);
509-
var owningRelationshipId = Guid.Parse(hrefSplit[1]);
510-
this.Cache.AddSingleValueReferencePropertyIdentifier(poco.Id, "owningRelationship", owningRelationshipId);
562+
if (Guid.TryParse(hrefSplit[1], out var owningRelationshipId))
563+
{
564+
this.Cache.AddSingleValueReferencePropertyIdentifier(poco.Id, "owningRelationship", owningRelationshipId);
565+
}
566+
else
567+
{
568+
this.logger.LogWarning("Failed to parse href GUID value '{HrefValue}' for property 'owningRelationship' on element {ElementId}", hrefSplit[1], poco.Id);
569+
}
511570
}
512571
else
513572
{
@@ -519,6 +578,10 @@ public override async Task<IAnnotatingElement> ReadAsync(XmlReader xmiReader, Ur
519578
break;
520579
}
521580

581+
default:
582+
this.logger.LogWarning("Unexpected element '{LocalName}' encountered while reading AnnotatingElement at line:position {LineNumber}:{LinePosition}", xmiReader.LocalName, xmlLineInfo?.LineNumber, xmlLineInfo?.LinePosition);
583+
await xmiReader.SkipAsync();
584+
break;
522585
}
523586
}
524587
}

0 commit comments

Comments
 (0)