Skip to content

Commit 4c2b601

Browse files
committed
DataType. ToSerializableString uses aliases.
1 parent 6ffc34f commit 4c2b601

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

Messages/DataType.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,12 +380,27 @@ public override string ToString()
380380
/// </summary>
381381
public static ISet<DataType> CandleSources { get; } = new HashSet<DataType>([Ticks, Level1, MarketDepth, OrderLog]);
382382

383+
private static readonly PairSet<string, DataType> _aliasToType = new(StringComparer.InvariantCultureIgnoreCase)
384+
{
385+
{ "level1", Level1 },
386+
{ "ticks", Ticks },
387+
{ "marketdepth", MarketDepth },
388+
{ "orderlog", OrderLog },
389+
{ "transactions", Transactions },
390+
{ "news", News },
391+
{ "securities", Securities },
392+
{ "positions", PositionChanges },
393+
};
394+
383395
/// <summary>
384396
/// Serialize <see cref="DataType"/> to <see cref="string"/>.
385397
/// </summary>
386398
/// <returns>The string representation of <see cref="DataType"/>.</returns>
387399
public string ToSerializableString()
388400
{
401+
if (_aliasToType.TryGetKey(this, out var alias))
402+
return alias;
403+
389404
var type = MessageType;
390405
var arg = Arg;
391406

@@ -402,6 +417,9 @@ public static DataType FromSerializableString(string value)
402417
if (value.IsEmpty())
403418
return null;
404419

420+
if (_aliasToType.TryGetValue(value, out var aliasDt))
421+
return aliasDt;
422+
405423
var parts = value.SplitByColon(false);
406424

407425
var msgType = parts[0].To<Type>();

Tests/DataTypeTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,26 @@ public void Name_Does_Not_Affect_Equality_Or_Hash()
168168
dt2.AreEqual(dt1);
169169
dt2.GetHashCode().AreEqual(dt1.GetHashCode());
170170
}
171+
172+
[TestMethod]
173+
public void SerializableString_Aliases()
174+
{
175+
static void TestAlias(DataType dt, string expected)
176+
{
177+
var s = dt.ToSerializableString();
178+
s.AssertEqual(expected);
179+
var back = DataType.FromSerializableString(s);
180+
back.AreEqual(dt);
181+
182+
// also check case-insensitive
183+
var upper = expected.ToUpperInvariant();
184+
var back2 = DataType.FromSerializableString(upper);
185+
back2.AreEqual(dt);
186+
}
187+
188+
TestAlias(DataType.Level1, "level1");
189+
TestAlias(DataType.Ticks, "ticks");
190+
TestAlias(DataType.MarketDepth, "marketdepth");
191+
TestAlias(DataType.OrderLog, "orderlog");
192+
}
171193
}

0 commit comments

Comments
 (0)