Skip to content

Commit

Permalink
Support simple connection strings without key pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
ejsmith committed Oct 31, 2024
1 parent 86f19c9 commit a5c7758
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/Foundatio/Utility/ConnectionStringParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static class ConnectionStringParser

private static readonly Regex _connectionStringRegex = new(ConnectionStringPattern, RegexOptions.ExplicitCapture | RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace);

private static Dictionary<string, string> Parse(string connectionString, IDictionary<string, string> synonyms)
private static Dictionary<string, string> Parse(string connectionString, IDictionary<string, string> synonyms, string defaultKey = null)
{
var parseTable = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

Expand All @@ -42,7 +42,15 @@ private static Dictionary<string, string> 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;
Expand Down Expand Up @@ -87,9 +95,9 @@ private static bool IsKeyNameValid(string keyName)
return keyName[0] != ';' && !Char.IsWhiteSpace(keyName[0]) && keyName.IndexOf('\u0000') == -1;
}

public static Dictionary<string, string> ParseConnectionString(this string connectionString, IDictionary<string, string> synonyms = null)
public static Dictionary<string, string> ParseConnectionString(this string connectionString, IDictionary<string, string> synonyms = null, string defaultKey = null)
{
return Parse(connectionString, synonyms);
return Parse(connectionString, synonyms, defaultKey);
}

public static string BuildConnectionString(this IDictionary<string, string> options, IEnumerable<string> excludedKeys = null)
Expand Down
11 changes: 11 additions & 0 deletions tests/Foundatio.Tests/Utility/ConnectionStringParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit a5c7758

Please sign in to comment.