Skip to content

Commit 5812a72

Browse files
author
sam.gerene
committed
[Add] enum deserializers
1 parent 6500469 commit 5812a72

File tree

177 files changed

+4445
-1387
lines changed

Some content is hidden

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

177 files changed

+4445
-1387
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// -------------------------------------------------------------------------------------------------
2+
// <copyright file="FeatureDirectionKindDeserializer.cs" company="RHEA System S.A.">
3+
//
4+
// Copyright 2022 RHEA System S.A.
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// </copyright>
19+
// ------------------------------------------------------------------------------------------------
20+
21+
// ------------------------------------------------------------------------------------------------
22+
// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!--------
23+
// ------------------------------------------------------------------------------------------------
24+
25+
namespace SysML2.NET.Serializer.Json
26+
{
27+
using System;
28+
29+
/// <summary>
30+
/// The purpose of the <see cref="FeatureDirectionKindDeserializer"/> is to provide deserialization capabilities
31+
/// for the <see cref="FeatureDirectionKind"/> Enum
32+
/// </summary>
33+
internal static class FeatureDirectionKindDeserializer
34+
{
35+
/// <summary>
36+
/// Deserializes a string value to a
37+
/// </summary>
38+
/// <param name="value">
39+
/// The string representation of the <see cref="FeatureDirectionKind"/>
40+
/// </param>
41+
/// <returns>
42+
/// The value of the <see cref="FeatureDirectionKind"/>
43+
/// </returns>
44+
internal static FeatureDirectionKind Deserialize(string value)
45+
{
46+
switch (value)
47+
{
48+
case "IN":
49+
return FeatureDirectionKind.In;
50+
case "INOUT":
51+
return FeatureDirectionKind.Inout;
52+
case "OUT":
53+
return FeatureDirectionKind.Out;
54+
default:
55+
throw new ArgumentException($"{value} is not a valid FeatureDirectionKind", nameof(value));
56+
}
57+
}
58+
}
59+
}
60+
61+
// ------------------------------------------------------------------------------------------------
62+
// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!--------
63+
// ------------------------------------------------------------------------------------------------

SysML2.NET.CodeGenerator.Tests/Generators/HandleBarsGenerators/DtoDeSerializerGeneratorTestFixture.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ public void SetUp()
4949
dtoDeSerializerGenerator = new DtoDeSerializerGenerator();
5050
}
5151

52+
[Test]
53+
public void verify_enum_deserializers_are_generated()
54+
{
55+
Assert.That(async () => await dtoDeSerializerGenerator.GenerateEnumDeSerializers(rootPackage, dtoDirectoryInfo),
56+
Throws.Nothing);
57+
}
58+
5259
[Test]
5360
public void verify_dto_deserializers_are_generated()
5461
{

SysML2.NET.CodeGenerator.Tests/SysML2.NET.CodeGenerator.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
</PropertyGroup>
1818

1919
<ItemGroup>
20+
<Compile Remove="Expected\AutoGenDeSerializer\FeatureDirectionKindDeserializer.cs" />
2021
<Compile Remove="Expected\AutoGenDeSerializer\PartDefinitionDeSerializer.cs" />
2122
<Compile Remove="Expected\AutoGenEnum\VisibilityKind.cs" />
2223
<Compile Remove="Expected\AutoGenSerializer\ElementSerializer.cs" />
@@ -28,6 +29,9 @@
2829
</ItemGroup>
2930

3031
<ItemGroup>
32+
<Content Include="Expected\AutoGenDeSerializer\FeatureDirectionKindDeserializer.cs">
33+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
34+
</Content>
3135
<Content Include="Expected\AutoGenDeSerializer\PartDefinitionDeSerializer.cs">
3236
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
3337
</Content>

SysML2.NET.CodeGenerator/Generators/HandleBarsGenerators/DtoDeSerializerGenerator.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,39 @@ public class DtoDeSerializerGenerator : HandleBarsGenerator
5050
/// </returns>
5151
public override async Task Generate(EPackage package, DirectoryInfo outputDirectory)
5252
{
53+
await this.GenerateEnumDeSerializers(package, outputDirectory);
5354
await this.GenerateDtoDeSerializers(package, outputDirectory);
5455
await this.GenerateDeSerializationProvider(package, outputDirectory);
5556
}
56-
57+
58+
/// <summary>
59+
/// Generates the DeSerializer classes for each <see cref="EClass"/> in the provided <see cref="EPackage"/>
60+
/// </summary>
61+
/// <param name="package">
62+
/// the <see cref="EPackage"/> that contains the classes that need to be generated
63+
/// </param>
64+
/// <param name="outputDirectory">
65+
/// The target <see cref="DirectoryInfo"/>
66+
/// </param>
67+
/// <returns>
68+
/// an awaitable <see cref="Task"/>
69+
/// </returns>
70+
public async Task GenerateEnumDeSerializers(EPackage package, DirectoryInfo outputDirectory)
71+
{
72+
var template = this.Templates["enum-deserializer-template"];
73+
74+
foreach (var eClass in package.EClassifiers.OfType<EEnum>())
75+
{
76+
var generatedDeSerializer = template(eClass);
77+
78+
generatedDeSerializer = CodeCleanup(generatedDeSerializer);
79+
80+
var fileName = $"{eClass.Name.CapitalizeFirstLetter()}DeSerializer.cs";
81+
82+
await Write(generatedDeSerializer, outputDirectory, fileName);
83+
}
84+
}
85+
5786
/// <summary>
5887
/// Generates the DeSerializer classes for each <see cref="EClass"/> in the provided <see cref="EPackage"/>
5988
/// </summary>
@@ -126,6 +155,7 @@ protected override void RegisterHelpers()
126155
/// </summary>
127156
protected override void RegisterTemplates()
128157
{
158+
this.RegisterTemplate("enum-deserializer-template");
129159
this.RegisterTemplate("dto-deserializer-template");
130160
this.RegisterTemplate("dto-deserialization-provider-template");
131161
}

SysML2.NET.CodeGenerator/SysML2.NET.CodeGenerator.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@
5858
<None Update="Templates\dto-serializer-template.hbs">
5959
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
6060
</None>
61+
<None Update="Templates\enum-deserializer-template.hbs">
62+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
63+
</None>
6164
<None Update="Templates\enum-template.cshtml">
6265
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
6366
</None>

SysML2.NET.CodeGenerator/Templates/dto-deserializer-template.hbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ namespace SysML2.NET.Serializer.Json
163163
new NotImplementedException("nullable - {{EClass.Name}}.{{structuralFeature.Name}} is not yet supported");
164164
{{/if}}
165165
{{else if (StructuralFeature.QueryIsEnum structuralFeature )}}
166-
dtoInstance.{{String.CapitalizeFirstLetter structuralFeature.Name }} = ({{DTO.TypeName structuralFeature }})Enum.Parse(typeof({{DTO.TypeName structuralFeature }}), {{structuralFeature.Name }}Property.GetString());
166+
dtoInstance.{{String.CapitalizeFirstLetter structuralFeature.Name }} = {{ structuralFeature.EType.Name }}DeSerializer.DeserializeNullable({{ structuralFeature.Name }}Property.GetString());
167167
{{else}}
168168
dtoInstance.{{String.CapitalizeFirstLetter structuralFeature.Name }} = {{ structuralFeature.Name }}Property.GetString();
169169
{{/if}}
@@ -179,7 +179,7 @@ namespace SysML2.NET.Serializer.Json
179179
new NotImplementedException("Scalar - {{EClass.Name}}.{{structuralFeature.Name}} is not yet supported");
180180
{{/if}}
181181
{{else if (StructuralFeature.QueryIsEnum structuralFeature )}}
182-
dtoInstance.{{String.CapitalizeFirstLetter structuralFeature.Name }} = ({{DTO.TypeName structuralFeature }})Enum.Parse(typeof({{DTO.TypeName structuralFeature }}), {{structuralFeature.Name }}Property.GetString());
182+
dtoInstance.{{String.CapitalizeFirstLetter structuralFeature.Name }} = {{ structuralFeature.EType.Name }}DeSerializer.Deserialize({{ structuralFeature.Name }}Property.GetString());
183183
{{else}}
184184
var propertyValue = {{structuralFeature.Name }}Property.GetString();
185185
if (propertyValue != null)
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// -------------------------------------------------------------------------------------------------
2+
// <copyright file="{{this.Name}}DeSerializer.cs" company="RHEA System S.A.">
3+
//
4+
// Copyright 2022 RHEA System S.A.
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// </copyright>
19+
// ------------------------------------------------------------------------------------------------
20+
21+
// ------------------------------------------------------------------------------------------------
22+
// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!--------
23+
// ------------------------------------------------------------------------------------------------
24+
25+
namespace SysML2.NET.Serializer.Json
26+
{
27+
using System;
28+
29+
/// <summary>
30+
/// The purpose of the <see cref="{{this.Name}}DeSerializer"/> is to provide deserialization capabilities
31+
/// for the <see cref="{{this.Name}}"/> Enum
32+
/// </summary>
33+
internal static class {{this.Name}}DeSerializer
34+
{
35+
/// <summary>
36+
/// Deserializes a string value to a <see cref="{{this.Name}}"/>
37+
/// </summary>
38+
/// <param name="value">
39+
/// The string representation of the <see cref="{{this.Name}}"/>
40+
/// </param>
41+
/// <returns>
42+
/// The value of the <see cref="{{this.Name}}"/>
43+
/// </returns>
44+
internal static {{this.Name}} Deserialize(string value)
45+
{
46+
switch (value)
47+
{
48+
{{#with this as | EEnum |}}
49+
{{#each this.ELiterals as | enumerationLiteral |}}
50+
case "{{ String.Uppercase enumerationLiteral.Name }}":
51+
return {{ EEnum.Name }}.{{ String.PascalCase enumerationLiteral.Name }};
52+
{{/each}}
53+
default:
54+
throw new ArgumentException($"{value} is not a valid {{ EEnum.Name }}", nameof(value));
55+
{{/with}}
56+
}
57+
}
58+
59+
/// <summary>
60+
/// Deserializes a string value to a nullable <see cref="{{this.Name}}"/>
61+
/// </summary>
62+
/// <param name="value">
63+
/// The string representation of the <see cref="{{this.Name}}"/>
64+
/// </param>
65+
/// <returns>
66+
/// The value of the nullable <see cref="{{this.Name}}"/>
67+
/// </returns>
68+
internal static {{this.Name}}? DeserializeNullable(string value)
69+
{
70+
switch (value)
71+
{
72+
{{#with this as | EEnum |}}
73+
{{#each this.ELiterals as | enumerationLiteral |}}
74+
case "{{ String.Uppercase enumerationLiteral.Name }}":
75+
return {{ EEnum.Name }}.{{ String.PascalCase enumerationLiteral.Name }};
76+
{{/each}}
77+
case null:
78+
return null;
79+
default:
80+
throw new ArgumentException($"{value} is not a valid {{ EEnum.Name }}", nameof(value));
81+
{{/with}}
82+
}
83+
}
84+
}
85+
}
86+
87+
// ------------------------------------------------------------------------------------------------
88+
// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!--------
89+
// ------------------------------------------------------------------------------------------------

SysML2.NET.CodeGenerator/Templates/enum-template.hbs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525
namespace SysML2.NET
2626
{
2727
{{ #Documentation this }}
28-
public enum {{ this.Name }}
29-
{
28+
public enum {{ this.Name }}
29+
{
3030
{{#each this.ELiterals as | enumerationLiteral |}}
3131
{{ #Documentation enumerationLiteral }}
3232
{{ String.PascalCase enumerationLiteral.Name }} = {{ enumerationLiteral.Value }},
33-
33+
3434
{{/each}}
35-
}
35+
}
3636
}
3737

3838
// ------------------------------------------------------------------------------------------------

SysML2.NET.Serializer.Json/AutoGenDeSerializer/AcceptActionUsageDeSerializer.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ internal static IAcceptActionUsage DeSerialize(JsonElement jsonElement, Serializ
100100

101101
if (jsonElement.TryGetProperty("direction", out JsonElement directionProperty))
102102
{
103-
throw new NotImplementedException("AcceptActionUsage.direction is not yet supported");
103+
dtoInstance.Direction = FeatureDirectionKindDeSerializer.DeserializeNullable(directionProperty.GetString());
104104
}
105105
else
106106
{
@@ -253,12 +253,19 @@ internal static IAcceptActionUsage DeSerialize(JsonElement jsonElement, Serializ
253253

254254
if (jsonElement.TryGetProperty("owningRelationship", out JsonElement owningRelationshipProperty))
255255
{
256-
if (owningRelationshipProperty.TryGetProperty("@id", out JsonElement owningRelationshipIdProperty))
256+
if (owningRelationshipProperty.ValueKind == JsonValueKind.Null)
257257
{
258-
var propertyValue = owningRelationshipIdProperty.GetString();
259-
if (propertyValue != null)
258+
dtoInstance.OwningRelationship = null;
259+
}
260+
else
261+
{
262+
if (owningRelationshipProperty.TryGetProperty("@id", out JsonElement owningRelationshipIdProperty))
260263
{
261-
dtoInstance.OwningRelationship = Guid.Parse(propertyValue);
264+
var propertyValue = owningRelationshipIdProperty.GetString();
265+
if (propertyValue != null)
266+
{
267+
dtoInstance.OwningRelationship = Guid.Parse(propertyValue);
268+
}
262269
}
263270
}
264271
}
@@ -269,7 +276,7 @@ internal static IAcceptActionUsage DeSerialize(JsonElement jsonElement, Serializ
269276

270277
if (jsonElement.TryGetProperty("portionKind", out JsonElement portionKindProperty))
271278
{
272-
throw new NotImplementedException("AcceptActionUsage.portionKind is not yet supported");
279+
dtoInstance.PortionKind = PortionKindDeSerializer.DeserializeNullable(portionKindProperty.GetString());
273280
}
274281
else
275282
{

SysML2.NET.Serializer.Json/AutoGenDeSerializer/ActionDefinitionDeSerializer.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,19 @@ internal static IActionDefinition DeSerialize(JsonElement jsonElement, Serializa
181181

182182
if (jsonElement.TryGetProperty("owningRelationship", out JsonElement owningRelationshipProperty))
183183
{
184-
if (owningRelationshipProperty.TryGetProperty("@id", out JsonElement owningRelationshipIdProperty))
184+
if (owningRelationshipProperty.ValueKind == JsonValueKind.Null)
185185
{
186-
var propertyValue = owningRelationshipIdProperty.GetString();
187-
if (propertyValue != null)
186+
dtoInstance.OwningRelationship = null;
187+
}
188+
else
189+
{
190+
if (owningRelationshipProperty.TryGetProperty("@id", out JsonElement owningRelationshipIdProperty))
188191
{
189-
dtoInstance.OwningRelationship = Guid.Parse(propertyValue);
192+
var propertyValue = owningRelationshipIdProperty.GetString();
193+
if (propertyValue != null)
194+
{
195+
dtoInstance.OwningRelationship = Guid.Parse(propertyValue);
196+
}
190197
}
191198
}
192199
}

0 commit comments

Comments
 (0)