mirror of https://github.com/sean-m/McRule.git
Updated Dump function to print out arrays of objects in tables with handling for null properties and array properties. Not exhaustive or robust, just a debugging aid.
parent
e7b17dc0d3
commit
6178752da7
|
@ -1,6 +1,9 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using ConsoleTables;
|
||||
using McRule;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
|
@ -13,19 +16,81 @@ public static class MyExtensions {
|
|||
Console.WriteLine("> {0}\n", string.Join(", ", msgs));
|
||||
}
|
||||
|
||||
var thingType = thing.GetType();
|
||||
var isEnumerable = thingType.GetInterface("IEnumerable");
|
||||
|
||||
if (thing is String) {
|
||||
Console.WriteLine(thing);
|
||||
} else if (thing is Expression ex) {
|
||||
Console.WriteLine(ex.ToString());
|
||||
}else {
|
||||
} else if (isEnumerable != null) {
|
||||
var thingEnumerable = (IEnumerable)thing;
|
||||
var count = 0;
|
||||
foreach (var _ in thingEnumerable) count++;
|
||||
|
||||
if ((count < 1)) goto end;
|
||||
|
||||
|
||||
var thingEnum = thingEnumerable.GetEnumerator();
|
||||
if (!thingEnum.MoveNext()) goto end;
|
||||
|
||||
var first = thingEnum.Current;
|
||||
var props = first.GetType().GetProperties();
|
||||
|
||||
var table = new ConsoleTable(props.Select(x => x.Name).ToArray());
|
||||
foreach (var e in thingEnumerable) {
|
||||
var row = new List<object>();
|
||||
foreach (var p in props) {
|
||||
row.Add(FormatProperty(p.GetValue(e)));
|
||||
}
|
||||
|
||||
table.AddRow(row.ToArray());
|
||||
}
|
||||
|
||||
table.Write(Format.Minimal);
|
||||
} else {
|
||||
Console.WriteLine(JsonConvert.SerializeObject(thing, Formatting.Indented));
|
||||
}
|
||||
|
||||
end:
|
||||
return thing;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Format properties for being printed inside a table. For array objects with fewer than 4
|
||||
/// elements, wrap em in square brackets and comma delimit them. 4 or more, take the first
|
||||
/// three that way then append ellipsis and the number of elements. This is more for
|
||||
/// debugging than anything else.
|
||||
/// </summary>
|
||||
/// <param name="Property"></param>
|
||||
/// <returns></returns>
|
||||
private static string FormatProperty(dynamic Property) {
|
||||
if (Property == null) return "*NULL";
|
||||
|
||||
if (Property is String s) return s;
|
||||
|
||||
if (Property.GetType().GetInterface("IEnumerable") != null) {
|
||||
if (Property.Length < 4) {
|
||||
return $"[{String.Join(", ", Property)}]";
|
||||
} else {
|
||||
var elems = new List<string>();
|
||||
var count = 0;
|
||||
foreach (var el in Property) {
|
||||
count++;
|
||||
elems.Add(el);
|
||||
if (count >= 3) break;
|
||||
}
|
||||
|
||||
return $"[{String.Join(", ", elems)}...({Property.Length})]";
|
||||
}
|
||||
}
|
||||
|
||||
return Property?.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static class FilterRuleManager
|
||||
{
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="ConsoleTables" Version="2.4.2" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
|
|
Loading…
Reference in New Issue