Skip to content

Commit

Permalink
chore: enable nullability for ParameterBase
Browse files Browse the repository at this point in the history
enable nullability
rename namespaces
remove unnecessary usings
  • Loading branch information
RLittlesII committed May 8, 2024
1 parent c0fc582 commit 425799d
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 81 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 @@ public object this[string key]
/// <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 @@ public bool ContainsKey(string key) =>
/// 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-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-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-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;
}
}
44 changes: 9 additions & 35 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,7 +68,7 @@ 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);

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 @@ -164,19 +154,7 @@ public static INavigationParameters ParseQuery(Uri uri)
/// <param name="uri">A uri string.</param>
/// <returns>A <see cref="Uri"/>.</returns>
/// <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
{
return new Uri(uri, UriKind.RelativeOrAbsolute);
}
}
public static Uri Parse(string uri) => uri.StartsWith("/", StringComparison.Ordinal) ? new Uri("http://localhost" + uri, UriKind.Absolute) : new Uri(uri, UriKind.RelativeOrAbsolute);

/// <summary>
/// This will provide the existing <see cref="Uri"/> if it is already Absolute, otherwise
Expand All @@ -191,11 +169,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 Down Expand Up @@ -177,7 +176,7 @@ public void EnsureAbsoluteUriForAbsoluteUri()
[Fact]
public void ParseForNull()
{
var actual = Assert.Throws<ArgumentNullException>(() => UriParsingHelper.Parse(null));
var actual = Assert.Throws<ArgumentNullException>(() => UriParsingHelper.Parse(null!));

Check failure on line 179 in tests/Maui/Prism.Maui.Tests/Fixtures/Common/UriParsingHelperFixture.cs

View workflow job for this annotation

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

Prism.Maui.Tests.Fixtures.Common.UriParsingHelperFixture.ParseForNull

Assert.Throws() Failure: Exception type was not an exact match Expected: typeof(System.ArgumentNullException) Actual: typeof(System.NullReferenceException) ---- System.NullReferenceException : Object reference not set to an instance of an object.
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.Core.Tests.Common
{

public class ListDictionaryFixture
Expand Down
2 changes: 1 addition & 1 deletion tests/Prism.Core.Tests/Common/Mocks/MockEnum.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Prism.Tests.Common.Mocks
namespace Prism.Core.Tests.Common.Mocks
{
internal enum MockEnum
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Prism.Core.Tests/Common/Mocks/MockParameters.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using Prism.Common;

namespace Prism.Tests.Common.Mocks
namespace Prism.Core.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,7 +1,4 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Prism.Common;
using Prism.Common;
using Xunit;

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

namespace Prism.Tests.Common
namespace Prism.Core.Tests.Common
{
public class ParametersFixture
{
Expand All @@ -28,7 +27,7 @@ public void GetValuesOfT()
{
var parameters = new MockParameters("mock=Foo&mock=2&mock=Fizz");

IEnumerable<MockEnum> values = default;
IEnumerable<MockEnum> values = Enumerable.Empty<MockEnum>();

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

0 comments on commit 425799d

Please sign in to comment.