Skip to content

Commit 78e613b

Browse files
committed
Angular / ASP.NET
- fixed DefaultValues with empty generic like List<> - fixed self referencing data structures
1 parent 2cd05ca commit 78e613b

30 files changed

+281
-211
lines changed

Angular/KY.Generator.Angular.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<Authors>KY-Programming</Authors>
66
<Company>KY-Programming</Company>
77
<Product>KY.Generator</Product>
8-
<Version>9.0.0-preview.24</Version>
8+
<Version>9.0.0-preview.28</Version>
99
<Copyright>2023 - KY-Programming</Copyright>
1010
<Description>Angular Module for KY-Generator
1111
Download KY.Generator to use this module</Description>

Angular/Writers/AngularServiceWriter.cs

Lines changed: 67 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,12 @@ public virtual void Write(AngularWriteConfiguration configuration)
8989
.WithCode(Code.This().Field(httpField).Assign(Code.Local("http")).Close())
9090
.WithCode(Code.This().Property(serviceUrlProperty).Assign(Code.Local("document").Property("baseURI").NullCoalescing(Code.String(string.Empty).Close())));
9191

92+
List<MethodTemplate> convertDateMethods = new();
9293
string relativeModelPath = FileSystem.RelativeTo(configuration.Model?.RelativePath ?? ".", configuration.Service.RelativePath);
9394
relativeModelPath = string.IsNullOrEmpty(relativeModelPath) ? "." : relativeModelPath;
9495
bool addAppendMethod = false;
9596
bool addAppendDateMethod = false;
96-
bool appendConvertToDateMethod = false;
97+
bool appendConvertDateMethod = false;
9798
foreach (HttpServiceActionTransferObject action in controller.Actions)
9899
{
99100
string subjectName = action.Parameters.Any(x => x.Name == "subject") ? "rxjsSubject" : "subject";
@@ -154,7 +155,7 @@ public virtual void Write(AngularWriteConfiguration configuration)
154155
}
155156
if (isDateReturnType && isDateArrayReturnType)
156157
{
157-
appendConvertToDateMethod = true;
158+
appendConvertDateMethod = true;
158159
}
159160
string controllerRoute = controller.Route?.Trim('/').Replace("[controller]", controllerName.ToLower()) ?? controllerName.ToLower();
160161
string actionRoute = action.Route?.TrimEnd('/').Replace("[action]", action.Name.ToLower()).Replace("[controller]", controllerName.ToLower());
@@ -183,14 +184,28 @@ public virtual void Write(AngularWriteConfiguration configuration)
183184
}
184185
if (isDateReturnType)
185186
{
186-
nextCode = Code.This().Method("convertToDate", nextCode);
187+
nextCode = Code.This().Method("convertDate", nextCode);
187188
}
188189
if (isDateArrayReturnType)
189190
{
190-
nextCode = localCode.Method("map", Code.Lambda("entry", Code.This().Method("convertToDate", Code.Local("entry"))));
191+
nextCode = localCode.Method("map", Code.Lambda("entry", Code.This().Method("convertDate", Code.Local("entry"))));
191192
}
192193
nextMethod.WithParameter(Code.This().Method("fixUndefined", nextCode));
193-
appendConvertToDateMethod = this.WriteDateFixes(returnModel, isEnumerable, code, new List<string> { "result" }, new List<TypeTransferObject> { returnModel }) || appendConvertToDateMethod;
194+
if (this.WriteDateFixes(classTemplate, convertDateMethods, returnModel))
195+
{
196+
appendConvertDateMethod = true;
197+
string methodName = $"convert{returnModel.Name.ToPascalCase()}Date";
198+
if (isEnumerable)
199+
{
200+
code.AddLine(Code.Local("result").Method("forEach", Code.Lambda("m",
201+
Code.This().Method(methodName, Code.Local("m"))
202+
)).Close());
203+
}
204+
else
205+
{
206+
code.AddLine(Code.This().Method(methodName, Code.Local("result")).Close());
207+
}
208+
}
194209
}
195210
if (action.FixCasingWithMapping)
196211
{
@@ -354,10 +369,12 @@ public virtual void Write(AngularWriteConfiguration configuration)
354369
{
355370
this.AddAppendDateMethod(classTemplate);
356371
}
357-
if (appendConvertToDateMethod)
372+
if (appendConvertDateMethod)
358373
{
359-
this.AppendConvertToDateMethod(classTemplate);
374+
this.AppendConvertDateMethod(classTemplate);
360375
}
376+
classTemplate.Methods.RemoveRange(convertDateMethods);
377+
classTemplate.Methods.AddRange(convertDateMethods);
361378
this.AppendFixUndefined(classTemplate);
362379
}
363380
List<SignalRHubTransferObject> hubs = this.transferObjects.OfType<SignalRHubTransferObject>().ToList();
@@ -585,90 +602,83 @@ public virtual void Write(AngularWriteConfiguration configuration)
585602
}
586603
}
587604

588-
private bool WriteDateFixes(ModelTransferObject model, bool isModelEnumerable, MultilineCodeFragment code, IReadOnlyList<string> chain, IReadOnlyList<TypeTransferObject> typeChain)
605+
private bool WriteDateFixes(ClassTemplate classTemplate, List<MethodTemplate> convertDateMethods, ModelTransferObject model)
589606
{
590607
if (model == null)
591608
{
592609
return false;
593610
}
594-
bool datePropertyFound = false;
595-
IfTemplate ifTemplate = Code.If(this.WriteFieldChain(chain));
596-
MultilineCodeFragment innerCode = ifTemplate.Code;
597-
if (isModelEnumerable)
611+
string methodName = $"convert{model.Name.ToPascalCase()}Date";
612+
if (convertDateMethods.Any(x => x.Name.Equals(methodName, StringComparison.InvariantCultureIgnoreCase)))
598613
{
599-
innerCode = Code.Multiline();
600-
datePropertyFound = model.OriginalName == nameof(DateTime);
601-
if (datePropertyFound)
602-
{
603-
ifTemplate.WithCode(this.WriteFieldChain(chain).Assign(this.WriteFieldChain(chain).Method("map", Code.Lambda("entry",
604-
Code.TypeScript("this.convertToDate(entry)!")
605-
))));
606-
}
607-
else
608-
{
609-
ifTemplate.WithCode(this.WriteFieldChain(chain).Method("forEach", Code.Lambda("entry", innerCode)).Close());
610-
}
611-
chain = new List<string> { "entry" };
614+
return true;
612615
}
616+
bool hasLocalDateProperty = false;
617+
MethodTemplate convertDateMethodTemplate = classTemplate.AddMethod(methodName, Code.Void()).Private()
618+
.WithParameter(model.ToTemplate(), "model")
619+
.WithCode(Code.If(Code.Local("!model")).WithCode(Code.Return()));
620+
convertDateMethods.Add(convertDateMethodTemplate);
613621
foreach (PropertyTransferObject property in model.Properties)
614622
{
615623
TypeTransferObject type = model.Generics.FirstOrDefault(generic => generic.Alias?.Name == property.Name)?.Type ?? property.Type;
616-
if (type.Name != "Array" && typeChain.Any(typeFromChain => typeFromChain.Name == property.Type.Name && typeFromChain.Namespace == property.Type.Namespace))
617-
{
618-
continue;
619-
}
620624
IOptions propertyOptions = this.Options.Get(property);
621625
string propertyName = Formatter.FormatProperty(property.Name, propertyOptions);
622626
if (type.IgnoreNullable().OriginalName == nameof(DateTime))
623627
{
624-
datePropertyFound = true;
625-
AssignTemplate assignTemplate = isModelEnumerable
626-
? Code.Local("entry").Field(propertyName).Assign(Code.This().Method("convertToDate", Code.Local("entry").Field(propertyName)))
627-
: this.WriteFieldChain(chain).Field(propertyName).Assign(Code.This().Method("convertToDate", this.WriteFieldChain(chain).Field(propertyName)));
628+
hasLocalDateProperty = true;
629+
AssignTemplate assignTemplate = Code.Local("model").Field(propertyName).Assign(Code.This().Method("convertDate", Code.Local("model").Field(propertyName)));
628630
ICodeFragment lineTemplate;
629631
if (property.IsOptional || property.Type.IsNullable)
630632
{
631633
lineTemplate = assignTemplate.Close();
632634
}
633635
else
634636
{
635-
lineTemplate = assignTemplate.NullCoalescing(this.WriteFieldChain(chain).Field(propertyName).Close());
637+
lineTemplate = assignTemplate.NullCoalescing(Code.Local(propertyName).Close());
636638
}
637-
innerCode.AddLine(lineTemplate);
639+
convertDateMethodTemplate.WithCode(lineTemplate);
638640
}
639641
ModelTransferObject propertyModel = type as ModelTransferObject;
640642
TypeTransferObject entryType = propertyModel?.Generics.FirstOrDefault()?.Type;
641643
entryType = entryType == null ? null : model.Generics.FirstOrDefault(generic => generic.Alias?.Name == entryType.Name)?.Type ?? entryType;
642644
ModelTransferObject entryModel = entryType as ModelTransferObject ?? this.transferObjects.OfType<ModelTransferObject>().FirstOrDefault(x => x.Name == entryType?.Name && x.Namespace == entryType?.Namespace);
643-
List<string> nextChain = new(chain);
644-
nextChain.Add(propertyName);
645-
List<TypeTransferObject> nextTypeChain = new(typeChain);
646-
nextTypeChain.Add(type);
647645
if (propertyModel != null && propertyModel.IsEnumerable() && entryModel != null)
648646
{
649-
datePropertyFound = this.WriteDateFixes(entryModel, true, innerCode, nextChain, nextTypeChain) || datePropertyFound;
647+
if (entryModel.OriginalName == nameof(DateTime))
648+
{
649+
hasLocalDateProperty = true;
650+
convertDateMethodTemplate.WithCode(Code.Local("model").Field(propertyName).Assign(
651+
Code.Local("model").Field(propertyName).Method("map", Code.Lambda("m",
652+
Code.This().Method("convertDate", Code.Local("m"))
653+
)))
654+
);
655+
}
656+
else if (this.WriteDateFixes(classTemplate, convertDateMethods, entryModel))
657+
{
658+
hasLocalDateProperty = true;
659+
GenericAliasTransferObject aliasedType = model.Generics.FirstOrDefault(x => x.Alias.Name == property.Type.Generics.Single().Alias.Name)
660+
?? property.Type.Generics.Single();
661+
string convertMethodName = $"convert{aliasedType.Type.Name.ToPascalCase()}Date";
662+
convertDateMethodTemplate.WithCode(Code.Local("model").Field(propertyName + "?").Method("forEach", Code.Lambda("m", Code.This().Method(convertMethodName, Code.Local("m")))));
663+
}
650664
}
651665
else if (propertyModel != null && propertyModel.Properties.Count > 0)
652666
{
653-
datePropertyFound = this.WriteDateFixes(propertyModel, false, innerCode, nextChain, nextTypeChain) || datePropertyFound;
667+
if (this.WriteDateFixes(classTemplate, convertDateMethods, propertyModel))
668+
{
669+
hasLocalDateProperty = true;
670+
string convertMethodName = $"convert{property.Type.Name.ToPascalCase()}Date";
671+
convertDateMethodTemplate.WithCode(Code.This().Method(convertMethodName, Code.Local("model").Field(propertyName)));
672+
}
654673
}
655674
}
656-
if (datePropertyFound)
657-
{
658-
code.AddLine(ifTemplate);
659-
}
660-
return datePropertyFound;
661-
}
662-
663-
private ChainedCodeFragment WriteFieldChain(IEnumerable<string> chain)
664-
{
665-
List<string> chainList = chain.ToList();
666-
ChainedCodeFragment code = Code.Local(chainList.First());
667-
foreach (string field in chainList.Skip(1))
675+
if (hasLocalDateProperty)
668676
{
669-
code = code.Field(field);
677+
return true;
670678
}
671-
return code;
679+
convertDateMethods.Remove(convertDateMethodTemplate);
680+
classTemplate.Methods.Remove(convertDateMethodTemplate);
681+
return false;
672682
}
673683

674684
private void AddAppendMethod(ClassTemplate classTemplate)
@@ -717,9 +727,9 @@ private void AddAppendDateMethod(ClassTemplate classTemplate)
717727
.WithCode(Code.Return(Code.This().Method("append", Code.Local("url"), code, Code.Local("parameterName"), Code.Local("separator"))));
718728
}
719729

720-
private void AppendConvertToDateMethod(ClassTemplate classTemplate)
730+
private void AppendConvertDateMethod(ClassTemplate classTemplate)
721731
{
722-
classTemplate.AddMethod("convertToDate", Code.Type("Date | undefined")).Private()
732+
classTemplate.AddMethod("convertDate", Code.Type("Date | undefined")).Private()
723733
.WithParameter(Code.Type("string | Date | undefined"), "value")
724734
.WithCode(Code.Return(Code.InlineIf(Code.Local("value").Equals().String("0001-01-01T00:00:00"),
725735
Code.New(Code.Type("Date"), Code.String("0001-01-01T00:00:00Z")),

Annotations/KY.Generator.Annotations.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<Company>KY-Programming</Company>
66
<Authors>KY-Programming</Authors>
7-
<Version>9.0.0-preview.24</Version>
7+
<Version>9.0.0-preview.28</Version>
88
<Product>KY.Generator</Product>
99
<Description>Annotations for KY-Generator</Description>
1010
<Copyright>2023 - KY-Programming</Copyright>

AspDotNet/KY.Generator.AspDotNet.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<Company>KY-Programming</Company>
66
<Authors>KY-Programming</Authors>
7-
<Version>9.0.0-preview.24</Version>
7+
<Version>9.0.0-preview.28</Version>
88
<Product>KY.Generator</Product>
99
<Description>ASP.net Module for KY-Generator
1010
Download KY.Generator to use this module</Description>

CLI/nuget.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<metadata>
44
<id>KY.Generator.CLI</id>
55
<!-- Ensure nuget.targets version is also updated -->
6-
<version>9.0.0-preview.24</version>
6+
<version>9.0.0-preview.28</version>
77
<title>KY.Generator.CLI</title>
88
<authors>KY-Programming</authors>
99
<owners>KY-Programming</owners>

Common/KY.Generator.Common.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<Company>KY-Programming</Company>
66
<Authors>KY-Programming</Authors>
77
<Product>KY.Generator</Product>
8-
<Version>9.0.0-preview.24</Version>
8+
<Version>9.0.0-preview.28</Version>
99
<Description>Core elements for KY-Generator
1010
Download KY.Generator to use this module</Description>
1111
<Copyright>2023 - KY-Programming</Copyright>

Csharp/KY.Generator.CSharp.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<Authors>KY-Programming</Authors>
66
<Company>KY-Programming</Company>
77
<Product>KY.Generator</Product>
8-
<Version>9.0.0-preview.24</Version>
8+
<Version>9.0.0-preview.28</Version>
99
<Description>C# Module for KY-Generator
1010
Download KY.Generator to use this module</Description>
1111
<Copyright>2023 - KY-Programming</Copyright>

Fluent/KY.Generator.Fluent.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<Company>KY-Programming</Company>
66
<Authors>KY-Programming</Authors>
7-
<Version>9.0.0-preview.24</Version>
7+
<Version>9.0.0-preview.28</Version>
88
<Product>KY.Generator</Product>
99
<Description>Fluent language for KY-Generator</Description>
1010
<Copyright>2023 - KY-Programming</Copyright>

Fluent/nuget.nuspec

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<package>
44
<metadata>
55
<id>KY.Generator.Fluent</id>
6-
<version>9.0.0-preview.24</version>
6+
<version>9.0.0-preview.28</version>
77
<title>KY.Generator</title>
88
<authors>KY-Programming</authors>
99
<owners>KY-Programming</owners>
@@ -17,8 +17,8 @@
1717
<copyright>Copyright 2022</copyright>
1818
<tags>KY-Generator KY.Generator Fluent Language</tags>
1919
<dependencies>
20-
<dependency id="KY.Generator" version="9.0.0-preview.24"></dependency>
21-
<dependency id="KY.Generator.Common" version="9.0.0-preview.24"></dependency>
20+
<dependency id="KY.Generator" version="9.0.0-preview.28"></dependency>
21+
<dependency id="KY.Generator.Common" version="9.0.0-preview.28"></dependency>
2222
</dependencies>
2323
</metadata>
2424
<files>

Json/KY.Generator.Json.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<Company>KY-Programming</Company>
66
<Authors>KY-Programming</Authors>
7-
<Version>9.0.0-preview.24</Version>
7+
<Version>9.0.0-preview.28</Version>
88
<Product>KY.Generator</Product>
99
<Description>JSON Module for KY-Generator
1010
Download KY.Generator to use this module</Description>

0 commit comments

Comments
 (0)