Add selector parameter for key, value, or both (resolves #3)

Allows caller to specify a transformation to apply to key/value, presumably when either (or both) do not have an intended result of ToString
This commit is contained in:
Oliver Booth 2022-04-20 15:17:33 +01:00
parent 2325c45d84
commit 91b43790b3
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
1 changed files with 153 additions and 10 deletions

View File

@ -29,20 +29,90 @@ public static class DictionaryExtensions
return string.Empty; return string.Empty;
} }
foreach (char character in value) return value.Contains(' ') ? $"\"{value}\"" : value;
{
if (char.IsWhiteSpace(character))
{
return $"\"{value}\"";
}
}
return value;
} }
static string GetQueryParameter(KeyValuePair<TKey, TValue> pair) static string GetQueryParameter(KeyValuePair<TKey, TValue> pair)
{ {
return $"{pair.Key}={SanitizeValue(pair.Value?.ToString())}"); return $"{pair.Key}={SanitizeValue(pair.Value?.ToString())}";
}
return string.Join(';', source.Select(GetQueryParameter));
}
/// <summary>
/// Converts an <see cref="IEnumerable{T}" /> of <see cref="KeyValuePair{TKey, TValue}" /> to an data connection
/// string.
/// </summary>
/// <typeparam name="TKey">The type of the key element of the key/value pair.</typeparam>
/// <typeparam name="TValue">The type of the value element of the key/value pair.</typeparam>
/// <param name="source">The source dictionary.</param>
/// <param name="selector">
/// A transform function to apply to the <see cref="KeyValuePair{TKey,TValue}.Value" /> of each element.
/// </param>
/// <returns>A <see cref="string" /> representing the dictionary as a key=value set, concatenated with <c>;</c>.</returns>
public static string ToConnectionString<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> source,
Func<TValue, string?> selector)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
static string SanitizeValue(string? value)
{
if (value is null)
{
return string.Empty;
}
return value.Contains(' ') ? $"\"{value}\"" : value;
}
string GetQueryParameter(KeyValuePair<TKey, TValue> pair)
{
return $"{pair.Key}={SanitizeValue(selector(pair.Value))}";
}
return string.Join(';', source.Select(GetQueryParameter));
}
/// <summary>
/// Converts an <see cref="IEnumerable{T}" /> of <see cref="KeyValuePair{TKey, TValue}" /> to an data connection
/// string.
/// </summary>
/// <typeparam name="TKey">The type of the key element of the key/value pair.</typeparam>
/// <typeparam name="TValue">The type of the value element of the key/value pair.</typeparam>
/// <param name="source">The source dictionary.</param>
/// <param name="keySelector">
/// A transform function to apply to the <see cref="KeyValuePair{TKey,TValue}.Key" /> of each element.
/// </param>
/// <param name="valueSelector">
/// A transform function to apply to the <see cref="KeyValuePair{TKey,TValue}.Value" /> of each element.
/// </param>
/// <returns>A <see cref="string" /> representing the dictionary as a key=value set, concatenated with <c>;</c>.</returns>
public static string ToConnectionString<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> source,
Func<TKey, string> keySelector, Func<TValue, string?> valueSelector)
where TKey : notnull
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
static string SanitizeValue(string? value)
{
if (value is null)
{
return string.Empty;
}
return value.Contains(' ') ? $"\"{value}\"" : value;
}
string GetQueryParameter(KeyValuePair<TKey, TValue> pair)
{
return $"{keySelector(pair.Key)}={SanitizeValue(valueSelector(pair.Value))}";
} }
return string.Join(';', source.Select(GetQueryParameter)); return string.Join(';', source.Select(GetQueryParameter));
@ -72,4 +142,77 @@ public static class DictionaryExtensions
return string.Join('&', source.Select(GetQueryParameter)); return string.Join('&', source.Select(GetQueryParameter));
} }
/// <summary>
/// Converts an <see cref="IEnumerable{T}" /> of <see cref="KeyValuePair{TKey, TValue}" /> to a HTTP GET query string.
/// </summary>
/// <typeparam name="TKey">The type of the key element of the key/value pair.</typeparam>
/// <typeparam name="TValue">The type of the value element of the key/value pair.</typeparam>
/// <param name="source">The source dictionary.</param>
/// <param name="selector">
/// A transform function to apply to the <see cref="KeyValuePair{TKey,TValue}.Value" /> of each element.
/// </param>
/// <returns>A <see cref="string" /> representing the dictionary as a key=value set, concatenated with <c>&amp;</c>.</returns>
public static string ToGetParameters<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> source,
Func<TValue, string?> selector)
where TKey : notnull
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
if (selector is null)
{
throw new ArgumentNullException(nameof(selector));
}
// can't static here because of 'selector' parameter
string GetQueryParameter(KeyValuePair<TKey, TValue> pair)
{
string key = HttpUtility.UrlEncode(pair.Key.ToString())!;
string? value = HttpUtility.UrlEncode(selector(pair.Value));
return $"{key}={value}";
}
return string.Join('&', source.Select(GetQueryParameter));
}
/// <summary>
/// Converts an <see cref="IEnumerable{T}" /> of <see cref="KeyValuePair{TKey, TValue}" /> to a HTTP GET query string.
/// </summary>
/// <typeparam name="TKey">The type of the key element of the key/value pair.</typeparam>
/// <typeparam name="TValue">The type of the value element of the key/value pair.</typeparam>
/// <param name="source">The source dictionary.</param>
/// <param name="keySelector">
/// A transform function to apply to the <see cref="KeyValuePair{TKey,TValue}.Key" /> of each element.
/// </param>
/// <param name="valueSelector">
/// A transform function to apply to the <see cref="KeyValuePair{TKey,TValue}.Value" /> of each element.
/// </param>
/// <returns>A <see cref="string" /> representing the dictionary as a key=value set, concatenated with <c>&amp;</c>.</returns>
public static string ToGetParameters<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> source,
Func<TKey, string> keySelector, Func<TValue, string?> valueSelector)
where TKey : notnull
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
if (valueSelector is null)
{
throw new ArgumentNullException(nameof(valueSelector));
}
// can't static here because of selector parameters
string GetQueryParameter(KeyValuePair<TKey, TValue> pair)
{
string key = HttpUtility.UrlEncode(keySelector(pair.Key));
string? value = HttpUtility.UrlEncode(valueSelector(pair.Value));
return $"{key}={value}";
}
return string.Join('&', source.Select(GetQueryParameter));
}
} }