Skip to content

Commit

Permalink
Removed out parameter from async method which is not supported (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
Carael committed Apr 4, 2023
1 parent 6ecc424 commit 373722c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.
22 changes: 12 additions & 10 deletions samples/StarWars/DictionaryResourcesProvider.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -68,30 +67,33 @@ public DictionaryResourcesProvider()
};
}

private IReadOnlyDictionary<Language, IReadOnlyDictionary<string, string>> _masterDictionary;
private readonly IReadOnlyDictionary<Language, IReadOnlyDictionary<string, string>>
_masterDictionary;

public Task<bool> TryGetResourceAsync(
public Task<Resource?> TryGetResourceAsync(
string key,
CultureInfo culture,
[NotNullWhen(returnValue: true)] out Resource? res,
CancellationToken cancellationToken)
{
res = null;
return Task.FromResult(TryGetResource(key, culture));
}

private Resource? TryGetResource(string key, CultureInfo culture)
{
Language language = ToLanguage(culture);

if (!_masterDictionary.ContainsKey(language))
{
return Task.FromResult(false);
return null;
}
if (!_masterDictionary[language].ContainsKey(key))
{
return Task.FromResult(false);
return null;
}

string label = _masterDictionary[language][key];
res = new Resource(key, label);

return Task.FromResult(true);
return new Resource(key, label);
}

private static Language ToLanguage(CultureInfo culture)
Expand All @@ -109,4 +111,4 @@ private static Language ToLanguage(CultureInfo culture)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public async Task TryGetTranslationAsString_WhenResourceFound_ShouldReturnResour
//Arrange dependencies
var provider = new Mock<IResourcesProvider>(MockBehavior.Strict);
provider
.Setup(p => p.TryGetResourceAsync(key, culture, out resource, It.IsAny<CancellationToken>()))
.ReturnsAsync(true);
.Setup(p => p.TryGetResourceAsync(key, culture, It.IsAny<CancellationToken>()))
.ReturnsAsync(resource);

var adapter = new ResourcesProviderAdapter(provider.Object, observers: default!);

Expand All @@ -47,8 +47,9 @@ public async Task TryGetTranslationAsString_WhenResourceNotFound_ShouldReturnFal
//Arrange dependencies
var provider = new Mock<IResourcesProvider>(MockBehavior.Strict);
provider
.Setup(p => p.TryGetResourceAsync(key, culture, out resource, It.IsAny<CancellationToken>()))
.ReturnsAsync(false);
.Setup(p => p.TryGetResourceAsync(key, culture, It.IsAny<CancellationToken>()))
.ReturnsAsync(resource);

var observer = new DummyObserver();

var adapter = new ResourcesProviderAdapter(provider.Object, observers: new[] { observer });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -7,10 +6,9 @@ namespace HotChocolate.Extensions.Translation.Resources
{
public interface IResourcesProvider
{
Task<bool> TryGetResourceAsync(
Task<Resource?> TryGetResourceAsync(
string key,
CultureInfo culture,
[NotNullWhen(returnValue:true)] out Resource? res,
CancellationToken cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ public async Task<string> TryGetTranslationAsStringAsync(
string fallbackValue,
CancellationToken cancellationToken)
{
if (await _resourcesProvider
.TryGetResourceAsync(key, culture, out Resource? res, cancellationToken)
.ConfigureAwait(false))
var res = await _resourcesProvider
.TryGetResourceAsync(key, culture, cancellationToken)
.ConfigureAwait(false);

if (res is { })
{
return res.Value;
}
Expand All @@ -39,4 +41,4 @@ public async Task<string> TryGetTranslationAsStringAsync(
return fallbackValue;
}
}
}
}

0 comments on commit 373722c

Please sign in to comment.