fix(uns): omit blank optional keys from TagConfig + add omission tests; drop unused ParseInt (T4-T8 review)

This commit is contained in:
Joseph Doherty
2026-06-09 09:49:33 -04:00
parent 75021fa2c9
commit c0afecda50
10 changed files with 87 additions and 11 deletions
@@ -28,8 +28,6 @@
_m = AbCipTagConfigModel.FromJson(ConfigJson);
}
private static int ParseInt(object? v, int fallback = 0) => int.TryParse(v?.ToString(), out var i) ? i : fallback;
// TryParse so a bad/empty change value can never throw into the Blazor circuit — it falls back.
private static TEnum ParseEnum<TEnum>(object? v, TEnum fallback) where TEnum : struct, Enum
=> Enum.TryParse<TEnum>(v?.ToString(), out var r) ? r : fallback;
@@ -28,8 +28,6 @@
_m = AbLegacyTagConfigModel.FromJson(ConfigJson);
}
private static int ParseInt(object? v, int fallback = 0) => int.TryParse(v?.ToString(), out var i) ? i : fallback;
// TryParse so a bad/empty change value can never throw into the Blazor circuit — it falls back.
private static TEnum ParseEnum<TEnum>(object? v, TEnum fallback) where TEnum : struct, Enum
=> Enum.TryParse<TEnum>(v?.ToString(), out var r) ? r : fallback;
@@ -28,8 +28,6 @@
_m = FocasTagConfigModel.FromJson(ConfigJson);
}
private static int ParseInt(object? v, int fallback = 0) => int.TryParse(v?.ToString(), out var i) ? i : fallback;
// TryParse so a bad/empty change value can never throw into the Blazor circuit — it falls back.
private static TEnum ParseEnum<TEnum>(object? v, TEnum fallback) where TEnum : struct, Enum
=> Enum.TryParse<TEnum>(v?.ToString(), out var r) ? r : fallback;
@@ -28,8 +28,6 @@
_m = TwinCATTagConfigModel.FromJson(ConfigJson);
}
private static int ParseInt(object? v, int fallback = 0) => int.TryParse(v?.ToString(), out var i) ? i : fallback;
// TryParse so a bad/empty change value can never throw into the Blazor circuit — it falls back.
private static TEnum ParseEnum<TEnum>(object? v, TEnum fallback) where TEnum : struct, Enum
=> Enum.TryParse<TEnum>(v?.ToString(), out var r) ? r : fallback;
@@ -33,7 +33,10 @@ public static class TagConfigJson
public static TEnum GetEnum<TEnum>(JsonObject o, string name, TEnum fallback) where TEnum : struct, Enum
=> GetString(o, name) is { } s && Enum.TryParse<TEnum>(s, ignoreCase: true, out var v) ? v : fallback;
/// <summary>Sets a string/number/enum-name value (enums via ToString()). Null removes the key.</summary>
/// <summary>Sets a string/number/enum-name value (enums via ToString()). A null value REMOVES the key, so it is omitted from the serialised JSON.</summary>
public static void Set(JsonObject o, string name, object? value)
=> o[name] = value is null ? null : JsonValue.Create(value is Enum e ? e.ToString() : value);
{
if (value is null) { o.Remove(name); return; }
o[name] = JsonValue.Create(value is Enum e ? e.ToString() : value);
}
}
@@ -79,4 +79,24 @@ public sealed class AbCipTagConfigModelTests
{
new AbCipTagConfigModel { TagPath = "Motor1.Status" }.Validate().ShouldBeNull();
}
[Fact]
public void ToJson_omits_blank_deviceHostAddress()
{
var m = new AbCipTagConfigModel { TagPath = "Motor1.Status", DeviceHostAddress = "" };
var json = m.ToJson();
json.ShouldNotContain("deviceHostAddress");
}
[Fact]
public void ToJson_includes_deviceHostAddress_when_set()
{
var m = new AbCipTagConfigModel { TagPath = "Motor1.Status", DeviceHostAddress = "ab://host" };
var json = m.ToJson();
json.ShouldContain("\"deviceHostAddress\":\"ab://host\"");
}
}
@@ -79,4 +79,24 @@ public sealed class AbLegacyTagConfigModelTests
{
new AbLegacyTagConfigModel { Address = "N7:0" }.Validate().ShouldBeNull();
}
[Fact]
public void ToJson_omits_blank_deviceHostAddress()
{
var m = new AbLegacyTagConfigModel { Address = "N7:0", DeviceHostAddress = "" };
var json = m.ToJson();
json.ShouldNotContain("deviceHostAddress");
}
[Fact]
public void ToJson_includes_deviceHostAddress_when_set()
{
var m = new AbLegacyTagConfigModel { Address = "N7:0", DeviceHostAddress = "ab://host" };
var json = m.ToJson();
json.ShouldContain("\"deviceHostAddress\":\"ab://host\"");
}
}
@@ -79,4 +79,24 @@ public sealed class FocasTagConfigModelTests
{
new FocasTagConfigModel { Address = "R100" }.Validate().ShouldBeNull();
}
[Fact]
public void ToJson_omits_blank_deviceHostAddress()
{
var m = new FocasTagConfigModel { Address = "R100", DeviceHostAddress = "" };
var json = m.ToJson();
json.ShouldNotContain("deviceHostAddress");
}
[Fact]
public void ToJson_includes_deviceHostAddress_when_set()
{
var m = new FocasTagConfigModel { Address = "R100", DeviceHostAddress = "ab://host" };
var json = m.ToJson();
json.ShouldContain("\"deviceHostAddress\":\"ab://host\"");
}
}
@@ -129,6 +129,7 @@ public sealed class TagConfigJsonTests
TagConfigJson.Set(obj, "region", null);
TagConfigJson.GetString(obj, "region").ShouldBeNull();
obj.ContainsKey("region").ShouldBeFalse();
TagConfigJson.Serialize(obj).ShouldNotContain("region");
}
}
@@ -79,4 +79,24 @@ public sealed class TwinCATTagConfigModelTests
{
new TwinCATTagConfigModel { SymbolPath = "MAIN.bStart" }.Validate().ShouldBeNull();
}
[Fact]
public void ToJson_omits_blank_deviceHostAddress()
{
var m = new TwinCATTagConfigModel { SymbolPath = "MAIN.bStart", DeviceHostAddress = "" };
var json = m.ToJson();
json.ShouldNotContain("deviceHostAddress");
}
[Fact]
public void ToJson_includes_deviceHostAddress_when_set()
{
var m = new TwinCATTagConfigModel { SymbolPath = "MAIN.bStart", DeviceHostAddress = "ab://host" };
var json = m.ToJson();
json.ShouldContain("\"deviceHostAddress\":\"ab://host\"");
}
}