fix(dcl): format ArrayValue objects as comma-separated strings for display
ArrayValue from LmxProxy client was showing as type name in debug views. Added ValueFormatter utility and NormalizeValue in LmxProxyDataConnection to convert arrays at the adapter boundary. DateTime arrays remain as "System.DateTime[]" due to server-side v1 string serialization.
This commit is contained in:
59
src/ScadaLink.Commons/Types/ValueFormatter.cs
Normal file
59
src/ScadaLink.Commons/Types/ValueFormatter.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using System.Collections;
|
||||
using System.Reflection;
|
||||
|
||||
namespace ScadaLink.Commons.Types;
|
||||
|
||||
/// <summary>
|
||||
/// Formats attribute values for display. Handles scalar types directly
|
||||
/// and uses reflection to extract array contents from complex types
|
||||
/// (e.g., LmxProxy ArrayValue) rather than showing the type name.
|
||||
/// </summary>
|
||||
public static class ValueFormatter
|
||||
{
|
||||
/// <summary>
|
||||
/// Formats a value for display as a string. Returns the value's natural
|
||||
/// string representation for scalars, and comma-separated elements for
|
||||
/// array/collection types.
|
||||
/// </summary>
|
||||
public static string FormatDisplayValue(object? value)
|
||||
{
|
||||
if (value is null) return "";
|
||||
if (value is string s) return s;
|
||||
if (value is IFormattable) return value.ToString() ?? "";
|
||||
|
||||
// Check if it's an array-like container with typed sub-collections
|
||||
// (e.g., LmxProxy ArrayValue with BoolValues, Int32Values, etc.)
|
||||
var type = value.GetType();
|
||||
if (type.Namespace?.Contains("LmxProxy") == true || type.Name == "ArrayValue")
|
||||
{
|
||||
return FormatArrayContainer(value, type);
|
||||
}
|
||||
|
||||
// Fallback for IEnumerable (generic collections, arrays)
|
||||
if (value is IEnumerable enumerable)
|
||||
{
|
||||
return string.Join(",", enumerable.Cast<object?>().Select(e => e?.ToString() ?? ""));
|
||||
}
|
||||
|
||||
return value.ToString() ?? "";
|
||||
}
|
||||
|
||||
private static string FormatArrayContainer(object container, Type type)
|
||||
{
|
||||
// Look for the first non-null property that has a Values list
|
||||
foreach (var prop in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
|
||||
{
|
||||
var propValue = prop.GetValue(container);
|
||||
if (propValue is null) continue;
|
||||
|
||||
// Check if this property has a Values sub-property (e.g., BoolArray.Values)
|
||||
var valuesProp = propValue.GetType().GetProperty("Values");
|
||||
if (valuesProp?.GetValue(propValue) is IEnumerable values)
|
||||
{
|
||||
return string.Join(",", values.Cast<object?>().Select(e => e?.ToString() ?? ""));
|
||||
}
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user