Skip to content

Commit 6f43d2f

Browse files
author
sam.gerene
committed
[Add] DTO serializer project
1 parent 61a2079 commit 6f43d2f

File tree

11 files changed

+387
-11
lines changed

11 files changed

+387
-11
lines changed

SysML2.NET.CodeGenerator.Tests/Expected/AutoGenEnum/VisibilityKind.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
// </copyright>
1919
// ------------------------------------------------------------------------------------------------
2020

21+
// ------------------------------------------------------------------------------------------------
22+
// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!--------
23+
// ------------------------------------------------------------------------------------------------
24+
2125
namespace SysML2.NET.AutoGenEnum
2226
{
2327
/// <summary>
@@ -48,3 +52,7 @@ public enum VisibilityKind
4852
Public = 2
4953
}
5054
}
55+
56+
// ------------------------------------------------------------------------------------------------
57+
// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!--------
58+
// ------------------------------------------------------------------------------------------------
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// -------------------------------------------------------------------------------------------------
2+
// <copyright file="ElementSerializer.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
26+
{
27+
using System.Text.Json;
28+
29+
using SysML2.NET.DTO;
30+
31+
/// <summary>
32+
/// The purpose of the <see cref="ElementSerializer"/> is to build <see cref="JObject"/> and a <see cref="AnnouncementOfOpportunity"/> object
33+
/// </summary>
34+
internal static class ElementSerializer
35+
{
36+
/// <summary>
37+
/// Serializes an instance of <see cref="Element"/> using an <see cref="Utf8JsonWriter"/>
38+
/// </summary>
39+
/// <param name="element">
40+
/// The <see cref="Element"/> to serialize
41+
/// </param>
42+
/// <param name="writer">
43+
/// The target <see cref="Utf8JsonWriter"/>
44+
/// </param>
45+
/// <param name="serializationModeKind">
46+
/// enumeration specifying what kind of serialization shall be used
47+
/// </param>
48+
public static void Serialize(IElement element, Utf8JsonWriter writer, SerializationModeKind serializationModeKind)
49+
{
50+
writer.WriteStartObject();
51+
52+
writer.WritePropertyName("@id");
53+
writer.WriteStringValue(element.Id);
54+
55+
writer.WritePropertyName("@type");
56+
writer.WriteStringValue("Element");
57+
58+
writer.WriteStartArray("aliasIds");
59+
foreach (var aliasId in element.AliasIds)
60+
{
61+
writer.WriteStringValue(aliasId);
62+
}
63+
writer.WriteEndArray();
64+
65+
writer.WritePropertyName("elementId");
66+
writer.WriteStringValue(element.ElementId);
67+
68+
writer.WritePropertyName("name");
69+
writer.WriteStringValue(element.Name);
70+
71+
writer.WriteStartArray("ownedRelationship");
72+
foreach (var ownedRelationship in element.OwnedRelationship)
73+
{
74+
writer.WriteStringValue(ownedRelationship);
75+
}
76+
writer.WriteEndArray();
77+
78+
writer.WritePropertyName("owningRelationship");
79+
if (element.OwningRelationship.HasValue)
80+
{
81+
writer.WriteStringValue(element.OwningRelationship.Value);
82+
}
83+
else
84+
{
85+
writer.WriteNullValue();
86+
}
87+
88+
writer.WritePropertyName("shortName");
89+
writer.WriteStringValue(element.ShortName);
90+
91+
writer.WriteEndObject();
92+
}
93+
}
94+
}
95+
96+
// ------------------------------------------------------------------------------------------------
97+
// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!--------
98+
// ------------------------------------------------------------------------------------------------
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// -------------------------------------------------------------------------------------------------
2+
// <copyright file="DtoSerializerGeneratorTestFixture.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+
namespace SysML2.NET.CodeGenerator.Tests.Generators.HandleBarsGenerators
22+
{
23+
using System.IO;
24+
25+
using ECoreNetto;
26+
27+
using NUnit.Framework;
28+
29+
using SysML2.NET.CodeGenerator.Generators.HandleBarsGenerators;
30+
31+
[TestFixture]
32+
public class DtoSerializerGeneratorTestFixture
33+
{
34+
private DirectoryInfo dtoDirectoryInfo;
35+
36+
private DtoSerializerGenerator dtoSerializerGenerator;
37+
38+
private EPackage rootPackage;
39+
40+
[SetUp]
41+
public void SetUp()
42+
{
43+
var outputpath = TestContext.CurrentContext.TestDirectory;
44+
var directoryInfo = new DirectoryInfo(outputpath);
45+
dtoDirectoryInfo = directoryInfo.CreateSubdirectory("AutoGenSerializer");
46+
47+
rootPackage = DataModelLoader.Load();
48+
49+
dtoSerializerGenerator = new DtoSerializerGenerator();
50+
}
51+
52+
[Test]
53+
public void verify_dto_serializers_are_generated()
54+
{
55+
Assert.That(async () => await dtoSerializerGenerator.GenerateSerializers(rootPackage, dtoDirectoryInfo),
56+
Throws.Nothing);
57+
}
58+
}
59+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
<ItemGroup>
2020
<Compile Remove="Expected\AutoGenEnum\VisibilityKind.cs" />
21+
<Compile Remove="Expected\AutoGenSerializer\ElementSerializer.cs" />
2122
</ItemGroup>
2223

2324
<ItemGroup>
@@ -28,6 +29,9 @@
2829
<Content Include="Expected\AutoGenEnum\VisibilityKind.cs">
2930
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
3031
</Content>
32+
<Content Include="Expected\AutoGenSerializer\ElementSerializer.cs">
33+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
34+
</Content>
3135
</ItemGroup>
3236

3337
<ItemGroup>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// -------------------------------------------------------------------------------------------------
2-
// <copyright file="EnumGenerator.cs" company="RHEA System S.A.">
2+
// <copyright file="DtoGenerator.cs" company="RHEA System S.A.">
33
//
44
// Copyright 2022 RHEA System S.A.
55
//
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// -------------------------------------------------------------------------------------------------
2+
// <copyright file="DtoSerializerGenerator.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+
namespace SysML2.NET.CodeGenerator.Generators.HandleBarsGenerators
22+
{
23+
using System.Linq;
24+
using System.Threading.Tasks;
25+
26+
using System.IO;
27+
28+
using ECoreNetto;
29+
30+
using SysML2.NET.CodeGenerator.Extensions;
31+
using SysML2.NET.CodeGenerator.HandleBarHelpers;
32+
33+
/// <summary>
34+
/// A Handlebars based DTO code generator
35+
/// </summary>
36+
public class DtoSerializerGenerator : HandleBarsGenerator
37+
{
38+
/// <summary>
39+
/// Generates the <see cref="EClass"/> static serializers
40+
/// for each <see cref="EPackage"/>
41+
/// </summary>
42+
/// <param name="package">
43+
/// the <see cref="EPackage"/> that contains the <see cref="EClass"/> to generate
44+
/// </param>
45+
/// <param name="outputDirectory">
46+
/// The target <see cref="DirectoryInfo"/>
47+
/// </param>
48+
/// <returns>
49+
/// an awaitable <see cref="Task"/>
50+
/// </returns>
51+
public override async Task Generate(EPackage package, DirectoryInfo outputDirectory)
52+
{
53+
await this.GenerateSerializers(package, outputDirectory);
54+
}
55+
56+
public async Task GenerateSerializers(EPackage package, DirectoryInfo outputDirectory)
57+
{
58+
var template = this.Templates["dto-serializer-template"];
59+
60+
foreach (var eClass in package.EClassifiers.OfType<EClass>())
61+
{
62+
var generatedSerializer = template(eClass);
63+
64+
generatedSerializer = CodeCleanup(generatedSerializer);
65+
66+
var fileName = $"{eClass.Name.CapitalizeFirstLetter()}Serializer.cs";
67+
68+
await Write(generatedSerializer, outputDirectory, fileName);
69+
}
70+
}
71+
72+
/// <summary>
73+
/// Register the custom helpers
74+
/// </summary>
75+
protected override void RegisterHelpers()
76+
{
77+
this.Handlebars.RegisteredDocumentationHelper();
78+
this.Handlebars.RegisterTypeNameHelper();
79+
this.Handlebars.RegisterGeneralizationHelper();
80+
}
81+
82+
/// <summary>
83+
/// Register the code templates
84+
/// </summary>
85+
protected override void RegisterTemplates()
86+
{
87+
this.RegisterTemplate("dto-serializer-template");
88+
}
89+
}
90+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343
<None Update="Templates\dto-interface-template.hbs">
4444
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
4545
</None>
46+
<None Update="Templates\dto-serializer-template.hbs">
47+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
48+
</None>
4649
<None Update="Templates\enum-template.cshtml">
4750
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
4851
</None>
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// -------------------------------------------------------------------------------------------------
2+
// <copyright file="{{this.Name}}Serializer.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
26+
{
27+
using System.Text.Json;
28+
29+
using SysML2.NET.DTO;
30+
31+
/// <summary>
32+
/// The purpose of the <see cref="{{ this.Name }}"/> is to provide serialization
33+
/// and deserialization capabilities
34+
/// </summary>
35+
internal static class {{ this.Name }}Serializer
36+
{
37+
/// <summary>
38+
/// Serializes an instance of <see cref="Element"/> using an <see cref="Utf8JsonWriter"/>
39+
/// </summary>
40+
/// <param name="element">
41+
/// The <see cref="Element"/> to serialize
42+
/// </param>
43+
/// <param name="writer">
44+
/// The target <see cref="Utf8JsonWriter"/>
45+
/// </param>
46+
/// <param name="serializationModeKind">
47+
/// enumeration specifying what kind of serialization shall be used
48+
/// </param>
49+
public static void Serialize(I{{ this.Name }} {{ String.CamelCase this.Name }}, Utf8JsonWriter writer, SerializationModeKind serializationModeKind)
50+
{
51+
writer.WriteStartObject();
52+
53+
writer.WritePropertyName("@id");
54+
writer.WriteStringValue(element.Id);
55+
56+
writer.WritePropertyName("@type");
57+
writer.WriteStringValue("Element");
58+
59+
{{#each this.AllEStructuralFeatures as | structuralFeature |}}
60+
{{#unless structuralFeature.Derived }}
61+
{{#unless structuralFeature.Transient }}
62+
63+
{{#if structuralFeature.}}
64+
{{/if}}
65+
66+
67+
public {{#DTO.TypeName structuralFeature }} {{ String.PascalCase structuralFeature.Name }} { get; set; }
68+
69+
{{/unless}}
70+
{{/unless}}
71+
{{/each}}
72+
73+
writer.WriteEndObject();
74+
}
75+
76+
77+
}
78+
}
79+
80+
// ------------------------------------------------------------------------------------------------
81+
// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!--------
82+
// ------------------------------------------------------------------------------------------------

SysML2.NET.Serializer/AutoGenSerializer/ElementSerializer.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ internal static class ElementSerializer
3838
/// <param name="writer">
3939
/// The target <see cref="Utf8JsonWriter"/>
4040
/// </param>
41-
public static void Serialize(IElement element, Utf8JsonWriter writer)
41+
/// <param name="serializationModeKind">
42+
/// enumeration specifying what kind of serialization shall be used
43+
/// </param>
44+
public static void Serialize(IElement element, Utf8JsonWriter writer, SerializationModeKind serializationModeKind)
4245
{
4346
writer.WriteStartObject();
4447

0 commit comments

Comments
 (0)