From 43ada6042c51d98bddd4c123186ffd2ba89b29aa Mon Sep 17 00:00:00 2001 From: savorboard Date: Sat, 25 Dec 2021 19:01:59 +0800 Subject: [PATCH] Enable #nullable for AmazonSQS --- .../AmazonPolicyExtensions.cs | 4 +-- .../AmazonSQSConsumerClient.cs | 26 +++++++++---------- .../CAP.AmazonSQSOptions.cs | 8 +++--- .../DotNetCore.CAP.AmazonSQS.csproj | 4 +-- .../ITransport.AmazonSQS.cs | 16 ++++++------ .../SQSReceivedMessage.cs | 8 +++--- 6 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/DotNetCore.CAP.AmazonSQS/AmazonPolicyExtensions.cs b/src/DotNetCore.CAP.AmazonSQS/AmazonPolicyExtensions.cs index d3af025..1f9c652 100644 --- a/src/DotNetCore.CAP.AmazonSQS/AmazonPolicyExtensions.cs +++ b/src/DotNetCore.CAP.AmazonSQS/AmazonPolicyExtensions.cs @@ -209,7 +209,7 @@ namespace DotNetCore.CAP.AmazonSQS /// /// Source ARN /// Group prefix or null if group not present - private static string GetArnGroupPrefix(string arn) + private static string? GetArnGroupPrefix(string arn) { const char separator = '-'; if (string.IsNullOrEmpty(arn) || !arn.Contains(separator)) @@ -235,7 +235,7 @@ namespace DotNetCore.CAP.AmazonSQS /// /// Source ARN /// Group name or null if group not present - private static string GetGroupName(string arn) + private static string? GetGroupName(string arn) { const char separator = ':'; if (string.IsNullOrEmpty(arn) || !arn.Contains(separator)) diff --git a/src/DotNetCore.CAP.AmazonSQS/AmazonSQSConsumerClient.cs b/src/DotNetCore.CAP.AmazonSQS/AmazonSQSConsumerClient.cs index 9c1a463..01dca36 100644 --- a/src/DotNetCore.CAP.AmazonSQS/AmazonSQSConsumerClient.cs +++ b/src/DotNetCore.CAP.AmazonSQS/AmazonSQSConsumerClient.cs @@ -27,8 +27,8 @@ namespace DotNetCore.CAP.AmazonSQS private readonly string _groupId; private readonly AmazonSQSOptions _amazonSQSOptions; - private IAmazonSimpleNotificationService _snsClient; - private IAmazonSQS _sqsClient; + private IAmazonSimpleNotificationService? _snsClient; + private IAmazonSQS? _sqsClient; private string _queueUrl = string.Empty; public AmazonSQSConsumerClient(string groupId, IOptions options) @@ -37,9 +37,9 @@ namespace DotNetCore.CAP.AmazonSQS _amazonSQSOptions = options.Value; } - public event EventHandler OnMessageReceived; + public event EventHandler? OnMessageReceived; - public event EventHandler OnLog; + public event EventHandler? OnLog; public BrokerAddress BrokerAddress => new BrokerAddress("AmazonSQS", _queueUrl); @@ -57,7 +57,7 @@ namespace DotNetCore.CAP.AmazonSQS { var createTopicRequest = new CreateTopicRequest(topic.NormalizeForAws()); - var createTopicResponse = _snsClient.CreateTopicAsync(createTopicRequest).GetAwaiter().GetResult(); + var createTopicResponse = _snsClient!.CreateTopicAsync(createTopicRequest).GetAwaiter().GetResult(); topicArns.Add(createTopicResponse.TopicArn); } @@ -92,13 +92,13 @@ namespace DotNetCore.CAP.AmazonSQS while (true) { - var response = _sqsClient.ReceiveMessageAsync(request, cancellationToken).GetAwaiter().GetResult(); + var response = _sqsClient!.ReceiveMessageAsync(request, cancellationToken).GetAwaiter().GetResult(); if (response.Messages.Count == 1) { var messageObj = JsonSerializer.Deserialize(response.Messages[0].Body); - var header = messageObj.MessageAttributes.ToDictionary(x => x.Key, x => x.Value.Value); + var header = messageObj!.MessageAttributes.ToDictionary(x => x.Key, x => x.Value.Value); var body = messageObj.Message; var message = new TransportMessage(header, body != null ? Encoding.UTF8.GetBytes(body) : null); @@ -119,7 +119,7 @@ namespace DotNetCore.CAP.AmazonSQS { try { - _ = _sqsClient.DeleteMessageAsync(_queueUrl, (string)sender).GetAwaiter().GetResult(); + _ = _sqsClient!.DeleteMessageAsync(_queueUrl, (string)sender).GetAwaiter().GetResult(); } catch (InvalidIdFormatException ex) { @@ -127,12 +127,12 @@ namespace DotNetCore.CAP.AmazonSQS } } - public void Reject(object sender) + public void Reject(object? sender) { try { // Visible again in 3 seconds - _ = _sqsClient.ChangeMessageVisibilityAsync(_queueUrl, (string)sender, 3).GetAwaiter().GetResult(); + _ = _sqsClient!.ChangeMessageVisibilityAsync(_queueUrl, (string)sender!, 3).GetAwaiter().GetResult(); } catch (MessageNotInflightException ex) { @@ -237,7 +237,7 @@ namespace DotNetCore.CAP.AmazonSQS { Connect(initSNS: false, initSQS: true); - var queueAttributes = await _sqsClient.GetAttributesAsync(_queueUrl).ConfigureAwait(false); + var queueAttributes = await _sqsClient!.GetAttributesAsync(_queueUrl).ConfigureAwait(false); var sqsQueueArn = queueAttributes["QueueArn"]; @@ -263,12 +263,12 @@ namespace DotNetCore.CAP.AmazonSQS private async Task SubscribeToTopics(IEnumerable topics) { - var queueAttributes = await _sqsClient.GetAttributesAsync(_queueUrl).ConfigureAwait(false); + var queueAttributes = await _sqsClient!.GetAttributesAsync(_queueUrl).ConfigureAwait(false); var sqsQueueArn = queueAttributes["QueueArn"]; foreach (var topicArn in topics) { - await _snsClient.SubscribeAsync(new SubscribeRequest + await _snsClient!.SubscribeAsync(new SubscribeRequest { TopicArn = topicArn, Protocol = "sqs", diff --git a/src/DotNetCore.CAP.AmazonSQS/CAP.AmazonSQSOptions.cs b/src/DotNetCore.CAP.AmazonSQS/CAP.AmazonSQSOptions.cs index f8dcfd5..4fdf3f2 100644 --- a/src/DotNetCore.CAP.AmazonSQS/CAP.AmazonSQSOptions.cs +++ b/src/DotNetCore.CAP.AmazonSQS/CAP.AmazonSQSOptions.cs @@ -10,19 +10,19 @@ namespace DotNetCore.CAP // ReSharper disable once InconsistentNaming public class AmazonSQSOptions { - public RegionEndpoint Region { get; set; } + public RegionEndpoint Region { get; set; } = default!; - public AWSCredentials Credentials { get; set; } + public AWSCredentials? Credentials { get; set; } /// /// Overrides Service Url deduced from AWS Region. To use in local development environments like localstack. /// - public string SNSServiceUrl { get; set; } + public string? SNSServiceUrl { get; set; } /// /// Overrides Service Url deduced from AWS Region. To use in local development environments like localstack. /// - public string SQSServiceUrl { get; set; } + public string? SQSServiceUrl { get; set; } } } \ No newline at end of file diff --git a/src/DotNetCore.CAP.AmazonSQS/DotNetCore.CAP.AmazonSQS.csproj b/src/DotNetCore.CAP.AmazonSQS/DotNetCore.CAP.AmazonSQS.csproj index 6518763..9cd09e5 100644 --- a/src/DotNetCore.CAP.AmazonSQS/DotNetCore.CAP.AmazonSQS.csproj +++ b/src/DotNetCore.CAP.AmazonSQS/DotNetCore.CAP.AmazonSQS.csproj @@ -2,12 +2,12 @@ netstandard2.1 - DotNetCore.CAP.AmazonSQS + enable $(PackageTags);AmazonSQS;SQS - bin\$(Configuration)\netstandard2.1\DotNetCore.CAP.AmazonSQS.xml + true 1701;1702;1705;CS1591 diff --git a/src/DotNetCore.CAP.AmazonSQS/ITransport.AmazonSQS.cs b/src/DotNetCore.CAP.AmazonSQS/ITransport.AmazonSQS.cs index 44c8583..0bbb8b9 100644 --- a/src/DotNetCore.CAP.AmazonSQS/ITransport.AmazonSQS.cs +++ b/src/DotNetCore.CAP.AmazonSQS/ITransport.AmazonSQS.cs @@ -22,8 +22,8 @@ namespace DotNetCore.CAP.AmazonSQS private readonly ILogger _logger; private readonly IOptions _sqsOptions; private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1); - private IAmazonSimpleNotificationService _snsClient; - private IDictionary _topicArnMaps; + private IAmazonSimpleNotificationService? _snsClient; + private IDictionary? _topicArnMaps; public AmazonSQSTransport(ILogger logger, IOptions sqsOptions) { @@ -41,7 +41,7 @@ namespace DotNetCore.CAP.AmazonSQS if (TryGetOrCreateTopicArn(message.GetName().NormalizeForAws(), out var arn)) { - string bodyJson = null; + string? bodyJson = null; if (message.Body != null) { bodyJson = Encoding.UTF8.GetString(message.Body); @@ -59,7 +59,7 @@ namespace DotNetCore.CAP.AmazonSQS MessageAttributes = attributes }; - await _snsClient.PublishAsync(request); + await _snsClient!.PublishAsync(request); _logger.LogDebug($"SNS topic message [{message.GetName().NormalizeForAws()}] has been published."); return OperateResult.Success; @@ -117,7 +117,7 @@ namespace DotNetCore.CAP.AmazonSQS { _topicArnMaps = new Dictionary(); - string nextToken = null; + string? nextToken = null; do { var topics = nextToken == null @@ -143,15 +143,15 @@ namespace DotNetCore.CAP.AmazonSQS } } - private bool TryGetOrCreateTopicArn(string topicName, out string topicArn) + private bool TryGetOrCreateTopicArn(string topicName,[System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out string? topicArn) { topicArn = null; - if (_topicArnMaps.TryGetValue(topicName, out topicArn)) + if (_topicArnMaps!.TryGetValue(topicName, out topicArn)) { return true; } - var response = _snsClient.CreateTopicAsync(topicName).GetAwaiter().GetResult(); + var response = _snsClient!.CreateTopicAsync(topicName).GetAwaiter().GetResult(); if (string.IsNullOrEmpty(response.TopicArn)) { diff --git a/src/DotNetCore.CAP.AmazonSQS/SQSReceivedMessage.cs b/src/DotNetCore.CAP.AmazonSQS/SQSReceivedMessage.cs index 893b418..53fcd64 100644 --- a/src/DotNetCore.CAP.AmazonSQS/SQSReceivedMessage.cs +++ b/src/DotNetCore.CAP.AmazonSQS/SQSReceivedMessage.cs @@ -4,15 +4,15 @@ namespace DotNetCore.CAP.AmazonSQS { class SQSReceivedMessage { - public string Message { get; set; } + public string? Message { get; set; } - public Dictionary MessageAttributes { get; set; } + public Dictionary MessageAttributes { get; set; } = default!; } class SQSReceivedMessageAttributes { - public string Type { get; set; } + public string? Type { get; set; } - public string Value { get; set; } + public string? Value { get; set; } } }