Ejemplo n.º 1
0
        public static LambdaFunctionModel Build(IMethodSymbol lambdaMethodSymbol, IMethodSymbol configureMethodSymbol, GeneratorExecutionContext context)
        {
            var lambdaMethod    = LambdaMethodModelBuilder.Build(lambdaMethodSymbol, configureMethodSymbol, context);
            var generatedMethod = GeneratedMethodModelBuilder.Build(lambdaMethodSymbol, configureMethodSymbol, lambdaMethod, context);
            var model           = new LambdaFunctionModel()
            {
                GeneratedMethod = generatedMethod,
                LambdaMethod    = lambdaMethod,
                Serializer      = "System.Text.Json.JsonSerializer", // TODO: replace serializer with assembly serializer
                StartupType     = configureMethodSymbol != null?TypeModelBuilder.Build(configureMethodSymbol.ContainingType, context) : null,
                                      SourceGeneratorVersion = context.Compilation
                                                               .ReferencedAssemblyNames.FirstOrDefault(x => string.Equals(x.Name, "Amazon.Lambda.Annotations"))
                                                               ?.Version.ToString()
            };

            return(model);
        }
Ejemplo n.º 2
0
        public static IList <ParameterModel> Build(IMethodSymbol methodSymbol,
                                                   GeneratorExecutionContext context)
        {
            var models = new List <ParameterModel>();

            foreach (var parameter in methodSymbol.Parameters)
            {
                models.Add(new ParameterModel
                {
                    Name       = parameter.Name,
                    Type       = TypeModelBuilder.Build(parameter.Type, context),
                    Attributes = parameter.GetAttributes().Select(att => AttributeModelBuilder.Build(att, context)).ToList()
                });
            }

            return(models);
        }
        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);
            }
        }
Ejemplo n.º 4
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);
        }