mirror of
https://github.com/PepperDash/Essentials.git
synced 2026-02-15 12:44:58 +00:00
refactor: moved markdown method inside join data
feat: proper error handling
This commit is contained in:
@@ -16,5 +16,9 @@ namespace PepperDash.Essentials.Core
|
|||||||
{
|
{
|
||||||
return string.IsNullOrEmpty(s.Trim()) ? null : s;
|
return string.IsNullOrEmpty(s.Trim()) ? null : s;
|
||||||
}
|
}
|
||||||
|
public static string ReplaceIfNullOrEmpty(this string s, string newString)
|
||||||
|
{
|
||||||
|
return string.IsNullOrEmpty(s) ? newString : s;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Data;
|
||||||
|
using System.Globalization;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
@@ -196,19 +198,6 @@ namespace PepperDash.Essentials.Core
|
|||||||
|
|
||||||
protected void AddJoins(Type type)
|
protected void AddJoins(Type type)
|
||||||
{
|
{
|
||||||
// Add all the JoinDataComplete properties to the Joins Dictionary and pass in the offset
|
|
||||||
//Joins = this.GetType()
|
|
||||||
// .GetCType()
|
|
||||||
// .GetFields(BindingFlags.Public | BindingFlags.Instance)
|
|
||||||
// .Where(field => field.IsDefined(typeof(JoinNameAttribute), true))
|
|
||||||
// .Select(field => (JoinDataComplete)field.GetValue(this))
|
|
||||||
// .ToDictionary(join => join.GetNameAttribute(), join =>
|
|
||||||
// {
|
|
||||||
// join.SetJoinOffset(_joinOffset);
|
|
||||||
// return join;
|
|
||||||
// });
|
|
||||||
|
|
||||||
//type = this.GetType(); <- this wasn't working because 'this' was always the base class, never the derived class
|
|
||||||
var fields =
|
var fields =
|
||||||
type.GetCType()
|
type.GetCType()
|
||||||
.GetFields(BindingFlags.Public | BindingFlags.Instance)
|
.GetFields(BindingFlags.Public | BindingFlags.Instance)
|
||||||
@@ -222,7 +211,7 @@ namespace PepperDash.Essentials.Core
|
|||||||
|
|
||||||
if (value == null)
|
if (value == null)
|
||||||
{
|
{
|
||||||
Debug.Console(0, "Unable to caset base class to {0}", type.Name);
|
Debug.Console(0, "Unable to cast base class to {0}", type.Name);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -356,25 +345,15 @@ namespace PepperDash.Essentials.Core
|
|||||||
const int joinSpanLen = 9;
|
const int joinSpanLen = 9;
|
||||||
const int typeLen = 19;
|
const int typeLen = 19;
|
||||||
const int capabilitiesLen = 12;
|
const int capabilitiesLen = 12;
|
||||||
var descriptionLen = (from @join in joins
|
var descriptionLen = (from @join in joins select @join.Value into j select j.Metadata.Description.Length).Concat(new[] {11}).Max();
|
||||||
select String.IsNullOrEmpty(@join.Value.AttributeName)
|
|
||||||
? @join.Value.Metadata.Description
|
|
||||||
: @join.Value.AttributeName into description
|
|
||||||
select description.Length)
|
|
||||||
.Concat(new[] { 11 }).Max();
|
|
||||||
|
|
||||||
var joinNumberHeader = String.Format("Join Number").PadRight(joinNumberLen, ' ');
|
|
||||||
var joinSpanHeader = String.Format("Join Span").PadRight(joinSpanLen, ' ');
|
|
||||||
var descriptionHeader = String.Format("Description").PadRight(descriptionLen, ' ');
|
|
||||||
var typeHeader = String.Format("Type").PadRight(typeLen, ' ');
|
|
||||||
var capabilitiesHeader = String.Format("Capabilities").PadRight(capabilitiesLen, ' ');
|
|
||||||
//build header
|
//build header
|
||||||
sb.AppendLine(String.Format(stringFormatter,
|
sb.AppendLine(String.Format(stringFormatter,
|
||||||
joinNumberHeader,
|
String.Format("Join Number").PadRight(joinNumberLen, ' '),
|
||||||
joinSpanHeader,
|
String.Format("Join Span").PadRight(joinSpanLen, ' '),
|
||||||
descriptionHeader,
|
String.Format("Description").PadRight(descriptionLen, ' '),
|
||||||
typeHeader,
|
String.Format("Type").PadRight(typeLen, ' '),
|
||||||
capabilitiesHeader));
|
String.Format("Capabilities").PadRight(capabilitiesLen, ' ')));
|
||||||
//build table seperator
|
//build table seperator
|
||||||
sb.AppendLine(String.Format(stringFormatter,
|
sb.AppendLine(String.Format(stringFormatter,
|
||||||
new String('-', joinNumberLen),
|
new String('-', joinNumberLen),
|
||||||
@@ -385,21 +364,7 @@ namespace PepperDash.Essentials.Core
|
|||||||
|
|
||||||
foreach (var join in joins)
|
foreach (var join in joins)
|
||||||
{
|
{
|
||||||
var joinNumber = String.Format("{0}", join.Value.JoinNumber).PadRight(joinNumberLen, ' ');
|
sb.AppendLine(join.Value.GetMarkdownFormattedData(stringFormatter, descriptionLen));
|
||||||
var joinSpan = String.Format("{0}", join.Value.JoinSpan).PadRight(joinSpanLen);
|
|
||||||
var joinDescription = String.Format(String.IsNullOrEmpty(join.Value.AttributeName)
|
|
||||||
? join.Value.Metadata.Description
|
|
||||||
: join.Value.AttributeName).PadRight(descriptionLen, ' ');
|
|
||||||
var joinType = String.Format("{0}", join.Value.Metadata.JoinType.ToString()).PadRight(typeLen, ' ');
|
|
||||||
var joinCapabilities =
|
|
||||||
String.Format("{0}", join.Value.Metadata.JoinCapabilities.ToString()).PadRight(capabilitiesLen, ' ');
|
|
||||||
|
|
||||||
sb.AppendLine(String.Format("| {0} | {1} | {2} | {3} | {4} |",
|
|
||||||
joinNumber,
|
|
||||||
joinSpan,
|
|
||||||
joinDescription,
|
|
||||||
joinType,
|
|
||||||
joinCapabilities));
|
|
||||||
}
|
}
|
||||||
sb.AppendLine();
|
sb.AppendLine();
|
||||||
return sb;
|
return sb;
|
||||||
@@ -571,6 +536,64 @@ namespace PepperDash.Essentials.Core
|
|||||||
Metadata = metadata;
|
Metadata = metadata;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public string GetMarkdownFormattedData(string stringFormatter, int descriptionLen)
|
||||||
|
{
|
||||||
|
|
||||||
|
//Fixed Width Headers
|
||||||
|
var joinNumberLen = String.Format("Join Number").Length;
|
||||||
|
var joinSpanLen = String.Format("Join Span").Length;
|
||||||
|
var typeLen = String.Format("AnalogDigitalSerial").Length;
|
||||||
|
var capabilitiesLen = String.Format("ToFromFusion").Length;
|
||||||
|
|
||||||
|
//Track which one failed, if it did
|
||||||
|
const string placeholder = "unknown";
|
||||||
|
var dataArray = new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
{"joinNumber", placeholder.PadRight(joinNumberLen, ' ')},
|
||||||
|
{"joinSpan", placeholder.PadRight(joinSpanLen, ' ')},
|
||||||
|
{"description", placeholder.PadRight(descriptionLen, ' ')},
|
||||||
|
{"joinType", placeholder.PadRight(typeLen, ' ')},
|
||||||
|
{"capabilities", placeholder.PadRight(capabilitiesLen, ' ')}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
dataArray["joinNumber"] = String.Format("{0}", JoinNumber.ToString(CultureInfo.InvariantCulture).ReplaceIfNullOrEmpty(placeholder)).PadRight(joinNumberLen, ' ');
|
||||||
|
dataArray["joinSpan"] = String.Format("{0}", JoinSpan.ToString(CultureInfo.InvariantCulture).ReplaceIfNullOrEmpty(placeholder)).PadRight(joinSpanLen, ' ');
|
||||||
|
dataArray["description"] = String.Format("{0}", Metadata.Description.ReplaceIfNullOrEmpty(placeholder)).PadRight(descriptionLen, ' ');
|
||||||
|
dataArray["joinType"] = String.Format("{0}", Metadata.JoinType.ToString().ReplaceIfNullOrEmpty(placeholder)).PadRight(typeLen, ' ');
|
||||||
|
dataArray["capabilities"] = String.Format("{0}", Metadata.JoinCapabilities.ToString().ReplaceIfNullOrEmpty(placeholder)).PadRight(capabilitiesLen, ' ');
|
||||||
|
|
||||||
|
return String.Format(stringFormatter,
|
||||||
|
dataArray["joinNumber"],
|
||||||
|
dataArray["joinSpan"],
|
||||||
|
dataArray["description"],
|
||||||
|
dataArray["joinType"],
|
||||||
|
dataArray["capabilities"]);
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
//Don't Throw - we don't want to kill the system if this falls over - it's not mission critical. Print the error, use placeholder data
|
||||||
|
var errorKey = string.Empty;
|
||||||
|
foreach (var item in dataArray)
|
||||||
|
{
|
||||||
|
if (item.Value.TrimEnd() == placeholder) ;
|
||||||
|
errorKey = item.Key;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
Debug.Console(0, "Unable to decode join metadata {1}- {0}", e.Message, !String.IsNullOrEmpty(errorKey) ? (' ' + errorKey) : String.Empty);
|
||||||
|
return String.Format(stringFormatter,
|
||||||
|
dataArray["joinNumber"],
|
||||||
|
dataArray["joinSpan"],
|
||||||
|
dataArray["description"],
|
||||||
|
dataArray["joinType"],
|
||||||
|
dataArray["capabilities"]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sets the join offset value
|
/// Sets the join offset value
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user