@@ -89,11 +89,12 @@ public virtual void Write(AngularWriteConfiguration configuration)
89
89
. WithCode ( Code . This ( ) . Field ( httpField ) . Assign ( Code . Local ( "http" ) ) . Close ( ) )
90
90
. WithCode ( Code . This ( ) . Property ( serviceUrlProperty ) . Assign ( Code . Local ( "document" ) . Property ( "baseURI" ) . NullCoalescing ( Code . String ( string . Empty ) . Close ( ) ) ) ) ;
91
91
92
+ List < MethodTemplate > convertDateMethods = new ( ) ;
92
93
string relativeModelPath = FileSystem . RelativeTo ( configuration . Model ? . RelativePath ?? "." , configuration . Service . RelativePath ) ;
93
94
relativeModelPath = string . IsNullOrEmpty ( relativeModelPath ) ? "." : relativeModelPath ;
94
95
bool addAppendMethod = false ;
95
96
bool addAppendDateMethod = false ;
96
- bool appendConvertToDateMethod = false ;
97
+ bool appendConvertDateMethod = false ;
97
98
foreach ( HttpServiceActionTransferObject action in controller . Actions )
98
99
{
99
100
string subjectName = action . Parameters . Any ( x => x . Name == "subject" ) ? "rxjsSubject" : "subject" ;
@@ -154,7 +155,7 @@ public virtual void Write(AngularWriteConfiguration configuration)
154
155
}
155
156
if ( isDateReturnType && isDateArrayReturnType )
156
157
{
157
- appendConvertToDateMethod = true ;
158
+ appendConvertDateMethod = true ;
158
159
}
159
160
string controllerRoute = controller . Route ? . Trim ( '/' ) . Replace ( "[controller]" , controllerName . ToLower ( ) ) ?? controllerName . ToLower ( ) ;
160
161
string actionRoute = action . Route ? . TrimEnd ( '/' ) . Replace ( "[action]" , action . Name . ToLower ( ) ) . Replace ( "[controller]" , controllerName . ToLower ( ) ) ;
@@ -183,14 +184,28 @@ public virtual void Write(AngularWriteConfiguration configuration)
183
184
}
184
185
if ( isDateReturnType )
185
186
{
186
- nextCode = Code . This ( ) . Method ( "convertToDate " , nextCode ) ;
187
+ nextCode = Code . This ( ) . Method ( "convertDate " , nextCode ) ;
187
188
}
188
189
if ( isDateArrayReturnType )
189
190
{
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" ) ) ) ) ;
191
192
}
192
193
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
+ }
194
209
}
195
210
if ( action . FixCasingWithMapping )
196
211
{
@@ -354,10 +369,12 @@ public virtual void Write(AngularWriteConfiguration configuration)
354
369
{
355
370
this . AddAppendDateMethod ( classTemplate ) ;
356
371
}
357
- if ( appendConvertToDateMethod )
372
+ if ( appendConvertDateMethod )
358
373
{
359
- this . AppendConvertToDateMethod ( classTemplate ) ;
374
+ this . AppendConvertDateMethod ( classTemplate ) ;
360
375
}
376
+ classTemplate . Methods . RemoveRange ( convertDateMethods ) ;
377
+ classTemplate . Methods . AddRange ( convertDateMethods ) ;
361
378
this . AppendFixUndefined ( classTemplate ) ;
362
379
}
363
380
List < SignalRHubTransferObject > hubs = this . transferObjects . OfType < SignalRHubTransferObject > ( ) . ToList ( ) ;
@@ -585,90 +602,83 @@ public virtual void Write(AngularWriteConfiguration configuration)
585
602
}
586
603
}
587
604
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 )
589
606
{
590
607
if ( model == null )
591
608
{
592
609
return false ;
593
610
}
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 ) ) )
598
613
{
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 ;
612
615
}
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 ) ;
613
621
foreach ( PropertyTransferObject property in model . Properties )
614
622
{
615
623
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
- }
620
624
IOptions propertyOptions = this . Options . Get ( property ) ;
621
625
string propertyName = Formatter . FormatProperty ( property . Name , propertyOptions ) ;
622
626
if ( type . IgnoreNullable ( ) . OriginalName == nameof ( DateTime ) )
623
627
{
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 ) ) ) ;
628
630
ICodeFragment lineTemplate ;
629
631
if ( property . IsOptional || property . Type . IsNullable )
630
632
{
631
633
lineTemplate = assignTemplate . Close ( ) ;
632
634
}
633
635
else
634
636
{
635
- lineTemplate = assignTemplate . NullCoalescing ( this . WriteFieldChain ( chain ) . Field ( propertyName ) . Close ( ) ) ;
637
+ lineTemplate = assignTemplate . NullCoalescing ( Code . Local ( propertyName ) . Close ( ) ) ;
636
638
}
637
- innerCode . AddLine ( lineTemplate ) ;
639
+ convertDateMethodTemplate . WithCode ( lineTemplate ) ;
638
640
}
639
641
ModelTransferObject propertyModel = type as ModelTransferObject ;
640
642
TypeTransferObject entryType = propertyModel ? . Generics . FirstOrDefault ( ) ? . Type ;
641
643
entryType = entryType == null ? null : model . Generics . FirstOrDefault ( generic => generic . Alias ? . Name == entryType . Name ) ? . Type ?? entryType ;
642
644
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 ) ;
647
645
if ( propertyModel != null && propertyModel . IsEnumerable ( ) && entryModel != null )
648
646
{
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
+ }
650
664
}
651
665
else if ( propertyModel != null && propertyModel . Properties . Count > 0 )
652
666
{
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
+ }
654
673
}
655
674
}
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 )
668
676
{
669
- code = code . Field ( field ) ;
677
+ return true ;
670
678
}
671
- return code ;
679
+ convertDateMethods . Remove ( convertDateMethodTemplate ) ;
680
+ classTemplate . Methods . Remove ( convertDateMethodTemplate ) ;
681
+ return false ;
672
682
}
673
683
674
684
private void AddAppendMethod ( ClassTemplate classTemplate )
@@ -717,9 +727,9 @@ private void AddAppendDateMethod(ClassTemplate classTemplate)
717
727
. WithCode ( Code . Return ( Code . This ( ) . Method ( "append" , Code . Local ( "url" ) , code , Code . Local ( "parameterName" ) , Code . Local ( "separator" ) ) ) ) ;
718
728
}
719
729
720
- private void AppendConvertToDateMethod ( ClassTemplate classTemplate )
730
+ private void AppendConvertDateMethod ( ClassTemplate classTemplate )
721
731
{
722
- classTemplate . AddMethod ( "convertToDate " , Code . Type ( "Date | undefined" ) ) . Private ( )
732
+ classTemplate . AddMethod ( "convertDate " , Code . Type ( "Date | undefined" ) ) . Private ( )
723
733
. WithParameter ( Code . Type ( "string | Date | undefined" ) , "value" )
724
734
. WithCode ( Code . Return ( Code . InlineIf ( Code . Local ( "value" ) . Equals ( ) . String ( "0001-01-01T00:00:00" ) ,
725
735
Code . New ( Code . Type ( "Date" ) , Code . String ( "0001-01-01T00:00:00Z" ) ) ,
0 commit comments