Refactoring the RecordController

This commit is contained in:
lastvoidtemplar
2021-07-09 21:13:42 +03:00
parent c1501e00b7
commit 97692e08ae
6 changed files with 106 additions and 34 deletions

View File

@@ -27,7 +27,7 @@ namespace BoerseDataConvert
{ {
StringBuilder xmlRecord = new StringBuilder(); StringBuilder xmlRecord = new StringBuilder();
xmlRecord.Append($" <record id=\"{count}\">\n"); xmlRecord.Append($" <record id=\"{count}\">\n");
foreach (var tagValue in record.TagsValues) foreach (var tagValue in record)
{ {
try try
{ {
@@ -43,27 +43,16 @@ namespace BoerseDataConvert
count++; count++;
return xmlRecord.ToString(); return xmlRecord.ToString();
} }
private string CheckTagValue(string tag, string value) private string CheckTagValue(int tag, string value)
{ {
string[] tagLine;
try//Checks if the tag exists if(tagsTable.CheckInvalidTag(tag)) throw new ArgumentException($"WARN: Invalid tag \"{tag}\", {cur_fileName} line {count + 1}");
string tagname = tagsTable.GetTagName(tag);
if (value != "NULL" && tagsTable.HaveValueRanges(tag))//Checks if the tag have not a value ranges
{ {
tagLine = tagsTable.GetTagValue(tag); if (tagsTable.IsString(tag) && tagsTable.CheckStringLength(tag,value.Length))//Checks if value type is string
} {
catch (KeyNotFoundException) throw new ArgumentException($"WARN: Too long value \"{tag}\", \"{value}\", max allowed \"{tagsTable.GetTagName(tag)}\", {cur_fileName} line {count + 1}");
{
throw new ArgumentException($"WARN: Invalid tag \"{tag}\", {cur_fileName} line {count + 1}");
}
string tagname = tagLine[0];
if (value != "NULL" && tagLine.Length == 2)//Checks if the tag have not a value ranges
{
string[] valueType = tagLine[1].Split('-').ToArray();
if (valueType.Length == 2)//Checks if value type is string
{
if (value.Length > int.Parse(valueType[1]))//Checks if the length of the value is bigger than permitted
{
throw new ArgumentException($"WARN: Too long value \"{tag}\", \"{value}\", max allowed \"{valueType[1]}\", {cur_fileName} line {count + 1}");
}
} }
else//Checks if value type is decimal else//Checks if value type is decimal
{ {

View File

@@ -8,6 +8,6 @@ namespace BoerseDataConvert
{ {
public class Record public class Record
{ {
public List<KeyValuePair<string, string>>TagsValues; public List<KeyValuePair<int, string>>TagsValues;
} }
} }

View File

@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BoerseDataConvert
{
public class Tag
{
private string name;
private bool isString;
private int stringLength;
private bool haveValueRanges;
private string[] valueRanges;
public Tag(string[] input)
{
name = input[0];
string[] type = input[1].Split('-').ToArray();
if (type.Length == 2)
{
isString = true;
stringLength = int.Parse(type[1]);
}
else isString = false;
if (input.Length == 3)
{
valueRanges = input[2].Split('#').ToArray();
}
else haveValueRanges = false;
}
public string Name
{
get { return name; }
}
public bool IsString
{
get { return isString; }
}
public bool HaveValueRanges
{
get { return haveValueRanges; }
}
public int StringLength
{
get { return stringLength; }
}
public bool ValidValue(string value)
{
foreach (string validValue in valueRanges)
{
if (validValue == value)
{
return true;
}
}
return false;
}
}
}

View File

@@ -10,11 +10,11 @@ namespace BoerseDataConvert
public class TagsTable public class TagsTable
{ {
StreamReader reader ; StreamReader reader ;
Dictionary<string, string[]> table; Tag[] table;//name,isSring,stringLength,
public TagsTable(string tags) public TagsTable(string tags)
{ {
reader = new StreamReader(tags); reader = new StreamReader(tags);
table = new Dictionary<string, string[]>(); table = new Tag[int.Parse(reader.ReadLine())];
LoadInfo(); LoadInfo();
} }
private void LoadInfo() private void LoadInfo()
@@ -24,13 +24,37 @@ namespace BoerseDataConvert
while (!reader.EndOfStream) while (!reader.EndOfStream)
{ {
string[] line = reader.ReadLine().Split('|').ToArray(); string[] line = reader.ReadLine().Split('|').ToArray();
table.Add(line[0], line.Skip(1).ToArray()); table[int.Parse(line[0])]= new Tag(line);
} }
} }
} }
public string[] GetTagValue(string tag) public bool CheckInvalidTag(int tag)
{ {
return table[tag]; return table[tag]==null;
}
public string GetTagName(int tag)
{
return table[tag].Name;
}
public bool IsString(int tag)
{
return table[tag].IsString;
}
public bool HaveValueRanges(int tag)
{
return table[tag].HaveValueRanges;
}
public int GetMaxValueLengthToBig(int tag)
{
return table[tag].StringLength;
}
public bool CheckStringLength(int tag, int value)
{
return table[tag].StringLength<value;
}
public bool CheckValidValue(int tag, string value)
{
return table[tag].ValidValue(value);
} }
} }
} }

View File

@@ -35,16 +35,13 @@ namespace BoerseDataConvert
s = reader.ReadLine(); s = reader.ReadLine();
s = reader.ReadLine(); s = reader.ReadLine();
} }
string[] sr = s.Split("|").ToArray(); string[] sr = s.Split("|").ToArray();
List<KeyValuePair<string, string>>a = new List<KeyValuePair<string, string>>(); Record record = new Record();
foreach (var item in sr) foreach (var item in sr)
{ {
string[] d = item.Split('#').ToArray(); string[] d = item.Split('#',2).ToArray();
var pair = new KeyValuePair<string, string>(d[0], String.Join('#', d.Skip(1))); record.Add(int.Parse(d[0]), d[1]);
a.Add(pair); }
}
Record record = new Record();
record.TagsValues = a;
return record; return record;
} }
internal static void EndFile() internal static void EndFile()

View File

@@ -1,3 +1,4 @@
901
001|Product_Type|str-3|WAR#KO#EXO#AZE#AKA#BSK#IND 001|Product_Type|str-3|WAR#KO#EXO#AZE#AKA#BSK#IND
002|Type_Underlying_Instrument|str-3|BND#BSK#COM#CUR#FI#FON#FUT#IND#KOB#MUL#STO#DER#ETC#PRC 002|Type_Underlying_Instrument|str-3|BND#BSK#COM#CUR#FI#FON#FUT#IND#KOB#MUL#STO#DER#ETC#PRC
004|Option_Type|str-4|PUT#CALL#NULL 004|Option_Type|str-4|PUT#CALL#NULL