Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: enable nullability for ParameterBase #3133

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
}
i++;
}
string key = null;
string? key = null;
string value;
if (num4 >= 0)
{
Expand All @@ -75,7 +72,7 @@
/// </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 @@
/// <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 @@ -116,13 +113,13 @@
/// <param name="key">The key to check in the collection.</param>
/// <returns><c>true</c> if key exists; else returns <c>false</c>.</returns>
public bool ContainsKey(string key) =>
_entries.ContainsKey(key);

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

View workflow job for this annotation

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

Argument of type 'List<KeyValuePair<string, object?>>' cannot be used for parameter 'parameters' of type 'IEnumerable<KeyValuePair<string, object>>' in 'bool ParametersExtensions.ContainsKey(IEnumerable<KeyValuePair<string, object>> parameters, string key)' due to differences in the nullability of reference types.

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

View workflow job for this annotation

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

Argument of type 'List<KeyValuePair<string, object?>>' cannot be used for parameter 'parameters' of type 'IEnumerable<KeyValuePair<string, object>>' in 'bool ParametersExtensions.ContainsKey(IEnumerable<KeyValuePair<string, object>> parameters, string key)' due to differences in the nullability of reference types.

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

View workflow job for this annotation

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

Argument of type 'List<KeyValuePair<string, object?>>' cannot be used for parameter 'parameters' of type 'IEnumerable<KeyValuePair<string, object>>' in 'bool ParametersExtensions.ContainsKey(IEnumerable<KeyValuePair<string, object>> parameters, string key)' due to differences in the nullability of reference types.

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

View workflow job for this annotation

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

Argument of type 'List<KeyValuePair<string, object?>>' cannot be used for parameter 'parameters' of type 'IEnumerable<KeyValuePair<string, object>>' in 'bool ParametersExtensions.ContainsKey(IEnumerable<KeyValuePair<string, object>> parameters, string key)' due to differences in the nullability of reference types.

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

View workflow job for this annotation

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

Argument of type 'List<KeyValuePair<string, object?>>' cannot be used for parameter 'parameters' of type 'IEnumerable<KeyValuePair<string, object>>' in 'bool ParametersExtensions.ContainsKey(IEnumerable<KeyValuePair<string, object>> parameters, string key)' due to differences in the nullability of reference types.

/// <summary>
/// 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 All @@ -132,7 +129,7 @@
/// <param name="key">The key for the value to be returned.</param>
/// <returns>Returns a matching parameter of <typeparamref name="T"/> if one exists in the Collection.</returns>
public T GetValue<T>(string key) =>
_entries.GetValue<T>(key);

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

View workflow job for this annotation

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

Argument of type 'List<KeyValuePair<string, object?>>' cannot be used for parameter 'parameters' of type 'IEnumerable<KeyValuePair<string, object>>' in 'T ParametersExtensions.GetValue<T>(IEnumerable<KeyValuePair<string, object>> parameters, string key)' due to differences in the nullability of reference types.

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

View workflow job for this annotation

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

Argument of type 'List<KeyValuePair<string, object?>>' cannot be used for parameter 'parameters' of type 'IEnumerable<KeyValuePair<string, object>>' in 'T ParametersExtensions.GetValue<T>(IEnumerable<KeyValuePair<string, object>> parameters, string key)' due to differences in the nullability of reference types.

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

View workflow job for this annotation

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

Argument of type 'List<KeyValuePair<string, object?>>' cannot be used for parameter 'parameters' of type 'IEnumerable<KeyValuePair<string, object>>' in 'T ParametersExtensions.GetValue<T>(IEnumerable<KeyValuePair<string, object>> parameters, string key)' due to differences in the nullability of reference types.

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

View workflow job for this annotation

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

Argument of type 'List<KeyValuePair<string, object?>>' cannot be used for parameter 'parameters' of type 'IEnumerable<KeyValuePair<string, object>>' in 'T ParametersExtensions.GetValue<T>(IEnumerable<KeyValuePair<string, object>> parameters, string key)' due to differences in the nullability of reference types.

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

View workflow job for this annotation

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

Argument of type 'List<KeyValuePair<string, object?>>' cannot be used for parameter 'parameters' of type 'IEnumerable<KeyValuePair<string, object>>' in 'T ParametersExtensions.GetValue<T>(IEnumerable<KeyValuePair<string, object>> parameters, string key)' due to differences in the nullability of reference types.

/// <summary>
/// Returns an IEnumerable of all parameters.
Expand All @@ -141,7 +138,7 @@
/// <param name="key">The key for the values to be returned.</param>
///<returns>Returns a IEnumerable of all the instances of type <typeparamref name="T"/>.</returns>
public IEnumerable<T> GetValues<T>(string key) =>
_entries.GetValues<T>(key);

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

View workflow job for this annotation

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

Argument of type 'List<KeyValuePair<string, object?>>' cannot be used for parameter 'parameters' of type 'IEnumerable<KeyValuePair<string, object>>' in 'IEnumerable<T> ParametersExtensions.GetValues<T>(IEnumerable<KeyValuePair<string, object>> parameters, string key)' due to differences in the nullability of reference types.

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

View workflow job for this annotation

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

Argument of type 'List<KeyValuePair<string, object?>>' cannot be used for parameter 'parameters' of type 'IEnumerable<KeyValuePair<string, object>>' in 'IEnumerable<T> ParametersExtensions.GetValues<T>(IEnumerable<KeyValuePair<string, object>> parameters, string key)' due to differences in the nullability of reference types.

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

View workflow job for this annotation

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

Argument of type 'List<KeyValuePair<string, object?>>' cannot be used for parameter 'parameters' of type 'IEnumerable<KeyValuePair<string, object>>' in 'IEnumerable<T> ParametersExtensions.GetValues<T>(IEnumerable<KeyValuePair<string, object>> parameters, string key)' due to differences in the nullability of reference types.

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

View workflow job for this annotation

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

Argument of type 'List<KeyValuePair<string, object?>>' cannot be used for parameter 'parameters' of type 'IEnumerable<KeyValuePair<string, object>>' in 'IEnumerable<T> ParametersExtensions.GetValues<T>(IEnumerable<KeyValuePair<string, object>> parameters, string key)' due to differences in the nullability of reference types.

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

View workflow job for this annotation

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

Argument of type 'List<KeyValuePair<string, object?>>' cannot be used for parameter 'parameters' of type 'IEnumerable<KeyValuePair<string, object>>' in 'IEnumerable<T> ParametersExtensions.GetValues<T>(IEnumerable<KeyValuePair<string, object>> parameters, string key)' due to differences in the nullability of reference types.

/// <summary>
/// Checks to see if the parameter collection contains the value.
Expand All @@ -150,7 +147,7 @@
/// <param name="key">The key for the value to be returned.</param>
/// <param name="value">Value of the returned parameter if it exists.</param>
public bool TryGetValue<T>(string key, [MaybeNullWhen(false)] out T value) =>
_entries.TryGetValue(key, out value);

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

View workflow job for this annotation

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

Argument of type 'List<KeyValuePair<string, object?>>' cannot be used for parameter 'parameters' of type 'IEnumerable<KeyValuePair<string, object>>' in 'bool ParametersExtensions.TryGetValue<T>(IEnumerable<KeyValuePair<string, object>> parameters, string key, out T value)' due to differences in the nullability of reference types.

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

View workflow job for this annotation

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

Argument of type 'List<KeyValuePair<string, object?>>' cannot be used for parameter 'parameters' of type 'IEnumerable<KeyValuePair<string, object>>' in 'bool ParametersExtensions.TryGetValue<T>(IEnumerable<KeyValuePair<string, object>> parameters, string key, out T value)' due to differences in the nullability of reference types.

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

View workflow job for this annotation

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

Argument of type 'List<KeyValuePair<string, object?>>' cannot be used for parameter 'parameters' of type 'IEnumerable<KeyValuePair<string, object>>' in 'bool ParametersExtensions.TryGetValue<T>(IEnumerable<KeyValuePair<string, object>> parameters, string key, out T value)' due to differences in the nullability of reference types.

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

View workflow job for this annotation

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

Argument of type 'List<KeyValuePair<string, object?>>' cannot be used for parameter 'parameters' of type 'IEnumerable<KeyValuePair<string, object>>' in 'bool ParametersExtensions.TryGetValue<T>(IEnumerable<KeyValuePair<string, object>> parameters, string key, out T value)' due to differences in the nullability of reference types.

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

View workflow job for this annotation

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

Argument of type 'List<KeyValuePair<string, object?>>' cannot be used for parameter 'parameters' of type 'IEnumerable<KeyValuePair<string, object>>' in 'bool ParametersExtensions.TryGetValue<T>(IEnumerable<KeyValuePair<string, object>> parameters, string key, out T value)' due to differences in the nullability of reference types.

IEnumerator IEnumerable.GetEnumerator() =>
GetEnumerator();
Expand Down Expand Up @@ -181,7 +178,7 @@

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 @@
/// </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 @@ -45,7 +46,7 @@
}
}

return GetDefault(type);

Check warning on line 49 in src/Prism.Core/Common/ParametersExtensions.cs

View workflow job for this annotation

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

Possible null reference return.

Check warning on line 49 in src/Prism.Core/Common/ParametersExtensions.cs

View workflow job for this annotation

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

Possible null reference return.

Check warning on line 49 in src/Prism.Core/Common/ParametersExtensions.cs

View workflow job for this annotation

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

Possible null reference return.

Check warning on line 49 in src/Prism.Core/Common/ParametersExtensions.cs

View workflow job for this annotation

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

Possible null reference return.

Check warning on line 49 in src/Prism.Core/Common/ParametersExtensions.cs

View workflow job for this annotation

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

Possible null reference return.

Check warning on line 49 in src/Prism.Core/Common/ParametersExtensions.cs

View workflow job for this annotation

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

Possible null reference return.

Check warning on line 49 in src/Prism.Core/Common/ParametersExtensions.cs

View workflow job for this annotation

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

Possible null reference return.

Check warning on line 49 in src/Prism.Core/Common/ParametersExtensions.cs

View workflow job for this annotation

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

Possible null reference return.

Check warning on line 49 in src/Prism.Core/Common/ParametersExtensions.cs

View workflow job for this annotation

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

Possible null reference return.

Check warning on line 49 in src/Prism.Core/Common/ParametersExtensions.cs

View workflow job for this annotation

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

Possible null reference return.
}

/// <summary>
Expand Down Expand Up @@ -74,7 +75,7 @@
}
}

value = default;

Check warning on line 78 in src/Prism.Core/Common/ParametersExtensions.cs

View workflow job for this annotation

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

Possible null reference assignment.

Check warning on line 78 in src/Prism.Core/Common/ParametersExtensions.cs

View workflow job for this annotation

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

Possible null reference assignment.

Check warning on line 78 in src/Prism.Core/Common/ParametersExtensions.cs

View workflow job for this annotation

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

Possible null reference assignment.

Check warning on line 78 in src/Prism.Core/Common/ParametersExtensions.cs

View workflow job for this annotation

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

Possible null reference assignment.

Check warning on line 78 in src/Prism.Core/Common/ParametersExtensions.cs

View workflow job for this annotation

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

Possible null reference assignment.
return false;
}

Expand Down Expand Up @@ -106,7 +107,7 @@

private static bool TryGetValueInternal(KeyValuePair<string, object> kvp, Type type, out object value)
{
value = GetDefault(type);

Check warning on line 110 in src/Prism.Core/Common/ParametersExtensions.cs

View workflow job for this annotation

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

Possible null reference assignment.

Check warning on line 110 in src/Prism.Core/Common/ParametersExtensions.cs

View workflow job for this annotation

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

Possible null reference assignment.

Check warning on line 110 in src/Prism.Core/Common/ParametersExtensions.cs

View workflow job for this annotation

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

Possible null reference assignment.

Check warning on line 110 in src/Prism.Core/Common/ParametersExtensions.cs

View workflow job for this annotation

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

Possible null reference assignment.

Check warning on line 110 in src/Prism.Core/Common/ParametersExtensions.cs

View workflow job for this annotation

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

Possible null reference assignment.
var valueAsString = kvp.Value is string str ? str : kvp.Value?.ToString();
var success = false;
if (kvp.Value == null)
Expand Down Expand Up @@ -156,13 +157,6 @@
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
Loading