Skip to content

Commit 99ea72e

Browse files
Jalal Amini Robatilukebakken
Jalal Amini Robati
authored andcommitted
add DeliveryModes convenience class
change NonPersistent const name in DeliveryModes to Transient Update DeliveryMode to be an enum
1 parent 6bef5ce commit 99ea72e

File tree

8 files changed

+83
-22
lines changed

8 files changed

+83
-22
lines changed

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public struct BasicProperties : IBasicProperties, IAmqpHeader
4545
public string? ContentType { get; set; }
4646
public string? ContentEncoding { get; set; }
4747
public IDictionary<string, object?>? Headers { get; set; }
48-
public byte DeliveryMode { get; set; }
48+
public DeliveryModes DeliveryMode { get; set; }
4949
public byte Priority { get; set; }
5050
public string? CorrelationId { get; set; }
5151
public string? ReplyTo { get; set; }
@@ -59,8 +59,15 @@ public struct BasicProperties : IBasicProperties, IAmqpHeader
5959

6060
public bool Persistent
6161
{
62-
readonly get { return DeliveryMode == 2; }
63-
set { DeliveryMode = value ? (byte)2 : (byte)1; }
62+
get
63+
{
64+
return DeliveryMode == DeliveryModes.Persistent;
65+
}
66+
67+
set
68+
{
69+
DeliveryMode = value ? DeliveryModes.Persistent : DeliveryModes.Transient;
70+
}
6471
}
6572

6673
public PublicationAddress? ReplyToAddress
@@ -171,7 +178,7 @@ readonly int IAmqpWriteable.WriteTo(Span<byte> span)
171178
if (IsDeliveryModePresent())
172179
{
173180
bitValue.SetBit(DeliveryModeBit);
174-
span.GetOffset(offset++) = DeliveryMode;
181+
span.GetOffset(offset++) = (byte)DeliveryMode;
175182
}
176183

177184
if (IsPriorityPresent())
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// This source code is dual-licensed under the Apache License, version
2+
// 2.0, and the Mozilla Public License, version 2.0.
3+
//
4+
// The APL v2.0:
5+
//
6+
//---------------------------------------------------------------------------
7+
// Copyright (c) 2007-2020 VMware, Inc.
8+
//
9+
// Licensed under the Apache License, Version 2.0 (the "License");
10+
// you may not use this file except in compliance with the License.
11+
// You may obtain a copy of the License at
12+
//
13+
// https://www.apache.org/licenses/LICENSE-2.0
14+
//
15+
// Unless required by applicable law or agreed to in writing, software
16+
// distributed under the License is distributed on an "AS IS" BASIS,
17+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
// See the License for the specific language governing permissions and
19+
// limitations under the License.
20+
//---------------------------------------------------------------------------
21+
//
22+
// The MPL v2.0:
23+
//
24+
//---------------------------------------------------------------------------
25+
// This Source Code Form is subject to the terms of the Mozilla Public
26+
// License, v. 2.0. If a copy of the MPL was not distributed with this
27+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
28+
//
29+
// Copyright (c) 2007-2020 VMware, Inc. All rights reserved.
30+
//---------------------------------------------------------------------------
31+
32+
namespace RabbitMQ.Client
33+
{
34+
/// <summary>
35+
/// Convenience enum providing compile-time names for persistent modes.
36+
/// </summary>
37+
public enum DeliveryModes : byte
38+
{
39+
/// <summary>
40+
/// Value for transient delivery mode (not durable).
41+
/// </summary>
42+
Transient = 1,
43+
44+
/// <summary>
45+
/// Value for persistent delivery mode (durable).
46+
/// </summary>
47+
Persistent = 2
48+
}
49+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public interface IReadOnlyBasicProperties
6969
/// <summary>
7070
/// Non-persistent (1) or persistent (2).
7171
/// </summary>
72-
byte DeliveryMode { get; }
72+
DeliveryModes DeliveryMode { get; }
7373

7474
/// <summary>
7575
/// Message expiration specification.
@@ -236,7 +236,7 @@ public interface IBasicProperties : IReadOnlyBasicProperties
236236
/// <summary>
237237
/// Non-persistent (1) or persistent (2).
238238
/// </summary>
239-
new byte DeliveryMode { get; set; }
239+
new DeliveryModes DeliveryMode { get; set; }
240240

241241
/// <summary>
242242
/// Message expiration specification.

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ namespace RabbitMQ.Client
4444
private readonly string? _contentType;
4545
private readonly string? _contentEncoding;
4646
private readonly IDictionary<string, object?>? _headers;
47-
private readonly byte _deliveryMode;
47+
private readonly DeliveryModes _deliveryMode;
4848
private readonly byte _priority;
4949
private readonly string? _correlationId;
5050
private readonly string? _replyTo;
@@ -59,7 +59,7 @@ namespace RabbitMQ.Client
5959
public string? ContentType => _contentType;
6060
public string? ContentEncoding => _contentEncoding;
6161
public IDictionary<string, object?>? Headers => _headers;
62-
public byte DeliveryMode => _deliveryMode;
62+
public DeliveryModes DeliveryMode => _deliveryMode;
6363
public byte Priority => _priority;
6464
public string? CorrelationId => _correlationId;
6565
public string? ReplyTo => _replyTo;
@@ -71,7 +71,7 @@ namespace RabbitMQ.Client
7171
public string? AppId => _appId;
7272
public string? ClusterId => _clusterId;
7373

74-
public bool Persistent => DeliveryMode == 2;
74+
public bool Persistent => DeliveryMode == DeliveryModes.Persistent;
7575

7676
public PublicationAddress? ReplyToAddress
7777
{
@@ -90,7 +90,7 @@ public ReadOnlyBasicProperties(ReadOnlySpan<byte> span)
9090
if (bits.IsBitSet(BasicProperties.ContentTypeBit)) { offset += WireFormatting.ReadShortstr(span.Slice(offset), out _contentType); }
9191
if (bits.IsBitSet(BasicProperties.ContentEncodingBit)) { offset += WireFormatting.ReadShortstr(span.Slice(offset), out _contentEncoding); }
9292
if (bits.IsBitSet(BasicProperties.HeaderBit)) { offset += WireFormatting.ReadDictionary(span.Slice(offset), out var tmpDirectory); _headers = tmpDirectory; }
93-
if (bits.IsBitSet(BasicProperties.DeliveryModeBit)) { _deliveryMode = span[offset++]; }
93+
if (bits.IsBitSet(BasicProperties.DeliveryModeBit)) { _deliveryMode = (DeliveryModes)span[offset++]; }
9494
if (bits.IsBitSet(BasicProperties.PriorityBit)) { _priority = span[offset++]; }
9595
if (bits.IsBitSet(BasicProperties.CorrelationIdBit)) { offset += WireFormatting.ReadShortstr(span.Slice(offset), out _correlationId); }
9696
if (bits.IsBitSet(BasicProperties.ReplyToBit)) { offset += WireFormatting.ReadShortstr(span.Slice(offset), out _replyTo); }

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ int IAmqpWriteable.GetRequiredBufferSize()
2727
public string? ContentEncoding => default;
2828
public string? ContentType => default;
2929
public string? CorrelationId => default;
30-
public byte DeliveryMode => default;
30+
public DeliveryModes DeliveryMode => default;
3131
public string? Expiration => default;
3232
public IDictionary<string, object?>? Headers => default;
3333
public string? MessageId => default;

projects/Unit/APIApproval.Approve.verified.txt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ namespace RabbitMQ.Client
7272
public string? ContentEncoding { get; set; }
7373
public string? ContentType { get; set; }
7474
public string? CorrelationId { get; set; }
75-
public byte DeliveryMode { get; set; }
75+
public RabbitMQ.Client.DeliveryModes DeliveryMode { get; set; }
7676
public string? Expiration { get; set; }
7777
public System.Collections.Generic.IDictionary<string, object?>? Headers { get; set; }
7878
public string? MessageId { get; set; }
@@ -248,6 +248,11 @@ namespace RabbitMQ.Client
248248
public DefaultEndpointResolver(System.Collections.Generic.IEnumerable<RabbitMQ.Client.AmqpTcpEndpoint> tcpEndpoints) { }
249249
public System.Collections.Generic.IEnumerable<RabbitMQ.Client.AmqpTcpEndpoint> All() { }
250250
}
251+
public enum DeliveryModes : byte
252+
{
253+
Transient = 1,
254+
Persistent = 2,
255+
}
251256
public static class EndpointResolverExtensions
252257
{
253258
public static T SelectOne<T>(this RabbitMQ.Client.IEndpointResolver resolver, System.Func<RabbitMQ.Client.AmqpTcpEndpoint, T> selector) { }
@@ -333,7 +338,7 @@ namespace RabbitMQ.Client
333338
new string? ContentEncoding { get; set; }
334339
new string? ContentType { get; set; }
335340
new string? CorrelationId { get; set; }
336-
new byte DeliveryMode { get; set; }
341+
new RabbitMQ.Client.DeliveryModes DeliveryMode { get; set; }
337342
new string? Expiration { get; set; }
338343
new System.Collections.Generic.IDictionary<string, object?>? Headers { get; set; }
339344
new string? MessageId { get; set; }
@@ -527,7 +532,7 @@ namespace RabbitMQ.Client
527532
string? ContentEncoding { get; }
528533
string? ContentType { get; }
529534
string? CorrelationId { get; }
530-
byte DeliveryMode { get; }
535+
RabbitMQ.Client.DeliveryModes DeliveryMode { get; }
531536
string? Expiration { get; }
532537
System.Collections.Generic.IDictionary<string, object?>? Headers { get; }
533538
string? MessageId { get; }
@@ -610,7 +615,7 @@ namespace RabbitMQ.Client
610615
public string? ContentEncoding { get; }
611616
public string? ContentType { get; }
612617
public string? CorrelationId { get; }
613-
public byte DeliveryMode { get; }
618+
public RabbitMQ.Client.DeliveryModes DeliveryMode { get; }
614619
public string? Expiration { get; }
615620
public System.Collections.Generic.IDictionary<string, object?>? Headers { get; }
616621
public string? MessageId { get; }

projects/Unit/TestBasicProperties.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void TestPersistentPropertyChangesDeliveryMode_PersistentTrueDelivery2()
4848
};
4949

5050
// Assert
51-
Assert.Equal(2, subject.DeliveryMode);
51+
Assert.Equal(DeliveryModes.Persistent, subject.DeliveryMode);
5252
Assert.True(subject.Persistent);
5353

5454
Span<byte> span = new byte[1024];
@@ -57,13 +57,13 @@ public void TestPersistentPropertyChangesDeliveryMode_PersistentTrueDelivery2()
5757
// Read from Stream
5858
var propertiesFromStream = new ReadOnlyBasicProperties(span.Slice(0, offset));
5959

60-
Assert.Equal(2, propertiesFromStream.DeliveryMode);
60+
Assert.Equal(DeliveryModes.Persistent, propertiesFromStream.DeliveryMode);
6161
Assert.True(propertiesFromStream.Persistent);
6262

6363
// Verify Basic Properties
6464
var basicProperties = new BasicProperties(propertiesFromStream);
6565

66-
Assert.Equal(2, basicProperties.DeliveryMode);
66+
Assert.Equal(DeliveryModes.Persistent, basicProperties.DeliveryMode);
6767
Assert.True(basicProperties.Persistent);
6868
}
6969

@@ -79,7 +79,7 @@ public void TestPersistentPropertyChangesDeliveryMode_PersistentFalseDelivery1()
7979
};
8080

8181
// Assert
82-
Assert.Equal(1, subject.DeliveryMode);
82+
Assert.Equal(DeliveryModes.Transient, subject.DeliveryMode);
8383
Assert.False(subject.Persistent);
8484

8585
Span<byte> span = new byte[1024];
@@ -88,13 +88,13 @@ public void TestPersistentPropertyChangesDeliveryMode_PersistentFalseDelivery1()
8888
// Read from Stream
8989
var propertiesFromStream = new ReadOnlyBasicProperties(span.Slice(0, offset));
9090

91-
Assert.Equal(1, propertiesFromStream.DeliveryMode);
91+
Assert.Equal(DeliveryModes.Transient, propertiesFromStream.DeliveryMode);
9292
Assert.False(propertiesFromStream.Persistent);
9393

9494
// Verify Basic Properties
9595
var basicProperties = new BasicProperties(propertiesFromStream);
9696

97-
Assert.Equal(1, basicProperties.DeliveryMode);
97+
Assert.Equal(DeliveryModes.Transient, basicProperties.DeliveryMode);
9898
Assert.False(basicProperties.Persistent);
9999
}
100100

projects/Unit/TestContentHeaderCodec.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public void TestFullProperties()
8484
ContentEncoding = "C",
8585
ClusterId = "D",
8686
CorrelationId = "E",
87-
DeliveryMode = 1,
87+
DeliveryMode = DeliveryModes.Transient,
8888
Expiration = "F",
8989
MessageId = "G",
9090
Priority = 2,

0 commit comments

Comments
 (0)