save progress
This commit is contained in:
@@ -3,6 +3,7 @@ using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using StellaOps.Scanner.CallGraph.Serialization;
|
||||
using StellaOps.Scanner.Reachability;
|
||||
|
||||
namespace StellaOps.Scanner.CallGraph;
|
||||
@@ -12,10 +13,18 @@ public sealed record CallGraphSnapshot(
|
||||
[property: JsonPropertyName("graphDigest")] string GraphDigest,
|
||||
[property: JsonPropertyName("language")] string Language,
|
||||
[property: JsonPropertyName("extractedAt")] DateTimeOffset ExtractedAt,
|
||||
[property: JsonPropertyName("nodes")] ImmutableArray<CallGraphNode> Nodes,
|
||||
[property: JsonPropertyName("edges")] ImmutableArray<CallGraphEdge> Edges,
|
||||
[property: JsonPropertyName("entrypointIds")] ImmutableArray<string> EntrypointIds,
|
||||
[property: JsonPropertyName("sinkIds")] ImmutableArray<string> SinkIds)
|
||||
[property: JsonPropertyName("nodes")]
|
||||
[property: JsonConverter(typeof(ImmutableArrayJsonConverter<CallGraphNode>))]
|
||||
ImmutableArray<CallGraphNode> Nodes,
|
||||
[property: JsonPropertyName("edges")]
|
||||
[property: JsonConverter(typeof(ImmutableArrayJsonConverter<CallGraphEdge>))]
|
||||
ImmutableArray<CallGraphEdge> Edges,
|
||||
[property: JsonPropertyName("entrypointIds")]
|
||||
[property: JsonConverter(typeof(ImmutableArrayJsonConverter<string>))]
|
||||
ImmutableArray<string> EntrypointIds,
|
||||
[property: JsonPropertyName("sinkIds")]
|
||||
[property: JsonConverter(typeof(ImmutableArrayJsonConverter<string>))]
|
||||
ImmutableArray<string> SinkIds)
|
||||
{
|
||||
public CallGraphSnapshot Trimmed()
|
||||
{
|
||||
@@ -286,7 +295,9 @@ public static class CallGraphDigests
|
||||
public sealed record ReachabilityPath(
|
||||
[property: JsonPropertyName("entrypointId")] string EntrypointId,
|
||||
[property: JsonPropertyName("sinkId")] string SinkId,
|
||||
[property: JsonPropertyName("nodeIds")] ImmutableArray<string> NodeIds)
|
||||
[property: JsonPropertyName("nodeIds")]
|
||||
[property: JsonConverter(typeof(ImmutableArrayJsonConverter<string>))]
|
||||
ImmutableArray<string> NodeIds)
|
||||
{
|
||||
public ReachabilityPath Trimmed()
|
||||
{
|
||||
@@ -309,9 +320,15 @@ public sealed record ReachabilityAnalysisResult(
|
||||
[property: JsonPropertyName("graphDigest")] string GraphDigest,
|
||||
[property: JsonPropertyName("language")] string Language,
|
||||
[property: JsonPropertyName("computedAt")] DateTimeOffset ComputedAt,
|
||||
[property: JsonPropertyName("reachableNodeIds")] ImmutableArray<string> ReachableNodeIds,
|
||||
[property: JsonPropertyName("reachableSinkIds")] ImmutableArray<string> ReachableSinkIds,
|
||||
[property: JsonPropertyName("paths")] ImmutableArray<ReachabilityPath> Paths,
|
||||
[property: JsonPropertyName("reachableNodeIds")]
|
||||
[property: JsonConverter(typeof(ImmutableArrayJsonConverter<string>))]
|
||||
ImmutableArray<string> ReachableNodeIds,
|
||||
[property: JsonPropertyName("reachableSinkIds")]
|
||||
[property: JsonConverter(typeof(ImmutableArrayJsonConverter<string>))]
|
||||
ImmutableArray<string> ReachableSinkIds,
|
||||
[property: JsonPropertyName("paths")]
|
||||
[property: JsonConverter(typeof(ImmutableArrayJsonConverter<ReachabilityPath>))]
|
||||
ImmutableArray<ReachabilityPath> Paths,
|
||||
[property: JsonPropertyName("resultDigest")] string ResultDigest)
|
||||
{
|
||||
public ReachabilityAnalysisResult Trimmed()
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace StellaOps.Scanner.CallGraph.Serialization;
|
||||
|
||||
/// <summary>
|
||||
/// System.Text.Json converter for <see cref="ImmutableArray{T}"/> to ensure default serializer options
|
||||
/// can round-trip call graph models without requiring per-call JsonSerializerOptions registration.
|
||||
/// </summary>
|
||||
public sealed class ImmutableArrayJsonConverter<T> : JsonConverter<ImmutableArray<T>>
|
||||
{
|
||||
public override ImmutableArray<T> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
if (reader.TokenType == JsonTokenType.Null)
|
||||
{
|
||||
return ImmutableArray<T>.Empty;
|
||||
}
|
||||
|
||||
var values = JsonSerializer.Deserialize<List<T>>(ref reader, options);
|
||||
if (values is null || values.Count == 0)
|
||||
{
|
||||
return ImmutableArray<T>.Empty;
|
||||
}
|
||||
|
||||
return ImmutableArray.CreateRange(values);
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, ImmutableArray<T> value, JsonSerializerOptions options)
|
||||
{
|
||||
writer.WriteStartArray();
|
||||
|
||||
var normalized = value.IsDefault ? ImmutableArray<T>.Empty : value;
|
||||
foreach (var item in normalized)
|
||||
{
|
||||
JsonSerializer.Serialize(writer, item, options);
|
||||
}
|
||||
|
||||
writer.WriteEndArray();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user