using ZB.MOM.WW.ScadaBridge.Commons.Types;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
namespace ZB.MOM.WW.ScadaBridge.Transport.Serialization;
///
/// Import-time normalization of attribute values to the native-typed JSON form.
///
/// Bundles exported before the native-typed-JSON change carry List attribute
/// values in the old quoted-element form (e.g. ["10","20"] for an
/// list). Already-exported bundle files can't be
/// rewritten, so List values are normalised on import: every DTO→entity write
/// site routes the value through so imported
/// data lands native ([10,20]). The central DB normalizer remains the
/// backstop for anything that slips through.
///
///
/// Non-List attributes and null/empty values pass through unchanged. A value
/// that fails to decode (malformed JSON / un-parseable element) is left exactly
/// as-is so the import still succeeds — the DB normalizer is the backstop.
///
///
internal static class ImportValueNormalizer
{
///
/// Returns the native-typed JSON form of a List attribute value, or the
/// value unchanged for non-List / null / empty / malformed inputs.
///
/// The attribute value as carried by the bundle DTO.
/// The attribute's declared data type.
/// The List element type (null for scalars).
public static string? NormalizeListValue(string? value, DataType dataType, DataType? elementType)
{
if (dataType != DataType.List || string.IsNullOrEmpty(value))
{
return value;
}
try
{
return AttributeValueCodec.Encode(
AttributeValueCodec.Decode(value, DataType.List, elementType));
}
catch (FormatException)
{
// Leave malformed values exactly as imported; the DB normalizer is
// the backstop. Never abort the import for a single bad value.
return value;
}
}
}