fix(adminui): AB forms read/write the factory-bound *Ms duration keys, migrate legacy TimeSpan strings (#576)
Claude-Session: https://claude.ai/code/session_01Ef2ozxS7QPanGT8uoATbTe
This commit is contained in:
+58
-7
@@ -16,15 +16,37 @@ internal static class DriverConfigJsonOverlay
|
||||
/// <param name="originalJson">The stored config as loaded; blank or non-object input yields <paramref name="typedJson"/> unchanged.</param>
|
||||
/// <param name="typedJson">The form's own serialised options.</param>
|
||||
/// <param name="ownedPaths">Dot-separated, case-insensitive property paths the form edits (e.g. <c>transport</c>, <c>probe.enabled</c>).</param>
|
||||
public static string Apply(string? originalJson, string typedJson, IEnumerable<string> ownedPaths)
|
||||
/// <param name="timeSpanToMs">
|
||||
/// (TimeSpan-string path, integer-millisecond path) pairs. The typed options serialise durations as
|
||||
/// <c>"00:00:05"</c> but the driver factories bind <c>*Ms</c> integers (#576); each pair is rewritten
|
||||
/// in the typed output (and the legacy TimeSpan path becomes owned, so a stale one is removed).
|
||||
/// </param>
|
||||
public static string Apply(string? originalJson, string typedJson, IEnumerable<string> ownedPaths,
|
||||
IReadOnlyList<(string TimeSpanPath, string MsPath)>? timeSpanToMs = null)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(originalJson)) return typedJson;
|
||||
JsonObject? original;
|
||||
try { original = JsonNode.Parse(originalJson) as JsonObject; }
|
||||
catch (JsonException) { return typedJson; }
|
||||
if (original is null || JsonNode.Parse(typedJson) is not JsonObject typed) return typedJson;
|
||||
if (JsonNode.Parse(typedJson) is not JsonObject typed) return typedJson;
|
||||
var owned = ownedPaths.ToList();
|
||||
foreach (var (tsPath, msPath) in timeSpanToMs ?? [])
|
||||
{
|
||||
owned.Add(tsPath);
|
||||
owned.Add(msPath);
|
||||
if (Get(typed, tsPath) is JsonValue v && v.TryGetValue<string>(out var text)
|
||||
&& TimeSpan.TryParse(text, System.Globalization.CultureInfo.InvariantCulture, out var ts))
|
||||
{
|
||||
RemovePath(typed, tsPath);
|
||||
Set(typed, msPath, (int)ts.TotalMilliseconds);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var path in ownedPaths)
|
||||
JsonObject? original = null;
|
||||
if (!string.IsNullOrWhiteSpace(originalJson))
|
||||
{
|
||||
try { original = JsonNode.Parse(originalJson) as JsonObject; }
|
||||
catch (JsonException) { /* unusable original: fall through to the typed output */ }
|
||||
}
|
||||
if (original is null) return typed.ToJsonString();
|
||||
|
||||
foreach (var path in owned)
|
||||
{
|
||||
var segments = path.Split('.');
|
||||
JsonObject? from = typed;
|
||||
@@ -50,6 +72,35 @@ internal static class DriverConfigJsonOverlay
|
||||
return original.ToJsonString();
|
||||
}
|
||||
|
||||
private static JsonNode? Get(JsonObject root, string path)
|
||||
{
|
||||
JsonNode? cur = root;
|
||||
foreach (var seg in path.Split('.'))
|
||||
cur = cur is JsonObject o ? Find(o, seg) : null;
|
||||
return cur;
|
||||
}
|
||||
|
||||
private static void RemovePath(JsonObject root, string path)
|
||||
{
|
||||
var segs = path.Split('.');
|
||||
JsonObject? cur = root;
|
||||
for (var i = 0; i < segs.Length - 1 && cur is not null; i++)
|
||||
cur = Find(cur, segs[i]) as JsonObject;
|
||||
if (cur is not null) Remove(cur, segs[^1]);
|
||||
}
|
||||
|
||||
private static void Set(JsonObject root, string path, JsonNode value)
|
||||
{
|
||||
var segs = path.Split('.');
|
||||
var cur = root;
|
||||
for (var i = 0; i < segs.Length - 1; i++)
|
||||
{
|
||||
if (Find(cur, segs[i]) is not JsonObject next) { next = []; cur[segs[i]] = next; }
|
||||
cur = next;
|
||||
}
|
||||
cur[segs[^1]] = value;
|
||||
}
|
||||
|
||||
private static JsonNode? Find(JsonObject o, string key)
|
||||
{
|
||||
foreach (var kv in o)
|
||||
|
||||
Reference in New Issue
Block a user