Skip to content

Commit 9aeb757

Browse files
PublicationAddress.TryParse: return parsed value by reference
1 parent daaae07 commit 9aeb757

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

projects/RabbitMQ.Client/client/api/PublicationAddress.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,19 +116,23 @@ public static PublicationAddress Parse(string uriLikeString)
116116
return null;
117117
}
118118

119-
public static PublicationAddress TryParse(string uriLikeString)
119+
public static bool TryParse(string uriLikeString, out PublicationAddress result)
120120
{
121121
// Callers such as IBasicProperties.ReplyToAddress
122122
// expect null result for invalid input.
123123
// The regex.Match() throws on null arguments so we perform explicit check here
124124
if (uriLikeString == null)
125125
{
126-
return null;
126+
result = null;
127+
return false;
127128
} else {
128129
try {
129-
return Parse(uriLikeString);
130+
var res = Parse(uriLikeString);
131+
result = res;
132+
return true;
130133
} catch {
131-
return null;
134+
result = null;
135+
return false;
132136
}
133137
}
134138
}

projects/RabbitMQ.Client/client/impl/BasicProperties.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,11 @@ public bool Persistent
115115
/// </summary>
116116
public PublicationAddress ReplyToAddress
117117
{
118-
get { return PublicationAddress.TryParse(ReplyTo); }
118+
get {
119+
PublicationAddress result;
120+
PublicationAddress.TryParse(ReplyTo, out result);
121+
return result;
122+
}
119123
set { ReplyTo = value.ToString(); }
120124
}
121125

projects/Unit/TestBasicProperties.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,9 @@ public void TestProperties_ReplyTo(
135135

136136
// Assert
137137
bool isReplyToPresent = replyTo != null;
138-
string replyToAddress = PublicationAddress.TryParse(replyTo)?.ToString();
138+
PublicationAddress result;
139+
PublicationAddress.TryParse(replyTo, out result);
140+
string replyToAddress = result?.ToString();
139141
Assert.AreEqual(isReplyToPresent, subject.IsReplyToPresent());
140142

141143
var writer = new Impl.ContentHeaderPropertyWriter(new byte[1024]);

projects/Unit/TestPublicationAddress.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,15 @@ public void TestParseFailWithUnparseableInput()
7171
[Test]
7272
public void TestTryParseFail()
7373
{
74-
Assert.IsNull(PublicationAddress.TryParse(null));
75-
Assert.IsNull(PublicationAddress.TryParse("not a valid URI"));
76-
Assert.IsNull(PublicationAddress.TryParse("}}}}}}}}"));
74+
PublicationAddress result;
75+
PublicationAddress.TryParse(null, out result);
76+
Assert.IsNull(result);
77+
78+
PublicationAddress.TryParse("not a valid URI", out result);
79+
Assert.IsNull(result);
80+
81+
PublicationAddress.TryParse("}}}}}}}}", out result);
82+
Assert.IsNull(result);
7783
}
7884

7985
[Test]

0 commit comments

Comments
 (0)