Skip to content

Commit

Permalink
Merge pull request #3133 from RLittlesII/chore/nullability/parameters
Browse files Browse the repository at this point in the history
chore: enable nullability for ParameterBase
  • Loading branch information
dansiegel committed May 10, 2024
2 parents c0fc582 + a0bff20 commit c333750
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 74 deletions.
4 changes: 2 additions & 2 deletions src/Prism.Core/Common/IParameters.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

#nullable enable
namespace Prism.Common
{
/// <summary>
Expand Down Expand Up @@ -65,6 +65,6 @@ public interface IParameters : IEnumerable<KeyValuePair<string, object>>
/// </summary>
/// <param name="key">The key of the parameter to get.</param>
/// <returns>A matching value if it exists.</returns>
object this[string key] { get; }
object? this[string key] { get; }
}
}
3 changes: 0 additions & 3 deletions src/Prism.Core/Common/ListDictionary.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System;
using System.Collections.Generic;

namespace Prism.Common
{
/// <summary>
Expand Down
19 changes: 8 additions & 11 deletions src/Prism.Core/Common/ParametersBase.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;

#nullable enable
namespace Prism.Common
{
/// <summary>
/// This is a generic parameters base class used for Dialog Parameters and Navigation Parameters.
/// </summary>
public abstract class ParametersBase : IParameters
{
private readonly List<KeyValuePair<string, object>> _entries = [];
private readonly List<KeyValuePair<string, object?>> _entries = [];

/// <summary>
/// Default constructor.
Expand Down Expand Up @@ -51,7 +48,7 @@ protected ParametersBase(string query)
}
i++;
}
string key = null;
string? key = null;
string value;
if (num4 >= 0)
{
Expand All @@ -75,7 +72,7 @@ protected ParametersBase(string query)
/// </summary>
/// <param name="key">The key for the value to be returned.</param>
/// <returns>The value of the parameter referenced by the key; otherwise <c>null</c>.</returns>
public object this[string key]
public object? this[string key]
{
get
{
Expand Down Expand Up @@ -108,7 +105,7 @@ protected ParametersBase(string query)
/// <param name="key">The key to reference this value in the parameters collection.</param>
/// <param name="value">The value of the parameter to store.</param>
public void Add(string key, object value) =>
_entries.Add(new KeyValuePair<string, object>(key, value));
_entries.Add(new KeyValuePair<string, object?>(key, value));

/// <summary>
/// Checks collection for presence of key.
Expand All @@ -122,7 +119,7 @@ protected ParametersBase(string query)
/// Gets an enumerator for the KeyValuePairs in parameter collection.
/// </summary>
/// <returns>Enumerator.</returns>
public IEnumerator<KeyValuePair<string, object>> GetEnumerator() =>
public IEnumerator<KeyValuePair<string, object?>> GetEnumerator() =>

Check warning on line 122 in src/Prism.Core/Common/ParametersBase.cs

View workflow job for this annotation

GitHub Actions / build-prism-core / Build Prism.Core

Nullability of reference types in return type of 'IEnumerator<KeyValuePair<string, object?>> ParametersBase.GetEnumerator()' doesn't match implicitly implemented member 'IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator()'.

Check warning on line 122 in src/Prism.Core/Common/ParametersBase.cs

View workflow job for this annotation

GitHub Actions / build-prism-core / Build Prism.Core

Nullability of reference types in return type of 'IEnumerator<KeyValuePair<string, object?>> ParametersBase.GetEnumerator()' doesn't match implicitly implemented member 'IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator()'.

Check warning on line 122 in src/Prism.Core/Common/ParametersBase.cs

View workflow job for this annotation

GitHub Actions / build-prism-wpf / Build Prism.Wpf

Nullability of reference types in return type of 'IEnumerator<KeyValuePair<string, object?>> ParametersBase.GetEnumerator()' doesn't match implicitly implemented member 'IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator()'.

Check warning on line 122 in src/Prism.Core/Common/ParametersBase.cs

View workflow job for this annotation

GitHub Actions / build-prism-wpf / Build Prism.Wpf

Nullability of reference types in return type of 'IEnumerator<KeyValuePair<string, object?>> ParametersBase.GetEnumerator()' doesn't match implicitly implemented member 'IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator()'.

Check warning on line 122 in src/Prism.Core/Common/ParametersBase.cs

View workflow job for this annotation

GitHub Actions / build-prism-forms / Build Prism.Forms

Nullability of reference types in return type of 'IEnumerator<KeyValuePair<string, object?>> ParametersBase.GetEnumerator()' doesn't match implicitly implemented member 'IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator()'.

Check warning on line 122 in src/Prism.Core/Common/ParametersBase.cs

View workflow job for this annotation

GitHub Actions / build-prism-forms / Build Prism.Forms

Nullability of reference types in return type of 'IEnumerator<KeyValuePair<string, object?>> ParametersBase.GetEnumerator()' doesn't match implicitly implemented member 'IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator()'.

Check warning on line 122 in src/Prism.Core/Common/ParametersBase.cs

View workflow job for this annotation

GitHub Actions / build-prism-maui / Build Prism.Maui

Nullability of reference types in return type of 'IEnumerator<KeyValuePair<string, object?>> ParametersBase.GetEnumerator()' doesn't match implicitly implemented member 'IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator()'.

Check warning on line 122 in src/Prism.Core/Common/ParametersBase.cs

View workflow job for this annotation

GitHub Actions / build-prism-maui / Build Prism.Maui

Nullability of reference types in return type of 'IEnumerator<KeyValuePair<string, object?>> ParametersBase.GetEnumerator()' doesn't match implicitly implemented member 'IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator()'.

Check warning on line 122 in src/Prism.Core/Common/ParametersBase.cs

View workflow job for this annotation

GitHub Actions / build-prism-uno / Build Prism.Uno

Nullability of reference types in return type of 'IEnumerator<KeyValuePair<string, object?>> ParametersBase.GetEnumerator()' doesn't match implicitly implemented member 'IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator()'.

Check warning on line 122 in src/Prism.Core/Common/ParametersBase.cs

View workflow job for this annotation

GitHub Actions / build-prism-uno / Build Prism.Uno

Nullability of reference types in return type of 'IEnumerator<KeyValuePair<string, object?>> ParametersBase.GetEnumerator()' doesn't match implicitly implemented member 'IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator()'.
_entries.GetEnumerator();

/// <summary>
Expand Down Expand Up @@ -181,7 +178,7 @@ public override string ToString()

queryBuilder.Append(Uri.EscapeDataString(kvp.Key));
queryBuilder.Append('=');
queryBuilder.Append(Uri.EscapeDataString(kvp.Value?.ToString() is string str ? str : string.Empty));
queryBuilder.Append(Uri.EscapeDataString(kvp.Value?.ToString() ?? string.Empty));
}
}

Expand All @@ -193,7 +190,7 @@ public override string ToString()
/// </summary>
/// <param name="parameters">An IEnumerable of KeyValuePairs to add to the current parameter list.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
public void FromParameters(IEnumerable<KeyValuePair<string, object>> parameters) =>
public void FromParameters(IEnumerable<KeyValuePair<string, object?>> parameters) =>
_entries.AddRange(parameters);
}
}
10 changes: 2 additions & 8 deletions src/Prism.Core/Common/ParametersExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.ComponentModel;
Expand Down Expand Up @@ -156,13 +157,6 @@ private static bool TryGetValueInternal(KeyValuePair<string, object> kvp, Type t
public static bool ContainsKey(this IEnumerable<KeyValuePair<string, object>> parameters, string key) =>
parameters.Any(x => string.Compare(x.Key, key, StringComparison.Ordinal) == 0);

private static object GetDefault(Type type)
{
if (type.IsValueType)
{
return Activator.CreateInstance(type);
}
return null;
}
private static object? GetDefault(Type type) => type.IsValueType ? Activator.CreateInstance(type) : null;
}
}
46 changes: 15 additions & 31 deletions src/Prism.Core/Common/UriParsingHelper.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
using System;
using System.Collections.Generic;
using Prism.Dialogs;
using Prism.Navigation;

#nullable enable
namespace Prism.Common
{
/// <summary>
/// Helper class for parsing <see cref="Uri"/> instances.
/// </summary>
public static class UriParsingHelper
{
private static readonly char[] _pathDelimiter = { '/' };
private static readonly char[] _pathDelimiter = ['/'];

/// <summary>
/// Gets the Uri segments from a deep linked Navigation Uri
Expand Down Expand Up @@ -40,10 +39,7 @@ public static Queue<string> GetUriSegments(Uri uri)
/// </summary>
/// <param name="segment">A Navigation Segment</param>
/// <returns>The navigation segment name from the provided segment.</returns>
public static string GetSegmentName(string segment)
{
return segment.Split('?')[0];
}
public static string GetSegmentName(string segment) => segment.Split('?')[0];

/// <summary>
/// Gets the Segment Parameters from a Navigation Segment that may contain a querystring
Expand Down Expand Up @@ -72,11 +68,11 @@ public static INavigationParameters GetSegmentParameters(string segment)
/// <param name="uriSegment">The <see cref="Uri"/> segment</param>
/// <param name="parameters">The existing <see cref="INavigationParameters"/>.</param>
/// <returns>The combined <see cref="INavigationParameters"/>.</returns>
public static INavigationParameters GetSegmentParameters(string uriSegment, INavigationParameters parameters)
public static INavigationParameters GetSegmentParameters(string uriSegment, INavigationParameters? parameters)
{
var navParameters = GetSegmentParameters(uriSegment);

if (parameters != null)
if (parameters is not null)
{
foreach (KeyValuePair<string, object> navigationParameter in parameters)
{
Expand Down Expand Up @@ -114,7 +110,7 @@ public static IDialogParameters GetSegmentDialogParameters(string segment)
/// <param name="uriSegment">A navigation segment which may contain a querystring.</param>
/// <param name="parameters">Existing <see cref="IDialogParameters"/>.</param>
/// <returns></returns>
public static IDialogParameters GetSegmentParameters(string uriSegment, IDialogParameters parameters)
public static IDialogParameters GetSegmentParameters(string uriSegment, IDialogParameters? parameters)
{
var dialogParameters = GetSegmentDialogParameters(uriSegment);

Expand All @@ -133,19 +129,13 @@ public static IDialogParameters GetSegmentParameters(string uriSegment, IDialogP
/// Gets the query part of <paramref name="uri"/>.
/// </summary>
/// <param name="uri">The Uri.</param>
public static string GetQuery(Uri uri)
{
return EnsureAbsolute(uri).Query;
}
public static string GetQuery(Uri uri) => EnsureAbsolute(uri).Query;

/// <summary>
/// Gets the AbsolutePath part of <paramref name="uri"/>.
/// </summary>
/// <param name="uri">The Uri.</param>
public static string GetAbsolutePath(Uri uri)
{
return EnsureAbsolute(uri).AbsolutePath;
}
public static string GetAbsolutePath(Uri uri) => EnsureAbsolute(uri).AbsolutePath;

/// <summary>
/// Parses the query of <paramref name="uri"/> into a dictionary.
Expand All @@ -166,16 +156,14 @@ public static INavigationParameters ParseQuery(Uri uri)
/// <exception cref="ArgumentNullException">Throws an <see cref="ArgumentNullException"/> when the string is null or empty.</exception>
public static Uri Parse(string uri)
{
if (uri == null) throw new ArgumentNullException(nameof(uri));

if (uri.StartsWith("/", StringComparison.Ordinal))
{
return new Uri("http://localhost" + uri, UriKind.Absolute);
}
else
if (uri == null)
{
return new Uri(uri, UriKind.RelativeOrAbsolute);
throw new ArgumentNullException(nameof(uri));
}

return uri.StartsWith("/", StringComparison.Ordinal)
? new Uri("http://localhost" + uri, UriKind.Absolute)
: new Uri(uri, UriKind.RelativeOrAbsolute);
}

/// <summary>
Expand All @@ -191,11 +179,7 @@ public static Uri EnsureAbsolute(Uri uri)
return uri;
}

if ((uri != null) && !uri.OriginalString.StartsWith("/", StringComparison.Ordinal))
{
return new Uri("http://localhost/" + uri, UriKind.Absolute);
}
return new Uri("http://localhost" + uri, UriKind.Absolute);
return !uri.OriginalString.StartsWith("/", StringComparison.Ordinal) ? new Uri("http://localhost/" + uri, UriKind.Absolute) : new Uri("http://localhost" + uri, UriKind.Absolute);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System;
using Prism.Common;
using Prism.Common;

namespace Prism.Maui.Tests.Fixtures.Common;

#nullable enable
public class UriParsingHelperFixture
{
const string _relativeUri = "MainPage?id=3&name=dan";
Expand All @@ -14,7 +13,7 @@ public class UriParsingHelperFixture
[Fact]
public void ParametersParsedFromNullSegment()
{
var parameters = UriParsingHelper.GetSegmentParameters(null);
var parameters = UriParsingHelper.GetSegmentParameters(null!);
Assert.NotNull(parameters);
}

Expand Down Expand Up @@ -62,7 +61,7 @@ public void ParametersParsedFromNavigationParametersInRelativeUri()
{ "name", "dan" }
};

var parameters = UriParsingHelper.GetSegmentParameters("MainPage" + navParameters.ToString());
var parameters = UriParsingHelper.GetSegmentParameters("MainPage" + navParameters);

Assert.NotEmpty(parameters);

Expand All @@ -82,7 +81,7 @@ public void ParametersParsedFromNavigationParametersInAbsoluteUri()
{ "name", "dan" }
};

var parameters = UriParsingHelper.GetSegmentParameters("http://www.dansiegel.net/MainPage" + navParameters.ToString());
var parameters = UriParsingHelper.GetSegmentParameters("http://www.dansiegel.net/MainPage" + navParameters);

Assert.NotEmpty(parameters);

Expand All @@ -93,6 +92,13 @@ public void ParametersParsedFromNavigationParametersInAbsoluteUri()
Assert.Equal("dan", parameters["name"]);
}

[Fact]
public void TargetNameParsedFromEmptySegment()
{
var target = UriParsingHelper.GetSegmentName(string.Empty);
Assert.Equal(string.Empty, target);
}

[Fact]
public void TargetNameParsedFromSingleSegment()
{
Expand Down Expand Up @@ -177,7 +183,7 @@ public void EnsureAbsoluteUriForAbsoluteUri()
[Fact]
public void ParseForNull()
{
var actual = Assert.Throws<ArgumentNullException>(() => UriParsingHelper.Parse(null));
var actual = Assert.Throws<ArgumentNullException>(() => UriParsingHelper.Parse(null!));
Assert.NotNull(actual);
Assert.Equal("uri", actual.ParamName);
}
Expand Down
4 changes: 1 addition & 3 deletions tests/Prism.Core.Tests/Common/ListDictionaryFixture.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;
using System.Collections.Generic;
using Prism.Common;
using Xunit;

namespace Prism.Wpf.Tests
namespace Prism.Tests.Common
{

public class ListDictionaryFixture
Expand Down
2 changes: 1 addition & 1 deletion tests/Prism.Core.Tests/Common/Mocks/MockParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Prism.Tests.Common.Mocks
{
internal class MockParameters : ParametersBase
{
public MockParameters() : base() { }
public MockParameters() { }
public MockParameters(string query) : base(query) { }
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Prism.Common;
using Prism.Common;
using Xunit;

namespace Prism.Core.Tests.Common;
namespace Prism.Tests.Common;

#nullable enable
public class MulticastExceptionHandlerFixture
Expand Down
5 changes: 2 additions & 3 deletions tests/Prism.Core.Tests/Common/ParametersFixture.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Linq;
#nullable enable
using Prism.Tests.Common.Mocks;
using Xunit;

Expand Down Expand Up @@ -28,7 +27,7 @@ public void GetValuesOfT()
{
var parameters = new MockParameters("mock=Foo&mock=2&mock=Fizz");

IEnumerable<MockEnum> values = default;
IEnumerable<MockEnum> values = [];

var ex = Record.Exception(() => values = parameters.GetValues<MockEnum>("mock"));
Assert.Null(ex);
Expand Down

0 comments on commit c333750

Please sign in to comment.