Use IEnumerable<KeyValuePair<>> over Dictionary

Also relocate to child namespace.

Resolves #8 and is item for #7
This commit is contained in:
Oliver Booth 2021-01-18 22:02:58 +00:00
parent 31d1dc21df
commit 9db0249790
2 changed files with 95 additions and 123 deletions

View File

@ -1,123 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
namespace X10D
{
/// <summary>
/// A set of extension methods for <see cref="Dictionary{TKey,TValue}" />.
/// </summary>
public static class DictionaryExtensions
{
/// <summary>
/// Converts a <see cref="IReadOnlyDictionary{T1,T2}" /> to an object-relational-safe connection string.
/// </summary>
/// <typeparam name="T1">The key type.</typeparam>
/// <typeparam name="T2">The value type.</typeparam>
/// <param name="dictionary">The dictionary.</param>
/// <returns>Returns a <see cref="string" /> representing the dictionary as a key=value; set.</returns>
public static string ToConnectionString<T1, T2>(this IReadOnlyDictionary<T1, T2> dictionary)
{
if (dictionary is null)
{
throw new ArgumentNullException(nameof(dictionary));
}
static string SanitizeValue(string? value)
{
if (value is null)
{
return string.Empty;
}
for (var index = 0; index < value.Length; index++)
{
if (char.IsWhiteSpace(value[index]))
{
return $"\"{value}\"";
}
}
return value;
}
var list = new List<string>();
foreach (var pair in dictionary)
{
list.Add($"{pair.Key}={SanitizeValue(pair.Value?.ToString())}");
}
return string.Join(";", list);
}
/// <summary>
/// Converts a <see cref="IDictionary{T1,T2}" /> to an object-relational-safe connection string.
/// </summary>
/// <typeparam name="T1">The key type.</typeparam>
/// <typeparam name="T2">The value type.</typeparam>
/// <param name="dictionary">The dictionary.</param>
/// <returns>Returns a <see cref="string" /> representing the dictionary as a key=value; set.</returns>
public static string ToConnectionString<T1, T2>(this IDictionary<T1, T2> dictionary)
{
return ((IReadOnlyDictionary<T1, T2>)dictionary).ToConnectionString();
}
/// <summary>
/// Converts a <see cref="Dictionary{T1,T2}" /> to an object-relational-safe connection string.
/// </summary>
/// <typeparam name="T1">The key type.</typeparam>
/// <typeparam name="T2">The value type.</typeparam>
/// <param name="dictionary">The dictionary.</param>
/// <returns>Returns a <see cref="string" /> representing the dictionary as a key=value; set.</returns>
public static string ToConnectionString<T1, T2>(this Dictionary<T1, T2> dictionary)
{
return ((IReadOnlyDictionary<T1, T2>)dictionary).ToConnectionString();
}
/// <summary>
/// Converts an <see cref="IReadOnlyDictionary{T1,T2}" /> to a HTTP GET parameter string.
/// </summary>
/// <typeparam name="T1">The key type.</typeparam>
/// <typeparam name="T2">The value type.</typeparam>
/// <param name="dictionary">The dictionary.</param>
/// <returns>Returns a <see cref="string" /> representing the dictionary as a key=value& set.</returns>
public static string ToGetParameters<T1, T2>(this IReadOnlyDictionary<T1, T2> dictionary)
{
static string Sanitize(KeyValuePair<T1, T2> kvp)
{
var key = HttpUtility.UrlEncode(kvp.Key.ToString());
var value = HttpUtility.UrlEncode(kvp.Value.ToString());
return $"{key}={value}";
}
return string.Join("&", dictionary.Select(Sanitize));
}
/// <summary>
/// Converts an <see cref="IDictionary{T1,T2}" /> to a HTTP GET parameter string.
/// </summary>
/// <typeparam name="T1">The key type.</typeparam>
/// <typeparam name="T2">The value type.</typeparam>
/// <param name="dictionary">The dictionary.</param>
/// <returns>Returns a <see cref="string" /> representing the dictionary as a key=value& set.</returns>
public static string ToGetParameters<T1, T2>(this IDictionary<T1, T2> dictionary)
{
return ((IReadOnlyDictionary<T1, T2>)dictionary).ToGetParameters();
}
/// <summary>
/// Converts a <see cref="Dictionary{T1,T2}" /> to a HTTP GET parameter string.
/// </summary>
/// <typeparam name="T1">The key type.</typeparam>
/// <typeparam name="T2">The value type.</typeparam>
/// <param name="dictionary">The dictionary.</param>
/// <returns>Returns a <see cref="string" /> representing the dictionary as a key=value& set.</returns>
public static string ToGetParameters<T1, T2>(this Dictionary<T1, T2> dictionary)
{
return ((IReadOnlyDictionary<T1, T2>)dictionary).ToGetParameters();
}
}
}

View File

@ -0,0 +1,95 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Web;
namespace X10D.DictionaryExtensions
{
/// <summary>
/// Extension methods for <see cref="Dictionary{TKey,TValue}" /> and similar types.
/// </summary>
public static class DictionaryExtensions
{
/// <summary>
/// Converts an <see cref="IEnumerable{T}" /> of <see cref="KeyValuePair{TKey, TValue}" /> to an data connection
/// string.
/// </summary>
/// <typeparam name="TKey">The key type.</typeparam>
/// <typeparam name="TValue">The value type.</typeparam>
/// <param name="value">The source dictionary.</param>
/// <returns>A <see cref="string" /> representing the dictionary as a <c>key=value;</c> set.</returns>
public static string ToConnectionString<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> value)
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
static string SanitizeValue(string? value)
{
if (value is null)
{
return string.Empty;
}
for (var index = 0; index < value.Length; index++)
{
if (char.IsWhiteSpace(value[index]))
{
return $"\"{value}\"";
}
}
return value;
}
var list = new List<string>();
// ReSharper disable once UseDeconstruction
// .NET Standard 2.0 does not support tuple deconstruct for KeyValuePair<K, V>
foreach (var pair in value)
{
list.Add($"{pair.Key}={SanitizeValue(pair.Value?.ToString())}");
}
return string.Join(";", list);
}
/// <returns>A <see cref="string" /> representing the dictionary as a <c>key=value;</c> set.</returns>
///
///
/// <summary>
/// Converts an <see cref="IEnumerable{T}" /> of <see cref="KeyValuePair{TKey, TValue}" /> to a HTTP GET parameter
/// string.
/// </summary>
/// <typeparam name="TKey">The key type.</typeparam>
/// <typeparam name="TValue">The value type.</typeparam>
/// <param name="value">The source dictionary.</param>
/// <returns>Returns a <see cref="string" /> representing the dictionary as a key=value& set.</returns>
[SuppressMessage("ReSharper", "UseDeconstructionOnParameter")]
[SuppressMessage("ReSharper", "UseDeconstruction")]
public static string ToGetParameters<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> value)
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
static string Sanitize(KeyValuePair<TKey, TValue> pair)
{
var key = HttpUtility.UrlEncode(pair.Key?.ToString());
var value = HttpUtility.UrlEncode(pair.Value?.ToString());
return $"{key}={value}";
}
var list = new List<string>();
foreach (var pair in value)
{
list.Add(Sanitize(pair));
}
return string.Join(";", list);
}
}
}