Skip to content

Commit baf6f02

Browse files
author
sam.gerene
committed
[Add] static serializers, static SerializationProvider
[Add] ISerializer and Serializer
1 parent 17bf0ed commit baf6f02

File tree

177 files changed

+12054
-497
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

+12054
-497
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,12 @@ public void verify_dto_serializers_are_generated()
5555
Assert.That(async () => await dtoSerializerGenerator.GenerateSerializers(rootPackage, dtoDirectoryInfo),
5656
Throws.Nothing);
5757
}
58+
59+
[Test]
60+
public void verify_SerializationProvider_is_generated()
61+
{
62+
Assert.That(async () => await dtoSerializerGenerator.GenerateSerializationProvider(rootPackage, dtoDirectoryInfo),
63+
Throws.Nothing);
64+
}
5865
}
5966
}

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public class DtoSerializerGenerator : HandleBarsGenerator
5151
public override async Task Generate(EPackage package, DirectoryInfo outputDirectory)
5252
{
5353
await this.GenerateSerializers(package, outputDirectory);
54+
await this.GenerateSerializationProvider(package, outputDirectory);
5455
}
5556

5657
public async Task GenerateSerializers(EPackage package, DirectoryInfo outputDirectory)
@@ -68,7 +69,22 @@ public async Task GenerateSerializers(EPackage package, DirectoryInfo outputDire
6869
await Write(generatedSerializer, outputDirectory, fileName);
6970
}
7071
}
71-
72+
73+
public async Task GenerateSerializationProvider(EPackage package, DirectoryInfo outputDirectory)
74+
{
75+
var template = this.Templates["dto-serialization-provider-template"];
76+
77+
var eClasses = package.EClassifiers.OfType<EClass>().OrderBy(x => x.Name).ToList();
78+
79+
var generatedSerializationProvider = template(eClasses);
80+
81+
generatedSerializationProvider = CodeCleanup(generatedSerializationProvider);
82+
83+
var fileName = "SerializationProvider.cs";
84+
85+
await Write(generatedSerializationProvider, outputDirectory, fileName);
86+
}
87+
7288
/// <summary>
7389
/// Register the custom helpers
7490
/// </summary>
@@ -87,6 +103,7 @@ protected override void RegisterHelpers()
87103
protected override void RegisterTemplates()
88104
{
89105
this.RegisterTemplate("dto-serializer-template");
106+
this.RegisterTemplate("dto-serialization-provider-template");
90107
}
91108
}
92109
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@
4343
<None Update="Templates\dto-interface-template.hbs">
4444
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
4545
</None>
46+
<None Update="Templates\dto-serialization-provider-template.hbs">
47+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
48+
</None>
49+
<None Update="Templates\dto-serialization-provider-template.hbs">
50+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
51+
</None>
4652
<None Update="Templates\dto-serializer-template.hbs">
4753
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
4854
</None>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// -------------------------------------------------------------------------------------------------
2+
// <copyright file="SerializationProvider.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.AutoGenSerializer
26+
{
27+
using System;
28+
using System.Collections.Generic;
29+
using System.Text.Json;
30+
31+
/// <summary>
32+
/// Delegate provider for the appropriate serialization method to serialize a <see cref="Type" />
33+
/// </summary>
34+
internal static class SerializationProvider
35+
{
36+
private static readonly Dictionary<Type, Action<object, Utf8JsonWriter, SerializationModeKind>> SerializerActionMap = new Dictionary<Type, Action<object, Utf8JsonWriter, SerializationModeKind>>
37+
{
38+
{{#each this as | eClass |}}
39+
{ typeof(DTO.{{ eClass.Name }}), {{ eClass.Name }}Serializer.Serialize },
40+
{{/each}}
41+
};
42+
43+
/// <summary>
44+
/// Provides the delegate <see cref="Action{object, Utf8JsonWriter, SerializationModeKind}"/> for the
45+
/// <see cref="Type"/> that is to be serialized
46+
/// </summary>
47+
/// <param name="type">
48+
/// The subject <see cref="Type"/> that is to be serialized
49+
/// </param>
50+
/// <returns>
51+
/// A Delegate of <see cref="Action{object, Utf8JsonWriter, SerializationModeKind}"/>
52+
/// </returns>
53+
/// <exception cref="NotSupportedException">
54+
/// Thrown when the <see cref="Type"/> is not supported.
55+
/// </exception>
56+
internal static Action<object, Utf8JsonWriter, SerializationModeKind> Provide(Type type)
57+
{
58+
if (!SerializerActionMap.TryGetValue(type, out var action))
59+
{
60+
throw new NotSupportedException($"The {type.Name} is not supported by the SerializationProvider.");
61+
}
62+
63+
return action;
64+
}
65+
}
66+
}
67+
68+
// ------------------------------------------------------------------------------------------------
69+
// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!--------
70+
// ------------------------------------------------------------------------------------------------

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!--------
2323
// ------------------------------------------------------------------------------------------------
2424

25-
namespace SysML2.NET.Serializer
25+
namespace SysML2.NET.Serializer.Json
2626
{
27+
using System;
2728
using System.Text.Json;
2829

2930
using SysML2.NET.DTO;
@@ -37,7 +38,7 @@ namespace SysML2.NET.Serializer
3738
/// <summary>
3839
/// Serializes an instance of <see cref="I{{ this.Name }}"/> using an <see cref="Utf8JsonWriter"/>
3940
/// </summary>
40-
/// <param name="{{String.LowerCaseFirstLetter this.Name }}">
41+
/// <param name="obj">
4142
/// The <see cref="I{{ this.Name }}"/> to serialize
4243
/// </param>
4344
/// <param name="writer">
@@ -46,8 +47,13 @@ namespace SysML2.NET.Serializer
4647
/// <param name="serializationModeKind">
4748
/// enumeration specifying what kind of serialization shall be used
4849
/// </param>
49-
public static void Serialize(I{{ this.Name }} i{{this.Name }}, Utf8JsonWriter writer, SerializationModeKind serializationModeKind)
50+
internal static void Serialize(object obj, Utf8JsonWriter writer, SerializationModeKind serializationModeKind)
5051
{
52+
if (!(obj is I{{ this.Name }} i{{this.Name }}))
53+
{
54+
throw new ArgumentException("The object shall be an I{{ this.Name }}", nameof(obj));
55+
}
56+
5157
writer.WriteStartObject();
5258

5359
writer.WritePropertyName("@id");

0 commit comments

Comments
 (0)