-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathX9FieldDataType.cs
202 lines (194 loc) · 6.7 KB
/
X9FieldDataType.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using System;
using System.Linq;
using System.Runtime.Serialization;
namespace CompAnalytics.X9
{
[DataContract]
[Serializable]
public class X9FieldDataType
{
[DataMember]
public string Name { get; set; }
[DataMember]
public string SpecTypeName { get; set; }
/// <summary>
/// We can safely represent data stored in this type using this CLR type, but it may not be
/// the type we'd like to use for reading and writing the value
/// </summary>
public Type NominalClrType { get; set; }
[DataMember]
public PaddingType PaddingType { get; set; } = PaddingType.None;
internal object DeserializeToNominalType(string val)
{
if (!string.IsNullOrWhiteSpace(val))
{
string unpadded = this.UnpadValue(val);
if (!string.IsNullOrWhiteSpace(unpadded))
{
if (this.NominalClrType == typeof(long?))
{
return long.Parse(unpadded);
}
else
{
return Convert.ChangeType(unpadded, this.NominalClrType);
}
}
else
{
return null;
}
}
else
{
return null;
}
}
internal string SerializeFromNominalType(object val, X9FieldType fieldType)
{
int length = fieldType.Length.Value;
string unpadded = Convert.ToString(val) ?? "";
if (unpadded.Length > length)
{
throw new ArgumentException($"Cannot set field {fieldType.Name} value to '{unpadded}'. The length of the data " +
$"({unpadded.Length}) exceeds the available length for this field ({length}).");
}
return string.IsNullOrWhiteSpace(unpadded)
? new string(' ', length) // if no value, we fill with spaces instead of the field type's pad character
: this.PadValue(unpadded, length);
}
internal string UnpadValue(string serialized)
{
string result;
switch (this.PaddingType)
{
case PaddingType.BlankLeft:
case PaddingType.BlankRight:
result = serialized.Trim();
break;
case PaddingType.ZeroLeft:
result = long.Parse(serialized).ToString();
break;
default:
result = serialized;
break;
}
return result;
}
internal string PadValue(string deserialized, int length)
{
// TODO: Throw if string is longer than desired
string result;
switch (this.PaddingType)
{
case PaddingType.BlankLeft:
result = deserialized.PadLeft(length);
break;
case PaddingType.BlankRight:
result = deserialized.PadRight(length);
break;
case PaddingType.ZeroLeft:
result = deserialized.PadLeft(length, '0');
break;
default:
result = deserialized;
break;
}
return result;
}
}
public static class X9FieldDataTypes
{
public static readonly X9FieldDataType Alphabetic = new X9FieldDataType
{
Name = "Alphabetic",
SpecTypeName = "A",
NominalClrType = typeof(string),
PaddingType = PaddingType.BlankRight
};
public static readonly X9FieldDataType Numeric = new X9FieldDataType
{
Name = "Numeric",
SpecTypeName = "N",
NominalClrType = typeof(long?),
PaddingType = PaddingType.ZeroLeft
};
public static readonly X9FieldDataType Blank = new X9FieldDataType
{
Name = "Blank",
SpecTypeName = "B",
NominalClrType = typeof(string),
PaddingType = PaddingType.BlankRight
};
public static readonly X9FieldDataType SpecialCharacters = new X9FieldDataType
{
Name = "SpecialCharacters",
SpecTypeName = "S",
NominalClrType = typeof(string),
PaddingType = PaddingType.BlankRight
};
public static readonly X9FieldDataType Alphameric = new X9FieldDataType
{
Name = "Alphameric",
SpecTypeName = "AN",
NominalClrType = typeof(string),
PaddingType = PaddingType.BlankRight
};
public static readonly X9FieldDataType AlphamericSpecial = new X9FieldDataType
{
Name = "AlphamericSpecial",
SpecTypeName = "ANS",
NominalClrType = typeof(string),
PaddingType = PaddingType.BlankRight
};
public static readonly X9FieldDataType NumericBlank = new X9FieldDataType
{
Name = "NumericBlank",
SpecTypeName = "NB",
NominalClrType = typeof(long?),
PaddingType = PaddingType.BlankRight
};
public static readonly X9FieldDataType NumericSpecial = new X9FieldDataType
{
Name = "NumericSpecial",
SpecTypeName = "NS",
NominalClrType = typeof(string),
PaddingType = PaddingType.BlankRight
};
public static readonly X9FieldDataType NumericBlankSpecialMicr = new X9FieldDataType
{
Name = "NumericBlankSpecialMicr",
SpecTypeName = "NBSM",
NominalClrType = typeof(string),
PaddingType = PaddingType.BlankLeft
};
public static readonly X9FieldDataType NumericBlankSpecialMicrOnUs = new X9FieldDataType
{
Name = "NumericBlankSpecialMicrOnUs",
SpecTypeName = "NB",
NominalClrType = typeof(string),
PaddingType = PaddingType.BlankLeft
};
public static readonly X9FieldDataType Binary = new X9FieldDataType
{
Name = "Binary",
SpecTypeName = "Binary"
};
}
public enum PaddingType
{
None,
/// <summary>
/// Left-justify, pad with spaces on the right
/// </summary>
BlankRight,
/// <summary>
/// Right-justify, pad with spaces on the left
/// </summary>
BlankLeft,
/// <summary>
/// Right-justify, pad with zeroes on the right
/// </summary>
ZeroLeft
}
}