|
5 | 5 | CustomT4Host host = (CustomT4Host) Host; |
6 | 6 | var model = host.CurrentModel; |
7 | 7 | CodeWriterAndroid writer = (CodeWriterAndroid) host.CodeWriter; |
| 8 | + bool logTemplateSrc = false; |
8 | 9 | var c = host.CurrentType; |
| 10 | +#> |
| 11 | +<# if (logTemplateSrc) { #> |
| 12 | +// Template Source: <#= TemplateName(host.TemplateFile) #> |
| 13 | +<# } #> |
| 14 | +<#+ |
9 | 15 |
|
10 | | - if (c == null) |
11 | | - { |
12 | | -// throw new InvalidOperationException("Unable to get the current type!"); |
| 16 | + /// Is this request type a post? |
| 17 | + public bool IsPostRequestType(OdcmMethod method) { |
| 18 | + return method.IsAction() |
| 19 | + && null != method.Parameters |
| 20 | + && method.Parameters.Any(); |
| 21 | + } |
| 22 | + |
| 23 | + /// Choose an intermediate class type based on GETs/POSTs |
| 24 | + public string RequestBuilderSuperClass(OdcmObject currentType) { |
| 25 | + var isPostAction = |
| 26 | + IsPostRequestType( |
| 27 | + currentType.AsOdcmMethod() |
| 28 | + ); |
| 29 | + |
| 30 | + string superClass; |
| 31 | + |
| 32 | + if (isPostAction) { |
| 33 | + superClass = "BasePostMethodRequestBuilder"; |
| 34 | + } else { |
| 35 | + superClass = "BaseGetMethodRequestBuilder"; |
| 36 | + } |
| 37 | + |
| 38 | + return superClass; |
| 39 | + } |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Get the name of the current template being processed |
| 43 | + /// </summary> |
| 44 | + /// <param name="templateFile">The full path of the current template</param> |
| 45 | + /// <returns>The template name, relative to the Templates directory.</returns> |
| 46 | + public string TemplateName(string templateFile) { |
| 47 | + return templateFile.Substring(templateFile.LastIndexOf("Templates")); |
13 | 48 | } |
14 | 49 |
|
15 | | -#> |
16 | | -<# //="// Template Source: '" + host.TemplateFile + "', Name: '" + c.Name + "' , TypeName: '" + TypeName(c) + "'"#> |
17 | | -<#+ |
18 | 50 | public string TypeName(OdcmObject c) { |
19 | 51 | if (c is OdcmMethod) { |
20 | 52 | return ((OdcmMethod)c).Class.Name.ToUpperFirstChar() + c.Name.Substring(c.Name.IndexOf(".") + 1).ToUpperFirstChar(); |
|
331 | 363 | return "Base" + TypeBody(c); |
332 | 364 | } |
333 | 365 |
|
| 366 | + /// Creates an 'm' prepended name for a field |
| 367 | + /// based on its name as defined by the service $metadata |
334 | 368 | public string FieldName(OdcmParameter c) { |
335 | 369 | return "m" + ParamName(c).ToUpperFirstChar(); |
336 | 370 | } |
337 | 371 |
|
| 372 | + /// Returns the name of the service <parameter> with the first char lowercase |
338 | 373 | public string ParamName(OdcmParameter c) { |
339 | 374 | return c.Name.ToLowerFirstChar(); |
340 | 375 | } |
341 | 376 |
|
| 377 | + /// The Type of this param, as a string |
342 | 378 | public string ParamType(OdcmParameter c) { |
343 | 379 | if (c.IsCollection){ |
344 | 380 | return String.Format("List<{0}>", c.Type.GetTypeString()); |
|
352 | 388 | } |
353 | 389 | return "Void"; |
354 | 390 | } |
355 | | - |
356 | | - public string MethodParametersSignature(OdcmObject c) { |
| 391 | + |
| 392 | + public string MethodParametersSignature(OdcmMethod method) { |
357 | 393 | var parameterSignatureBuilder = new StringBuilder(); |
358 | | - foreach (var p in c.AsOdcmMethod().Parameters) |
| 394 | + foreach (var p in method.Parameters) |
359 | 395 | { |
360 | 396 | parameterSignatureBuilder.AppendFormat(", final {0} {1}", ParamType(p), ParamName(p)); |
361 | 397 | } |
362 | 398 | return parameterSignatureBuilder.ToString(); |
363 | 399 | } |
364 | 400 |
|
365 | | - public string MethodParametersValues(OdcmObject c) { |
| 401 | + public string MethodParametersValues(OdcmMethod method) { |
366 | 402 | var parameterValuesBuilder = new StringBuilder(); |
367 | | - foreach (var p in c.AsOdcmMethod().Parameters) |
| 403 | + foreach (var p in method.Parameters) |
368 | 404 | { |
369 | 405 | parameterValuesBuilder.AppendFormat(", {0}", ParamName(p)); |
370 | 406 | } |
@@ -477,6 +513,10 @@ import java.util.List;"; |
477 | 513 | host.TemplateInfo.OutputParentDirectory); |
478 | 514 | } |
479 | 515 |
|
| 516 | + /// Creates a class declaration |
| 517 | + /// name = the name of the class |
| 518 | + /// extends = the class it extends |
| 519 | + /// implements = the interface it extends |
480 | 520 | public string CreateClassDef(string name, string extends = null, string implements = null) |
481 | 521 | { |
482 | 522 | return this.CreateClassOrInterface(name, true, extends, implements); |
@@ -631,12 +671,13 @@ public {1} {2}{3}{4} {{"; |
631 | 671 | "; |
632 | 672 | foreach (var p in parameters) |
633 | 673 | { |
634 | | - var propertyType = p.IsCollection ? string.Format("List<{0}>", ParamType(p)) : ParamType(p); |
635 | | - sb.AppendFormat(format, |
| 674 | + sb.AppendFormat( |
| 675 | + format, |
636 | 676 | ParamName(p).SplitCamelCase(), |
637 | 677 | ParamName(p), |
638 | 678 | ParamType(p), |
639 | | - p.SanitizePropertyName().ToLowerFirstChar()); |
| 679 | + p.SanitizePropertyName().ToLowerFirstChar() |
| 680 | + ); |
640 | 681 | } |
641 | 682 | return sb.ToString(); |
642 | 683 | } |
|
0 commit comments