perf(calc-contracts): single-parse Inspect (Driver.Calculation.Contracts-001)
Inspect parsed the TagConfig blob once itself (root kind + the present-but-discarded changeTriggered/timerIntervalMs checks) and then called TryReadTagConfig(reference, out _), which parsed the same string from scratch again for the scriptId rule. The field-by-field read is now TryReadRoot(JsonElement), which both entry points share, so Inspect consults the one authority over the root it already has. The public string-based TryReadTagConfig signature other modules consume is unchanged. Pure refactor: added CalculationTagDefinitionFactoryInspectTests (17 cases pinning the exact wording, the rejection-vs-advisory split and their order, and the never-stricter-than-the-runtime-read invariant). They pass unchanged against the pre-refactor code — verified by stashing the production change.
This commit is contained in:
+39
-21
@@ -68,26 +68,7 @@ public static class CalculationTagDefinitionFactory
|
||||
try
|
||||
{
|
||||
using var doc = JsonDocument.Parse(tagConfig);
|
||||
var root = doc.RootElement;
|
||||
if (root.ValueKind != JsonValueKind.Object) return false;
|
||||
|
||||
var scriptId = root.TryGetProperty("scriptId", out var sidEl) && sidEl.ValueKind == JsonValueKind.String
|
||||
? sidEl.GetString() : null;
|
||||
if (string.IsNullOrWhiteSpace(scriptId)) return false;
|
||||
|
||||
var scriptSource = root.TryGetProperty("scriptSource", out var srcEl) && srcEl.ValueKind == JsonValueKind.String
|
||||
? srcEl.GetString() ?? string.Empty : string.Empty;
|
||||
|
||||
// changeTriggered defaults to true (mini-design §2) — only an explicit false disables it.
|
||||
var changeTriggered = !(root.TryGetProperty("changeTriggered", out var ctEl)
|
||||
&& ctEl.ValueKind == JsonValueKind.False);
|
||||
|
||||
var timerMs = root.TryGetProperty("timerIntervalMs", out var tEl)
|
||||
&& tEl.ValueKind == JsonValueKind.Number && tEl.TryGetInt32(out var ms) && ms > 0
|
||||
? ms : (int?)null;
|
||||
|
||||
fields = new CalculationTagConfigFields(scriptId!, scriptSource, changeTriggered, timerMs);
|
||||
return true;
|
||||
return TryReadRoot(doc.RootElement, out fields);
|
||||
}
|
||||
catch (JsonException)
|
||||
{
|
||||
@@ -95,6 +76,41 @@ public static class CalculationTagDefinitionFactory
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The field-by-field read, over an already-parsed root. Split out of
|
||||
/// <see cref="TryReadTagConfig"/> so <see cref="Inspect"/> — which has necessarily parsed the blob
|
||||
/// already to reach its root-kind and present-but-discarded checks — can consult the one authority
|
||||
/// on what a usable <c>scriptId</c> is without parsing the same string a second time
|
||||
/// (Driver.Calculation.Contracts-001). The public string-based entry point is what other modules
|
||||
/// consume, so it keeps its signature.
|
||||
/// </summary>
|
||||
/// <param name="root">The parsed <c>TagConfig</c> root element.</param>
|
||||
/// <param name="fields">The read fields when this returns <see langword="true"/>.</param>
|
||||
/// <returns><see langword="true"/> when the root is an object naming a script.</returns>
|
||||
private static bool TryReadRoot(JsonElement root, out CalculationTagConfigFields fields)
|
||||
{
|
||||
fields = default;
|
||||
if (root.ValueKind != JsonValueKind.Object) return false;
|
||||
|
||||
var scriptId = root.TryGetProperty("scriptId", out var sidEl) && sidEl.ValueKind == JsonValueKind.String
|
||||
? sidEl.GetString() : null;
|
||||
if (string.IsNullOrWhiteSpace(scriptId)) return false;
|
||||
|
||||
var scriptSource = root.TryGetProperty("scriptSource", out var srcEl) && srcEl.ValueKind == JsonValueKind.String
|
||||
? srcEl.GetString() ?? string.Empty : string.Empty;
|
||||
|
||||
// changeTriggered defaults to true (mini-design §2) — only an explicit false disables it.
|
||||
var changeTriggered = !(root.TryGetProperty("changeTriggered", out var ctEl)
|
||||
&& ctEl.ValueKind == JsonValueKind.False);
|
||||
|
||||
var timerMs = root.TryGetProperty("timerIntervalMs", out var tEl)
|
||||
&& tEl.ValueKind == JsonValueKind.Number && tEl.TryGetInt32(out var ms) && ms > 0
|
||||
? ms : (int?)null;
|
||||
|
||||
fields = new CalculationTagConfigFields(scriptId!, scriptSource, changeTriggered, timerMs);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deploy-time inspection of a calc tag's <c>TagConfig</c> blob. Warns for a blob
|
||||
/// <see cref="TryReadTagConfig"/> rejects (unparseable JSON, a non-object root, or no usable
|
||||
@@ -130,7 +146,9 @@ public static class CalculationTagDefinitionFactory
|
||||
return warnings;
|
||||
}
|
||||
|
||||
if (!TryReadTagConfig(reference, out _))
|
||||
// The SAME read the runtime performs, over the root already parsed above — never a second
|
||||
// parse of the same string, and never a reimplementation of the scriptId rule.
|
||||
if (!TryReadRoot(root, out _))
|
||||
{
|
||||
warnings.Add(
|
||||
"Calculation TagConfig has no usable 'scriptId' (a non-blank string) — the driver does not "
|
||||
|
||||
Reference in New Issue
Block a user