Skip to content

Commit dc2eceb

Browse files
committed
ASP.NET/Angular
- handling of unknown results (e.g. object) improved Common - generic method support added
1 parent 6bf7342 commit dc2eceb

File tree

12 files changed

+98
-6
lines changed

12 files changed

+98
-6
lines changed

Angular/Writers/AngularServiceWriter.cs

+10-1
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,18 @@ public virtual void Write(AngularWriteConfiguration configuration, List<ITransfe
103103
TypeTransferObject returnModelType = isEnumerable ? action.ReturnType.Generics.First().Type : action.ReturnType;
104104
ModelTransferObject returnModel = returnModelType as ModelTransferObject ?? transferObjects.OfType<ModelTransferObject>().FirstOrDefault(x => x.Equals(returnModelType));
105105
this.AddUsing(action.ReturnType, classTemplate, configuration, relativeModelPath);
106+
TypeTemplate aliasType = null;
107+
if (returnType.Name == "unknown")
108+
{
109+
aliasType = Code.Type("TDefault");
110+
returnType = aliasType;
111+
}
106112
MethodTemplate methodTemplate = classTemplate.AddMethod(action.Name, Code.Generic("Observable", returnType))
107113
.FormatName(configuration);
114+
if (aliasType != null)
115+
{
116+
methodTemplate.WithGeneric(aliasType.Name, Code.Type("unknown"));
117+
}
108118
TypeTemplate subjectType = Code.Generic("Subject", returnType);
109119
methodTemplate.WithCode(Code.Declare(subjectType, subjectName, Code.New(subjectType)));
110120
foreach (HttpServiceActionParameterTransferObject parameter in action.Parameters)
@@ -269,7 +279,6 @@ public virtual void Write(AngularWriteConfiguration configuration, List<ITransfe
269279
{
270280
parameters.Add(Code.Local(action.Parameters.Single(x => x.FromBody).Name));
271281
}
272-
parameters.AddRange(inlineParameters.Concat(urlDirectParameters).Concat(urlParameters).Select(parameter => Code.Local(parameter.Name)));
273282
if (actionTypeOptions[action.Type]?.HasHttpOptions ?? false)
274283
{
275284
parameters.Add(Code.Local("httpOptions"));

Core/Languages/BaseLanguage.cs

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ protected BaseLanguage()
7070
this.TemplateWriters.Add(typeof(LocalVariableTemplate), new LocalVariableWriter());
7171
this.TemplateWriters.Add(typeof(MethodTemplate), new MethodWriter());
7272
this.TemplateWriters.Add(typeof(ExtensionMethodTemplate), new MethodWriter());
73+
this.TemplateWriters.Add(typeof(MethodGenericTemplate), new MethodGenericWriter());
7374
this.TemplateWriters.Add(typeof(MultilineCodeFragment), new MultilineCodeFragmentWriter(this));
7475
this.TemplateWriters.Add(typeof(NamespaceTemplate), new NamespaceWriter(this));
7576
this.TemplateWriters.Add(typeof(NewTemplate), new NewWriter());

Core/Templates/Extensions/MethodTemplateExtension.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ public static MethodTemplate Override(this MethodTemplate methodTemplate, bool v
3838
return methodTemplate;
3939
}
4040

41+
public static MethodTemplate WithGeneric(this MethodTemplate methodTemplate, string alias, TypeTemplate defaultType = null)
42+
{
43+
methodTemplate.Generics.Add(new MethodGenericTemplate(alias, defaultType));
44+
return methodTemplate;
45+
}
46+
4147
public static ParameterTemplate AddParameter(this MethodTemplate methodTemplate, TypeTemplate type, string name, ICodeFragment defaultValue = null)
4248
{
4349
var parameter = new ParameterTemplate(type, name, defaultValue);
@@ -87,4 +93,4 @@ public static MethodTemplate FormatName(this MethodTemplate methodTemplate, ILan
8793
return methodTemplate;
8894
}
8995
}
90-
}
96+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace KY.Generator.Templates
2+
{
3+
public class MethodGenericTemplate : ICodeFragment
4+
{
5+
public string Alias { get; set; }
6+
public TypeTemplate DefaultType { get; set; }
7+
8+
public MethodGenericTemplate(string alias, TypeTemplate defaultType = null)
9+
{
10+
this.Alias = alias;
11+
this.DefaultType = defaultType;
12+
}
13+
}
14+
}

Core/Templates/Structural/MethodTemplate.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class MethodTemplate : AttributeableTempalte
1616
public MultilineCodeFragment Code { get; }
1717
public ClassTemplate Class { get; }
1818
public CommentTemplate Comment { get; set; }
19+
public List<MethodGenericTemplate> Generics { get; set; }
1920

2021
public MethodTemplate(ClassTemplate classTemplate, string name, TypeTemplate type)
2122
{
@@ -25,6 +26,7 @@ public MethodTemplate(ClassTemplate classTemplate, string name, TypeTemplate typ
2526
this.Visibility = Visibility.Public;
2627
this.Parameters = new List<ParameterTemplate>();
2728
this.Code = new MultilineCodeFragment();
29+
this.Generics = new List<MethodGenericTemplate>();
2830
}
2931
}
30-
}
32+
}

Core/Writers/MethodGenericWriter.cs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using KY.Generator.Output;
2+
using KY.Generator.Templates;
3+
4+
namespace KY.Generator.Writers
5+
{
6+
public class MethodGenericWriter : ITemplateWriter
7+
{
8+
public void Write(ICodeFragment fragment, IOutputCache output)
9+
{
10+
MethodGenericTemplate template = (MethodGenericTemplate)fragment;
11+
output.Add(template.Alias)
12+
.If(template.DefaultType != null).Add(" = ").Add(template.DefaultType).EndIf();
13+
}
14+
}
15+
}

Core/Writers/MethodWriter.cs

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Linq;
1+
using System;
2+
using System.Linq;
23
using KY.Generator.Output;
34
using KY.Generator.Templates;
45

@@ -9,13 +10,18 @@ public class MethodWriter : ITemplateWriter
910
public virtual void Write(ICodeFragment fragment, IOutputCache output)
1011
{
1112
MethodTemplate template = (MethodTemplate)fragment;
13+
if (template.Generics != null && template.Generics.Any(x => x.DefaultType != null))
14+
{
15+
throw new InvalidOperationException($"This language does not support default types for generic methods. {template.Class.Name}.{template.Name}");
16+
}
1217
output.Add(template.Comment)
1318
.Add(template.Attributes)
1419
.Add(template.Visibility.ToString().ToLower()).Add(" ")
1520
.If(template.IsStatic).Add("static ").EndIf()
1621
.If(template.IsOverride).Add("override ").EndIf()
1722
.If(template.Type != null).Add(template.Type).Add(" ").EndIf()
1823
.Add(template.Name)
24+
.If(template.Generics != null && template.Generics.Count > 0).Add("<").Add(template.Generics, ", ").Add(">").EndIf()
1925
.Add("(")
2026
.If(template is ExtensionMethodTemplate).Add("this ").EndIf()
2127
.Add(template.Parameters.OrderBy(x => x.DefaultValue == null ? 0 : 1), ", ")
@@ -29,4 +35,4 @@ public virtual void Write(ICodeFragment fragment, IOutputCache output)
2935
protected virtual void BeforeBlock(ICodeFragment fragment, IOutputCache output)
3036
{ }
3137
}
32-
}
38+
}

Tests/WebApiController/ClientApp/src/app/edge-cases/components/edge-cases.component.html

+6
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,9 @@
1313
<button (click)="withHeader()">With Header</button>
1414
<span>{{headerResult}}</span>
1515
</div>
16+
17+
<div>
18+
<input [(ngModel)]="unknownValue">
19+
<button (click)="getUnknown()">Get Unknown</button>
20+
<span>{{unknownResult}}</span>
21+
</div>

Tests/WebApiController/ClientApp/src/app/edge-cases/components/edge-cases.component.ts

+7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export class EdgeCasesComponent implements OnInit {
1111
public guid: string;
1212
public headerValue: string;
1313
public headerResult: string;
14+
public unknownValue: string;
15+
public unknownResult: unknown;
1416

1517
constructor(
1618
private readonly service: EdgeCasesService
@@ -35,4 +37,9 @@ export class EdgeCasesComponent implements OnInit {
3537
const httpOptions = { headers: { 'headerValue': this.headerValue || '' } };
3638
this.service.fromHeader(value, httpOptions).subscribe(result => this.headerResult = result, error => this.headerResult = error.message);
3739
}
40+
41+
public getUnknown(): void {
42+
this.unknownResult = 'Loading...';
43+
this.service.unknownResult(this.unknownValue).subscribe(result => this.unknownResult = (result as {value: string}).value);
44+
}
3845
}

Tests/WebApiController/ClientApp/src/app/edge-cases/services/edge-cases.service.ts

+9
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,15 @@ export class EdgeCasesService {
130130
return subject;
131131
}
132132

133+
public unknownResult<TDefault = unknown>(value: string, httpOptions?: {}): Observable<TDefault> {
134+
let subject = new Subject<TDefault>();
135+
this.http.get<TDefault>(this.serviceUrl + "/edgecases/unknownresult" + "?value=" + this.convertAny(value), httpOptions).subscribe((result) => {
136+
subject.next(result);
137+
subject.complete();
138+
}, (error) => subject.error(error));
139+
return subject;
140+
}
141+
133142
public convertAny(value: any): string {
134143
return value === null || value === undefined ? "" : value.toString();
135144
}

Tests/WebApiController/Controllers/EdgeCasesController.cs

+16
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,24 @@ public GenericResult<ExclusiveGenericComplexResult> GenericComplexResult()
7676
{
7777
return new GenericResult<ExclusiveGenericComplexResult>(new List<ExclusiveGenericComplexResult> { new ExclusiveGenericComplexResult() });
7878
}
79+
80+
[HttpGet("[action]")]
81+
public object UnknownResult(string value)
82+
{
83+
return new UnknownResult(value);
84+
}
7985
}
8086

8187
public class ExclusiveGenericComplexResult
8288
{ }
89+
90+
class UnknownResult
91+
{
92+
public string Value { get; }
93+
94+
public UnknownResult(string value)
95+
{
96+
this.Value = value;
97+
}
98+
}
8399
}

TypeScript/Writers/TypeScriptMethodWriter.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public virtual void Write(ICodeFragment fragment, IOutputCache output)
1717
.If(template.IsStatic).Add("static ").EndIf()
1818
.If(template.IsOverride).Add("override ").EndIf()
1919
.Add(template.Name)
20+
.If(template.Generics != null && template.Generics.Count > 0).Add("<").Add(template.Generics, ", ").Add(">").EndIf()
2021
.Add("(")
2122
.Add(template.Parameters.OrderBy(x => x.DefaultValue == null ? 0 : 1), ", ")
2223
.Add(")")
@@ -26,4 +27,4 @@ public virtual void Write(ICodeFragment fragment, IOutputCache output)
2627
.EndBlock();
2728
}
2829
}
29-
}
30+
}

0 commit comments

Comments
 (0)