Skip to content

Commit 4b558b0

Browse files
committed
Replace use of strings with ExchangeName, QueueName, ConsumerTag, and RoutingKey
1 parent 5453730 commit 4b558b0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+667
-523
lines changed

projects/RabbitMQ.Client/PublicAPI.Unshipped.txt

Lines changed: 63 additions & 45 deletions
Large diffs are not rendered by default.

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

Lines changed: 90 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@
3535

3636
namespace RabbitMQ.Client
3737
{
38-
public abstract class AmqpString
38+
// TODO lazy string value
39+
public abstract class AmqpString : IEquatable<AmqpString>
3940
{
4041
private readonly string _value;
4142
private readonly ReadOnlyMemory<byte> _stringBytes;
43+
private readonly int _byteCount;
4244

4345
protected AmqpString()
4446
{
@@ -77,6 +79,22 @@ public AmqpString(string value, ushort maxLen, Encoding encoding, string validat
7779

7880
_value = value;
7981
_stringBytes = new ReadOnlyMemory<byte>(encoding.GetBytes(value));
82+
_byteCount = _stringBytes.Length;
83+
}
84+
85+
public int ByteCount => _byteCount;
86+
87+
public bool IsEmpty
88+
{
89+
get
90+
{
91+
return _value == string.Empty;
92+
}
93+
}
94+
95+
public bool Contains(string value)
96+
{
97+
return _value.Contains(value);
8098
}
8199

82100
public override string ToString()
@@ -94,10 +112,66 @@ public static implicit operator ReadOnlyMemory<byte>(AmqpString amqpString)
94112
return amqpString._stringBytes;
95113
}
96114

115+
public static implicit operator ReadOnlySpan<byte>(AmqpString amqpString)
116+
{
117+
return amqpString._stringBytes.Span;
118+
}
119+
97120
private bool isAscii(string value)
98121
{
99122
return Encoding.UTF8.GetByteCount(value) == value.Length;
100123
}
124+
125+
public override bool Equals(object obj)
126+
{
127+
if (obj is null)
128+
{
129+
return false;
130+
}
131+
132+
if (Object.ReferenceEquals(this, obj))
133+
{
134+
return true;
135+
}
136+
137+
AmqpString amqpStringObj = obj as AmqpString;
138+
if (amqpStringObj is null)
139+
{
140+
return false;
141+
}
142+
143+
return Equals(amqpStringObj);
144+
}
145+
146+
public bool Equals(AmqpString other)
147+
{
148+
return _value == other._value;
149+
}
150+
151+
public override int GetHashCode()
152+
{
153+
return _value.GetHashCode();
154+
}
155+
156+
public static bool operator ==(AmqpString exchangeType1, AmqpString exchangeType2)
157+
{
158+
if (exchangeType1 is null || exchangeType2 is null)
159+
{
160+
return Object.Equals(exchangeType1, exchangeType2);
161+
}
162+
163+
return exchangeType1.Equals(exchangeType2);
164+
}
165+
166+
public static bool operator !=(AmqpString exchangeType1, AmqpString exchangeType2)
167+
{
168+
if (exchangeType1 is null || exchangeType2 is null)
169+
{
170+
return false == Object.Equals(exchangeType1, exchangeType2);
171+
}
172+
173+
return false == exchangeType1.Equals(exchangeType2);
174+
}
101175
}
102176

103177
/*
@@ -121,7 +195,8 @@ public ExchangeName(string exchangeName)
121195
{
122196
}
123197

124-
public static explicit operator ExchangeName(string value)
198+
// TODO explicit
199+
public static implicit operator ExchangeName(string value)
125200
{
126201
return new ExchangeName(value);
127202
}
@@ -143,15 +218,21 @@ private QueueName() : base()
143218
{
144219
}
145220

146-
public QueueName(string exchangeName)
147-
: base(exchangeName, 127, Encoding.ASCII, "^[a-zA-Z0-9-_.:]*$")
221+
public QueueName(string queueName)
222+
: base(queueName, 127, Encoding.ASCII, "^[a-zA-Z0-9-_.:]*$")
148223
{
149224
}
150225

151-
public static explicit operator QueueName(string value)
226+
// TODO explicit
227+
public static implicit operator QueueName(string value)
152228
{
153229
return new QueueName(value);
154230
}
231+
232+
public static explicit operator RoutingKey(QueueName value)
233+
{
234+
return new RoutingKey(value);
235+
}
155236
}
156237

157238
/*
@@ -174,7 +255,8 @@ public RoutingKey(string exchangeName)
174255
{
175256
}
176257

177-
public static explicit operator RoutingKey(string value)
258+
// TODO explicit
259+
public static implicit operator RoutingKey(string value)
178260
{
179261
return new RoutingKey(value);
180262
}
@@ -199,7 +281,8 @@ public ConsumerTag(string exchangeName)
199281
{
200282
}
201283

202-
public static explicit operator ConsumerTag(string value)
284+
// TODO explicit
285+
public static implicit operator ConsumerTag(string value)
203286
{
204287
return new ConsumerTag(value);
205288
}

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

Lines changed: 102 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,42 +29,86 @@
2929
// Copyright (c) 2007-2020 VMware, Inc. All rights reserved.
3030
//---------------------------------------------------------------------------
3131

32+
using System;
3233
using System.Collections.Generic;
34+
using System.Linq;
3335

3436
namespace RabbitMQ.Client
3537
{
3638
/// <summary>
37-
/// Convenience class providing compile-time names for standard exchange types.
39+
/// Convenience struct providing compile-time names for standard exchange types.
3840
/// </summary>
3941
/// <remarks>
4042
/// Use the static members of this class as values for the
4143
/// "exchangeType" arguments for IChannel methods such as
4244
/// ExchangeDeclare. The broker may be extended with additional
4345
/// exchange types that do not appear in this class.
4446
/// </remarks>
45-
public static class ExchangeType
47+
public class ExchangeType : IEquatable<ExchangeType>
4648
{
49+
private const string EmptyStr = "";
50+
private const string FanoutStr = "fanout";
51+
private const string DirectStr = "direct";
52+
private const string TopicStr = "topic";
53+
private const string HeadersStr = "headers";
54+
55+
private static readonly string[] s_all = { EmptyStr, FanoutStr, DirectStr, TopicStr, HeadersStr };
56+
57+
private readonly string _value;
58+
59+
internal static readonly ExchangeType s_empty = new ExchangeType(string.Empty);
60+
4761
/// <summary>
4862
/// Exchange type used for AMQP direct exchanges.
4963
/// </summary>
50-
public const string Direct = "direct";
64+
public static readonly ExchangeType Direct = new ExchangeType(DirectStr);
5165

5266
/// <summary>
5367
/// Exchange type used for AMQP fanout exchanges.
5468
/// </summary>
55-
public const string Fanout = "fanout";
69+
public static readonly ExchangeType Fanout = new ExchangeType(FanoutStr);
5670

5771
/// <summary>
5872
/// Exchange type used for AMQP headers exchanges.
5973
/// </summary>
60-
public const string Headers = "headers";
74+
public static readonly ExchangeType Headers = new ExchangeType(HeadersStr);
6175

6276
/// <summary>
6377
/// Exchange type used for AMQP topic exchanges.
6478
/// </summary>
65-
public const string Topic = "topic";
79+
public static readonly ExchangeType Topic = new ExchangeType(TopicStr);
80+
81+
internal ExchangeType(string value)
82+
{
83+
if (false == s_all.Contains(value))
84+
{
85+
throw new ArgumentOutOfRangeException(nameof(value));
86+
}
87+
_value = value;
88+
}
6689

67-
private static readonly string[] s_all = { Fanout, Direct, Topic, Headers };
90+
public override string ToString()
91+
{
92+
return _value;
93+
}
94+
95+
public static implicit operator ExchangeType(string value)
96+
{
97+
return new ExchangeType(value);
98+
}
99+
100+
public static implicit operator string(ExchangeType exchangeType)
101+
{
102+
return exchangeType._value;
103+
}
104+
105+
internal int ByteCount
106+
{
107+
get
108+
{
109+
return _value.Length;
110+
}
111+
}
68112

69113
/// <summary>
70114
/// Retrieve a collection containing all standard exchange types.
@@ -73,5 +117,56 @@ public static ICollection<string> All()
73117
{
74118
return s_all;
75119
}
120+
121+
public override bool Equals(object obj)
122+
{
123+
if (obj is null)
124+
{
125+
return false;
126+
}
127+
128+
if (Object.ReferenceEquals(this, obj))
129+
{
130+
return true;
131+
}
132+
133+
ExchangeType exchangeTypeObj = obj as ExchangeType;
134+
if (exchangeTypeObj is null)
135+
{
136+
return false;
137+
}
138+
139+
return Equals(exchangeTypeObj);
140+
}
141+
142+
public bool Equals(ExchangeType other)
143+
{
144+
return _value == other._value;
145+
}
146+
147+
public override int GetHashCode()
148+
{
149+
return _value.GetHashCode();
150+
}
151+
152+
public static bool operator ==(ExchangeType exchangeType1, ExchangeType exchangeType2)
153+
{
154+
if (exchangeType1 is null || exchangeType2 is null)
155+
{
156+
return Object.Equals(exchangeType1, exchangeType2);
157+
}
158+
159+
return exchangeType1.Equals(exchangeType2);
160+
}
161+
162+
public static bool operator !=(ExchangeType exchangeType1, ExchangeType exchangeType2)
163+
{
164+
if (exchangeType1 is null || exchangeType2 is null)
165+
{
166+
return false == Object.Equals(exchangeType1, exchangeType2);
167+
}
168+
169+
return false == exchangeType1.Equals(exchangeType2);
170+
}
76171
}
77172
}

0 commit comments

Comments
 (0)