Skip to content

Commit e018148

Browse files
author
Robert Anderson
committed
Function overload implementation for CSharp
1 parent e653d80 commit e018148

File tree

6 files changed

+148
-527
lines changed

6 files changed

+148
-527
lines changed

Templates/CSharp/Base/EntityRequestBuilder.Base.template.tt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,15 @@ public string GetStreamProperties(OdcmClass entity)
265265
// Creates the set of property definitions for OData method properties on the entity
266266
public string GetMethodProperties(OdcmClass entity)
267267
{
268+
var allMethods = new List<OdcmMethod>();
269+
foreach (var method in entity.Methods)
270+
{
271+
allMethods.Add(method);
272+
allMethods.AddRange(method.Overloads);
273+
}
274+
268275
var methodPropertiesStringBuilder = new StringBuilder();
269-
foreach(var method in entity.Methods)
276+
foreach(var method in allMethods)
270277
{
271278
var methodName = this.GetMethodName(method);
272279
var baseName = string.Concat(this.GetEntityNameString(method.Class), methodName);

Templates/CSharp/Base/IEntityRequestBuilder.Base.template.tt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,15 @@ public string GetStreamProperties(OdcmClass entity)
212212
// Creates the set of property definitions for OData method properties on the entity
213213
public string GetMethodProperties(OdcmClass entity)
214214
{
215+
var allMethods = new List<OdcmMethod>();
216+
foreach (var method in entity.Methods)
217+
{
218+
allMethods.Add(method);
219+
allMethods.AddRange(method.Overloads);
220+
}
221+
215222
var methodPropertiesStringBuilder = new StringBuilder();
216-
foreach(var method in entity.Methods)
223+
foreach(var method in allMethods)
217224
{
218225
var methodName = this.GetMethodName(method);
219226
var baseName = string.Concat(this.GetEntityNameString(method.Class), methodName);

Templates/CSharp/Model/MethodRequestBody.cs.tt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
var method = host.CurrentType.AsOdcmMethod();
77
var entityName = method.Class.Name.ToCheckedCase();
88
var requestBody = entityName + method.Name.Substring(method.Name.IndexOf('.') + 1).ToCheckedCase() + "RequestBody";
9+
10+
// These types of request bodies are only used for OData actions, which when containing parameters,
11+
// will always result in POST calls. The OData spec is explicit in saying that overload methods bound
12+
// to other types are explicitly not allowed. Therefore, any overload methods found here are invalid
13+
// and violate the spec.
14+
System.Diagnostics.Debug.Assert(!method.Overloads.Any(), "Overload actions are not allowed in OData services");
15+
916
#>
1017

1118
namespace <#=method.Namespace.GetNamespaceName()#>

Templates/CSharp/Requests/IMethodRequestBuilder.cs.tt

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,6 @@ namespace <#=method.Namespace.GetNamespaceName()#>
1919
/// </summary>
2020
public partial interface I<#=requestBuilderType#>
2121
{
22-
<#
23-
foreach (var param in method.Parameters)
24-
{
25-
var paramTypeString = param.Type.GetTypeString();
26-
27-
if (param.IsCollection)
28-
{
29-
paramTypeString = string.Format("IEnumerable<{0}>", paramTypeString);
30-
}
31-
else if (!param.Type.IsTypeNullable() && param.IsNullable)
32-
{
33-
paramTypeString = string.Format("{0}?", paramTypeString);
34-
}
35-
#>
36-
37-
/// <summary>
38-
/// Gets the <#=param.Name.ToCheckedCase()#>.
39-
/// </summary>
40-
<#=paramTypeString#> <#=param.Name.ToCheckedCase()#> { get; }
41-
<#
42-
}
43-
#>
44-
4522
/// <summary>
4623
/// Builds the request.
4724
/// </summary>

Templates/CSharp/Requests/MethodRequest.cs.tt

Lines changed: 43 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -58,104 +58,28 @@ namespace <#=method.Namespace.GetNamespaceName()#>
5858
/// </summary>
5959
public partial class <#=requestType#> : BaseRequest, I<#=requestType#>
6060
{
61-
<#
62-
var paramStringBuilder = new System.Text.StringBuilder();
63-
var requestBodyInitializerBuilder = new System.Text.StringBuilder();
64-
65-
paramStringBuilder.Append(Environment.NewLine);
66-
paramStringBuilder.Append(" ");
67-
paramStringBuilder.Append("string requestUrl,");
68-
paramStringBuilder.Append(Environment.NewLine);
69-
paramStringBuilder.Append(" ");
70-
paramStringBuilder.Append("IBaseClient client,");
71-
paramStringBuilder.Append(Environment.NewLine);
72-
paramStringBuilder.Append(" ");
73-
paramStringBuilder.Append("IEnumerable<Option> options");
74-
75-
requestBodyInitializerBuilder.Append("this.Method = \"" + httpMethod + "\";");
76-
77-
// Sort the method parameters by required then optional to match the ordering in request builder initialization.
78-
var methodParameters = new List<OdcmParameter>();
79-
methodParameters.AddRange(method.Parameters.Where(param => !param.IsNullable));
80-
methodParameters.AddRange(method.Parameters.Where(param => param.IsNullable));
81-
82-
if (includeRequestBody)
83-
{
84-
paramStringBuilder.Append(",");
85-
86-
requestBodyInitializerBuilder.Append(Environment.NewLine);
87-
requestBodyInitializerBuilder.Append(" ");
88-
requestBodyInitializerBuilder.Append("this.ContentType = \"application/json\";");
89-
requestBodyInitializerBuilder.Append(Environment.NewLine);
90-
requestBodyInitializerBuilder.Append(" ");
91-
requestBodyInitializerBuilder.Append("this.RequestBody = new " + requestType + "Body();");
92-
93-
foreach (var param in methodParameters)
94-
{
95-
var paramTypeString = param.Type.GetTypeString();
96-
97-
if (param.IsCollection)
98-
{
99-
paramTypeString = string.Format("IEnumerable<{0}>", paramTypeString);
100-
}
101-
else if (!param.Type.IsTypeNullable())
102-
{
103-
paramTypeString = string.Format("{0}?", paramTypeString);
104-
}
105-
106-
paramStringBuilder.Append(Environment.NewLine);
107-
paramStringBuilder.Append(" ");
108-
109-
var paramLowerChar = param.Name.ToLowerFirstChar();
110-
111-
paramStringBuilder.AppendFormat("{0} {1} = null,", paramTypeString, paramLowerChar);
112-
113-
requestBodyInitializerBuilder.Append(Environment.NewLine);
114-
requestBodyInitializerBuilder.Append(" ");
115-
116-
if (!param.IsNullable && !param.Type.IsTypeNullable() && !param.IsCollection)
117-
{
118-
requestBodyInitializerBuilder.Append(Environment.NewLine);
119-
requestBodyInitializerBuilder.Append(" ");
120-
requestBodyInitializerBuilder.AppendFormat("if ({0}.HasValue)", paramLowerChar);
121-
requestBodyInitializerBuilder.Append(Environment.NewLine);
122-
requestBodyInitializerBuilder.Append(" ");
123-
requestBodyInitializerBuilder.Append("{");
124-
requestBodyInitializerBuilder.Append(Environment.NewLine);
125-
requestBodyInitializerBuilder.Append(" ");
126-
requestBodyInitializerBuilder.Append("this.RequestBody.");
127-
requestBodyInitializerBuilder.Append(param.Name.ToCheckedCase());
128-
requestBodyInitializerBuilder.Append(" = ");
129-
requestBodyInitializerBuilder.Append(paramLowerChar);
130-
requestBodyInitializerBuilder.Append(".Value;");
131-
requestBodyInitializerBuilder.Append(Environment.NewLine);
132-
requestBodyInitializerBuilder.Append(" ");
133-
requestBodyInitializerBuilder.Append("}");
134-
}
135-
else
136-
{
137-
requestBodyInitializerBuilder.Append("this.RequestBody.");
138-
requestBodyInitializerBuilder.Append(param.Name.ToCheckedCase());
139-
requestBodyInitializerBuilder.Append(" = ");
140-
requestBodyInitializerBuilder.Append(paramLowerChar);
141-
requestBodyInitializerBuilder.Append(";");
142-
}
143-
}
144-
145-
paramStringBuilder.Remove(paramStringBuilder.Length - 1, 1);
146-
}
147-
#>
148-
14961
/// <summary>
15062
/// Constructs a new <#=requestType#>.
15163
/// </summary>
152-
public <#=requestType#>(<#=paramStringBuilder.ToString()#>)
64+
public <#=requestType#>(
65+
string requestUrl,
66+
IBaseClient client,
67+
IEnumerable<Option> options)
15368
: base(requestUrl, client, options)
15469
{
155-
<#=requestBodyInitializerBuilder.ToString()#>
70+
this.Method = "<#=httpMethod#>";
71+
<#
72+
if (includeRequestBody)
73+
{
74+
#>
75+
this.ContentType = "application/json";
76+
this.RequestBody = new <#=requestType#>Body();
77+
<#
78+
}
79+
#>
15680
}
15781
<#
158-
if (hasParameters && method.IsAction())
82+
if (includeRequestBody)
15983
{
16084
#>
16185

@@ -165,31 +89,6 @@ namespace <#=method.Namespace.GetNamespaceName()#>
16589
public <#=requestType#>Body RequestBody { get; private set; }
16690
<#
16791
}
168-
169-
var methodReturnTag = sendAsyncReturnType == null
170-
? "The task to await."
171-
: string.Concat("The", sendAsyncReturnType);
172-
173-
var methodParameter = "null";
174-
if (includeRequestBody)
175-
{
176-
methodParameter = "this.RequestBody";
177-
}
178-
179-
string sendParameterHeadersForOverload, sendOverloadParameters;
180-
181-
if (returnsStream)
182-
{
183-
sendParameterHeadersForOverload = @"<param name=""cancellationToken"">The <see cref=""CancellationToken""/> for the request.</param>
184-
/// <param name=""httpCompletionOption"">The <see cref=""HttpCompletionOption""/> for the request.</param>";
185-
186-
sendOverloadParameters = "CancellationToken cancellationToken, HttpCompletionOption httpCompletionOption = HttpCompletionOption.ResponseContentRead";
187-
}
188-
else
189-
{
190-
sendParameterHeadersForOverload = "<param name=\"cancellationToken\">The <see cref=\"CancellationToken\"/> for the request.</param>";
191-
sendOverloadParameters = "CancellationToken cancellationToken";
192-
}
19392
#>
19493

19594
/// <summary>
@@ -203,11 +102,36 @@ namespace <#=method.Namespace.GetNamespaceName()#>
203102
/// <summary>
204103
/// Issues the <#=httpMethod#> request.
205104
/// </summary>
206-
/// <#=sendParameterHeadersForOverload#>
207-
/// <returns><#=methodReturnTag#></returns>
208-
public <#=methodReturnType#> <#=httpMethod.ToLower().ToCheckedCase()#>Async(<#=sendOverloadParameters#>)
105+
/// <param name=""cancellationToken"">The <see cref=""CancellationToken""/> for the request.</param>
106+
<#
107+
if (returnsStream)
108+
{
109+
#>
110+
/// <param name=""httpCompletionOption"">The <see cref=""HttpCompletionOption""/> for the request.</param>
111+
<#
112+
}
113+
#>
114+
/// <returns>The task to await for async call.</returns>
115+
public <#=methodReturnType#> <#=httpMethod.ToLower().ToCheckedCase()#>Async(
116+
<#
117+
if (returnsStream)
118+
{
119+
#>
120+
CancellationToken cancellationToken,
121+
HttpCompletionOption httpCompletionOption = HttpCompletionOption.ResponseContentRead)
122+
<#
123+
}
124+
else
125+
{
126+
#>
127+
CancellationToken cancellationToken)
128+
<#
129+
}
130+
#>
209131
{
210132
<#
133+
var methodParameter = includeRequestBody ? "this.RequestBody" : "null";
134+
211135
if (isCollection)
212136
{
213137
#>

0 commit comments

Comments
 (0)