// ------------------------------------------------------------------------------
//
// This code was generated by a tool.
// Runtime Version: 17.0.0.0
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
//
// ------------------------------------------------------------------------------
namespace Amazon.Lambda.Annotations.SourceGenerator.Templates
{
using System.Linq;
using System.Text;
using System.Collections.Generic;
using Amazon.Lambda.Annotations.SourceGenerator.Extensions;
using Amazon.Lambda.Annotations.SourceGenerator.Validation;
using Amazon.Lambda.Annotations.SourceGenerator.Models;
using Amazon.Lambda.Annotations.SourceGenerator.Models.Attributes;
using System;
///
/// Class to produce the template output
///
#line 1 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "17.0.0.0")]
public partial class APIGatewaySetupParameters : APIGatewaySetupParametersBase
{
#line hidden
///
/// Create the template output
///
public virtual string TransformText()
{
#line 10 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
ParameterSignature = string.Join(", ", _model.LambdaMethod.Parameters
.Select(p =>
{
// Pass the same context parameter for ILambdaContext that comes from the generated method.
if (p.Type.FullName == TypeFullNames.ILambdaContext)
{
return "__context__";
}
// Pass the same request parameter for Request Type that comes from the generated method.
if (TypeFullNames.Requests.Contains(p.Type.FullName))
{
return "__request__";
}
return p.Name;
}));
var restApiAttribute = _model.LambdaMethod.Attributes.FirstOrDefault(att => att.Type.FullName == TypeFullNames.RestApiAttribute) as AttributeModel;
var httpApiAttribute = _model.LambdaMethod.Attributes.FirstOrDefault(att => att.Type.FullName == TypeFullNames.HttpApiAttribute) as AttributeModel;
if (restApiAttribute != null && httpApiAttribute != null)
{
throw new NotSupportedException($"A method cannot have both {TypeFullNames.RestApiAttribute} and {TypeFullNames.HttpApiAttribute} attribute at the same time.");
}
var routeParameters = restApiAttribute?.Data?.GetTemplateParameters() ?? httpApiAttribute?.Data?.GetTemplateParameters() ?? new HashSet();
var (routeTemplateValid, missingRouteParams) = RouteParametersValidator.Validate(routeParameters, _model.LambdaMethod.Parameters);
if (!routeTemplateValid)
{
var template = restApiAttribute?.Data?.Template ?? httpApiAttribute?.Data?.Template ?? string.Empty;
throw new InvalidOperationException($"Route template {template} is invalid. Missing {string.Join(",", missingRouteParams)} parameters in method definition.");
}
if (_model.LambdaMethod.Parameters.HasConvertibleParameter())
{
#line default
#line hidden
this.Write(" var validationErrors = new List();\r\n\r\n");
#line 50 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
}
foreach (var parameter in _model.LambdaMethod.Parameters)
{
if (parameter.Type.FullName == TypeFullNames.ILambdaContext || TypeFullNames.Requests.Contains(parameter.Type.FullName))
{
// No action required for ILambdaContext and RequestType, they are passed from the generated method parameter directly to the original method.
}
else if (parameter.Attributes.Any(att => att.Type.FullName == TypeFullNames.FromServiceAttribute))
{
#line default
#line hidden
this.Write(" var ");
#line 62 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(parameter.Name));
#line default
#line hidden
this.Write(" = scope.ServiceProvider.GetRequiredService<");
#line 62 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(parameter.Type.FullName));
#line default
#line hidden
this.Write(">();\r\n");
#line 63 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
}
else if (parameter.Attributes.Any(att => att.Type.FullName == TypeFullNames.FromQueryAttribute))
{
var fromQueryAttribute = parameter.Attributes.First(att => att.Type.FullName == TypeFullNames.FromQueryAttribute) as AttributeModel;
// Use parameter name as key, if Name has not specified explicitly in the attribute definition.
var parameterKey = fromQueryAttribute?.Data?.Name ?? parameter.Name;
var queryStringParameters = "QueryStringParameters";
#line default
#line hidden
this.Write(" var ");
#line 75 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(parameter.Name));
#line default
#line hidden
this.Write(" = default(");
#line 75 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(parameter.Type.FullName));
#line default
#line hidden
this.Write(");\r\n");
#line 76 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
if (parameter.Type.IsEnumerable && parameter.Type.IsGenericType)
{
// In HTTP API V2 multiple values for the same parameter are represented via comma separated string
// Therefore, it is required to split the string to convert to an enumerable
// and convert individual item to target data type.
var commaSplit = "";
if (httpApiAttribute?.Data.Version == Amazon.Lambda.Annotations.APIGateway.HttpApiVersion.V2)
{
commaSplit = @".Split("","")";
}
// HTTP API V1 and Rest API, multiple values for the same parameter are provided
// dedicated dictionary of string key and list value.
if (restApiAttribute != null || httpApiAttribute?.Data.Version == Amazon.Lambda.Annotations.APIGateway.HttpApiVersion.V1)
{
queryStringParameters = "MultiValueQueryStringParameters";
}
if (parameter.Type.TypeArguments.Count != 1)
{
throw new NotSupportedException("Only one type argument is supported for generic types.");
}
// Generic types are mapped using Select statement to the target parameter type argument.
var typeArgument = parameter.Type.TypeArguments.First();
#line default
#line hidden
this.Write(" if (__request__.");
#line 104 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(queryStringParameters));
#line default
#line hidden
this.Write("?.ContainsKey(\"");
#line 104 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(parameterKey));
#line default
#line hidden
this.Write("\") == true)\r\n {\r\n ");
#line 106 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(parameter.Name));
#line default
#line hidden
this.Write(" = __request__.");
#line 106 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(queryStringParameters));
#line default
#line hidden
this.Write("[\"");
#line 106 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(parameterKey));
#line default
#line hidden
this.Write("\"]");
#line 106 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(commaSplit));
#line default
#line hidden
this.Write("\r\n .Select(q =>\r\n {\r\n " +
" try\r\n {\r\n return (");
#line 111 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(typeArgument.FullName));
#line default
#line hidden
this.Write(")Convert.ChangeType(q, typeof(");
#line 111 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(typeArgument.FullNameWithoutAnnotations));
#line default
#line hidden
this.Write(@"));
}
catch (Exception e) when (e is InvalidCastException || e is FormatException || e is OverflowException || e is ArgumentException)
{
validationErrors.Add($""Value {q} at '");
#line 115 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(parameterKey));
#line default
#line hidden
this.Write("\' failed to satisfy constraint: {e.Message}\");\r\n retur" +
"n default;\r\n }\r\n })\r\n " +
" .ToList();\r\n }\r\n\r\n");
#line 122 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
}
else
{
// Non-generic types are mapped directly to the target parameter.
#line default
#line hidden
this.Write(" if (__request__.");
#line 128 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(queryStringParameters));
#line default
#line hidden
this.Write("?.ContainsKey(\"");
#line 128 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(parameterKey));
#line default
#line hidden
this.Write("\") == true)\r\n {\r\n try\r\n {\r\n " +
" ");
#line 132 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(parameter.Name));
#line default
#line hidden
this.Write(" = (");
#line 132 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(parameter.Type.FullName));
#line default
#line hidden
this.Write(")Convert.ChangeType(__request__.");
#line 132 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(queryStringParameters));
#line default
#line hidden
this.Write("[\"");
#line 132 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(parameterKey));
#line default
#line hidden
this.Write("\"], typeof(");
#line 132 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(parameter.Type.FullNameWithoutAnnotations));
#line default
#line hidden
this.Write("));\r\n }\r\n catch (Exception e) when (e is InvalidCas" +
"tException || e is FormatException || e is OverflowException || e is ArgumentExc" +
"eption)\r\n {\r\n validationErrors.Add($\"Value {__" +
"request__.");
#line 136 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(queryStringParameters));
#line default
#line hidden
this.Write("[\"");
#line 136 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(parameterKey));
#line default
#line hidden
this.Write("\"]} at \'");
#line 136 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(parameterKey));
#line default
#line hidden
this.Write("\' failed to satisfy constraint: {e.Message}\");\r\n }\r\n }\r" +
"\n\r\n");
#line 140 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
}
}
else if (parameter.Attributes.Any(att => att.Type.FullName == TypeFullNames.FromHeaderAttribute))
{
var fromHeaderAttribute =
parameter.Attributes.First(att => att.Type.FullName == TypeFullNames.FromHeaderAttribute) as
AttributeModel;
// Use parameter name as key, if Name has not specified explicitly in the attribute definition.
var headerKey = fromHeaderAttribute?.Data?.Name ?? parameter.Name;
var headers = "Headers";
#line default
#line hidden
this.Write(" var ");
#line 156 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(parameter.Name));
#line default
#line hidden
this.Write(" = default(");
#line 156 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(parameter.Type.FullName));
#line default
#line hidden
this.Write(");\r\n");
#line 157 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
if (parameter.Type.IsEnumerable && parameter.Type.IsGenericType)
{
// In HTTP API V2 multiple values for the same header are represented via comma separated string
// Therefore, it is required to split the string to convert to an enumerable
// and convert individual item to target data type.
var commaSplit = "";
if (httpApiAttribute?.Data.Version == Amazon.Lambda.Annotations.APIGateway.HttpApiVersion.V2)
{
commaSplit = @".Split("","")";
}
// HTTP API V1 and Rest API, multiple values for the same header are provided
// dedicated dictionary of string key and list value.
if (restApiAttribute != null || httpApiAttribute?.Data.Version == Amazon.Lambda.Annotations.APIGateway.HttpApiVersion.V1)
{
headers = "MultiValueHeaders";
}
if (parameter.Type.TypeArguments.Count != 1)
{
throw new NotSupportedException("Only one type argument is supported for generic types.");
}
// Generic types are mapped using Select statement to the target parameter type argument.
var typeArgument = parameter.Type.TypeArguments.First();
#line default
#line hidden
this.Write(" if (__request__.");
#line 185 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(headers));
#line default
#line hidden
this.Write("?.Any(x => string.Equals(x.Key, \"");
#line 185 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(headerKey));
#line default
#line hidden
this.Write("\", StringComparison.OrdinalIgnoreCase)) == true)\r\n {\r\n " +
"");
#line 187 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(parameter.Name));
#line default
#line hidden
this.Write(" = __request__.");
#line 187 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(headers));
#line default
#line hidden
this.Write(".First(x => string.Equals(x.Key, \"");
#line 187 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(headerKey));
#line default
#line hidden
this.Write("\", StringComparison.OrdinalIgnoreCase)).Value");
#line 187 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(commaSplit));
#line default
#line hidden
this.Write("\r\n .Select(q =>\r\n {\r\n " +
" try\r\n {\r\n return (");
#line 192 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(typeArgument.FullName));
#line default
#line hidden
this.Write(")Convert.ChangeType(q, typeof(");
#line 192 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(typeArgument.FullNameWithoutAnnotations));
#line default
#line hidden
this.Write(@"));
}
catch (Exception e) when (e is InvalidCastException || e is FormatException || e is OverflowException || e is ArgumentException)
{
validationErrors.Add($""Value {q} at '");
#line 196 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(headerKey));
#line default
#line hidden
this.Write("\' failed to satisfy constraint: {e.Message}\");\r\n retur" +
"n default;\r\n }\r\n })\r\n " +
" .ToList();\r\n }\r\n\r\n");
#line 203 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
}
else
{
// Non-generic types are mapped directly to the target parameter.
#line default
#line hidden
this.Write(" if (__request__.");
#line 209 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(headers));
#line default
#line hidden
this.Write("?.Any(x => string.Equals(x.Key, \"");
#line 209 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(headerKey));
#line default
#line hidden
this.Write("\", StringComparison.OrdinalIgnoreCase)) == true)\r\n {\r\n " +
"try\r\n {\r\n ");
#line 213 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(parameter.Name));
#line default
#line hidden
this.Write(" = (");
#line 213 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(parameter.Type.FullName));
#line default
#line hidden
this.Write(")Convert.ChangeType(__request__.");
#line 213 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(headers));
#line default
#line hidden
this.Write(".First(x => string.Equals(x.Key, \"");
#line 213 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(headerKey));
#line default
#line hidden
this.Write("\", StringComparison.OrdinalIgnoreCase)).Value, typeof(");
#line 213 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(parameter.Type.FullNameWithoutAnnotations));
#line default
#line hidden
this.Write("));\r\n }\r\n catch (Exception e) when (e is InvalidCas" +
"tException || e is FormatException || e is OverflowException || e is ArgumentExc" +
"eption)\r\n {\r\n validationErrors.Add($\"Value {__" +
"request__.");
#line 217 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(headers));
#line default
#line hidden
this.Write(".First(x => string.Equals(x.Key, \"");
#line 217 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(headerKey));
#line default
#line hidden
this.Write("\", StringComparison.OrdinalIgnoreCase)).Value} at \'");
#line 217 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(headerKey));
#line default
#line hidden
this.Write("\' failed to satisfy constraint: {e.Message}\");\r\n }\r\n }\r" +
"\n\r\n");
#line 221 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
}
}
else if (parameter.Attributes.Any(att => att.Type.FullName == TypeFullNames.FromBodyAttribute))
{
// string parameter does not need to be de-serialized
if (parameter.Type.IsString())
{
#line default
#line hidden
this.Write(" var ");
#line 230 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(parameter.Name));
#line default
#line hidden
this.Write(" = __request__.Body;\r\n\r\n");
#line 232 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
}
else
{
#line default
#line hidden
this.Write(" var ");
#line 237 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(parameter.Name));
#line default
#line hidden
this.Write(" = default(");
#line 237 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(parameter.Type.FullName));
#line default
#line hidden
this.Write(");\r\n try\r\n {\r\n ");
#line 240 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(parameter.Name));
#line default
#line hidden
this.Write(" = ");
#line 240 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(_model.Serializer));
#line default
#line hidden
this.Write(".Deserialize<");
#line 240 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(parameter.Type.FullName));
#line default
#line hidden
this.Write(">(__request__.Body);\r\n }\r\n catch (Exception e)\r\n " +
" {\r\n validationErrors.Add($\"Value {__request__.Body} at \'body\' fa" +
"iled to satisfy constraint: {e.Message}\");\r\n }\r\n\r\n");
#line 247 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
}
}
else if (parameter.Attributes.Any(att => att.Type.FullName == TypeFullNames.FromRouteAttribute) || routeParameters.Contains(parameter.Name))
{
var fromRouteAttribute = parameter.Attributes?.FirstOrDefault(att => att.Type.FullName == TypeFullNames.FromRouteAttribute) as AttributeModel;
// Use parameter name as key, if Name has not specified explicitly in the attribute definition.
var routeKey = fromRouteAttribute?.Data?.Name ?? parameter.Name;
#line default
#line hidden
this.Write(" var ");
#line 257 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(parameter.Name));
#line default
#line hidden
this.Write(" = default(");
#line 257 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(parameter.Type.FullName));
#line default
#line hidden
this.Write(");\r\n if (__request__.PathParameters?.ContainsKey(\"");
#line 258 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(routeKey));
#line default
#line hidden
this.Write("\") == true)\r\n {\r\n try\r\n {\r\n " +
" ");
#line 262 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(parameter.Name));
#line default
#line hidden
this.Write(" = (");
#line 262 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(parameter.Type.FullName));
#line default
#line hidden
this.Write(")Convert.ChangeType(__request__.PathParameters[\"");
#line 262 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(routeKey));
#line default
#line hidden
this.Write("\"], typeof(");
#line 262 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(parameter.Type.FullNameWithoutAnnotations));
#line default
#line hidden
this.Write(@"));
}
catch (Exception e) when (e is InvalidCastException || e is FormatException || e is OverflowException || e is ArgumentException)
{
validationErrors.Add($""Value {__request__.PathParameters[""");
#line 266 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(routeKey));
#line default
#line hidden
this.Write("\"]} at \'");
#line 266 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(routeKey));
#line default
#line hidden
this.Write("\' failed to satisfy constraint: {e.Message}\");\r\n }\r\n }\r" +
"\n\r\n");
#line 270 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
}
else
{
throw new NotSupportedException($"{parameter.Name} parameter of type {parameter.Type.FullName} passing is not supported.");
}
}
if (_model.LambdaMethod.Parameters.HasConvertibleParameter())
{
#line default
#line hidden
this.Write(" // return 400 Bad Request if there exists a validation error\r\n " +
" if (validationErrors.Any())\r\n {\r\n var errorResult" +
" = new ");
#line 284 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(restApiAttribute != null || httpApiAttribute?.Data.Version == Amazon.Lambda.Annotations.APIGateway.HttpApiVersion.V1 ? "Amazon.Lambda.APIGatewayEvents.APIGatewayProxyResponse" : "Amazon.Lambda.APIGatewayEvents.APIGatewayHttpApiV2ProxyResponse"));
#line default
#line hidden
this.Write(@"
{
Body = @$""{{""""message"""": """"{validationErrors.Count} validation error(s) detected: {string.Join("","", validationErrors)}""""}}"",
Headers = new Dictionary
{
{""Content-Type"", ""application/json""},
{""x-amzn-ErrorType"", ""ValidationException""}
},
StatusCode = 400
};
");
#line 294 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
if(_model.LambdaMethod.ReturnsIHttpResults)
{
#line default
#line hidden
this.Write(" var errorStream = new System.IO.MemoryStream();\r\n " +
"System.Text.Json.JsonSerializer.Serialize(errorStream, errorResult);\r\n " +
" return errorStream;\r\n");
#line 301 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
}
else
{
#line default
#line hidden
this.Write(" return errorResult;\r\n");
#line 307 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
}
#line default
#line hidden
this.Write(" }\r\n\r\n");
#line 312 "C:\codebase\aws-lambda-dotnet\Libraries\src\Amazon.Lambda.Annotations.SourceGenerator\Templates\APIGatewaySetupParameters.tt"
}
#line default
#line hidden
return this.GenerationEnvironment.ToString();
}
}
#line default
#line hidden
#region Base class
///
/// Base class for this transformation
///
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "17.0.0.0")]
public class APIGatewaySetupParametersBase
{
#region Fields
private global::System.Text.StringBuilder generationEnvironmentField;
private global::System.CodeDom.Compiler.CompilerErrorCollection errorsField;
private global::System.Collections.Generic.List indentLengthsField;
private string currentIndentField = "";
private bool endsWithNewline;
private global::System.Collections.Generic.IDictionary sessionField;
#endregion
#region Properties
///
/// The string builder that generation-time code is using to assemble generated output
///
protected System.Text.StringBuilder GenerationEnvironment
{
get
{
if ((this.generationEnvironmentField == null))
{
this.generationEnvironmentField = new global::System.Text.StringBuilder();
}
return this.generationEnvironmentField;
}
set
{
this.generationEnvironmentField = value;
}
}
///
/// The error collection for the generation process
///
public System.CodeDom.Compiler.CompilerErrorCollection Errors
{
get
{
if ((this.errorsField == null))
{
this.errorsField = new global::System.CodeDom.Compiler.CompilerErrorCollection();
}
return this.errorsField;
}
}
///
/// A list of the lengths of each indent that was added with PushIndent
///
private System.Collections.Generic.List indentLengths
{
get
{
if ((this.indentLengthsField == null))
{
this.indentLengthsField = new global::System.Collections.Generic.List();
}
return this.indentLengthsField;
}
}
///
/// Gets the current indent we use when adding lines to the output
///
public string CurrentIndent
{
get
{
return this.currentIndentField;
}
}
///
/// Current transformation session
///
public virtual global::System.Collections.Generic.IDictionary Session
{
get
{
return this.sessionField;
}
set
{
this.sessionField = value;
}
}
#endregion
#region Transform-time helpers
///
/// Write text directly into the generated output
///
public void Write(string textToAppend)
{
if (string.IsNullOrEmpty(textToAppend))
{
return;
}
// If we're starting off, or if the previous text ended with a newline,
// we have to append the current indent first.
if (((this.GenerationEnvironment.Length == 0)
|| this.endsWithNewline))
{
this.GenerationEnvironment.Append(this.currentIndentField);
this.endsWithNewline = false;
}
// Check if the current text ends with a newline
if (textToAppend.EndsWith(global::System.Environment.NewLine, global::System.StringComparison.CurrentCulture))
{
this.endsWithNewline = true;
}
// This is an optimization. If the current indent is "", then we don't have to do any
// of the more complex stuff further down.
if ((this.currentIndentField.Length == 0))
{
this.GenerationEnvironment.Append(textToAppend);
return;
}
// Everywhere there is a newline in the text, add an indent after it
textToAppend = textToAppend.Replace(global::System.Environment.NewLine, (global::System.Environment.NewLine + this.currentIndentField));
// If the text ends with a newline, then we should strip off the indent added at the very end
// because the appropriate indent will be added when the next time Write() is called
if (this.endsWithNewline)
{
this.GenerationEnvironment.Append(textToAppend, 0, (textToAppend.Length - this.currentIndentField.Length));
}
else
{
this.GenerationEnvironment.Append(textToAppend);
}
}
///
/// Write text directly into the generated output
///
public void WriteLine(string textToAppend)
{
this.Write(textToAppend);
this.GenerationEnvironment.AppendLine();
this.endsWithNewline = true;
}
///
/// Write formatted text directly into the generated output
///
public void Write(string format, params object[] args)
{
this.Write(string.Format(global::System.Globalization.CultureInfo.CurrentCulture, format, args));
}
///
/// Write formatted text directly into the generated output
///
public void WriteLine(string format, params object[] args)
{
this.WriteLine(string.Format(global::System.Globalization.CultureInfo.CurrentCulture, format, args));
}
///
/// Raise an error
///
public void Error(string message)
{
System.CodeDom.Compiler.CompilerError error = new global::System.CodeDom.Compiler.CompilerError();
error.ErrorText = message;
this.Errors.Add(error);
}
///
/// Raise a warning
///
public void Warning(string message)
{
System.CodeDom.Compiler.CompilerError error = new global::System.CodeDom.Compiler.CompilerError();
error.ErrorText = message;
error.IsWarning = true;
this.Errors.Add(error);
}
///
/// Increase the indent
///
public void PushIndent(string indent)
{
if ((indent == null))
{
throw new global::System.ArgumentNullException("indent");
}
this.currentIndentField = (this.currentIndentField + indent);
this.indentLengths.Add(indent.Length);
}
///
/// Remove the last indent that was added with PushIndent
///
public string PopIndent()
{
string returnValue = "";
if ((this.indentLengths.Count > 0))
{
int indentLength = this.indentLengths[(this.indentLengths.Count - 1)];
this.indentLengths.RemoveAt((this.indentLengths.Count - 1));
if ((indentLength > 0))
{
returnValue = this.currentIndentField.Substring((this.currentIndentField.Length - indentLength));
this.currentIndentField = this.currentIndentField.Remove((this.currentIndentField.Length - indentLength));
}
}
return returnValue;
}
///
/// Remove any indentation
///
public void ClearIndent()
{
this.indentLengths.Clear();
this.currentIndentField = "";
}
#endregion
#region ToString Helpers
///
/// Utility class to produce culture-oriented representation of an object as a string.
///
public class ToStringInstanceHelper
{
private System.IFormatProvider formatProviderField = global::System.Globalization.CultureInfo.InvariantCulture;
///
/// Gets or sets format provider to be used by ToStringWithCulture method.
///
public System.IFormatProvider FormatProvider
{
get
{
return this.formatProviderField ;
}
set
{
if ((value != null))
{
this.formatProviderField = value;
}
}
}
///
/// This is called from the compile/run appdomain to convert objects within an expression block to a string
///
public string ToStringWithCulture(object objectToConvert)
{
if ((objectToConvert == null))
{
throw new global::System.ArgumentNullException("objectToConvert");
}
System.Type t = objectToConvert.GetType();
System.Reflection.MethodInfo method = t.GetMethod("ToString", new System.Type[] {
typeof(System.IFormatProvider)});
if ((method == null))
{
return objectToConvert.ToString();
}
else
{
return ((string)(method.Invoke(objectToConvert, new object[] {
this.formatProviderField })));
}
}
}
private ToStringInstanceHelper toStringHelperField = new ToStringInstanceHelper();
///
/// Helper to produce culture-oriented representation of an object as a string
///
public ToStringInstanceHelper ToStringHelper
{
get
{
return this.toStringHelperField;
}
}
#endregion
}
#endregion
}