Skip to content

Commit afa3c82

Browse files
authored
Merge pull request #101 from rabbitmq/lukebakken/bytecapacity-refactor
`ByteCapacity` refactor
2 parents 8202d00 + 48ea8ef commit afa3c82

File tree

9 files changed

+137
-44
lines changed

9 files changed

+137
-44
lines changed

.ci/windows/versions.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"erlang": "27.1.2",
3-
"rabbitmq": "4.0.4"
2+
"erlang": "27.2",
3+
"rabbitmq": "4.0.5"
44
}

.github/workflows/wf_build-and-test.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
path: |
2424
~/.nuget/packages
2525
~/AppData/Local/NuGet/v3-cache
26-
key: ${{ runner.os }}-v0-nuget-${{ hashFiles('**/*.csproj') }}
26+
key: ${{ runner.os }}-v0-nuget-${{ hashFiles('**/*.csproj','Directory.Packages.props') }}
2727
restore-keys: |
2828
${{ runner.os }}-v0-nuget-
2929
- name: Build (Debug)
@@ -55,6 +55,14 @@ jobs:
5555
runs-on: ubuntu-latest
5656
steps:
5757
- uses: actions/checkout@v4
58+
- uses: actions/cache@v4
59+
with:
60+
path: |
61+
~/.nuget/packages
62+
~/.local/share/NuGet/v3-cache
63+
key: ${{ runner.os }}-v0-nuget-${{ hashFiles('**/*.csproj','Directory.Packages.props') }}
64+
restore-keys: |
65+
${{ runner.os }}-v0-nuget-
5866
- name: Build (Debug)
5967
run: dotnet build ${{ github.workspace }}/Build.csproj
6068
- name: Verify

Directory.Build.props

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
<IncludeSymbols>true</IncludeSymbols>
1515
<IsPackable>false</IsPackable>
1616
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
17-
<NoWarn>$(NoWarn);CS1591</NoWarn>
1817
<PackageId>$(AssemblyName)</PackageId>
1918
<PackageProjectUrl>https://github.com/rabbitmq/rabbitmq-amqp-dotnet-client</PackageProjectUrl>
2019
<PackageReleaseNotes>https://github.com/rabbitmq/rabbitmq-amqp-dotnet-client/releases/latest</PackageReleaseNotes>

RabbitMQ.AMQP.Client/ByteCapacity.cs

Lines changed: 105 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,63 +7,114 @@
77

88
namespace RabbitMQ.AMQP.Client
99
{
10-
public partial class ByteCapacity : IEquatable<ByteCapacity>
10+
/// <summary>
11+
/// Class for specifying a binary size, with units
12+
/// </summary>
13+
public class ByteCapacity : IEquatable<ByteCapacity>
1114
{
15+
private const int KilobytesMultiplier = 1000;
16+
private const int MegabytesMultiplier = 1000 * 1000;
17+
private const int GigabytesMultiplier = 1000 * 1000 * 1000;
18+
private const long TerabytesMultiplier = 1000L * 1000L * 1000L * 1000L;
19+
20+
private static readonly Regex s_sizeRegex = new(@"^(\d+)([kKmMgGtTpP]?[bB]?)$", RegexOptions.Compiled);
21+
1222
private readonly long _bytes;
13-
private string _input;
1423

15-
private ByteCapacity(long bytes, string input)
24+
/// <summary>
25+
///
26+
/// </summary>
27+
/// <param name="bytes"></param>
28+
public ByteCapacity(long bytes)
1629
{
17-
_input = input;
1830
_bytes = bytes;
1931
}
2032

21-
private ByteCapacity(long bytes) : this(bytes, bytes.ToString())
22-
{
23-
}
24-
25-
private const int KilobytesMultiplier = 1000;
26-
private const int MegabytesMultiplier = 1000 * 1000;
27-
private const int GigabytesMultiplier = 1000 * 1000 * 1000;
28-
private const long TerabytesMultiplier = 1000L * 1000L * 1000L * 1000L;
29-
33+
/// <summary>
34+
/// Specify an amount in bytes
35+
/// </summary>
36+
/// <param name="bytes">The size, in bytes</param>
37+
/// <returns><see cref="ByteCapacity"/></returns>
3038
public static ByteCapacity B(long bytes)
3139
{
3240
return new ByteCapacity(bytes);
3341
}
3442

35-
public static ByteCapacity Kb(long megabytes)
43+
/// <summary>
44+
/// Specify an amount, in kilobytes
45+
/// </summary>
46+
/// <param name="kilobytes">The size, in kilobytes</param>
47+
/// <returns><see cref="ByteCapacity"/></returns>
48+
public static ByteCapacity Kb(long kilobytes)
3649
{
37-
return new ByteCapacity(megabytes * KilobytesMultiplier);
50+
return new ByteCapacity(kilobytes * KilobytesMultiplier);
3851
}
3952

53+
/// <summary>
54+
/// Specify an amount, in megabytes
55+
/// </summary>
56+
/// <param name="megabytes">The size, in megabytes</param>
57+
/// <returns><see cref="ByteCapacity"/></returns>
4058
public static ByteCapacity Mb(long megabytes)
4159
{
4260
return new ByteCapacity(megabytes * MegabytesMultiplier);
4361
}
4462

63+
/// <summary>
64+
/// Specify an amount, in gigabytes
65+
/// </summary>
66+
/// <param name="gigabytes">The size, in gigabytes</param>
67+
/// <returns><see cref="ByteCapacity"/></returns>
4568
public static ByteCapacity Gb(long gigabytes)
4669
{
4770
return new ByteCapacity(gigabytes * GigabytesMultiplier);
4871
}
4972

73+
/// <summary>
74+
/// Specify an amount, in terabytes
75+
/// </summary>
76+
/// <param name="terabytes">The size, in terabytes</param>
77+
/// <returns><see cref="ByteCapacity"/></returns>
5078
public static ByteCapacity Tb(long terabytes)
5179
{
5280
return new ByteCapacity(terabytes * TerabytesMultiplier);
5381
}
5482

55-
private static readonly Regex s_sizeRegex = new Regex(@"^(\d+)([kKmMgGtTpP]?[bB]?)$", RegexOptions.Compiled);
83+
/// <summary>
84+
/// Explicitly convert a string into a <see cref="ByteCapacity"/>
85+
/// </summary>
86+
/// <param name="value">The value, as string</param>
87+
/// <returns><see cref="ByteCapacity"/></returns>
88+
public static explicit operator ByteCapacity(string value)
89+
{
90+
return Parse(value);
91+
}
92+
93+
/// <summary>
94+
/// Cast a <see cref="ByteCapacity"/> into a <see cref="long"/>
95+
/// </summary>
96+
/// <param name="value">The value</param>
97+
/// <returns>The total number of bytes.</returns>
98+
public static implicit operator long(ByteCapacity value)
99+
{
100+
return value._bytes;
101+
}
56102

57-
public static ByteCapacity From(string value)
103+
/// <summary>
104+
/// Parse a string into a <see cref="ByteCapacity"/>
105+
/// </summary>
106+
/// <param name="value">The value, as string</param>
107+
/// <returns><see cref="ByteCapacity"/></returns>
108+
public static ByteCapacity Parse(string value)
58109
{
59110
Match match = s_sizeRegex.Match(value);
60111
if (!match.Success)
61112
{
62113
throw new ArgumentException("Invalid capacity size format.", nameof(value));
63114
}
64115

65-
var size = long.Parse(match.Groups[1].Value);
66-
var unit = match.Groups[2].Value.ToLower();
116+
long size = long.Parse(match.Groups[1].Value);
117+
string unit = match.Groups[2].Value.ToLowerInvariant();
67118

68119
return unit switch
69120
{
@@ -75,24 +126,53 @@ public static ByteCapacity From(string value)
75126
};
76127
}
77128

78-
public long ToBytes()
129+
/// <summary>
130+
/// Returns a value indicating whether this instance is equal to a specified <see cref="ByteCapacity"/> value.
131+
/// </summary>
132+
/// <param name="obj">An object to compare with this instance.</param>
133+
/// <returns>true if obj is an instance of <see cref="ByteCapacity"/>and equals the value of this instance; otherwise, false.</returns>
134+
public override bool Equals(object? obj)
79135
{
80-
return _bytes;
136+
if (obj is null)
137+
{
138+
return false;
139+
}
140+
141+
if (ReferenceEquals(this, obj))
142+
{
143+
return true;
144+
}
145+
146+
return Equals(obj as ByteCapacity);
81147
}
82148

83-
public bool Equals(ByteCapacity? other)
149+
/// <summary>
150+
/// Returns a value indicating whether this instance is equal to a specified <see cref="ByteCapacity"/> value.
151+
/// </summary>
152+
/// <param name="obj">An object to compare with this instance.</param>
153+
/// <returns>true if obj has the same value as this instance; otherwise, false.</returns>
154+
public bool Equals(ByteCapacity? obj)
84155
{
85-
if (ReferenceEquals(null, other))
156+
if (obj is null)
86157
{
87158
return false;
88159
}
89160

90-
if (ReferenceEquals(this, other))
161+
if (ReferenceEquals(this, obj))
91162
{
92163
return true;
93164
}
94165

95-
return _bytes == other._bytes;
166+
return _bytes == obj._bytes;
167+
}
168+
169+
/// <summary>
170+
/// Returns the hash code for this instance.
171+
/// </summary>
172+
/// <returns>A 32-bit signed integer hash code.</returns>
173+
public override int GetHashCode()
174+
{
175+
return _bytes.GetHashCode();
96176
}
97177
}
98178
}

RabbitMQ.AMQP.Client/Impl/AmqpQueueSpecification.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,8 @@ public IQueueSpecification OverflowStrategy(OverFlowStrategy overflow)
226226

227227
public IQueueSpecification MaxLengthBytes(ByteCapacity maxLengthBytes)
228228
{
229-
Utils.ValidatePositive("Max length", maxLengthBytes.ToBytes());
230-
_queueArguments["x-max-length-bytes"] = maxLengthBytes.ToBytes();
229+
Utils.ValidatePositive("Max length", maxLengthBytes);
230+
_queueArguments["x-max-length-bytes"] = (long)maxLengthBytes;
231231
return this;
232232
}
233233

@@ -385,8 +385,8 @@ public IStreamSpecification MaxAge(TimeSpan maxAge)
385385

386386
public IStreamSpecification MaxSegmentSizeBytes(ByteCapacity maxSegmentSize)
387387
{
388-
Utils.ValidatePositive("x-stream-max-segment-size-bytes", maxSegmentSize.ToBytes());
389-
_parent._queueArguments["x-stream-max-segment-size-bytes"] = maxSegmentSize.ToBytes();
388+
Utils.ValidatePositive("x-stream-max-segment-size-bytes", maxSegmentSize);
389+
_parent._queueArguments["x-stream-max-segment-size-bytes"] = (long)maxSegmentSize;
390390
return this;
391391
}
392392

RabbitMQ.AMQP.Client/PublicAPI.Unshipped.txt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const RabbitMQ.AMQP.Client.Consts.Queues = "queues" -> string!
1111
const RabbitMQ.AMQP.Client.MetricsReporter.MeterName = "RabbitMQ.Amqp" -> string!
1212
const RabbitMQ.AMQP.Client.MetricsReporter.MetricPrefix = "rabbitmq.amqp" -> string!
1313
override RabbitMQ.AMQP.Client.BackOffDelayPolicy.ToString() -> string!
14+
override RabbitMQ.AMQP.Client.ByteCapacity.Equals(object? obj) -> bool
15+
override RabbitMQ.AMQP.Client.ByteCapacity.GetHashCode() -> int
1416
override RabbitMQ.AMQP.Client.ClusterConnectionSettings.Equals(object? obj) -> bool
1517
override RabbitMQ.AMQP.Client.ClusterConnectionSettings.GetHashCode() -> int
1618
override RabbitMQ.AMQP.Client.ConnectionSettings.Equals(object? obj) -> bool
@@ -48,8 +50,8 @@ RabbitMQ.AMQP.Client.BackOffDelayPolicy.Reset() -> void
4850
RabbitMQ.AMQP.Client.BadRequestException
4951
RabbitMQ.AMQP.Client.BadRequestException.BadRequestException(string! message) -> void
5052
RabbitMQ.AMQP.Client.ByteCapacity
51-
RabbitMQ.AMQP.Client.ByteCapacity.Equals(RabbitMQ.AMQP.Client.ByteCapacity? other) -> bool
52-
RabbitMQ.AMQP.Client.ByteCapacity.ToBytes() -> long
53+
RabbitMQ.AMQP.Client.ByteCapacity.ByteCapacity(long bytes) -> void
54+
RabbitMQ.AMQP.Client.ByteCapacity.Equals(RabbitMQ.AMQP.Client.ByteCapacity? obj) -> bool
5355
RabbitMQ.AMQP.Client.ClassicQueueMode
5456
RabbitMQ.AMQP.Client.ClassicQueueMode.Default = 0 -> RabbitMQ.AMQP.Client.ClassicQueueMode
5557
RabbitMQ.AMQP.Client.ClassicQueueMode.Lazy = 1 -> RabbitMQ.AMQP.Client.ClassicQueueMode
@@ -762,10 +764,12 @@ RabbitMQ.AMQP.Client.TlsSettings.RemoteCertificateValidationCallback.set -> void
762764
RabbitMQ.AMQP.Client.TlsSettings.TlsSettings() -> void
763765
RabbitMQ.AMQP.Client.TlsSettings.TlsSettings(System.Security.Authentication.SslProtocols protocols) -> void
764766
static RabbitMQ.AMQP.Client.ByteCapacity.B(long bytes) -> RabbitMQ.AMQP.Client.ByteCapacity!
765-
static RabbitMQ.AMQP.Client.ByteCapacity.From(string! value) -> RabbitMQ.AMQP.Client.ByteCapacity!
767+
static RabbitMQ.AMQP.Client.ByteCapacity.explicit operator RabbitMQ.AMQP.Client.ByteCapacity!(string! value) -> RabbitMQ.AMQP.Client.ByteCapacity!
766768
static RabbitMQ.AMQP.Client.ByteCapacity.Gb(long gigabytes) -> RabbitMQ.AMQP.Client.ByteCapacity!
767-
static RabbitMQ.AMQP.Client.ByteCapacity.Kb(long megabytes) -> RabbitMQ.AMQP.Client.ByteCapacity!
769+
static RabbitMQ.AMQP.Client.ByteCapacity.implicit operator long(RabbitMQ.AMQP.Client.ByteCapacity! value) -> long
770+
static RabbitMQ.AMQP.Client.ByteCapacity.Kb(long kilobytes) -> RabbitMQ.AMQP.Client.ByteCapacity!
768771
static RabbitMQ.AMQP.Client.ByteCapacity.Mb(long megabytes) -> RabbitMQ.AMQP.Client.ByteCapacity!
772+
static RabbitMQ.AMQP.Client.ByteCapacity.Parse(string! value) -> RabbitMQ.AMQP.Client.ByteCapacity!
769773
static RabbitMQ.AMQP.Client.ByteCapacity.Tb(long terabytes) -> RabbitMQ.AMQP.Client.ByteCapacity!
770774
static RabbitMQ.AMQP.Client.ConnectionSettings.ProcessUriSegmentsForVirtualHost(System.Uri! uri) -> string!
771775
static RabbitMQ.AMQP.Client.ConnectionSettings.ProcessUserInfo(System.Uri! uri) -> (string? user, string? password)

RabbitMQ.AMQP.Client/RabbitMQ.AMQP.Client.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
-->
2121
<LangVersion>9.0</LangVersion>
2222
<Nullable>enable</Nullable>
23+
<NoWarn>$(NoWarn);CS1591</NoWarn>
2324
</PropertyGroup>
2425

2526
<ItemGroup>

Tests/ByteCapacityTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class ByteCapacityTests
2323
[InlineData("23TB", 23 * 1000L * 1000L * 1000L * 1000L)]
2424
public void FromShouldReturnTheCorrectBytesValues(string input, long expectedBytes)
2525
{
26-
Assert.Equal(expectedBytes, ByteCapacity.From(input).ToBytes());
26+
Assert.Equal(expectedBytes, (ByteCapacity)input);
2727
}
2828

2929
[Theory]
@@ -38,17 +38,17 @@ public void FromShouldReturnTheCorrectBytesValues(string input, long expectedByt
3838

3939
public void FromShouldThrowExceptionWhenInvalidInput(string input)
4040
{
41-
Assert.Throws<ArgumentException>(() => ByteCapacity.From(input));
41+
Assert.Throws<ArgumentException>(() => (ByteCapacity)input);
4242
}
4343

4444
[Fact]
4545
public void ByteCapacityShouldReturnTheValidBytes()
4646
{
47-
Assert.Equal(99, ByteCapacity.B(99).ToBytes());
48-
Assert.Equal(76000, ByteCapacity.Kb(76).ToBytes());
49-
Assert.Equal(789 * 1000 * 1000, ByteCapacity.Mb(789).ToBytes());
50-
Assert.Equal(134 * 1000L * 1000L * 1000L, ByteCapacity.Gb(134).ToBytes());
51-
Assert.Equal(12 * 1000L * 1000L * 1000L * 1000L, ByteCapacity.Tb(12).ToBytes());
47+
Assert.Equal(99, (long)ByteCapacity.B(99));
48+
Assert.Equal(76000, (long)ByteCapacity.Kb(76));
49+
Assert.Equal(789 * 1000 * 1000, (long)ByteCapacity.Mb(789));
50+
Assert.Equal(134 * 1000L * 1000L * 1000L, (long)ByteCapacity.Gb(134));
51+
Assert.Equal(12 * 1000L * 1000L * 1000L * 1000L, (long)ByteCapacity.Tb(12));
5252
}
5353

5454
[Theory]

Tests/Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<IsTestProject>true</IsTestProject>
2222
<Nullable>enable</Nullable>
2323
<SignAssembly>True</SignAssembly>
24+
<NoWarn>$(NoWarn);CS1591</NoWarn>
2425
</PropertyGroup>
2526

2627
<ItemGroup>

0 commit comments

Comments
 (0)