Handle blank lines in LeafInput

This commit is contained in:
2023-02-27 10:54:43 -05:00
parent 48ab9a19e8
commit 2082eba527
9 changed files with 116 additions and 43 deletions
+24 -21
View File
@@ -9,7 +9,7 @@ namespace LeafWeb.Core.Utility
{
public static void Register()
{
TypeDescriptor.AddAttributes(typeof(Boolean),
TypeDescriptor.AddAttributes(typeof(bool),
new TypeConverterAttribute(typeof(BoolTypeConverter)));
}
@@ -24,26 +24,29 @@ namespace LeafWeb.Core.Utility
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (value is bool)
return value;
if (value is string)
{
var s = value as string;
if (string.IsNullOrEmpty(s))
return false;
switch (s.Trim().ToUpper())
{
case "TRUE":
case "YES":
case "1":
case "-1":
return true;
switch (value)
{
case bool _:
return value;
case string s:
{
if (string.IsNullOrEmpty(s))
return false;
switch (s.Trim().ToUpper())
{
case "TRUE":
case "YES":
case "1":
case "-1":
return true;
default:
return false;
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
default:
return false;
}
}
default:
return base.ConvertTo(context, culture, value, destinationType);
}
}
}
}
+3 -6
View File
@@ -52,8 +52,7 @@ namespace LeafWeb.Core.Utility
if (property != null)
{
object convertedVal;
if (!TryConvertValue(property, value, out convertedVal))
if (!TryConvertValue(property, value, out var convertedVal))
throw new ParseException($"Cannot convert value '{value}' for {property.Name} at line number {lineNumber}");
property.Set(obj, convertedVal);
}
@@ -83,8 +82,7 @@ namespace LeafWeb.Core.Utility
if (property != null)
{
object convertedVal;
if (!TryConvertValue(property, value, out convertedVal))
if (!TryConvertValue(property, value, out var convertedVal))
throw new ParseException($"Cannot convert value '{value}' for {property.Name} in position {position}");
property.Set(obj, convertedVal);
}
@@ -113,8 +111,7 @@ namespace LeafWeb.Core.Utility
if (property != null)
{
object convertedVal;
if (!TryConvertValue(property, value, out convertedVal))
if (!TryConvertValue(property, value, out var convertedVal))
throw new ParseException($"Cannot convert value '{value}' for {property.Name} in position {position}");
property.Set(obj, convertedVal);
}