public static GeneratedMethodModel Build(IMethodSymbol lambdaMethodSymbol,
                                                 IMethodSymbol configureMethodSymbol,
                                                 LambdaMethodModel lambdaMethodModel,
                                                 GeneratorExecutionContext context)
        {
            var model = new GeneratedMethodModel
            {
                Usings         = BuildUsings(lambdaMethodSymbol, configureMethodSymbol, context),
                Parameters     = BuildParameters(lambdaMethodSymbol, lambdaMethodModel, context),
                ReturnType     = BuildResponseType(lambdaMethodSymbol, lambdaMethodModel, context),
                ContainingType = BuildContainingType(lambdaMethodSymbol),
            };

            return(model);
        }
        private static TypeModel BuildResponseType(IMethodSymbol lambdaMethodSymbol,
                                                   LambdaMethodModel lambdaMethodModel, GeneratorExecutionContext context)
        {
            var task = context.Compilation.GetTypeByMetadataName(TypeFullNames.Task1);

            if (lambdaMethodSymbol.HasAttribute(context, TypeFullNames.RestApiAttribute))
            {
                var symbol = lambdaMethodModel.IsAsync ?
                             task.Construct(context.Compilation.GetTypeByMetadataName(TypeFullNames.APIGatewayProxyResponse)):
                             context.Compilation.GetTypeByMetadataName(TypeFullNames.APIGatewayProxyResponse);
                return(TypeModelBuilder.Build(symbol, context));
            }
            else if (lambdaMethodSymbol.HasAttribute(context, TypeFullNames.HttpApiAttribute))
            {
                var version = GetHttpApiVersion(lambdaMethodSymbol, context);
                switch (version)
                {
                case HttpApiVersion.V1:
                {
                    var symbol = lambdaMethodModel.IsAsync ?
                                 task.Construct(context.Compilation.GetTypeByMetadataName(TypeFullNames.APIGatewayProxyResponse)):
                                 context.Compilation.GetTypeByMetadataName(TypeFullNames.APIGatewayProxyResponse);
                    return(TypeModelBuilder.Build(symbol, context));;
                }

                case HttpApiVersion.V2:
                {
                    var symbol = lambdaMethodModel.IsAsync ?
                                 task.Construct(context.Compilation.GetTypeByMetadataName(TypeFullNames.APIGatewayHttpApiV2ProxyResponse)):
                                 context.Compilation.GetTypeByMetadataName(TypeFullNames.APIGatewayHttpApiV2ProxyResponse);
                    return(TypeModelBuilder.Build(symbol, context));;
                }

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
            else
            {
                return(lambdaMethodModel.ReturnType);
            }
        }
示例#3
0
        public static LambdaMethodModel Build(IMethodSymbol lambdaMethodSymbol,
                                              IMethodSymbol configureMethodSymbol,
                                              GeneratorExecutionContext context)
        {
            var model = new LambdaMethodModel
            {
                IsAsync                  = lambdaMethodSymbol.IsAsync,
                ReturnType               = TypeModelBuilder.Build(lambdaMethodSymbol.ReturnType, context),
                ReturnsVoid              = lambdaMethodSymbol.ReturnsVoid,
                ReturnsTask              = lambdaMethodSymbol.ReturnType.Equals(context.Compilation.GetTypeByMetadataName("System.Threading.Tasks.Task"), SymbolEqualityComparer.Default),
                Parameters               = ParameterModelBuilder.Build(lambdaMethodSymbol, context),
                Name                     = lambdaMethodSymbol.Name,
                ContainingAssembly       = lambdaMethodSymbol.ContainingAssembly.Name,
                ContainingNamespace      = lambdaMethodSymbol.ContainingNamespace.ToDisplayString(),
                Events                   = EventTypeBuilder.Build(lambdaMethodSymbol, context),
                ContainingType           = TypeModelBuilder.Build(lambdaMethodSymbol.ContainingType, context),
                UsingDependencyInjection = configureMethodSymbol != null,
                Attributes               = lambdaMethodSymbol.GetAttributes().Select(att => AttributeModelBuilder.Build(att, context)).ToList()
            };

            return(model);
        }
        private static IList <ParameterModel> BuildParameters(IMethodSymbol lambdaMethodSymbol,
                                                              LambdaMethodModel lambdaMethodModel, GeneratorExecutionContext context)
        {
            var parameters = new List <ParameterModel>();

            var contextParameter = new ParameterModel
            {
                Name = "__context__",
                Type = new TypeModel
                {
                    FullName = TypeFullNames.ILambdaContext
                }
            };

            if (lambdaMethodSymbol.HasAttribute(context, TypeFullNames.RestApiAttribute))
            {
                var symbol           = context.Compilation.GetTypeByMetadataName(TypeFullNames.APIGatewayProxyRequest);
                var type             = TypeModelBuilder.Build(symbol, context);
                var requestParameter = new ParameterModel
                {
                    Name = "__request__",
                    Type = type
                };
                parameters.Add(requestParameter);
                parameters.Add(contextParameter);
            }
            else if (lambdaMethodSymbol.HasAttribute(context, TypeFullNames.HttpApiAttribute))
            {
                var       version = GetHttpApiVersion(lambdaMethodSymbol, context);
                TypeModel type;
                switch (version)
                {
                case HttpApiVersion.V1:
                {
                    var symbol = context.Compilation.GetTypeByMetadataName(TypeFullNames.APIGatewayProxyRequest);
                    type = TypeModelBuilder.Build(symbol, context);
                    break;
                }

                case HttpApiVersion.V2:
                {
                    var symbol = context.Compilation.GetTypeByMetadataName(TypeFullNames.APIGatewayHttpApiV2ProxyRequest);
                    type = TypeModelBuilder.Build(symbol, context);
                    break;
                }

                default:
                    throw new ArgumentOutOfRangeException();
                }

                var requestParameter = new ParameterModel
                {
                    Name = "__request__",
                    Type = type
                };
                parameters.Add(requestParameter);
                parameters.Add(contextParameter);
            }
            else
            {
                // Lambda method with no event attribute are plain lambda functions, therefore, generated method will have
                // same parameter as original method except DI injected parameters
                foreach (var param in lambdaMethodModel.Parameters)
                {
                    if (param.Attributes.Any(att => att.Type.FullName == TypeFullNames.FromServiceAttribute))
                    {
                        continue;
                    }
                    // If the Lambda function is taking in the ILambdaContext object make sure in the generated wrapper code we
                    // use the same system name for the ILambdaContext variable as all of the other places use.
                    else if (param.Type.FullName == TypeFullNames.ILambdaContext)
                    {
                        param.Name = "__context__";
                    }

                    parameters.Add(param);
                }
            }

            return(parameters);
        }