Skip to content

Commit

Permalink
Fixed warnings CS8603 and CS8625 in Encamina.Enmarcha.AI
Browse files Browse the repository at this point in the history
  • Loading branch information
HugoRamosEs committed Apr 26, 2024
1 parent 30d6f74 commit 20c0894
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>latest</LangVersion>
<Nullable>disable</Nullable>
<Nullable>enable</Nullable>
<RepositoryType>git</RepositoryType>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public interface ICognitiveServiceFactory<out TCognitiveService>
/// Gets the specific cognitive service by its given name.
/// </summary>
/// <param name="cognitiveServiceName">The cognitive service name.</param>
/// <param name="throwIfNotFound">A flag indicating wheter an expection is thrown if the cognitive service is not found.</param>
/// <param name="throwIfNotFound">A flag indicating whether an exception is thrown if the cognitive service is not found.</param>
/// <returns>A valid instances of <typeparamref name="TCognitiveService"/> found by its name.</returns>
TCognitiveService GetByName(string cognitiveServiceName, bool throwIfNotFound);
TCognitiveService? GetByName(string cognitiveServiceName, bool throwIfNotFound);
}

2 changes: 1 addition & 1 deletion src/Encamina.Enmarcha.AI.Abstractions/ITextSplitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public interface ITextSplitter
/// <param name="chunks">The collection of chunks to join.</param>
/// <param name="separator">The separator to use between chunks.</param>
/// <returns>A single string with all the chunks joined together by the specified separator.</returns>
string JoinChunks(IEnumerable<string> chunks, string separator);
string? JoinChunks(IEnumerable<string> chunks, string separator);

/// <summary>
/// Splits the specified text, using the specified length function.
Expand Down
2 changes: 1 addition & 1 deletion src/Encamina.Enmarcha.AI.Abstractions/TextSplitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public virtual IEnumerable<string> Split(string text, Func<string, int> lengthFu
/// <exception cref="ArgumentNullException">
/// Thrown when any of the parameters <paramref name="chunks"/> or <paramref name="separator"/> is <see langword="null"/>.
/// </exception>"
public virtual string JoinChunks(IEnumerable<string> chunks, string separator)
public virtual string? JoinChunks(IEnumerable<string> chunks, string separator)
{
Guard.IsNotNull(chunks);
Guard.IsNotNull(separator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,5 +226,5 @@ public float TopProbability
/// <summary>
/// Gets a unique identifier representing the end-user requesting the completion, which can help monitoring and detecting abuse.
/// </summary>
public string UserId { get; init; } = null;
public string? UserId { get; init; } = null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public virtual async Task<IReadOnlyCollection<IAnswer>> ProcessAnswersAsync(IEnu
/// <inheritdoc/>
public virtual Task<MetadataOptions> ProcessMessageAsync(string message, CancellationToken cancellationToken)
{
return ProcessMessageAsync(message, null, cancellationToken);
return ProcessMessageAsync(message, null!, cancellationToken);
}

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ internal class CachedTableStorageCompositeMetadataHandler : IMetadataHandler
private const string RegexFormat = @"(?:^|\W){0}(?:$|\W)|";

private readonly CachedTableStorageCompositeMetadataHandlerOptions options;
private readonly IMemoryCache memoryCache;
private readonly IMemoryCache? memoryCache;

/// <summary>
/// Initializes a new instance of the <see cref="CachedTableStorageCompositeMetadataHandler"/> class.
/// </summary>
/// <param name="options">Configuration options for this specific metadata handler.</param>
/// <param name="memoryCache">An optional valid instance of a memory cache to improve performance by storing parameters and values retrieved from the Table Storage.</param>
public CachedTableStorageCompositeMetadataHandler(IOptions<CachedTableStorageCompositeMetadataHandlerOptions> options, IMemoryCache memoryCache = null)
public CachedTableStorageCompositeMetadataHandler(IOptions<CachedTableStorageCompositeMetadataHandlerOptions> options, IMemoryCache? memoryCache = null)
{
this.memoryCache = memoryCache;
this.options = options?.Value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ public virtual async Task<QuestionResult> GetAnswersAsync(QuestionRequest reques
};
}

private static QueryFilters BuildQueryFilters(IQuestionRequestOptions requestOptions)
private static QueryFilters? BuildQueryFilters(IQuestionRequestOptions requestOptions)
{
if (requestOptions == null)
{
return null;
return null!;
}

var queryFilter = new QueryFilters()
Expand All @@ -92,7 +92,7 @@ private static MetadataFilter BuildMetadataFilter(MetadataOptions metadataOption
{
if (metadataOptions == null || (!metadataOptions.Metadata?.Any() ?? true))
{
return null;
return null!;
}

var metadataFilter = new MetadataFilter()
Expand Down
10 changes: 6 additions & 4 deletions src/Encamina.Enmarcha.AI/CognitiveServiceFactoryBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using CommunityToolkit.Diagnostics;
using System.Diagnostics.CodeAnalysis;

using CommunityToolkit.Diagnostics;

using Encamina.Enmarcha.AI.Abstractions;

Expand All @@ -21,7 +23,7 @@ public class CognitiveServiceFactoryBase<TCognitiveServiceBase, TCognitiveServic
/// Initializes a new instance of the <see cref="CognitiveServiceFactoryBase{TCognitiveServiceBase, TCognitiveServiceOptions}"/> class.
/// </summary>
/// <param name="configurations">The configurations for the cognitive services.</param>
/// <param name="factory">A factory to create instances of the cognitive serivces.</param>
/// <param name="factory">A factory to create instances of the cognitive services.</param>
protected CognitiveServiceFactoryBase(IOptions<ICognitiveServiceConfigurationsBase<TCognitiveServiceOptions>> configurations, Func<TCognitiveServiceOptions, TCognitiveServiceBase> factory)
{
Guard.IsNotNull(configurations);
Expand All @@ -35,7 +37,7 @@ protected CognitiveServiceFactoryBase(IOptions<ICognitiveServiceConfigurationsBa
}

/// <inheritdoc/>
public virtual TCognitiveServiceBase GetByName(string cognitiveServiceName, bool throwIfNotFound)
public virtual TCognitiveServiceBase? GetByName(string cognitiveServiceName, bool throwIfNotFound)
{
var key = cognitiveServiceName.ToUpperInvariant();

Expand All @@ -53,5 +55,5 @@ public virtual TCognitiveServiceBase GetByName(string cognitiveServiceName, bool
}

/// <inheritdoc/>
public TCognitiveServiceBase GetByName(string cognitiveServiceName) => GetByName(cognitiveServiceName, true);
public TCognitiveServiceBase GetByName(string cognitiveServiceName) => GetByName(cognitiveServiceName, true)!;
}
42 changes: 21 additions & 21 deletions src/Encamina.Enmarcha.AI/CognitiveServiceProviderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ namespace Encamina.Enmarcha.AI;
/// </summary>
public class CognitiveServiceProviderBase : ICognitiveServiceProvider
{
private readonly ICognitiveServiceFactory<IConversationAnalysisService> conversationAnalysisServiceFactory;
private readonly ICognitiveServiceFactory<ILanguageDetectionService> languageDetectionServiceFactory;
private readonly ICognitiveServiceFactory<IQuestionAnsweringService> questionsAnsweringServiceFactory;
private readonly ICognitiveServiceFactory<ITextTranslationService> textTranslationServiceFactory;
private readonly ICognitiveServiceFactory<IIntentPredictionService> intentPredictionServiceFactory;
private readonly ICognitiveServiceFactory<IConversationAnalysisService>? conversationAnalysisServiceFactory;
private readonly ICognitiveServiceFactory<IIntentPredictionService>? intentPredictionServiceFactory;
private readonly ICognitiveServiceFactory<ILanguageDetectionService>? languageDetectionServiceFactory;
private readonly ICognitiveServiceFactory<IQuestionAnsweringService>? questionsAnsweringServiceFactory;
private readonly ICognitiveServiceFactory<ITextTranslationService>? textTranslationServiceFactory;

/// <summary>
/// Initializes a new instance of the <see cref="CognitiveServiceProviderBase"/> class.
Expand All @@ -30,11 +30,11 @@ public class CognitiveServiceProviderBase : ICognitiveServiceProvider
/// <param name="textTranslationServiceFactory">>A cognitive service factory for text translation cognitive services.</param>
[SuppressMessage(@"Critical Code Smell", @"S2360:Optional parameters should not be used", Justification = @"Optional parameters allows supporting conditional dependency injection!")]
protected CognitiveServiceProviderBase(
ICognitiveServiceFactory<IConversationAnalysisService> conversationAnalysisServiceFactory = null,
ICognitiveServiceFactory<IIntentPredictionService> intentPredictionServiceFactory = null,
ICognitiveServiceFactory<ILanguageDetectionService> languageDetectionServiceFactory = null,
ICognitiveServiceFactory<IQuestionAnsweringService> questionsAnsweringServiceFactory = null,
ICognitiveServiceFactory<ITextTranslationService> textTranslationServiceFactory = null)
ICognitiveServiceFactory<IConversationAnalysisService>? conversationAnalysisServiceFactory = null,
ICognitiveServiceFactory<IIntentPredictionService>? intentPredictionServiceFactory = null,
ICognitiveServiceFactory<ILanguageDetectionService>? languageDetectionServiceFactory = null,
ICognitiveServiceFactory<IQuestionAnsweringService>? questionsAnsweringServiceFactory = null,
ICognitiveServiceFactory<ITextTranslationService>? textTranslationServiceFactory = null)
{
this.conversationAnalysisServiceFactory = conversationAnalysisServiceFactory;
this.intentPredictionServiceFactory = intentPredictionServiceFactory;
Expand All @@ -44,56 +44,56 @@ protected CognitiveServiceProviderBase(
}

/// <inheritdoc/>
public virtual IConversationAnalysisService GetConversationAnalysisService(string serviceName) => GetByName(conversationAnalysisServiceFactory, serviceName);
public virtual IConversationAnalysisService GetConversationAnalysisService(string serviceName) => GetByName(conversationAnalysisServiceFactory!, serviceName);

/// <inheritdoc/>
public virtual ILanguageDetectionService GetLanguageDetectionService(string serviceName) => GetByName(languageDetectionServiceFactory, serviceName);
public virtual ILanguageDetectionService GetLanguageDetectionService(string serviceName) => GetByName(languageDetectionServiceFactory!, serviceName);

/// <inheritdoc/>
public virtual IQuestionAnsweringService GetQuestionsAnsweringService(string serviceName) => GetByName(questionsAnsweringServiceFactory, serviceName);
public virtual IQuestionAnsweringService GetQuestionsAnsweringService(string serviceName) => GetByName(questionsAnsweringServiceFactory!, serviceName);

/// <inheritdoc/>
public virtual ITextTranslationService GetTextTranslationService(string serviceName) => GetByName(textTranslationServiceFactory, serviceName);
public virtual ITextTranslationService GetTextTranslationService(string serviceName) => GetByName(textTranslationServiceFactory!, serviceName);

/// <inheritdoc/>
public virtual IIntentPredictionService GetIntentPredictionService(string serviceName) => GetByName(intentPredictionServiceFactory, serviceName);
public virtual IIntentPredictionService GetIntentPredictionService(string serviceName) => GetByName(intentPredictionServiceFactory!, serviceName);

/// <inheritdoc/>
public bool TryGetConversationAnalysisService(string serviceName, out IConversationAnalysisService service)
{
service = GetByName(conversationAnalysisServiceFactory, serviceName, false);
service = GetByName(conversationAnalysisServiceFactory!, serviceName, false);

return service != null;
}

/// <inheritdoc/>
public bool TryGetLanguageDetectionService(string serviceName, out ILanguageDetectionService service)
{
service = GetByName(languageDetectionServiceFactory, serviceName, false);
service = GetByName(languageDetectionServiceFactory!, serviceName, false);

return service != null;
}

/// <inheritdoc/>
public bool TryGetQuestionsAnsweringService(string serviceName, out IQuestionAnsweringService service)
{
service = GetByName(questionsAnsweringServiceFactory, serviceName, false);
service = GetByName(questionsAnsweringServiceFactory!, serviceName, false);

return service != null;
}

/// <inheritdoc/>
public bool TryGetTextTranslationService(string serviceName, out ITextTranslationService service)
{
service = GetByName(textTranslationServiceFactory, serviceName, false);
service = GetByName(textTranslationServiceFactory!, serviceName, false);

return service != null;
}

/// <inheritdoc/>
public bool TryGetIntentPredictionService(string serviceName, out IIntentPredictionService service)
{
service = GetByName(intentPredictionServiceFactory, serviceName, false);
service = GetByName(intentPredictionServiceFactory!, serviceName, false);

return service != null;
}
Expand All @@ -102,7 +102,7 @@ private static TCognitiveService GetByName<TCognitiveService>(ICognitiveServiceF
where TCognitiveService : class, ICognitiveService
{
return cognitiveServiceFactory != null
? cognitiveServiceFactory.GetByName(serviceName, throwIfNotFound)
? cognitiveServiceFactory.GetByName(serviceName, throwIfNotFound)!
: throw new InvalidOperationException(string.Format(Resources.ExceptionMessages.NotConfiguredCognitiveServiceFactory, typeof(TCognitiveService).Name));
}
}
10 changes: 5 additions & 5 deletions src/Encamina.Enmarcha.AI/DefaultCognitiveServiceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ internal sealed class DefaultCognitiveServiceProvider : CognitiveServiceProvider
/// <param name="questionsAnsweringServiceFactory">>A cognitive service factory for questions answering cognitive services.</param>
/// <param name="textTranslationServiceFactory">>A cognitive service factory for text translation cognitive services.</param>
public DefaultCognitiveServiceProvider(
ICognitiveServiceFactory<IConversationAnalysisService> conversationAnalysisServiceFactory = null,
ICognitiveServiceFactory<IIntentPredictionService> intentPredictionServiceFactory = null,
ICognitiveServiceFactory<ILanguageDetectionService> languageDetectionServiceFactory = null,
ICognitiveServiceFactory<IQuestionAnsweringService> questionsAnsweringServiceFactory = null,
ICognitiveServiceFactory<ITextTranslationService> textTranslationServiceFactory = null)
ICognitiveServiceFactory<IConversationAnalysisService>? conversationAnalysisServiceFactory = null,
ICognitiveServiceFactory<IIntentPredictionService>? intentPredictionServiceFactory = null,
ICognitiveServiceFactory<ILanguageDetectionService>? languageDetectionServiceFactory = null,
ICognitiveServiceFactory<IQuestionAnsweringService>? questionsAnsweringServiceFactory = null,
ICognitiveServiceFactory<ITextTranslationService>? textTranslationServiceFactory = null)
: base(conversationAnalysisServiceFactory, intentPredictionServiceFactory, languageDetectionServiceFactory, questionsAnsweringServiceFactory, textTranslationServiceFactory)
{
}
Expand Down

0 comments on commit 20c0894

Please sign in to comment.