Skip to content

Commit 6c78361

Browse files
committed
Remove public static method
1 parent 5a9b565 commit 6c78361

3 files changed

Lines changed: 37 additions & 80 deletions

File tree

src/Npgsql/NpgsqlConnectionStringBuilder.cs

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,7 +1578,7 @@ public bool IncludeErrorDetails
15781578

15791579
internal void PostProcessAndValidate()
15801580
{
1581-
LoadConfigurationFromPgServiceFile(this);
1581+
LoadConfigurationFromPgServiceFile();
15821582

15831583
if (string.IsNullOrWhiteSpace(Host))
15841584
throw new ArgumentException("Host can't be null");
@@ -1687,7 +1687,7 @@ public override bool Equals(object? obj)
16871687
/// <summary>
16881688
/// Gets an <see cref="ICollection" /> containing the keys of the <see cref="NpgsqlConnectionStringBuilder"/>.
16891689
/// </summary>
1690-
public new ICollection<string> Keys => base.Keys.Cast<string>().ToArray()!;
1690+
public new ICollection<string> Keys => base.Keys.Cast<string>().ToArray();
16911691

16921692
/// <summary>
16931693
/// Gets an <see cref="ICollection" /> containing the values in the <see cref="NpgsqlConnectionStringBuilder"/>.
@@ -1745,60 +1745,41 @@ protected override void GetProperties(Hashtable propertyDescriptors)
17451745

17461746
#endregion
17471747

1748-
#region Static methods
1748+
#region Load from Postgres service file
17491749

1750-
/// <summary>
1751-
/// Loads configuration from a Postgres connection service file
1752-
/// </summary>
1753-
/// <param name="serviceName">The name of the service to load.</param>
1754-
/// <returns>A builder instance if the service is found; null otherwise</returns>
1755-
/// <exception cref="ArgumentException">if serviceName is null and the PGSERVICE environment variable is not set.</exception>
1756-
public static NpgsqlConnectionStringBuilder? FromPgServiceFile(string? serviceName = null)
1757-
{
1758-
return LoadConfigurationFromPgServiceFile(null, serviceName
1759-
?? PostgresEnvironment.Service
1760-
?? throw new ArgumentException($"{nameof(serviceName)} is mandatory when the PGSERVICE environment variable is not set.", nameof(serviceName)));
1761-
}
1762-
1763-
static NpgsqlConnectionStringBuilder? LoadConfigurationFromPgServiceFile(NpgsqlConnectionStringBuilder? source, string? serviceName = null)
1750+
void LoadConfigurationFromPgServiceFile()
17641751
{
1765-
var service = serviceName ?? source?.Service ?? PostgresEnvironment.Service;
1766-
if (service is null)
1752+
Service ??= PostgresEnvironment.Service;
1753+
if (Service is null)
17671754
{
1768-
return null;
1755+
return;
17691756
}
17701757

1771-
if (source is not null)
1772-
{
1773-
source.Service = service;
1774-
}
1758+
if (!LoadConfigurationFromIniFile(PostgresEnvironment.UserServiceFile))
1759+
LoadConfigurationFromIniFile(PostgresEnvironment.SystemServiceFile);
17751760

1776-
return LoadConfigurationFromIniFile(PostgresEnvironment.UserServiceFile)
1777-
?? LoadConfigurationFromIniFile(PostgresEnvironment.SystemServiceFile);
1778-
1779-
NpgsqlConnectionStringBuilder? LoadConfigurationFromIniFile(string? filePath)
1761+
bool LoadConfigurationFromIniFile(string? filePath)
17801762
{
17811763
if (filePath is null || !File.Exists(filePath))
17821764
{
1783-
return null;
1765+
return false;
17841766
}
17851767

17861768
var settings = ReadIniFile(filePath);
17871769
if (settings is null)
17881770
{
1789-
return null;
1771+
return false;
17901772
}
17911773

1792-
var builder = source ?? new NpgsqlConnectionStringBuilder();
17931774
foreach (var kv in settings)
17941775
{
1795-
if (!builder.Keys.Contains(kv.Key))
1776+
if (ContainsKey(kv.Key) && !Keys.Contains(kv.Key))
17961777
{
1797-
builder[kv.Key] = kv.Value;
1778+
this[kv.Key] = kv.Value;
17981779
}
17991780
}
18001781

1801-
return builder;
1782+
return true;
18021783
}
18031784

18041785
Dictionary<string, string>? ReadIniFile(string filePath)
@@ -1834,7 +1815,7 @@ protected override void GetProperties(Hashtable propertyDescriptors)
18341815
// remove the brackets
18351816
var sectionPrefix = line.Substring(1, line.Length - 2).Trim();
18361817
// Check whether it is the specified service
1837-
if (sectionPrefix == service)
1818+
if (sectionPrefix == Service)
18381819
{
18391820
settings = new Dictionary<string, string>();
18401821
}
@@ -1850,7 +1831,7 @@ protected override void GetProperties(Hashtable propertyDescriptors)
18501831

18511832
// key = value OR "value"
18521833
var separator = line.IndexOf('=');
1853-
if (separator < 0)
1834+
if (separator <= 0)
18541835
{
18551836
throw new FormatException($"Unrecognized line format: '{rawLine}'.");
18561837
}

src/Npgsql/PublicAPI.Unshipped.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ Npgsql.NpgsqlSlimDataSourceBuilder.UseRootCertificate(System.Security.Cryptograp
3535
Npgsql.NpgsqlSlimDataSourceBuilder.UseRootCertificateCallback(System.Func<System.Security.Cryptography.X509Certificates.X509Certificate2!>? rootCertificateCallback) -> Npgsql.NpgsqlSlimDataSourceBuilder!
3636
Npgsql.NpgsqlSlimDataSourceBuilder.UseSystemTextJson(System.Text.Json.JsonSerializerOptions? serializerOptions = null, System.Type![]? jsonbClrTypes = null, System.Type![]? jsonClrTypes = null) -> Npgsql.NpgsqlSlimDataSourceBuilder!
3737
Npgsql.NpgsqlSlimDataSourceBuilder.UseUserCertificateValidationCallback(System.Net.Security.RemoteCertificateValidationCallback! userCertificateValidationCallback) -> Npgsql.NpgsqlSlimDataSourceBuilder!
38-
static Npgsql.NpgsqlConnectionStringBuilder.FromPgServiceFile(string? serviceName = null) -> Npgsql.NpgsqlConnectionStringBuilder?
3938
*REMOVED*static NpgsqlTypes.NpgsqlBox.Parse(string! s) -> NpgsqlTypes.NpgsqlBox
4039
*REMOVED*static NpgsqlTypes.NpgsqlCircle.Parse(string! s) -> NpgsqlTypes.NpgsqlCircle
4140
*REMOVED*static NpgsqlTypes.NpgsqlLine.Parse(string! s) -> NpgsqlTypes.NpgsqlLine

test/Npgsql.Tests/ConnectionStringBuilderTests.cs

Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,13 @@ public void PgService_ViaProperty()
178178

179179
private void PgService_With(Func<string?, string?, int?, bool, NpgsqlConnectionStringBuilder> factory)
180180
{
181+
// Nominal case
181182
var builder = factory("MyService", "MyHost", null, false);
182183
builder.PostProcessAndValidate();
183184
Assert.That(builder, Is.Not.Null);
184185
Assert.That(builder.Count, Is.EqualTo(2));
185186

187+
// Missing service file is ignored
186188
var tempFile = Path.GetTempFileName();
187189
using var pgServiceFileVariable = SetEnvironmentVariable("PGSERVICEFILE", tempFile);
188190
builder = factory("MyService", "MyHost", null, false);
@@ -192,12 +194,14 @@ private void PgService_With(Func<string?, string?, int?, bool, NpgsqlConnectionS
192194

193195
try
194196
{
197+
// Comments are ignored
195198
File.WriteAllText(tempFile, "# test");
196199
builder = factory("MyService", "MyHost", null, false);
197200
builder.PostProcessAndValidate();
198201
Assert.That(builder, Is.Not.Null);
199202
Assert.That(builder.Count, Is.EqualTo(2));
200203

204+
// Other services are ignored
201205
File.WriteAllText(tempFile, """
202206
[OtherService]
203207
Host=test
@@ -208,6 +212,22 @@ private void PgService_With(Func<string?, string?, int?, bool, NpgsqlConnectionS
208212
Assert.That(builder, Is.Not.Null);
209213
Assert.That(builder.Count, Is.EqualTo(2));
210214

215+
// Unknown settings are ignored
216+
File.WriteAllText(tempFile, """
217+
[MyService]
218+
Unknown=test
219+
Port=1234
220+
""");
221+
builder = factory("MyService", "MyHost", null, false);
222+
builder.PostProcessAndValidate();
223+
builder.PostProcessAndValidate();
224+
Assert.That(builder, Is.Not.Null);
225+
Assert.That(builder.Count, Is.EqualTo(3));
226+
Assert.That(builder.Host, Is.EqualTo("MyHost"));
227+
Assert.That(builder.Port, Is.EqualTo(1234));
228+
Assert.That(builder.Service, Is.EqualTo("MyService"));
229+
230+
// Overridden settings are ignored
211231
File.WriteAllText(tempFile, """
212232
[MyService]
213233
Host=test
@@ -242,47 +262,4 @@ private void PgService_With(Func<string?, string?, int?, bool, NpgsqlConnectionS
242262
File.Delete(tempFile);
243263
}
244264
}
245-
246-
[Test]
247-
[NonParallelizable] // Sets environment variable
248-
public void FromPgServiceFile()
249-
{
250-
Assert.Throws<ArgumentException>(() => NpgsqlConnectionStringBuilder.FromPgServiceFile());
251-
252-
Assert.Null(NpgsqlConnectionStringBuilder.FromPgServiceFile("MyService"));
253-
254-
using var pgServiceVariable = SetEnvironmentVariable("PGSERVICE", "MyService");
255-
Assert.Null(NpgsqlConnectionStringBuilder.FromPgServiceFile());
256-
257-
var tempFile = Path.GetTempFileName();
258-
using var pgServiceFileVariable = SetEnvironmentVariable("PGSERVICEFILE", tempFile);
259-
Assert.Null(NpgsqlConnectionStringBuilder.FromPgServiceFile());
260-
261-
try
262-
{
263-
File.WriteAllText(tempFile, "# test");
264-
Assert.Null(NpgsqlConnectionStringBuilder.FromPgServiceFile());
265-
266-
File.WriteAllText(tempFile, """
267-
[OtherService]
268-
Host=test
269-
Port=1234
270-
""");
271-
Assert.Null(NpgsqlConnectionStringBuilder.FromPgServiceFile());
272-
273-
File.WriteAllText(tempFile, """
274-
[MyService]
275-
Host=test
276-
Port=1234
277-
""");
278-
var builder = NpgsqlConnectionStringBuilder.FromPgServiceFile();
279-
Assert.NotNull(builder);
280-
Assert.AreEqual("test", builder!.Host);
281-
Assert.AreEqual(1234, builder.Port);
282-
}
283-
finally
284-
{
285-
File.Delete(tempFile);
286-
}
287-
}
288265
}

0 commit comments

Comments
 (0)