826312befe
Clears CommentChecker findings in src/ (3,280 -> 209, the remainder being false positives like PLC-5/IEEE-754/M0.0) by adding missing summaries/params/returns, normalising inheritdoc usage, and removing Gitea/review tracking IDs while keeping the rationale they annotated. Comment-only: comment-stripped sources are identical to the parent commit.
118 lines
5.0 KiB
C#
118 lines
5.0 KiB
C#
using System.Text.Json;
|
|
using System.Text.Json.Nodes;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Drivers;
|
|
|
|
/// <summary>
|
|
/// Preserve-unknown save for the typed driver forms. A form deserialises the stored DriverConfig
|
|
/// into a typed options object and re-serialises it on save, which erases every key the type does not
|
|
/// model (a newer option, a hand-edited key, <c>rawTags</c>, a device's extra keys…). This overlays only
|
|
/// the paths the form OWNS onto the ORIGINAL JSON object: an owned path is replaced by the form's value,
|
|
/// or removed when the form emits nothing/null ("Default"); every other key in the original survives.
|
|
/// </summary>
|
|
internal static class DriverConfigJsonOverlay
|
|
{
|
|
/// <summary>Overlays the <paramref name="ownedPaths"/> of <paramref name="typedJson"/> onto <paramref name="originalJson"/>.</summary>
|
|
/// <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>
|
|
/// <returns>The merged JSON document.</returns>
|
|
/// <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; 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 (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);
|
|
}
|
|
}
|
|
|
|
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;
|
|
JsonObject? to = original;
|
|
for (var i = 0; i < segments.Length - 1 && to is not null; i++)
|
|
{
|
|
from = from is null ? null : Find(from, segments[i]) as JsonObject;
|
|
var next = Find(to, segments[i]) as JsonObject;
|
|
if (next is null && from is not null)
|
|
{
|
|
next = [];
|
|
to[segments[i]] = next;
|
|
}
|
|
to = next;
|
|
}
|
|
if (to is null) continue;
|
|
|
|
var leaf = segments[^1];
|
|
var value = from is null ? null : Find(from, leaf);
|
|
Remove(to, leaf);
|
|
if (value is not null) to[leaf] = value.DeepClone();
|
|
}
|
|
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)
|
|
if (string.Equals(kv.Key, key, StringComparison.OrdinalIgnoreCase)) return kv.Value;
|
|
return null;
|
|
}
|
|
|
|
private static void Remove(JsonObject o, string key)
|
|
{
|
|
var hit = o.Select(kv => kv.Key).FirstOrDefault(k => string.Equals(k, key, StringComparison.OrdinalIgnoreCase));
|
|
if (hit is not null) o.Remove(hit);
|
|
}
|
|
}
|