-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
AmqpMessageBody.cs
234 lines (219 loc) · 9.73 KB
/
AmqpMessageBody.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
namespace Azure.Core.Amqp
{
/// <summary>
/// Represents an AMQP message body.
/// <seealso href="http://docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-messaging-v1.0-os.html#section-message-format" />
/// </summary>
public class AmqpMessageBody
{
/// <summary>
/// The data sections for the AMQP message body.
/// </summary>
private readonly IEnumerable<ReadOnlyMemory<byte>>? _data;
/// <summary>
/// The value section for the AMQP message body.
/// </summary>
private readonly object? _value;
/// <summary>
/// The sequence sections for the AMQP message body.
/// </summary>
private readonly IEnumerable<IList<object>>? _sequence;
/// <summary>
/// Gets the type of the message body.
/// </summary>
public AmqpMessageBodyType BodyType { get; }
/// <summary>
/// Initializes a new <see cref="AmqpMessageBody"/> instance with the
/// passed in data sections.
/// <seealso href="http://docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-messaging-v1.0-os.html#type-data" />
/// </summary>
/// <param name="data">The data sections.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
public AmqpMessageBody(IEnumerable<ReadOnlyMemory<byte>> data)
{
Argument.AssertNotNull(data, nameof(data));
_data = data;
BodyType = AmqpMessageBodyType.Data;
}
/// <summary>
/// Initializes a new <see cref="AmqpMessageBody"/> instance with the
/// passed in value.
/// <seealso href="http://docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-messaging-v1.0-os.html#type-amqp-value" />
/// </summary>
/// <param name="value">The value section.</param>
private AmqpMessageBody(object value)
{
ValidateAmqpPrimitive(value, nameof(value));
_value = value;
BodyType = AmqpMessageBodyType.Value;
}
/// <summary>
/// Initializes a new <see cref="AmqpMessageBody"/> instance with the
/// passed in sequence sections.
/// <seealso href="http://docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-messaging-v1.0-os.html#type-amqp-sequence" />
/// </summary>
/// <param name="sequence">The sequence sections.</param>
private AmqpMessageBody(IEnumerable<IList<object>> sequence)
{
ValidateAmqpPrimitive(sequence, nameof(sequence));
_sequence = sequence;
BodyType = AmqpMessageBodyType.Sequence;
}
/// <summary>
/// Creates a new <see cref="AmqpMessageBody"/> instance with the
/// passed in data sections.
/// <seealso href="http://docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-messaging-v1.0-os.html#type-data" />
/// </summary>
/// <param name="data">The data sections.</param>
public static AmqpMessageBody FromData(IEnumerable<ReadOnlyMemory<byte>> data) =>
new AmqpMessageBody(data);
/// <summary>
/// Creates a new <see cref="AmqpMessageBody"/> instance with the
/// passed in value.
/// <seealso href="http://docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-messaging-v1.0-os.html#type-amqp-value" />
/// </summary>
/// <param name="value">The value section.</param>
public static AmqpMessageBody FromValue(object value) =>
new AmqpMessageBody(value);
/// <summary>
/// Creates a new <see cref="AmqpMessageBody"/> instance with the
/// passed in sequence.
/// <seealso href="http://docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-messaging-v1.0-os.html#type-amqp-value" />
/// </summary>
/// <param name="sequence">The sequence sections.</param>
public static AmqpMessageBody FromSequence(IEnumerable<IList<object>> sequence) =>
new AmqpMessageBody(sequence);
/// <summary>
/// Try to get the data sections for the AMQP message body.
/// </summary>
/// <param name="data">If the <see cref="AmqpMessageBody"/> instance is <see cref="AmqpMessageBodyType.Data"/>, this
/// will be populated with the data sections for the message.</param>
/// <returns>True if the <see cref="AmqpMessageBody"/> instance is <see cref="AmqpMessageBodyType.Data"/>.</returns>
public bool TryGetData(out IEnumerable<ReadOnlyMemory<byte>>? data)
{
if (BodyType == AmqpMessageBodyType.Data)
{
data = _data;
return true;
}
data = null;
return false;
}
/// <summary>
/// Try to get the value section for the AMQP message body.
/// </summary>
/// <param name="value">If the <see cref="AmqpMessageBody"/> instance is <see cref="AmqpMessageBodyType.Value"/>, this
/// will be populated with the value section for the message.</param>
/// <returns>True if the <see cref="AmqpMessageBody"/> instance is <see cref="AmqpMessageBodyType.Value"/>.</returns>
public bool TryGetValue(out object? value)
{
if (BodyType == AmqpMessageBodyType.Value)
{
value = _value;
return true;
}
value = null;
return false;
}
/// <summary>
/// Try to get the sequence section for the AMQP message body.
/// </summary>
/// <param name="sequence">If the <see cref="AmqpMessageBody"/> instance is <see cref="AmqpMessageBodyType.Sequence"/>, this
/// will be populated with the sequence sections for the message.</param>
/// <returns>True if the <see cref="AmqpMessageBody"/> instance is <see cref="AmqpMessageBodyType.Sequence"/>.</returns>
public bool TryGetSequence(out IEnumerable<IList<object>>? sequence)
{
if (BodyType == AmqpMessageBodyType.Sequence)
{
sequence = _sequence;
return true;
}
sequence = null;
return false;
}
private static void ValidateAmqpPrimitive(object value, string paramName)
{
Argument.AssertNotNull(value, paramName);
switch (value)
{
case string:
case byte:
case sbyte:
case char:
case short:
case ushort:
case int:
case uint:
case long:
case ulong:
case float:
case double:
case decimal:
case bool:
case Guid:
case DateTime:
case DateTimeOffset:
case TimeSpan:
case Uri:
case IEnumerable<string>:
case IEnumerable<byte>:
case IEnumerable<sbyte>:
case IEnumerable<char>:
case IEnumerable<short>:
case IEnumerable<ushort>:
case IEnumerable<int>:
case IEnumerable<uint>:
case IEnumerable<long>:
case IEnumerable<ulong>:
case IEnumerable<float>:
case IEnumerable<double>:
case IEnumerable<decimal>:
case IEnumerable<bool>:
case IEnumerable<Guid>:
case IEnumerable<DateTime>:
case IEnumerable<DateTimeOffset>:
case IEnumerable<TimeSpan>:
case IEnumerable<Uri>:
case IEnumerable<KeyValuePair<string, string>>:
case IEnumerable<KeyValuePair<string, byte>>:
case IEnumerable<KeyValuePair<string, sbyte>>:
case IEnumerable<KeyValuePair<string, char>>:
case IEnumerable<KeyValuePair<string, short>>:
case IEnumerable<KeyValuePair<string, ushort>>:
case IEnumerable<KeyValuePair<string, int>>:
case IEnumerable<KeyValuePair<string, uint>>:
case IEnumerable<KeyValuePair<string, long>>:
case IEnumerable<KeyValuePair<string, ulong>>:
case IEnumerable<KeyValuePair<string, float>>:
case IEnumerable<KeyValuePair<string, double>>:
case IEnumerable<KeyValuePair<string, decimal>>:
case IEnumerable<KeyValuePair<string, bool>>:
case IEnumerable<KeyValuePair<string, Guid>>:
case IEnumerable<KeyValuePair<string, DateTime>>:
case IEnumerable<KeyValuePair<string, DateTimeOffset>>:
case IEnumerable<KeyValuePair<string, TimeSpan>>:
case IEnumerable<KeyValuePair<string, Uri>>:
return;
case KeyValuePair<string, object> kvp:
ValidateAmqpPrimitive(kvp.Value, paramName);
break;
case IEnumerable enumerable:
foreach (object val in enumerable)
{
ValidateAmqpPrimitive(val, paramName);
}
break;
default:
throw new NotSupportedException($"Values of type {value.GetType()} are not supported. " +
$"Only the following types are supported: string, byte, char, short, int, long, float, double, decimal, bool, " +
$"Guid, DateTime, DateTimeOffset, Timespan, Uri.");
}
}
}
}