ProcessData over Tuple
This commit is contained in:
21
Adaptation/Shared/ProcessData.cs
Normal file
21
Adaptation/Shared/ProcessData.cs
Normal file
@ -0,0 +1,21 @@
|
||||
namespace Adaptation.Shared;
|
||||
|
||||
public class ProcessData
|
||||
{
|
||||
|
||||
protected readonly string _Logistics;
|
||||
protected readonly string[] _Columns;
|
||||
protected readonly string[] _BodyLines;
|
||||
|
||||
public string Logistics => _Logistics;
|
||||
public string[] Columns => _Columns;
|
||||
public string[] BodyLines => _BodyLines;
|
||||
|
||||
public ProcessData(string logistics, string[] columns, string[] bodyLines)
|
||||
{
|
||||
_Logistics = logistics;
|
||||
_Columns = columns;
|
||||
_BodyLines = bodyLines;
|
||||
}
|
||||
|
||||
}
|
@ -86,7 +86,7 @@ public class ProcessDataStandardFormat
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Tuple<string, string[], string[]> GetLogisticsColumnsAndBody(string reportFullPath, string[] lines = null)
|
||||
public static ProcessData GetProcessData(string reportFullPath, string[] lines = null)
|
||||
{
|
||||
string segment;
|
||||
List<string> body = new();
|
||||
@ -137,24 +137,23 @@ public class ProcessDataStandardFormat
|
||||
break;
|
||||
}
|
||||
}
|
||||
return new Tuple<string, string[], string[]>(logistics.ToString(), columns.ToArray(), body.ToArray());
|
||||
return new(logistics.ToString(), columns.ToArray(), body.ToArray());
|
||||
}
|
||||
|
||||
public static JsonElement[] GetArray(Tuple<string, string[], string[]> pdsf, bool lookForNumbers = false)
|
||||
public static JsonElement[] GetArray(ProcessData processData, bool lookForNumbers = false)
|
||||
{
|
||||
JsonElement[] results;
|
||||
string logistics = pdsf.Item1;
|
||||
string[] columns = pdsf.Item2;
|
||||
string[] bodyLines = pdsf.Item3;
|
||||
if (bodyLines.Length == 0 || !bodyLines[0].Contains('\t'))
|
||||
if (processData.BodyLines.Length == 0 || !processData.BodyLines[0].Contains('\t'))
|
||||
results = JsonSerializer.Deserialize<JsonElement[]>("[]");
|
||||
else
|
||||
{
|
||||
string value;
|
||||
string[] segments;
|
||||
List<string> lines = new();
|
||||
StringBuilder stringBuilder = new();
|
||||
foreach (string bodyLine in bodyLines)
|
||||
foreach (string bodyLine in processData.BodyLines)
|
||||
{
|
||||
_ = stringBuilder.Clear();
|
||||
_ = stringBuilder.Append('{');
|
||||
segments = bodyLine.Trim().Split('\t');
|
||||
if (!lookForNumbers)
|
||||
@ -162,7 +161,7 @@ public class ProcessDataStandardFormat
|
||||
for (int c = 1; c < segments.Length; c++)
|
||||
{
|
||||
value = segments[c].Replace("\"", "\\\"").Replace("\\", "\\\\");
|
||||
_ = stringBuilder.Append('"').Append(columns[c]).Append("\":\"").Append(value).Append("\",");
|
||||
_ = stringBuilder.Append('"').Append(processData.Columns[c]).Append("\":\"").Append(value).Append("\",");
|
||||
}
|
||||
}
|
||||
else
|
||||
@ -171,49 +170,48 @@ public class ProcessDataStandardFormat
|
||||
{
|
||||
value = segments[c].Replace("\"", "\\\"").Replace("\\", "\\\\");
|
||||
if (string.IsNullOrEmpty(value))
|
||||
_ = stringBuilder.Append('"').Append(columns[c]).Append("\":").Append(value).Append("null,");
|
||||
_ = stringBuilder.Append('"').Append(processData.Columns[c]).Append("\":").Append(value).Append("null,");
|
||||
else if (value.All(char.IsDigit))
|
||||
_ = stringBuilder.Append('"').Append(columns[c]).Append("\":").Append(value).Append(',');
|
||||
_ = stringBuilder.Append('"').Append(processData.Columns[c]).Append("\":").Append(value).Append(',');
|
||||
else
|
||||
_ = stringBuilder.Append('"').Append(columns[c]).Append("\":\"").Append(value).Append("\",");
|
||||
_ = stringBuilder.Append('"').Append(processData.Columns[c]).Append("\":\"").Append(value).Append("\",");
|
||||
}
|
||||
}
|
||||
_ = stringBuilder.Remove(stringBuilder.Length - 1, 1);
|
||||
_ = stringBuilder.AppendLine("},");
|
||||
_ = stringBuilder.AppendLine("}");
|
||||
lines.Add(stringBuilder.ToString());
|
||||
}
|
||||
_ = stringBuilder.Remove(stringBuilder.Length - 3, 3);
|
||||
results = JsonSerializer.Deserialize<JsonElement[]>(string.Concat("[", stringBuilder, "]"));
|
||||
string json = $"[{string.Join(",", lines)}]";
|
||||
results = JsonSerializer.Deserialize<JsonElement[]>(json);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
public static Dictionary<string, List<string>> GetDictionary(Tuple<string, string[], string[]> pdsf)
|
||||
public static Dictionary<string, List<string>> GetDictionary(ProcessData processData)
|
||||
{
|
||||
Dictionary<string, List<string>> results = new();
|
||||
string[] segments;
|
||||
string[] columns = pdsf.Item2;
|
||||
string[] bodyLines = pdsf.Item3;
|
||||
foreach (string column in columns)
|
||||
foreach (string column in processData.Columns)
|
||||
results.Add(column, new List<string>());
|
||||
foreach (string bodyLine in bodyLines)
|
||||
foreach (string bodyLine in processData.BodyLines)
|
||||
{
|
||||
segments = bodyLine.Split('\t');
|
||||
for (int c = 1; c < segments.Length; c++)
|
||||
{
|
||||
if (c >= columns.Length)
|
||||
if (c >= processData.Columns.Length)
|
||||
continue;
|
||||
results[columns[c]].Add(segments[c]);
|
||||
results[processData.Columns[c]].Add(segments[c]);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
public static Tuple<string, Dictionary<Test, Dictionary<string, List<string>>>> GetTestDictionary(Tuple<string, string[], string[]> pdsf)
|
||||
public static Tuple<string, Dictionary<Test, Dictionary<string, List<string>>>> GetTestDictionary(ProcessData processData)
|
||||
{
|
||||
Dictionary<Test, Dictionary<string, List<string>>> results = new();
|
||||
List<string> collection;
|
||||
string testColumn = nameof(Test);
|
||||
Dictionary<string, List<string>> keyValuePairs = GetDictionary(pdsf);
|
||||
Dictionary<string, List<string>> keyValuePairs = GetDictionary(processData);
|
||||
if (!keyValuePairs.TryGetValue(testColumn, out collection))
|
||||
throw new Exception();
|
||||
int min;
|
||||
@ -254,7 +252,7 @@ public class ProcessDataStandardFormat
|
||||
}
|
||||
}
|
||||
}
|
||||
return new Tuple<string, Dictionary<Test, Dictionary<string, List<string>>>>(pdsf.Item1, results);
|
||||
return new Tuple<string, Dictionary<Test, Dictionary<string, List<string>>>>(processData.Logistics, results);
|
||||
}
|
||||
|
||||
private static string GetString(SearchFor searchFor, bool addSpaces, char separator = ' ')
|
||||
|
Reference in New Issue
Block a user