in Tools/LambdaTestTool-v2/src/Amazon.Lambda.TestTool/Processes/SQSEventSource/SQSEventSourceProcess.cs [118:239]
internal static List<SQSEventSourceConfig> LoadSQSEventSourceConfig(string sqsEventSourceConfigString)
{
if (File.Exists(sqsEventSourceConfigString))
{
sqsEventSourceConfigString = File.ReadAllText(sqsEventSourceConfigString);
}
sqsEventSourceConfigString = sqsEventSourceConfigString.Trim();
List<SQSEventSourceConfig>? configs = null;
var jsonOptions = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};
// Check to see if the config is in JSON array format.
// The JSON format provides consistency with the API Gateway config style.
if (sqsEventSourceConfigString.StartsWith('['))
{
try
{
configs = JsonSerializer.Deserialize<List<SQSEventSourceConfig>>(sqsEventSourceConfigString, jsonOptions);
if (configs == null)
{
throw new InvalidOperationException("Failed to parse SQS event source JSON config: " + sqsEventSourceConfigString);
}
}
catch(JsonException e)
{
throw new InvalidOperationException("Failed to parse SQS event source JSON config: " + sqsEventSourceConfigString, e);
}
}
// Config is a single object JSON document.
// The JSON format provides consistency with the API Gateway config style.
else if (sqsEventSourceConfigString.StartsWith('{'))
{
try
{
var config = JsonSerializer.Deserialize<SQSEventSourceConfig>(sqsEventSourceConfigString, jsonOptions);
if (config == null)
{
throw new InvalidOperationException("Failed to parse SQS event source JSON config: " + sqsEventSourceConfigString);
}
configs = new List<SQSEventSourceConfig> { config };
}
catch (JsonException e)
{
throw new InvalidOperationException("Failed to parse SQS event source JSON config: " + sqsEventSourceConfigString, e);
}
}
// Config is a QueueUrl only. The current test tool instance will be assumed the Lambda runtime api and the
// messages will be sent to the default function. Support this format allows for an
// simple CLI experience of just providing a single value for the default scenario.
else if (Uri.TryCreate(sqsEventSourceConfigString, UriKind.Absolute, out _))
{
configs = new List<SQSEventSourceConfig> { new SQSEventSourceConfig { QueueUrl = sqsEventSourceConfigString } };
}
// Config is in comma delimited key value pair format. This format allows setting all the parameters without having
// to deal with escaping quotes like the JSON format.
else
{
var config = new SQSEventSourceConfig();
var tokens = sqsEventSourceConfigString.Split(',');
foreach(var token in tokens)
{
if (string.IsNullOrWhiteSpace(token))
continue;
var keyValuePair = token.Split('=');
if (keyValuePair.Length != 2)
{
throw new InvalidOperationException("Failed to parse SQS event source config. Format should be \"QueueUrl=<value>,FunctionName=<value>,...\"");
}
switch (keyValuePair[0].ToLower().Trim())
{
case "batchsize":
if (!int.TryParse(keyValuePair[1].Trim(), out var batchSize))
{
throw new InvalidOperationException("Value for batch size is not a formatted integer");
}
config.BatchSize = batchSize;
break;
case "disablemessagedelete":
if (!bool.TryParse(keyValuePair[1].Trim(), out var disableMessageDelete))
{
throw new InvalidOperationException("Value for disable message delete is not a formatted boolean");
}
config.DisableMessageDelete = disableMessageDelete;
break;
case "functionname":
config.FunctionName = keyValuePair[1].Trim();
break;
case "lambdaruntimeapi":
config.LambdaRuntimeApi = keyValuePair[1].Trim();
break;
case "profile":
config.Profile = keyValuePair[1].Trim();
break;
case "queueurl":
config.QueueUrl = keyValuePair[1].Trim();
break;
case "region":
config.Region = keyValuePair[1].Trim();
break;
case "visibilitytimeout":
if (!int.TryParse(keyValuePair[1].Trim(), out var visibilityTimeout))
{
throw new InvalidOperationException("Value for visibility timeout is not a formatted integer");
}
config.VisibilityTimeout = visibilityTimeout;
break;
}
}
configs = new List<SQSEventSourceConfig> { config };
}
return configs;
}