Skip to content

Commit

Permalink
Added nullable reference types
Browse files Browse the repository at this point in the history
  • Loading branch information
chrissainty committed Feb 9, 2024
1 parent 9b90571 commit be3ef76
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static class BUnitLocalStorageTestExtensions
public static ILocalStorageService AddBlazoredLocalStorage(this TestContextBase context)
=> AddBlazoredLocalStorage(context, null);

public static ILocalStorageService AddBlazoredLocalStorage(this TestContextBase context, Action<LocalStorageOptions> configure)
public static ILocalStorageService AddBlazoredLocalStorage(this TestContextBase context, Action<LocalStorageOptions>? configure)
{
if (context is null)
throw new ArgumentNullException(nameof(context));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<Nullable>enable</Nullable>

<Authors>Chris Sainty</Authors>
<Company>Chris Sainty</Company>
Expand Down
29 changes: 13 additions & 16 deletions src/Blazored.LocalStorage.TestExtensions/InMemoryStorageProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Blazored.LocalStorage.TestExtensions
{
internal class InMemoryStorageProvider : IStorageProvider
{
private readonly Dictionary<string, string> _dataStore = new Dictionary<string, string>();
private readonly Dictionary<string, string> _dataStore = new();

public void Clear()
=> _dataStore.Clear();
Expand All @@ -22,34 +22,31 @@ public bool ContainKey(string key)
=> _dataStore.ContainsKey(key);

public ValueTask<bool> ContainKeyAsync(string key, CancellationToken cancellationToken = default)
=> new ValueTask<bool>(ContainKey(key));
=> new(ContainKey(key));

public string GetItem(string key)
public string? GetItem(string key)
=> _dataStore.ContainsKey(key) ? _dataStore[key] : default;

public ValueTask<string> GetItemAsync(string key, CancellationToken cancellationToken = default)
=> new ValueTask<string>(GetItem(key));
public ValueTask<string?> GetItemAsync(string key, CancellationToken cancellationToken = default)
=> new(GetItem(key));

public string Key(int index)
public string? Key(int index)
=> index > _dataStore.Count - 1 ? default : _dataStore.ElementAt(index).Key;

public ValueTask<string> KeyAsync(int index, CancellationToken cancellationToken = default)
=> new ValueTask<string>(Key(index));
public ValueTask<string?> KeyAsync(int index, CancellationToken cancellationToken = default)
=> new(Key(index));

public IEnumerable<string> Keys()
{
return _dataStore.Keys.ToList();
}

public ValueTask<IEnumerable<string>> KeysAsync(CancellationToken cancellationToken = default)
=> new ValueTask<IEnumerable<string>>(_dataStore.Keys.ToList());
public IEnumerable<string> Keys()
=> _dataStore.Keys.ToList();

public ValueTask<IEnumerable<string>> KeysAsync(CancellationToken cancellationToken = default)
=> new(_dataStore.Keys.ToList());

public int Length()
=> _dataStore.Count;

public ValueTask<int> LengthAsync(CancellationToken cancellationToken = default)
=> new ValueTask<int>(Length());
=> new(Length());

public void RemoveItem(string key)
=> _dataStore.Remove(key);
Expand Down
1 change: 1 addition & 0 deletions src/Blazored.LocalStorage/Blazored.LocalStorage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<Nullable>enable</Nullable>

<Authors>Chris Sainty</Authors>
<Company>Chris Sainty</Company>
Expand Down
14 changes: 8 additions & 6 deletions src/Blazored.LocalStorage/BrowserStorageProvider.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.JSInterop;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
using Blazored.LocalStorage.Exceptions;
Expand All @@ -12,7 +13,7 @@ internal class BrowserStorageProvider : IStorageProvider
private const string StorageNotAvailableMessage = "Unable to access the browser storage. This is most likely due to the browser settings.";

private readonly IJSRuntime _jSRuntime;
private readonly IJSInProcessRuntime _jSInProcessRuntime;
private readonly IJSInProcessRuntime? _jSInProcessRuntime;

public BrowserStorageProvider(IJSRuntime jSRuntime)
{
Expand All @@ -37,11 +38,11 @@ public async ValueTask ClearAsync(CancellationToken cancellationToken = default)
}
}

public async ValueTask<string> GetItemAsync(string key, CancellationToken cancellationToken = default)
public async ValueTask<string?> GetItemAsync(string key, CancellationToken cancellationToken = default)
{
try
{
return await _jSRuntime.InvokeAsync<string>("localStorage.getItem", cancellationToken, key);
return await _jSRuntime.InvokeAsync<string?>("localStorage.getItem", cancellationToken, key);
}
catch (Exception exception)
{
Expand All @@ -54,11 +55,11 @@ public async ValueTask<string> GetItemAsync(string key, CancellationToken cancel
}
}

public async ValueTask<string> KeyAsync(int index, CancellationToken cancellationToken = default)
public async ValueTask<string?> KeyAsync(int index, CancellationToken cancellationToken = default)
{
try
{
return await _jSRuntime.InvokeAsync<string>("localStorage.key", cancellationToken, index);
return await _jSRuntime.InvokeAsync<string?>("localStorage.key", cancellationToken, index);
}
catch (Exception exception)
{
Expand Down Expand Up @@ -175,7 +176,7 @@ public async ValueTask RemoveItemsAsync(IEnumerable<string> keys, CancellationTo
throw;
}
}

public void Clear()
{
CheckForInProcessRuntime();
Expand Down Expand Up @@ -341,6 +342,7 @@ public IEnumerable<string> Keys()
}
}

[MemberNotNull(nameof(_jSInProcessRuntime))]
private void CheckForInProcessRuntime()
{
if (_jSInProcessRuntime == null)
Expand Down
8 changes: 4 additions & 4 deletions src/Blazored.LocalStorage/ChangedEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace Blazored.LocalStorage
[ExcludeFromCodeCoverage]
public class ChangedEventArgs
{
public string Key { get; set; }
public object OldValue { get; set; }
public object NewValue { get; set; }
public string Key { get; set; } = null!; // Since .NET 6 is supported, `required` is not available yet
public object? OldValue { get; set; }
public object? NewValue { get; set; }
}
}
}
6 changes: 3 additions & 3 deletions src/Blazored.LocalStorage/ILocalStorageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public interface ILocalStorageService
/// (<see cref="JSRuntime.DefaultAsyncTimeout"/>) from being applied.
/// </param>
/// <returns>A <see cref="ValueTask"/> representing the completion of the operation.</returns>
ValueTask<T> GetItemAsync<T>(string key, CancellationToken cancellationToken = default);
ValueTask<T?> GetItemAsync<T>(string key, CancellationToken cancellationToken = default);

/// <summary>
/// Retrieve the specified data from local storage as a <see cref="string"/>.
Expand All @@ -33,7 +33,7 @@ public interface ILocalStorageService
/// (<see cref="JSRuntime.DefaultAsyncTimeout"/>) from being applied.
/// </param>
/// <returns>A <see cref="ValueTask"/> representing the completion of the operation.</returns>
ValueTask<string> GetItemAsStringAsync(string key, CancellationToken cancellationToken = default);
ValueTask<string?> GetItemAsStringAsync(string key, CancellationToken cancellationToken = default);

/// <summary>
/// Return the name of the key at the specified <paramref name="index"/>.
Expand All @@ -44,7 +44,7 @@ public interface ILocalStorageService
/// (<see cref="JSRuntime.DefaultAsyncTimeout"/>) from being applied.
/// </param>
/// <returns>A <see cref="ValueTask"/> representing the completion of the operation.</returns>
ValueTask<string> KeyAsync(int index, CancellationToken cancellationToken = default);
ValueTask<string?> KeyAsync(int index, CancellationToken cancellationToken = default);

/// <summary>
/// Returns a collection of strings representing the names of the keys in the local storage.
Expand Down
8 changes: 4 additions & 4 deletions src/Blazored.LocalStorage/IStorageProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ internal interface IStorageProvider
ValueTask ClearAsync(CancellationToken cancellationToken = default);
bool ContainKey(string key);
ValueTask<bool> ContainKeyAsync(string key, CancellationToken cancellationToken = default);
string GetItem(string key);
ValueTask<string> GetItemAsync(string key, CancellationToken cancellationToken = default);
string Key(int index);
ValueTask<string> KeyAsync(int index, CancellationToken cancellationToken = default);
string? GetItem(string key);
ValueTask<string?> GetItemAsync(string key, CancellationToken cancellationToken = default);
string? Key(int index);
ValueTask<string?> KeyAsync(int index, CancellationToken cancellationToken = default);
IEnumerable<string> Keys();
ValueTask<IEnumerable<string>> KeysAsync(CancellationToken cancellationToken = default);
int Length();
Expand Down
6 changes: 3 additions & 3 deletions src/Blazored.LocalStorage/ISyncLocalStorageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ public interface ISyncLocalStorageService
/// </summary>
/// <param name="key">A <see cref="string"/> value specifying the name of the local storage slot to use</param>
/// <returns>The data from the specified <paramref name="key"/> as a <typeparamref name="T"/></returns>
T GetItem<T>(string key);
T? GetItem<T>(string key);

/// <summary>
/// Retrieve the specified data from local storage as a <see cref="string"/>.
/// </summary>
/// <param name="key">A <see cref="string"/> value specifying the name of the storage slot to use</param>
/// <returns>The data associated with the specified <paramref name="key"/> as a <see cref="string"/></returns>
string GetItemAsString(string key);
string? GetItemAsString(string key);

/// <summary>
/// Return the name of the key at the specified <paramref name="index"/>.
/// </summary>
/// <param name="index"></param>
/// <returns>The name of the key at the specified <paramref name="index"/></returns>
string Key(int index);
string? Key(int index);

/// <summary>
/// Checks if the <paramref name="key"/> exists in local storage, but does not check its value.
Expand Down
29 changes: 14 additions & 15 deletions src/Blazored.LocalStorage/LocalStorageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public async ValueTask SetItemAsStringAsync(string key, string data, Cancellatio
RaiseOnChanged(key, e.OldValue, data);
}

public async ValueTask<T> GetItemAsync<T>(string key, CancellationToken cancellationToken = default)
public async ValueTask<T?> GetItemAsync<T>(string key, CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(nameof(key));
Expand All @@ -74,7 +74,7 @@ public async ValueTask<T> GetItemAsync<T>(string key, CancellationToken cancella
}
}

public ValueTask<string> GetItemAsStringAsync(string key, CancellationToken cancellationToken = default)
public ValueTask<string?> GetItemAsStringAsync(string key, CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(nameof(key));
Expand All @@ -96,7 +96,7 @@ public ValueTask ClearAsync(CancellationToken cancellationToken = default)
public ValueTask<int> LengthAsync(CancellationToken cancellationToken = default)
=> _storageProvider.LengthAsync(cancellationToken);

public ValueTask<string> KeyAsync(int index, CancellationToken cancellationToken = default)
public ValueTask<string?> KeyAsync(int index, CancellationToken cancellationToken = default)
=> _storageProvider.KeyAsync(index, cancellationToken);

public ValueTask<IEnumerable<string>> KeysAsync(CancellationToken cancellationToken = default)
Expand Down Expand Up @@ -134,8 +134,7 @@ public void SetItemAsString(string key, string data)
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(nameof(key));

if (data is null)
throw new ArgumentNullException(nameof(data));
ArgumentNullException.ThrowIfNull(data);

var e = RaiseOnChangingSync(key, data);

Expand All @@ -147,7 +146,7 @@ public void SetItemAsString(string key, string data)
RaiseOnChanged(key, e.OldValue, data);
}

public T GetItem<T>(string key)
public T? GetItem<T>(string key)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(nameof(key));
Expand All @@ -169,7 +168,7 @@ public T GetItem<T>(string key)
}
}

public string GetItemAsString(string key)
public string? GetItemAsString(string key)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(nameof(key));
Expand Down Expand Up @@ -202,14 +201,14 @@ public void Clear()
public int Length()
=> _storageProvider.Length();

public string Key(int index)
public string? Key(int index)
=> _storageProvider.Key(index);

public bool ContainKey(string key)
=> _storageProvider.ContainKey(key);

public event EventHandler<ChangingEventArgs> Changing;
private async Task<ChangingEventArgs> RaiseOnChangingAsync(string key, object data)
public event EventHandler<ChangingEventArgs>? Changing;
private async Task<ChangingEventArgs> RaiseOnChangingAsync(string key, object? data)
{
var e = new ChangingEventArgs
{
Expand All @@ -223,7 +222,7 @@ private async Task<ChangingEventArgs> RaiseOnChangingAsync(string key, object da
return e;
}

private ChangingEventArgs RaiseOnChangingSync(string key, object data)
private ChangingEventArgs RaiseOnChangingSync(string key, object? data)
{
var e = new ChangingEventArgs
{
Expand All @@ -237,7 +236,7 @@ private ChangingEventArgs RaiseOnChangingSync(string key, object data)
return e;
}

private async Task<T> GetItemInternalAsync<T>(string key, CancellationToken cancellationToken = default)
private async Task<T?> GetItemInternalAsync<T>(string key, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(key))
throw new ArgumentNullException(nameof(key));
Expand All @@ -256,7 +255,7 @@ private async Task<T> GetItemInternalAsync<T>(string key, CancellationToken canc
}
}

private object GetItemInternal(string key)
private object? GetItemInternal(string key)
{
if (string.IsNullOrEmpty(key))
throw new ArgumentNullException(nameof(key));
Expand All @@ -276,8 +275,8 @@ private object GetItemInternal(string key)
}
}

public event EventHandler<ChangedEventArgs> Changed;
private void RaiseOnChanged(string key, object oldValue, object data)
public event EventHandler<ChangedEventArgs>? Changed;
private void RaiseOnChanged(string key, object? oldValue, object? data)
{
var e = new ChangedEventArgs
{
Expand Down
2 changes: 1 addition & 1 deletion src/Blazored.LocalStorage/Serialization/IJsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ namespace Blazored.LocalStorage.Serialization
public interface IJsonSerializer
{
string Serialize<T>(T obj);
T Deserialize<T>(string text);
T? Deserialize<T>(string text);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ public SystemTextJsonSerializer(IOptions<LocalStorageOptions> options)
_options = options.Value.JsonSerializerOptions;
}

public SystemTextJsonSerializer(LocalStorageOptions localStorageOptions)
{
_options = localStorageOptions.JsonSerializerOptions;
public SystemTextJsonSerializer(LocalStorageOptions localStorageOptions)
{
_options = localStorageOptions.JsonSerializerOptions;
}

public T Deserialize<T>(string data)
public T? Deserialize<T>(string data)
=> JsonSerializer.Deserialize<T>(data, _options);

public string Serialize<T>(T data)
Expand Down
4 changes: 2 additions & 2 deletions src/Blazored.LocalStorage/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static class ServiceCollectionExtensions
public static IServiceCollection AddBlazoredLocalStorage(this IServiceCollection services)
=> AddBlazoredLocalStorage(services, null);

public static IServiceCollection AddBlazoredLocalStorage(this IServiceCollection services, Action<LocalStorageOptions> configure)
public static IServiceCollection AddBlazoredLocalStorage(this IServiceCollection services, Action<LocalStorageOptions>? configure)
{
services.TryAddScoped<IJsonSerializer, SystemTextJsonSerializer>();
services.TryAddScoped<IStorageProvider, BrowserStorageProvider>();
Expand Down Expand Up @@ -49,7 +49,7 @@ public static IServiceCollection AddBlazoredLocalStorageAsSingleton(this IServic
/// <param name="services"></param>
/// <param name="configure"></param>
/// <returns></returns>
public static IServiceCollection AddBlazoredLocalStorageAsSingleton(this IServiceCollection services, Action<LocalStorageOptions> configure)
public static IServiceCollection AddBlazoredLocalStorageAsSingleton(this IServiceCollection services, Action<LocalStorageOptions>? configure)
{
services.TryAddSingleton<IJsonSerializer, SystemTextJsonSerializer>();
services.TryAddSingleton<IStorageProvider, BrowserStorageProvider>();
Expand Down

0 comments on commit be3ef76

Please sign in to comment.