From a5c775896753ca5720a2fa332e7391a9f3ae8229 Mon Sep 17 00:00:00 2001 From: "Eric J. Smith" Date: Thu, 31 Oct 2024 00:07:26 -0500 Subject: [PATCH] Support simple connection strings without key pairs --- src/Foundatio/Utility/ConnectionStringParser.cs | 14 +++++++++++--- .../Utility/ConnectionStringParserTests.cs | 11 +++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/Foundatio/Utility/ConnectionStringParser.cs b/src/Foundatio/Utility/ConnectionStringParser.cs index 66befd6a..0fbae4cb 100644 --- a/src/Foundatio/Utility/ConnectionStringParser.cs +++ b/src/Foundatio/Utility/ConnectionStringParser.cs @@ -29,7 +29,7 @@ public static class ConnectionStringParser private static readonly Regex _connectionStringRegex = new(ConnectionStringPattern, RegexOptions.ExplicitCapture | RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace); - private static Dictionary Parse(string connectionString, IDictionary synonyms) + private static Dictionary Parse(string connectionString, IDictionary synonyms, string defaultKey = null) { var parseTable = new Dictionary(StringComparer.OrdinalIgnoreCase); @@ -42,7 +42,15 @@ private static Dictionary Parse(string connectionString, IDictio var match = _connectionStringRegex.Match(connectionString); if (!match.Success || (match.Length != connectionString.Length)) + { + if (defaultKey != null) + { + parseTable[defaultKey] = connectionString; + return parseTable; + } + throw new ArgumentException($"Format of the initialization string does not conform to specification starting at index {match.Length}"); + } int indexValue = 0; var keyValues = match.Groups[valueIndex].Captures; @@ -87,9 +95,9 @@ private static bool IsKeyNameValid(string keyName) return keyName[0] != ';' && !Char.IsWhiteSpace(keyName[0]) && keyName.IndexOf('\u0000') == -1; } - public static Dictionary ParseConnectionString(this string connectionString, IDictionary synonyms = null) + public static Dictionary ParseConnectionString(this string connectionString, IDictionary synonyms = null, string defaultKey = null) { - return Parse(connectionString, synonyms); + return Parse(connectionString, synonyms, defaultKey); } public static string BuildConnectionString(this IDictionary options, IEnumerable excludedKeys = null) diff --git a/tests/Foundatio.Tests/Utility/ConnectionStringParserTests.cs b/tests/Foundatio.Tests/Utility/ConnectionStringParserTests.cs index 591ecb30..43f2d725 100644 --- a/tests/Foundatio.Tests/Utility/ConnectionStringParserTests.cs +++ b/tests/Foundatio.Tests/Utility/ConnectionStringParserTests.cs @@ -42,6 +42,17 @@ public void CanParseQuotedConnectionString() Assert.Equal(connectionString, data.BuildConnectionString()); } + [Fact] + public void CanParseSimpleConnectionString() + { + const string connectionString = "localhost,6379"; + var data = connectionString.ParseConnectionString(defaultKey: "server"); + Assert.Single(data); + Assert.Equal("localhost,6379", data["server"]); + + Assert.Equal("server=localhost,6379", data.BuildConnectionString()); + } + [Fact] public void CanParseComplexQuotedConnectionString() {