Repository navigation
Expand file tree
/
Copy pathAsyncApiChannel.cs
More file actions
170 lines (141 loc) · 7.61 KB
/
Copy pathAsyncApiChannel.cs
File metadata and controls
170 lines (141 loc) · 7.61 KB
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
namespace ByteBard.AsyncAPI.Models
{
using System;
using System.Collections.Generic;
using System.Linq;
using ByteBard.AsyncAPI.Extensions;
using ByteBard.AsyncAPI.Models.Interfaces;
using ByteBard.AsyncAPI.Writers;
/// <summary>
/// Describes the operations available on a single channel.
/// </summary>
public class AsyncApiChannel : IAsyncApiSerializable, IAsyncApiExtensible
{
/// <summary>
/// An optional string representation of this channel's address. The address is typically the "topic name", "routing key", "event type", or "path". When null or absent, it MUST be interpreted as unknown. This is useful when the address is generated dynamically at runtime or can't be known upfront. It MAY contain Channel Address Expressions. Query parameters and fragments SHALL NOT be used, instead use bindings to define them.
/// </summary>
public virtual string? Address { get; set; }
/// <summary>
/// A map of the messages that will be sent to this channel by any application at any time. Every message sent to this channel MUST be valid against one, and only one, of the message objects defined in this map.
/// </summary>
public virtual IDictionary<string, AsyncApiMessage> Messages { get; set; } = new Dictionary<string, AsyncApiMessage>();
/// <summary>
/// A human-friendly title for the channel.
/// </summary>
public virtual string Title { get; set; }
/// <summary>
/// A short summary of the channel.
/// </summary>
public virtual string Summary { get; set; }
/// <summary>
/// an optional description of this channel item. CommonMark syntax can be used for rich text representation.
/// </summary>
public virtual string Description { get; set; }
/// <summary>
/// the servers on which this channel is available, specified as an optional unordered list of names (string keys) of Server Objects defined in the Servers Object (a map).
/// </summary>
/// <remarks>
/// If servers is absent or empty then this channel must be available on all servers defined in the Servers Object.
/// </remarks>
public virtual IList<AsyncApiServerReference> Servers { get; set; } = new List<AsyncApiServerReference>();
/// <summary>
/// A map of the parameters included in the channel address. It MUST be present only when the address contains Channel Address Expressions.
/// </summary>
public virtual IDictionary<string, AsyncApiParameter> Parameters { get; set; } = new Dictionary<string, AsyncApiParameter>();
/// <summary>
/// A list of tags for logical grouping of channels.
/// </summary>
public virtual IList<AsyncApiTag> Tags { get; set; } = new List<AsyncApiTag>();
/// <summary>
/// Additional external documentation for this channel.
/// </summary>
public virtual AsyncApiExternalDocumentation ExternalDocs { get; set; }
/// <summary>
/// a map where the keys describe the name of the protocol and the values describe protocol-specific definitions for the channel.
/// </summary>
public virtual AsyncApiBindings<IChannelBinding> Bindings { get; set; } = new AsyncApiBindings<IChannelBinding>();
/// <inheritdoc/>
public virtual IDictionary<string, IAsyncApiExtension> Extensions { get; set; } = new Dictionary<string, IAsyncApiExtension>();
public virtual void SerializeV2(IAsyncApiWriter writer)
{
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
writer.Workspace?.SerializationContext.Push(this);
try
{
writer.WriteStartObject();
// description
writer.WriteOptionalProperty(AsyncApiConstants.Description, this.Description);
// servers
writer.WriteOptionalCollection(AsyncApiConstants.Servers, this.Servers.Select(s => s.Reference.FragmentId).ToList(), (w, s) => w.WriteValue(s));
var operations = writer.Workspace.RootDocument?.Operations.Values.Where(operation => CheckOperationChannel(operation, writer)).ToList();
// subscribe (Now Send)
writer.WriteOptionalObject(AsyncApiConstants.Subscribe, operations?.FirstOrDefault(o => o.Action == AsyncApiAction.Send), (w, s) => s?.SerializeV2(w));
// publish (Now Receive)
writer.WriteOptionalObject(AsyncApiConstants.Publish, operations?.FirstOrDefault(o => o.Action == AsyncApiAction.Receive), (w, s) => s?.SerializeV2(w));
// parameters
writer.WriteOptionalMap(AsyncApiConstants.Parameters, this.Parameters, (writer, key, component) =>
{
if (component is AsyncApiParameterReference reference)
{
reference.SerializeV2(writer);
}
else
{
component.SerializeV2(writer);
}
});
writer.WriteOptionalObject(AsyncApiConstants.Bindings, this.Bindings, (w, t) => t.SerializeV2(w));
// extensions
writer.WriteExtensions(this.Extensions);
writer.WriteEndObject();
}
finally
{
writer.Workspace?.SerializationContext.Pop();
}
}
public virtual void SerializeV3(IAsyncApiWriter writer)
{
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
writer.Workspace?.SerializationContext.Push(this);
try
{
writer.WriteStartObject();
writer.WriteOptionalProperty(AsyncApiConstants.Address, this.Address);
writer.WriteRequiredMap(AsyncApiConstants.Messages, this.Messages, (w, k, m) => m.SerializeV3(w));
writer.WriteOptionalProperty(AsyncApiConstants.Title, this.Title);
writer.WriteOptionalProperty(AsyncApiConstants.Summary, this.Summary);
writer.WriteOptionalProperty(AsyncApiConstants.Description, this.Description);
writer.WriteOptionalCollection(AsyncApiConstants.Servers, this.Servers, (w, s) => s.Reference.SerializeV3(w));
if (this.Address.IsChannelAddressExpression())
{
writer.WriteOptionalMap(AsyncApiConstants.Parameters, this.Parameters, (w, key, p) => p.SerializeV3(w));
}
writer.WriteOptionalCollection(AsyncApiConstants.Tags, this.Tags, (w, t) => t.SerializeV3(w));
writer.WriteOptionalObject(AsyncApiConstants.ExternalDocs, this.ExternalDocs, (w, s) => s.SerializeV3(w));
writer.WriteOptionalObject(AsyncApiConstants.Bindings, this.Bindings, (w, t) => t.SerializeV3(w));
writer.WriteExtensions(this.Extensions);
writer.WriteEndObject();
}
finally
{
writer.Workspace?.SerializationContext.Pop();
}
}
private bool CheckOperationChannel(AsyncApiOperation operation, IAsyncApiWriter writer)
{
if (operation is AsyncApiOperationReference reference)
{
reference.Reference.Workspace = writer.Workspace;
}
operation.Channel.Reference.Workspace = writer.Workspace;
return operation.Channel.Equals(this);
}
}
}