🐞 Fix for #20

Sanitize string input, rather than T. Call ToString() on dictionary value
This commit is contained in:
Oliver Booth 2021-01-17 00:36:28 +00:00
parent 6f26c04f6c
commit 3ad20fdb40
1 changed files with 16 additions and 6 deletions

View File

@ -19,15 +19,25 @@ namespace X10D
/// <returns>Returns a <see cref="string" /> representing the dictionary as a key=value; set.</returns> /// <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) public static string ToConnectionString<T1, T2>(this IReadOnlyDictionary<T1, T2> dictionary)
{ {
static string SanitizeValue<T>(T value) static string SanitizeValue(string? value)
{ {
return value is string str && if (value is null)
Regex.IsMatch(str, "\\s") {
? $"\"{str}\"" return string.Empty;
: value.ToString(); }
for (var index = 0; index < value.Length; index++)
{
if (char.IsWhiteSpace(value[index]))
{
return $"\"{value}\"";
}
}
return value;
} }
var strings = dictionary.Select(o => $"{o.Key}={SanitizeValue(o.Value)}"); var strings = dictionary.Select(o => $"{o.Key}={SanitizeValue(o.Value?.ToString())}");
return string.Join(";", strings); return string.Join(";", strings);
} }