Skip to content

Commit

Permalink
Improve parsing of JSON messages in Slice tools (#4051)
Browse files Browse the repository at this point in the history
  • Loading branch information
pepone authored Sep 11, 2024
1 parent 0964f56 commit b2b4a49
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 26 deletions.
23 changes: 23 additions & 0 deletions tools/IceRpc.Slice.Tools/BuildTelemetry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) ZeroC, Inc.

using System.Text.Json.Serialization;

namespace IceRpc.Slice.Tools;

public class BuildTelemetry
{
[JsonPropertyName("hash")]
public string CompilationHash { get; set; } = "";

[JsonPropertyName("contains_slice1")]
public bool ContainsSlice1 { get; set; }

[JsonPropertyName("contains_slice2")]
public bool ContainsSlice2 { get; set; }

[JsonPropertyName("src_file_count")]
public int SourceFileCount { get; set; }

[JsonPropertyName("ref_file_count")]
public int ReferenceFileCount { get; set; }
}
13 changes: 0 additions & 13 deletions tools/IceRpc.Slice.Tools/DiagnosticParser.cs

This file was deleted.

26 changes: 13 additions & 13 deletions tools/IceRpc.Slice.Tools/SliceCCSharpTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text.Json;

namespace IceRpc.Slice.Tools;

Expand Down Expand Up @@ -61,6 +62,8 @@ public class SliceCCSharpTask : ToolTask
[Output]
public int ReferenceFileCount { get; set; }

private readonly JsonSerializerOptions _jsonSerializeOptions = new() { PropertyNameCaseInsensitive = true };

/// <inheritdoc/>
protected override string GenerateCommandLineCommands()
{
Expand Down Expand Up @@ -111,25 +114,22 @@ protected override void LogEventsFromTextOutput(string singleLine, MessageImport
{
if (messageImportance == MessageImportance.Low)
{
try
// Messages from stdout
if (JsonSerializer.Deserialize<BuildTelemetry>(
singleLine,
_jsonSerializeOptions) is BuildTelemetry buildTelemetry)
{
// Messages from stdout
var jsonDoc = System.Text.Json.JsonDocument.Parse(singleLine);
CompilationHash = jsonDoc.RootElement.GetProperty("hash").GetString();
ContainsSlice1 = jsonDoc.RootElement.GetProperty("contains_slice1").GetBoolean();
ContainsSlice2 = jsonDoc.RootElement.GetProperty("contains_slice2").GetBoolean();
SourceFileCount = jsonDoc.RootElement.GetProperty("src_file_count").GetInt32();
ReferenceFileCount = jsonDoc.RootElement.GetProperty("ref_file_count").GetInt32();
}
catch (System.Text.Json.JsonException ex)
{
Log.LogError($"Error parsing JSON: {ex.Message}. JSON content: {singleLine}");
CompilationHash = buildTelemetry.CompilationHash;
ContainsSlice1 = buildTelemetry.ContainsSlice1;
ContainsSlice2 = buildTelemetry.ContainsSlice2;
SourceFileCount = buildTelemetry.SourceFileCount;
ReferenceFileCount = buildTelemetry.ReferenceFileCount;
}
}
else if (DiagnosticParser.Parse(singleLine) is Diagnostic diagnostic)
else if (JsonSerializer.Deserialize<Diagnostic>(singleLine, _jsonSerializeOptions) is Diagnostic diagnostic)
{
diagnostic.SourceSpan ??= new SourceSpan();

LogSliceCompilerDiagnostic(
diagnostic.Severity,
diagnostic.Message,
Expand Down

0 comments on commit b2b4a49

Please sign in to comment.