forked from LEGO/AsyncAPI.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncApiChannel.cs
More file actions
115 lines (92 loc) · 4.38 KB
/
Copy pathAsyncApiChannel.cs
File metadata and controls
115 lines (92 loc) · 4.38 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
// Copyright (c) The LEGO Group. All rights reserved.
namespace LEGO.AsyncAPI.Models
{
using System;
using System.Collections.Generic;
using LEGO.AsyncAPI.Models.Interfaces;
using LEGO.AsyncAPI.Writers;
/// <summary>
/// Describes the operations available on a single channel.
/// </summary>
public class AsyncApiChannel : IAsyncApiReferenceable, IAsyncApiExtensible
{
/// <summary>
/// an optional description of this channel item. CommonMark syntax can be used for rich text representation.
/// </summary>
public 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 IList<string> Servers { get; set; } = new List<string>();
/// <summary>
/// a definition of the SUBSCRIBE operation, which defines the messages produced by the application and sent to the channel.
/// </summary>
public AsyncApiOperation Subscribe { get; set; }
/// <summary>
/// a definition of the PUBLISH operation, which defines the messages consumed by the application from the channel.
/// </summary>
public AsyncApiOperation Publish { get; set; }
/// <summary>
/// a map of the parameters included in the channel name. It SHOULD be present only when using channels with expressions (as defined by RFC 6570 section 2.2).
/// </summary>
public IDictionary<string, AsyncApiParameter> Parameters { get; set; } = new Dictionary<string, AsyncApiParameter>();
/// <summary>
/// a map where the keys describe the name of the protocol and the values describe protocol-specific definitions for the channel.
/// </summary>
public AsyncApiBindings<IChannelBinding> Bindings { get; set; } = new AsyncApiBindings<IChannelBinding>();
/// <inheritdoc/>
public IDictionary<string, IAsyncApiExtension> Extensions { get; set; } = new Dictionary<string, IAsyncApiExtension>();
public bool UnresolvedReference { get; set; }
public AsyncApiReference Reference { get; set; }
public void SerializeV2(IAsyncApiWriter writer)
{
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
if (this.Reference != null && !writer.GetSettings().ShouldInlineReference(this.Reference))
{
this.Reference.SerializeV2(writer);
return;
}
this.SerializeV2WithoutReference(writer);
}
public void SerializeV2WithoutReference(IAsyncApiWriter writer)
{
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
writer.WriteStartObject();
// description
writer.WriteOptionalProperty(AsyncApiConstants.Description, this.Description);
// servers
writer.WriteOptionalCollection(AsyncApiConstants.Servers, this.Servers, (w, s) => w.WriteValue(s));
// subscribe
writer.WriteOptionalObject(AsyncApiConstants.Subscribe, this.Subscribe, (w, s) => s.SerializeV2(w));
// publish
writer.WriteOptionalObject(AsyncApiConstants.Publish, this.Publish, (w, s) => s.SerializeV2(w));
// parameters
writer.WriteOptionalMap(AsyncApiConstants.Parameters, this.Parameters, (writer, key, component) =>
{
if (component.Reference != null &&
component.Reference.Type == ReferenceType.Channel &&
component.Reference.Id == key)
{
component.SerializeV2WithoutReference(writer);
}
else
{
component.SerializeV2(writer);
}
});
writer.WriteOptionalObject(AsyncApiConstants.Bindings, this.Bindings, (w, t) => t.SerializeV2(w));
// extensions
writer.WriteExtensions(this.Extensions);
writer.WriteEndObject();
}
}
}