This is an automated email from the ASF dual-hosted git repository.
blankensteiner pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar-dotpulsar.git
The following commit(s) were added to refs/heads/master by this push:
new 76b8c7b Implementing TryParse for MessageId
76b8c7b is described below
commit 76b8c7ba1d6fb6b20f45aa3f4a99c28e91c88e49
Author: Daniel Blankensteiner <[email protected]>
AuthorDate: Fri Aug 25 15:34:40 2023 +0200
Implementing TryParse for MessageId
---
src/DotPulsar/MessageId.cs | 74 ++++++++++++++++++++++++++++++++-
tests/DotPulsar.Tests/MessageIdTests.cs | 40 +++++++++++++++++-
2 files changed, 111 insertions(+), 3 deletions(-)
diff --git a/src/DotPulsar/MessageId.cs b/src/DotPulsar/MessageId.cs
index 55701a1..c236015 100644
--- a/src/DotPulsar/MessageId.cs
+++ b/src/DotPulsar/MessageId.cs
@@ -1,4 +1,4 @@
-/*
+/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@@ -40,7 +40,7 @@ public sealed class MessageId : IEquatable<MessageId>,
IComparable<MessageId>
public static MessageId Latest { get; }
/// <summary>
- /// Initializes a new instance using the specified ledgerId, entryId,
partition and batchIndex.
+ /// Initializes a new instance using the specified ledgerId, entryId,
partition, and batchIndex.
/// </summary>
public MessageId(ulong ledgerId, ulong entryId, int partition, int
batchIndex, string topic = "")
{
@@ -134,6 +134,76 @@ public sealed class MessageId : IEquatable<MessageId>,
IComparable<MessageId>
return $"{LedgerId}:{EntryId}:{Partition}:{BatchIndex}:{Topic}";
}
+ private static bool TryParse(ReadOnlySpan<char> s, out int result)
+ {
+#if NETSTANDARD2_0
+ return Int32.TryParse(s.ToString(), out result);
+#else
+ return Int32.TryParse(s, out result);
+#endif
+ }
+
+ private static bool TryParse(ReadOnlySpan<char> s, out ulong result)
+ {
+#if NETSTANDARD2_0
+ return UInt64.TryParse(s.ToString(), out result);
+#else
+ return UInt64.TryParse(s, out result);
+#endif
+ }
+
+ /// <summary>
+ /// Converts a string into a MessageId
+ /// </summary>
+ /// <returns>True is successfull and false if not</returns>
+ public static bool TryParse(string s, out MessageId result)
+ {
+ result = Earliest;
+ var input = s.AsMemory();
+ var startOfNextEntry = 0;
+ var index = 0;
+ var field = 0;
+ ulong ledgerId = 0;
+ ulong entryId = 0;
+ int partition = 0;
+ int batchIndex = 0;
+ var topic = string.Empty;
+
+ while (index <= s.Length)
+ {
+ if (index == s.Length || s[index] == ':')
+ {
+ var length = index - startOfNextEntry;
+ if (length == 0)
+ return false;
+
+ var value = input.Slice(startOfNextEntry, length);
+ if (field == 0 && !TryParse(value.Span, out ledgerId))
+ return false;
+ else if (field == 1 && !TryParse(value.Span, out entryId))
+ return false;
+ else if (field == 2 && !TryParse(value.Span, out partition))
+ return false;
+ else if (field == 3)
+ {
+ if (!TryParse(value.Span, out batchIndex))
+ return false;
+ if (index + 1 < s.Length)
+ topic = s.Substring(index + 1);
+ break;
+ }
+
+ startOfNextEntry = index + 1;
+ ++field;
+ }
+
+ ++index;
+ }
+
+ result = new MessageId(ledgerId, entryId, partition, batchIndex,
topic);
+ return true;
+ }
+
internal MessageIdData ToMessageIdData()
{
var messageIdData = new MessageIdData();
diff --git a/tests/DotPulsar.Tests/MessageIdTests.cs
b/tests/DotPulsar.Tests/MessageIdTests.cs
index 75396f6..491c2fc 100644
--- a/tests/DotPulsar.Tests/MessageIdTests.cs
+++ b/tests/DotPulsar.Tests/MessageIdTests.cs
@@ -1,4 +1,4 @@
-/*
+/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@@ -194,6 +194,44 @@ public class MessageIdTests
m1.Equals(m2).Should().BeFalse();
(m1 != m2).Should().BeTrue();
}
+
+ [Theory]
+ [InlineData("0.0.0.0")] // Wrong separator
+ [InlineData("A:0:0:0")] // Ledger id is not a number
+ [InlineData("0:A:0:0")] // Entry id is not a number
+ [InlineData("0:0:A:0")] // Partition id is not a number
+ [InlineData("0:0:0:A")] // Batch index is not a number
+ [InlineData(":::")] // Missing all
+ [InlineData(":0:0:0")] // Missing ledger id
+ [InlineData("0::0:0")] // Missing entry id
+ [InlineData("0:0::0")] // Missing partion
+ [InlineData("0:0:0:")] // Missing batch index
+ public void TryParse_GivenInvalidString_ShouldReturnFalse(string input)
+ {
+ // Arrange
+ var expected = MessageId.Earliest;
+
+ // Act
+ var result = MessageId.TryParse(input, out var actual);
+
+ // Assert
+ result.Should().BeFalse();
+ actual.Should().Be(expected);
+ }
+
+ [Fact]
+ public void TryParse_GivenValidString_ShouldReturnTrue()
+ {
+ // Arrange
+ var expected = new MessageId(1, 2, 3, 4, "topic");
+
+ // Act
+ var result = MessageId.TryParse(expected.ToString(), out var actual);
+
+ // Assert
+ result.Should().BeTrue();
+ actual.Should().Be(expected);
+ }
}
#nullable enable