Skip to content

Commit d70e6ca

Browse files
committed
improvement ConfigParser.GetValue<bool> and SetValue<bool> methods
added ConfigParserSettings.BooleanConverter and covered all these changes with unit tests
1 parent 13452a4 commit d70e6ca

File tree

5 files changed

+44
-21
lines changed

5 files changed

+44
-21
lines changed

src/ConfigParser.cs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ public virtual string GetValue(string sectionName, string keyName, string defaul
186186
public virtual bool GetValue(string sectionName, string keyName, bool defaultValue,
187187
BooleanConverter booleanConverter = null)
188188
{
189+
booleanConverter = booleanConverter ?? Settings.BooleanConverter;
190+
189191
var booleanValue = GetRawValue<string>(sectionName, keyName, null);
190192
if (string.IsNullOrWhiteSpace(booleanValue))
191193
{
@@ -198,13 +200,6 @@ public virtual bool GetValue(string sectionName, string keyName, bool defaultVal
198200
return defaultValue;
199201
}
200202

201-
if (booleanConverter != null && booleanConverter.CanConvertFrom(typeof(string)))
202-
{
203-
var value = booleanConverter.ConvertFrom(booleanValue);
204-
if (value is bool convertedBoolean)
205-
return convertedBoolean;
206-
}
207-
208203
#pragma warning disable IDE0046 // Convert to conditional expression
209204
foreach (var converter in YesNoBoolConverters)
210205
{
@@ -216,14 +211,21 @@ public virtual bool GetValue(string sectionName, string keyName, bool defaultVal
216211
}
217212
#pragma warning restore IDE0046 // Convert to conditional expression
218213

219-
return bool.TryParse(booleanValue, out var parseBoolean)
220-
? parseBoolean
221-
// if some day Boolean.ToString(IFormatProvider) will work
222-
// https://msdn.microsoft.com/en-us/library/s802ct92(v=vs.110).aspx#Anchor_1
223-
: true.ToString(Settings.Culture).ToLowerInvariant().Equals(
224-
booleanValue,
225-
StringComparison.InvariantCultureIgnoreCase
226-
);
214+
if(bool.TryParse(booleanValue, out var parseBoolean))
215+
return parseBoolean;
216+
217+
// if some day Boolean.ToString(IFormatProvider) will work
218+
// https://msdn.microsoft.com/en-us/library/s802ct92(v=vs.110).aspx#Anchor_1
219+
if (true.ToString(Settings.Culture).ToLowerInvariant().Equals(booleanValue, StringComparison.InvariantCultureIgnoreCase))
220+
return true;
221+
222+
if (booleanConverter == null || !booleanConverter.CanConvertFrom(typeof(string)))
223+
return defaultValue;
224+
225+
var value = booleanConverter.ConvertFrom(booleanValue);
226+
return value is bool convertedBoolean
227+
? convertedBoolean
228+
: defaultValue;
227229
}
228230

229231
/// <summary>
@@ -437,6 +439,7 @@ public virtual bool SetValue(string sectionName, string keyName, bool value,
437439
}
438440
}
439441

442+
booleanConverter = booleanConverter ?? Settings.BooleanConverter;
440443
return SetValue(sectionName, keyName, (null == booleanConverter)
441444
? value.ToString(Settings.Culture ?? CultureInfo.InvariantCulture).ToLowerInvariant()
442445
: booleanConverter.ConvertToString(value)

src/ConfigParserSettings.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.ComponentModel;
23
using System.Globalization;
34
using System.Linq;
45
using System.Text;
@@ -43,6 +44,14 @@ static ConfigParserSettings()
4344
/// </value>
4445
public Encoding Encoding { get; set; } = null;
4546

47+
/// <summary>
48+
/// Gets or sets the boolean converter.
49+
/// </summary>
50+
/// <value>
51+
/// The boolean converter.
52+
/// </value>
53+
public BooleanConverter BooleanConverter { get; set; } = null;
54+
4655
/// <summary>
4756
/// Gets the new line string.
4857
/// </summary>

src/Helpers/BooleanConverters/YesNoConverter.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,13 @@ public override object ConvertTo(ITypeDescriptorContext context, CultureInfo cul
6464
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
6565
{
6666
var booleanText = value as string;
67-
return no.Equals(booleanText, StringComparison.OrdinalIgnoreCase)
68-
? false
69-
: yes.Equals(booleanText, StringComparison.OrdinalIgnoreCase)
70-
? true
71-
: base.ConvertFrom(context, culture, value);
67+
if (no.Equals(booleanText, StringComparison.OrdinalIgnoreCase))
68+
return false;
69+
70+
if (yes.Equals(booleanText, StringComparison.OrdinalIgnoreCase))
71+
return true;
72+
73+
return null;
7274
}
7375

7476
/// <summary>

tests/ConfigParserTests.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,15 @@ public void BooleanValuesAreParsedCorrectly()
281281
Encoding = Encoding.UTF8,
282282
// if some day Boolean.ToString(IFormatProvider) will work
283283
// https://msdn.microsoft.com/en-us/library/s802ct92(v=vs.110).aspx#Anchor_1
284-
Culture = new CultureInfo("en-US")
284+
Culture = new CultureInfo("en-US"),
285+
BooleanConverter = new YesNoConverter("vero", "falso")
285286
}
286287
);
287288

289+
const string valoriItaliani = "ValoriItaliani"; // [ValoriItaliani]
290+
Assert.True(configFileEnglish.GetValue(valoriItaliani, "positivo", false)); // positivo = vero
291+
Assert.False(configFileEnglish.GetValue(valoriItaliani, "sampleOff", true)); // sampleOff = falso
292+
288293
const string simpleSection = "Simple"; // [Simple]
289294
Assert.False(configFileEnglish.GetValue(simpleSection, "empty", false)); // empty=
290295
Assert.True(configFileEnglish.GetValue(simpleSection, "numericTrue", false)); // numericTrue=1

tests/Resources/Values/boolean.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,7 @@ sampleOff=Off
1616
[EnabledDisabled]
1717
sampleOn=Enabled
1818
sampleOff=disabled
19+
20+
[ValoriItaliani]
21+
positivo = vero
22+
sampleOff = falso

0 commit comments

Comments
 (0)