Minor tweaks for better Ignitor debugability (#22446)

This commit is contained in:
Pranav K 2020-06-02 17:45:52 -07:00 committed by GitHub
parent bdc051a08a
commit 646dfc63e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 4 deletions

View File

@ -11,5 +11,7 @@ namespace Ignitor
{ {
_componentId = componentId; _componentId = componentId;
} }
public int ComponentId => _componentId;
} }
} }

View File

@ -3,11 +3,16 @@
#nullable enable #nullable enable
using System.Diagnostics;
namespace Ignitor namespace Ignitor
{ {
[DebuggerDisplay("{SerializedValue}")]
public abstract class Node public abstract class Node
{ {
public virtual ContainerNode? Parent { get; set; } public virtual ContainerNode? Parent { get; set; }
public string SerializedValue => NodeSerializer.Serialize(this);
} }
} }

View File

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
#nullable enable #nullable enable
@ -20,11 +21,22 @@ namespace Ignitor
} }
} }
public static string Serialize(Node node)
{
using (var writer = new StringWriter())
{
var serializer = new Serializer(writer);
serializer.Serialize(node);
return writer.ToString();
}
}
private class Serializer private class Serializer
{ {
private readonly TextWriter _writer; private readonly TextWriter _writer;
private int _depth; private int _depth;
private bool _atStartOfLine; private bool _atStartOfLine;
private HashSet<Node> _visited = new HashSet<Node>();
public Serializer(TextWriter writer) public Serializer(TextWriter writer)
{ {
@ -35,14 +47,25 @@ namespace Ignitor
{ {
foreach (var kvp in hive.Components) foreach (var kvp in hive.Components)
{ {
SerializeComponent(kvp.Key, kvp.Value); Serialize(kvp.Value);
} }
} }
private void Serialize(Node node) public void Serialize(Node node)
{ {
if (!_visited.Add(node))
{
// This is a child component of a previously seen component. Don't repeat it
return;
}
switch (node) switch (node)
{ {
case ComponentNode componentNode:
{
SerializeComponent(componentNode);
break;
}
case ElementNode elementNode: case ElementNode elementNode:
{ {
SerializeElement(elementNode); SerializeElement(elementNode);
@ -155,10 +178,10 @@ namespace Ignitor
} }
} }
private void SerializeComponent(int id, ComponentNode component) private void SerializeComponent(ComponentNode component)
{ {
Write("[Component ( "); Write("[Component ( ");
Write(id.ToString()); Write(component.ComponentId.ToString());
WriteLine(" )]"); WriteLine(" )]");
_depth++; _depth++;
SerializeChildren(component); SerializeChildren(component);