Skip to content

Commit 2a5d0f8

Browse files
[Implement] Serializer and DeSerializer classes
1 parent 53e830d commit 2a5d0f8

File tree

16 files changed

+7136
-106
lines changed

16 files changed

+7136
-106
lines changed

SysML2.NET.CodeGenerator.Tests/Generators/UmlHandleBarsGenerators/UmlMessagePackGeneratorTestFixture.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class UmlMessagePackGeneratorTestFixture
3131
{
3232
private DirectoryInfo umlMessagePackPayloadDirectoryInfo;
3333
private DirectoryInfo umlMessagePackFormatterDirectoryInfo;
34+
private DirectoryInfo umlMessagePackDataResolverGetFormatterHelperDirectoryInfo;
3435
private UmlMessagePackGenerator umlMessagePackGenerator;
3536

3637
[OneTimeSetUp]
@@ -40,6 +41,7 @@ public void SetUp()
4041

4142
this.umlMessagePackPayloadDirectoryInfo = directoryInfo.CreateSubdirectory(Path.Combine("UML", "_SysML2.NET.Serializer.MessagePack.Core.AutoGenMessagePackPayload"));
4243
this.umlMessagePackFormatterDirectoryInfo = directoryInfo.CreateSubdirectory(Path.Combine("UML", "_SysML2.NET.Serializer.MessagePack.Core.AutoGenMessagePackFormatter"));
44+
this.umlMessagePackDataResolverGetFormatterHelperDirectoryInfo = directoryInfo.CreateSubdirectory(Path.Combine("UML", "_SysML2.NET.Serializer.MessagePack.Core.AutoGenDataResolverGetFormatterHelper"));
4345

4446
this.umlMessagePackGenerator = new UmlMessagePackGenerator();
4547
}
@@ -52,6 +54,14 @@ await Assert.ThatAsync(() => this.umlMessagePackGenerator.GenerateMessagePackPay
5254
Throws.Nothing);
5355
}
5456

57+
[Test]
58+
public async Task Verify_that_DataResolverGetFormatterHelper_is_generated()
59+
{
60+
await Assert.ThatAsync(() => this.umlMessagePackGenerator.GenerateDataResolverGetFormatterHelper(GeneratorSetupFixture.XmiReaderResult,
61+
this.umlMessagePackDataResolverGetFormatterHelperDirectoryInfo),
62+
Throws.Nothing);
63+
}
64+
5565
[Test]
5666
public async Task Verify_that_PayloadFactory_is_generated()
5767
{

SysML2.NET.CodeGenerator/Generators/UmlHandleBarsGenerators/UmlMessagePackGenerator.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ public class UmlMessagePackGenerator : UmlHandleBarsGenerator
5858
/// </summary>
5959
private const string DtoMessagePackFormatterTemplateName = "core-dto-messagepackformatter-uml-template";
6060

61+
/// <summary>
62+
/// Gets the name of the template used to generate the MessagePack DataResolverGetFormatterHelper
63+
/// </summary>
64+
private const string MessagePackDataResolverGetFormatterHelper = "core-messagepack-DataResolverGetFormatterHelper";
65+
6166
/// <summary>
6267
/// Generates code specific to the concrete implementation
6368
/// </summary>
@@ -141,6 +146,39 @@ public async Task<string> GenerateMessagePackPayload(XmiReaderResult xmiReaderRe
141146
return generateMessagePackPayload;
142147
}
143148

149+
/// <summary>
150+
/// Generates the MessagePack DataResolverGetFormatterHelper
151+
/// </summary>
152+
/// <param name="xmiReaderResult">
153+
/// the <see cref="XmiReaderResult"/> holding the UML model
154+
/// </param>
155+
/// <param name="outputDirectory">
156+
/// The <see cref="DirectoryInfo"/> to which the generated class is written
157+
/// </param>
158+
/// <returns>
159+
/// The generated code as string
160+
/// </returns>
161+
public async Task<string> GenerateDataResolverGetFormatterHelper(XmiReaderResult xmiReaderResult, DirectoryInfo outputDirectory)
162+
{
163+
var template = this.Templates[MessagePackDataResolverGetFormatterHelper];
164+
165+
var classes = xmiReaderResult.QueryRoot(null, name: "SysML").QueryPackages()
166+
.SelectMany(x => x.PackagedElement.OfType<IClass>())
167+
.Where(x => !x.IsAbstract)
168+
.OrderBy(x => x.Name)
169+
.ToList();
170+
171+
var generateMessagePackDataResolverGetFormatterHelper = template(classes);
172+
173+
generateMessagePackDataResolverGetFormatterHelper = this.CodeCleanup(generateMessagePackDataResolverGetFormatterHelper);
174+
175+
const string fileName = "DataResolverGetFormatterHelper.cs";
176+
177+
await Write(generateMessagePackDataResolverGetFormatterHelper, outputDirectory, fileName);
178+
179+
return generateMessagePackDataResolverGetFormatterHelper;
180+
}
181+
144182
/// <summary>
145183
/// Generates the MessagePack PayloadMessagePackFormatter
146184
/// </summary>
@@ -264,6 +302,7 @@ protected override void RegisterTemplates()
264302
{
265303
this.RegisterTemplate(MessagePackPayloadTemplateName);
266304
this.RegisterTemplate(MessagePackPayloadFactoryTemplateName);
305+
this.RegisterTemplate(MessagePackDataResolverGetFormatterHelper);
267306
this.RegisterTemplate(PayloadMessagePackFormatterTemplateName);
268307
this.RegisterTemplate(DtoMessagePackFormatterTemplateName);
269308
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@
151151
<None Update="Templates\Uml\core-json-enum-deserializer-uml-template.hbs">
152152
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
153153
</None>
154+
<None Update="Templates\Uml\core-messagepack-DataResolverGetFormatterHelper.hbs">
155+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
156+
</None>
154157
<None Update="Templates\Uml\core-messagepack-payload-uml-template.hbs">
155158
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
156159
</None>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// -------------------------------------------------------------------------------------------------
2+
// <copyright file="DataResolverGetFormatterHelper.cs" company="Starion Group S.A.">
3+
//
4+
// Copyright 2022-2026 Starion Group 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.MessagePack.Core
26+
{
27+
using System;
28+
using System.Collections.Generic;
29+
30+
using global::MessagePack.Formatters;
31+
32+
/// <summary>
33+
/// Helper class that resolves a <see cref="IMessagePackFormatter"/> based on a <see cref="Type"/>
34+
/// </summary>
35+
internal static class DataResolverGetFormatterHelper
36+
{
37+
private static readonly Dictionary<Type, object> FormatterMap = new()
38+
{
39+
{ typeof(Payload), new PayloadMessagePackFormatter() },
40+
{{#each this as | class |}}
41+
{ typeof(SysML2.NET.Core.DTO.{{ #NamedElement.WriteFullyQualifiedNameSpace this }}.{{class.Name}}), new {{class.Name}}MessagePackFormatter() },
42+
{{/each}}
43+
};
44+
45+
/// <summary>
46+
/// Gets a <see cref="IMessagePackFormatter"/> for the specific <see cref="Type"/>
47+
/// </summary>
48+
/// <param name="t">
49+
/// The subject <see cref="Type"/>
50+
/// </param>
51+
/// <returns>
52+
/// an instance of <see cref="IMessagePackFormatter"/>
53+
/// </returns>
54+
internal static object GetFormatter(Type t)
55+
{
56+
object formatter;
57+
if (FormatterMap.TryGetValue(t, out formatter))
58+
{
59+
return formatter;
60+
}
61+
62+
// If type can not get, must return null for fallback mechanism.
63+
return null;
64+
}
65+
}
66+
}
67+
68+
// ------------------------------------------------------------------------------------------------
69+
// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!--------
70+
// ------------------------------------------------------------------------------------------------

SysML2.NET.CodeGenerator/Templates/Uml/core-messagepack-payloadfactory-uml-template.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ namespace SysML2.NET.Serializer.MessagePack.Core
4747
/// <returns>
4848
/// An instance of <see cref="Payload"/>
4949
/// </returns>
50-
internal static Payload ToPayload(this IReadOnlyCollection<IData> dataItems)
50+
internal static Payload ToPayload(this IEnumerable<IData> dataItems)
5151
{
5252
if (dataItems == null)
5353
{

0 commit comments

Comments
 (0)