refactor(commons): consolidate List element-type/coercion into AttributeValueCodec; InstanceActor + CLI reuse it (#93)
This commit is contained in:
@@ -896,20 +896,12 @@ public class InstanceActor : ReceiveActor
|
||||
|
||||
try
|
||||
{
|
||||
// Construct the typed list INSIDE the try: although the six valid
|
||||
// element types resolved by ListElementClrType cannot throw today,
|
||||
// keeping ListElementClrType / MakeGenericType / CreateInstance inside
|
||||
// the guarded block means any future change that introduces a throw
|
||||
// here is caught and turned into a Bad-quality result rather than
|
||||
// Coerce INSIDE the try: although the six valid element types cannot
|
||||
// throw on construction today, keeping the (shared) codec coercion
|
||||
// inside the guarded block means any future change that introduces a
|
||||
// throw is caught and turned into a Bad-quality result rather than
|
||||
// escaping into the actor and tripping supervision.
|
||||
var clrType = ListElementClrType(elementType);
|
||||
var list = (System.Collections.IList)Activator.CreateInstance(
|
||||
typeof(List<>).MakeGenericType(clrType))!;
|
||||
|
||||
foreach (var element in enumerable)
|
||||
list.Add(CoerceElement(element, elementType));
|
||||
|
||||
typedList = list;
|
||||
typedList = AttributeValueCodec.CoerceEnumerable(enumerable, elementType);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -922,46 +914,6 @@ public class InstanceActor : ReceiveActor
|
||||
}
|
||||
}
|
||||
|
||||
private static Type ListElementClrType(DataType t) => t switch
|
||||
{
|
||||
DataType.String => typeof(string),
|
||||
DataType.Int32 => typeof(int),
|
||||
DataType.Float => typeof(float),
|
||||
DataType.Double => typeof(double),
|
||||
DataType.Boolean => typeof(bool),
|
||||
DataType.DateTime => typeof(DateTime),
|
||||
_ => throw new FormatException($"Unsupported list element type '{t}'.")
|
||||
};
|
||||
|
||||
private static object CoerceElement(object? element, DataType t)
|
||||
{
|
||||
if (element is null)
|
||||
throw new FormatException("List elements may not be null.");
|
||||
|
||||
var culture = System.Globalization.CultureInfo.InvariantCulture;
|
||||
return t switch
|
||||
{
|
||||
DataType.String => Convert.ToString(element, culture)
|
||||
?? throw new FormatException("Null string element."),
|
||||
DataType.Int32 => element is string si
|
||||
? int.Parse(si, culture)
|
||||
: Convert.ToInt32(element, culture),
|
||||
DataType.Float => element is string sf
|
||||
? float.Parse(sf, culture)
|
||||
: Convert.ToSingle(element, culture),
|
||||
DataType.Double => element is string sd
|
||||
? double.Parse(sd, culture)
|
||||
: Convert.ToDouble(element, culture),
|
||||
DataType.Boolean => element is string sb
|
||||
? bool.Parse(sb)
|
||||
: Convert.ToBoolean(element, culture),
|
||||
DataType.DateTime => element is string sdt
|
||||
? DateTime.Parse(sdt, culture, System.Globalization.DateTimeStyles.RoundtripKind)
|
||||
: Convert.ToDateTime(element, culture),
|
||||
_ => throw new FormatException($"Unsupported list element type '{t}'.")
|
||||
};
|
||||
}
|
||||
|
||||
private void HandleConnectionQualityChanged(ConnectionQualityChanged qualityChanged)
|
||||
{
|
||||
_logger.LogWarning("Connection {Connection} quality changed to {Quality} for instance {Instance}",
|
||||
|
||||
Reference in New Issue
Block a user