using System;
using System.Collections.Generic;
using System.Linq;
using Amazon.Lambda.Annotations.SourceGenerator.Models.Attributes;
namespace Amazon.Lambda.Annotations.SourceGenerator.Models
{
///
/// Represents original method model used for generating the code from the template.
///
public class LambdaMethodModel
{
private AttributeModel _lambdaFunctionAttribute;
///
/// Returns true if original method returns void
///
public bool ReturnsVoid { get; set; }
///
/// Returns true if original method returns
///
public bool ReturnsVoidTask { get; set; }
///
/// Returns true if original method returns
///
public bool ReturnsGenericTask { get; set; }
///
/// Returns true if either the ReturnsVoidTask or ReturnsGenericTask are true
///
public bool ReturnsVoidOrGenericTask => ReturnsVoidTask || ReturnsGenericTask;
///
/// Gets or sets the return type of the method.
///
public TypeModel ReturnType { get; set; }
///
/// Returns true if the Lambda function returns either IHttpResult or Task
///
public bool ReturnsIHttpResults
{
get
{
if(ReturnsVoid)
{
return false;
}
if(ReturnType.FullName == TypeFullNames.IHttpResult)
{
return true;
}
if(ReturnsGenericTask && ReturnType.TypeArguments.Count == 1 && ReturnType.TypeArguments[0].FullName == TypeFullNames.IHttpResult)
{
return true;
}
return false;
}
}
///
/// Gets or sets the parameters of original method. If this method has no parameters, returns
/// an empty list.
///
public IList Parameters { get; set; }
///
/// Gets or sets name of the original method.
///
public string Name { get; set; }
///
/// Returns true if original method uses dependency injection.
///
public bool UsingDependencyInjection { get; set; }
///
/// Gets or sets the namespace for the nearest enclosing namespace. Returns null if the
/// symbol isn't contained in a namespace.
///
public string ContainingNamespace { get; set; }
///
/// Gets or sets the name of the assemblying containing the Lambda function.
///
public string ContainingAssembly { get; set; }
///
/// Gets or sets type of Lambda event
///
public List Events { get; set; }
///
/// Gets or sets the for the containing type. Returns null if the
/// symbol is not contained within a type.
///
public TypeModel ContainingType { get; set; }
///
/// Gets or sets the attributes of original method. There always exist in the list.
///
public IList Attributes { get; set; }
///
/// Gets attribute.
///
public AttributeModel LambdaFunctionAttribute
{
get
{
if (_lambdaFunctionAttribute == null)
{
var model = Attributes.First(att => att.Type.FullName == TypeFullNames.LambdaFunctionAttribute);
if (model is AttributeModel lambdaFunctionAttributeModel)
{
_lambdaFunctionAttribute = lambdaFunctionAttributeModel;
}
else
{
throw new InvalidOperationException($"Lambda method must has a {TypeFullNames.LambdaFunctionAttribute} attribute");
}
}
return _lambdaFunctionAttribute;
}
}
}
}