Skip to content

Commit

Permalink
improvement ConfigParser.GetValue<bool> and SetValue<bool> methods
Browse files Browse the repository at this point in the history
added ConfigParserSettings.BooleanConverter and covered all these changes with unit tests
  • Loading branch information
salaros committed Jun 19, 2018
1 parent 13452a4 commit d70e6ca
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 21 deletions.
33 changes: 18 additions & 15 deletions src/ConfigParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ public virtual string GetValue(string sectionName, string keyName, string defaul
public virtual bool GetValue(string sectionName, string keyName, bool defaultValue,
BooleanConverter booleanConverter = null)
{
booleanConverter = booleanConverter ?? Settings.BooleanConverter;

var booleanValue = GetRawValue<string>(sectionName, keyName, null);
if (string.IsNullOrWhiteSpace(booleanValue))
{
Expand All @@ -198,13 +200,6 @@ public virtual bool GetValue(string sectionName, string keyName, bool defaultVal
return defaultValue;
}

if (booleanConverter != null && booleanConverter.CanConvertFrom(typeof(string)))
{
var value = booleanConverter.ConvertFrom(booleanValue);
if (value is bool convertedBoolean)
return convertedBoolean;
}

#pragma warning disable IDE0046 // Convert to conditional expression
foreach (var converter in YesNoBoolConverters)
{
Expand All @@ -216,14 +211,21 @@ public virtual bool GetValue(string sectionName, string keyName, bool defaultVal
}
#pragma warning restore IDE0046 // Convert to conditional expression

return bool.TryParse(booleanValue, out var parseBoolean)
? parseBoolean
// if some day Boolean.ToString(IFormatProvider) will work
// https://msdn.microsoft.com/en-us/library/s802ct92(v=vs.110).aspx#Anchor_1
: true.ToString(Settings.Culture).ToLowerInvariant().Equals(
booleanValue,
StringComparison.InvariantCultureIgnoreCase
);
if(bool.TryParse(booleanValue, out var parseBoolean))
return parseBoolean;

// if some day Boolean.ToString(IFormatProvider) will work
// https://msdn.microsoft.com/en-us/library/s802ct92(v=vs.110).aspx#Anchor_1
if (true.ToString(Settings.Culture).ToLowerInvariant().Equals(booleanValue, StringComparison.InvariantCultureIgnoreCase))
return true;

if (booleanConverter == null || !booleanConverter.CanConvertFrom(typeof(string)))
return defaultValue;

var value = booleanConverter.ConvertFrom(booleanValue);
return value is bool convertedBoolean
? convertedBoolean
: defaultValue;
}

/// <summary>
Expand Down Expand Up @@ -437,6 +439,7 @@ public virtual bool SetValue(string sectionName, string keyName, bool value,
}
}

booleanConverter = booleanConverter ?? Settings.BooleanConverter;
return SetValue(sectionName, keyName, (null == booleanConverter)
? value.ToString(Settings.Culture ?? CultureInfo.InvariantCulture).ToLowerInvariant()
: booleanConverter.ConvertToString(value)
Expand Down
9 changes: 9 additions & 0 deletions src/ConfigParserSettings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
Expand Down Expand Up @@ -43,6 +44,14 @@ static ConfigParserSettings()
/// </value>
public Encoding Encoding { get; set; } = null;

/// <summary>
/// Gets or sets the boolean converter.
/// </summary>
/// <value>
/// The boolean converter.
/// </value>
public BooleanConverter BooleanConverter { get; set; } = null;

/// <summary>
/// Gets the new line string.
/// </summary>
Expand Down
12 changes: 7 additions & 5 deletions src/Helpers/BooleanConverters/YesNoConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@ public override object ConvertTo(ITypeDescriptorContext context, CultureInfo cul
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
var booleanText = value as string;
return no.Equals(booleanText, StringComparison.OrdinalIgnoreCase)
? false
: yes.Equals(booleanText, StringComparison.OrdinalIgnoreCase)
? true
: base.ConvertFrom(context, culture, value);
if (no.Equals(booleanText, StringComparison.OrdinalIgnoreCase))
return false;

if (yes.Equals(booleanText, StringComparison.OrdinalIgnoreCase))
return true;

return null;
}

/// <summary>
Expand Down
7 changes: 6 additions & 1 deletion tests/ConfigParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,15 @@ public void BooleanValuesAreParsedCorrectly()
Encoding = Encoding.UTF8,
// if some day Boolean.ToString(IFormatProvider) will work
// https://msdn.microsoft.com/en-us/library/s802ct92(v=vs.110).aspx#Anchor_1
Culture = new CultureInfo("en-US")
Culture = new CultureInfo("en-US"),
BooleanConverter = new YesNoConverter("vero", "falso")
}
);

const string valoriItaliani = "ValoriItaliani"; // [ValoriItaliani]
Assert.True(configFileEnglish.GetValue(valoriItaliani, "positivo", false)); // positivo = vero
Assert.False(configFileEnglish.GetValue(valoriItaliani, "sampleOff", true)); // sampleOff = falso

const string simpleSection = "Simple"; // [Simple]
Assert.False(configFileEnglish.GetValue(simpleSection, "empty", false)); // empty=
Assert.True(configFileEnglish.GetValue(simpleSection, "numericTrue", false)); // numericTrue=1
Expand Down
4 changes: 4 additions & 0 deletions tests/Resources/Values/boolean.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ sampleOff=Off
[EnabledDisabled]
sampleOn=Enabled
sampleOff=disabled

[ValoriItaliani]
positivo = vero
sampleOff = falso

0 comments on commit d70e6ca

Please sign in to comment.