Skip to content

Commit 60a9b47

Browse files
author
sam.gerene
committed
[Update] Serializer generator and generate Serializers
[Update] DeSerializer generatore and generate DeSerializers
1 parent 52b270c commit 60a9b47

File tree

170 files changed

+29440
-1577
lines changed

Some content is hidden

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

170 files changed

+29440
-1577
lines changed

SysML2.NET.CodeGenerator.Tests/Expected/AutoGenDeSerializer/PartDefinitionDeSerializer.cs

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
// -------------------------------------------------------------------------------------------------
22
// <copyright file="PartDefinitionDeSerializer.cs" company="RHEA System S.A.">
3-
//
3+
//
44
// Copyright 2022 RHEA System S.A.
5-
//
5+
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
88
// You may obtain a copy of the License at
9-
//
9+
//
1010
// http://www.apache.org/licenses/LICENSE-2.0
11-
//
11+
//
1212
// Unless required by applicable law or agreed to in writing, software
1313
// distributed under the License is distributed on an "AS IS" BASIS,
1414
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1515
// See the License for the specific language governing permissions and
1616
// limitations under the License.
17-
//
17+
//
1818
// </copyright>
1919
// ------------------------------------------------------------------------------------------------
2020

@@ -55,19 +55,19 @@ internal static class PartDefinitionDeSerializer
5555
/// </returns>
5656
internal static IPartDefinition DeSerialize(JsonElement jsonElement, SerializationModeKind serializationModeKind, ILoggerFactory loggerFactory = null)
5757
{
58-
var logger = loggerFactory == null ? NullLogger.Instance : loggerFactory.CreateLogger("IPartDefinition.DeSerialize");
58+
var logger = loggerFactory == null ? NullLogger.Instance : loggerFactory.CreateLogger("PartDefinitionDeSerializer");
5959

60-
if (!jsonElement.TryGetProperty("@type", out JsonElement typeProperty))
60+
if (!jsonElement.TryGetProperty("@type", out JsonElement @type))
6161
{
6262
throw new InvalidOperationException("The @type property is not available, the PartDefinitionDeSerializer cannot be used to deserialize this JsonElement");
6363
}
6464

65-
if (typeProperty.GetString() != "PartDefinition")
65+
if (@type.GetString() != "PartDefinition")
6666
{
67-
throw new InvalidOperationException($"The PartDefinitionDeSerializer can only be used to deserialize objects of type PartDefinition, a {typeProperty.GetString()} was provided");
67+
throw new InvalidOperationException($"The PartDefinitionDeSerializer can only be used to deserialize objects of type IPartDefinition, a {@type.GetString()} was provided");
6868
}
6969

70-
var partDefinition = new PartDefinition();
70+
var dtoInstance = new DTO.PartDefinition();
7171

7272
if (jsonElement.TryGetProperty("@id", out JsonElement idProperty))
7373
{
@@ -78,102 +78,105 @@ internal static IPartDefinition DeSerialize(JsonElement jsonElement, Serializati
7878
}
7979
else
8080
{
81-
partDefinition.Id = Guid.Parse(propertyValue);
81+
dtoInstance.Id = Guid.Parse(propertyValue);
8282
}
8383
}
8484

8585
if (jsonElement.TryGetProperty("aliasIds", out JsonElement aliasIdsProperty))
8686
{
87-
foreach (var item in aliasIdsProperty.EnumerateArray())
87+
foreach (var arrayItem in aliasIdsProperty.EnumerateArray())
8888
{
89-
partDefinition.AliasIds.Add(item.GetString());
89+
var propertyValue = arrayItem.GetString();
90+
if (propertyValue != null)
91+
{
92+
dtoInstance.AliasIds.Add(propertyValue);
93+
}
9094
}
9195
}
9296
else
9397
{
94-
logger.LogDebug($"the aliasIds Json property was not found in the PartDefinition: {partDefinition.Id}");
98+
logger.LogDebug($"the aliasIds Json property was not found in the PartDefinition: {dtoInstance.Id}");
9599
}
96100

97101
if (jsonElement.TryGetProperty("elementId", out JsonElement elementIdProperty))
98102
{
99103
var propertyValue = elementIdProperty.GetString();
100104
if (propertyValue != null)
101105
{
102-
partDefinition.ElementId = propertyValue;
106+
dtoInstance.ElementId = propertyValue;
103107
}
104108
}
105109
else
106110
{
107-
logger.LogDebug($"the elementId Json property was not found in the PartDefinition: {partDefinition.Id}");
111+
logger.LogDebug($"the elementId Json property was not found in the PartDefinition: {dtoInstance.Id}");
108112
}
109113

110114
if (jsonElement.TryGetProperty("isAbstract", out JsonElement isAbstractProperty))
111115
{
112-
partDefinition.IsAbstract = isAbstractProperty.GetBoolean();
116+
dtoInstance.IsAbstract = isAbstractProperty.GetBoolean();
113117
}
114118
else
115119
{
116-
logger.LogDebug($"the isAbstract Json property was not found in the PartDefinition: {partDefinition.Id}");
120+
logger.LogDebug($"the isAbstract Json property was not found in the PartDefinition: {dtoInstance.Id}");
117121
}
118122

119123
if (jsonElement.TryGetProperty("isIndividual", out JsonElement isIndividualProperty))
120124
{
121-
partDefinition.IsIndividual = isIndividualProperty.GetBoolean();
125+
dtoInstance.IsIndividual = isIndividualProperty.GetBoolean();
122126
}
123127
else
124128
{
125-
logger.LogDebug($"the isIndividual Json property was not found in the PartDefinition: {partDefinition.Id}");
129+
logger.LogDebug($"the isIndividual Json property was not found in the PartDefinition: {dtoInstance.Id}");
126130
}
127131

128132
if (jsonElement.TryGetProperty("isSufficient", out JsonElement isSufficientProperty))
129133
{
130-
partDefinition.IsSufficient = isSufficientProperty.GetBoolean();
134+
dtoInstance.IsSufficient = isSufficientProperty.GetBoolean();
131135
}
132136
else
133137
{
134-
logger.LogDebug($"the isSufficient Json property was not found in the PartDefinition: {partDefinition.Id}");
138+
logger.LogDebug($"the isSufficient Json property was not found in the PartDefinition: {dtoInstance.Id}");
135139
}
136140

137141
if (jsonElement.TryGetProperty("isVariation", out JsonElement isVariationProperty))
138142
{
139-
partDefinition.IsVariation = isVariationProperty.GetBoolean();
143+
dtoInstance.IsVariation = isVariationProperty.GetBoolean();
140144
}
141145
else
142146
{
143-
logger.LogDebug($"the isVariation Json property was not found in the PartDefinition: {partDefinition.Id}");
147+
logger.LogDebug($"the isVariation Json property was not found in the PartDefinition: {dtoInstance.Id}");
144148
}
145149

146150
if (jsonElement.TryGetProperty("name", out JsonElement nameProperty))
147151
{
148152
var propertyValue = nameProperty.GetString();
149153
if (propertyValue != null)
150154
{
151-
partDefinition.Name = propertyValue;
155+
dtoInstance.Name = propertyValue;
152156
}
153157
}
154158
else
155159
{
156-
logger.LogDebug($"the name Json property was not found in the PartDefinition: {partDefinition.Id}");
160+
logger.LogDebug($"the name Json property was not found in the PartDefinition: {dtoInstance.Id}");
157161
}
158162

159163
if (jsonElement.TryGetProperty("ownedRelationship", out JsonElement ownedRelationshipProperty))
160164
{
161-
foreach (var item in ownedRelationshipProperty.EnumerateArray())
165+
foreach (var arrayItem in ownedRelationshipProperty.EnumerateArray())
162166
{
163-
if (item.TryGetProperty("@id", out JsonElement ownedRelationshipIdProperty))
167+
if (arrayItem.TryGetProperty("@id", out JsonElement ownedRelationshipIdProperty))
164168
{
165169
var propertyValue = ownedRelationshipIdProperty.GetString();
166170
if (propertyValue != null)
167171
{
168-
169-
partDefinition.OwnedRelationship.Add(Guid.Parse(propertyValue));
172+
dtoInstance.OwnedRelationship.Add(Guid.Parse(propertyValue));
170173
}
171174
}
172175
}
173176
}
174177
else
175178
{
176-
logger.LogDebug($"the ownedRelationship Json property was not found in the PartDefinition: {partDefinition.Id}");
179+
logger.LogDebug($"the ownedRelationship Json property was not found in the PartDefinition: {dtoInstance.Id}");
177180
}
178181

179182
if (jsonElement.TryGetProperty("owningRelationship", out JsonElement owningRelationshipProperty))
@@ -183,29 +186,30 @@ internal static IPartDefinition DeSerialize(JsonElement jsonElement, Serializati
183186
var propertyValue = owningRelationshipIdProperty.GetString();
184187
if (propertyValue != null)
185188
{
186-
partDefinition.OwningRelationship = Guid.Parse(propertyValue);
189+
dtoInstance.OwningRelationship = Guid.Parse(propertyValue);
187190
}
188191
}
189192
}
190193
else
191194
{
192-
logger.LogDebug($"the owningRelationship Json property was not found in the PartDefinition: {partDefinition.Id}");
195+
logger.LogDebug($"the owningRelationship Json property was not found in the PartDefinition: {dtoInstance.Id}");
193196
}
194197

195198
if (jsonElement.TryGetProperty("shortName", out JsonElement shortNameProperty))
196199
{
197200
var propertyValue = shortNameProperty.GetString();
198201
if (propertyValue != null)
199202
{
200-
partDefinition.ShortName = propertyValue;
203+
dtoInstance.ShortName = propertyValue;
201204
}
202205
}
203206
else
204207
{
205-
logger.LogDebug($"the shortName Json property was not found in the PartDefinition: {partDefinition.Id}");
208+
logger.LogDebug($"the shortName Json property was not found in the PartDefinition: {dtoInstance.Id}");
206209
}
207210

208-
return partDefinition;
211+
212+
return dtoInstance;
209213
}
210214
}
211215
}

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

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// -------------------------------------------------------------------------------------------------
2-
// <copyright file="DtoSerializerGenerator.cs" company="RHEA System S.A.">
2+
// <copyright file="DtoDeSerializerGenerator.cs" company="RHEA System S.A.">
33
//
44
// Copyright 2022 RHEA System S.A.
55
//
@@ -33,7 +33,7 @@ namespace SysML2.NET.CodeGenerator.Generators.HandleBarsGenerators
3333
/// <summary>
3434
/// A Handlebars based DTO code generator
3535
/// </summary>
36-
public class DtoSerializerGenerator : HandleBarsGenerator
36+
public class DtoDeSerializerGenerator : HandleBarsGenerator
3737
{
3838
/// <summary>
3939
/// Generates the <see cref="EClass"/> static serializers
@@ -50,39 +50,63 @@ public class DtoSerializerGenerator : HandleBarsGenerator
5050
/// </returns>
5151
public override async Task Generate(EPackage package, DirectoryInfo outputDirectory)
5252
{
53-
await this.GenerateSerializers(package, outputDirectory);
54-
await this.GenerateSerializationProvider(package, outputDirectory);
53+
await this.GenerateDeSerializers(package, outputDirectory);
54+
await this.GenerateDeSerializationProvider(package, outputDirectory);
5555
}
5656

57-
public async Task GenerateSerializers(EPackage package, DirectoryInfo outputDirectory)
57+
/// <summary>
58+
/// Generates the DeSerializer classes for each <see cref="EClass"/> in the provided <see cref="EPackage"/>
59+
/// </summary>
60+
/// <param name="package">
61+
/// the <see cref="EPackage"/> that contains the classes that need to be generated
62+
/// </param>
63+
/// <param name="outputDirectory">
64+
/// The target <see cref="DirectoryInfo"/>
65+
/// </param>
66+
/// <returns>
67+
/// an awaitable <see cref="Task"/>
68+
/// </returns>
69+
public async Task GenerateDeSerializers(EPackage package, DirectoryInfo outputDirectory)
5870
{
59-
var template = this.Templates["dto-serializer-template"];
71+
var template = this.Templates["dto-deserializer-template"];
6072

61-
foreach (var eClass in package.EClassifiers.OfType<EClass>())
73+
foreach (var eClass in package.EClassifiers.OfType<EClass>().Where(x => !x.Abstract))
6274
{
6375
var generatedSerializer = template(eClass);
6476

6577
generatedSerializer = CodeCleanup(generatedSerializer);
6678

67-
var fileName = $"{eClass.Name.CapitalizeFirstLetter()}Serializer.cs";
79+
var fileName = $"{eClass.Name.CapitalizeFirstLetter()}DeSerializer.cs";
6880

6981
await Write(generatedSerializer, outputDirectory, fileName);
7082
}
7183
}
7284

73-
public async Task GenerateSerializationProvider(EPackage package, DirectoryInfo outputDirectory)
85+
/// <summary>
86+
/// Generates the DeSerializationProvider class
87+
/// </summary>
88+
/// <param name="package">
89+
/// the <see cref="EPackage"/> that contains the classes that need to be generated
90+
/// </param>
91+
/// <param name="outputDirectory">
92+
/// The target <see cref="DirectoryInfo"/>
93+
/// </param>
94+
/// <returns>
95+
/// an awaitable <see cref="Task"/>
96+
/// </returns>
97+
public async Task GenerateDeSerializationProvider(EPackage package, DirectoryInfo outputDirectory)
7498
{
75-
var template = this.Templates["dto-serialization-provider-template"];
99+
var template = this.Templates["dto-deserialization-provider-template"];
76100

77-
var eClasses = package.EClassifiers.OfType<EClass>().OrderBy(x => x.Name).ToList();
101+
var eClasses = package.EClassifiers.OfType<EClass>().Where(x => !x.Abstract).OrderBy(x => x.Name).ToList();
78102

79-
var generatedSerializationProvider = template(eClasses);
103+
var generatedDeSerializationProvider = template(eClasses);
80104

81-
generatedSerializationProvider = CodeCleanup(generatedSerializationProvider);
105+
generatedDeSerializationProvider = CodeCleanup(generatedDeSerializationProvider);
82106

83-
var fileName = "SerializationProvider.cs";
107+
var fileName = "DeSerializationProvider.cs";
84108

85-
await Write(generatedSerializationProvider, outputDirectory, fileName);
109+
await Write(generatedDeSerializationProvider, outputDirectory, fileName);
86110
}
87111

88112
/// <summary>
@@ -102,8 +126,8 @@ protected override void RegisterHelpers()
102126
/// </summary>
103127
protected override void RegisterTemplates()
104128
{
105-
this.RegisterTemplate("dto-serializer-template");
106-
this.RegisterTemplate("dto-serialization-provider-template");
129+
this.RegisterTemplate("dto-deserializer-template");
130+
this.RegisterTemplate("dto-deserialization-provider-template");
107131
}
108132
}
109133
}

0 commit comments

Comments
 (0)