forked from ByteBardOrg/AsyncAPI.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncApiChannelDeserializer.cs
More file actions
99 lines (83 loc) · 4.04 KB
/
Copy pathAsyncApiChannelDeserializer.cs
File metadata and controls
99 lines (83 loc) · 4.04 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
namespace ByteBard.AsyncAPI.Readers
{
using ByteBard.AsyncAPI.Extensions;
using ByteBard.AsyncAPI.Models;
using ByteBard.AsyncAPI.Readers.ParseNodes;
using System.Collections.Generic;
using System.Threading;
internal static partial class AsyncApiV2Deserializer
{
private static readonly FixedFieldMap<AsyncApiChannel> ChannelFixedFields = new()
{
{ "description", (a, n) => { a.Description = n.GetScalarValue(); } },
{ "servers", (a, n) => { a.Servers = n.CreateSimpleList(s => new AsyncApiServerReference(GetServerReferenceKey(s))); } },
{ "subscribe", (a, n) => { /* happens after initial reading */ } },
{ "publish", (a, n) => { /* happens after initial reading */ } },
{ "parameters", (a, n) => { a.Parameters = n.CreateMap(LoadParameter); } },
{ "bindings", (a, n) => { a.Bindings = LoadChannelBindings(n); } },
};
private static string GetServerReferenceKey(ValueNode valueNode)
{
var stringValue = valueNode.GetScalarValue();
return stringValue.StartsWith("#/servers/") ? stringValue : "#/servers/" + stringValue;
}
private static readonly PatternFieldMap<AsyncApiChannel> ChannelPatternFields =
new()
{
{ s => s.StartsWith("x-"), (a, p, n) => a.AddExtension(p, LoadExtension(p, n)) },
};
public static AsyncApiChannel LoadChannel(ParseNode node, string channelAddress = null)
{
var mapNode = node.CheckMapNode("channel");
var pointer = mapNode.GetReferencePointer();
if (pointer != null)
{
return new AsyncApiChannelReference(pointer);
}
var channel = new AsyncApiChannel();
ParseMap(mapNode, channel, ChannelFixedFields, ChannelPatternFields);
if (channelAddress != null)
{
channel.Address = channelAddress;
}
LoadV2Operation(mapNode["subscribe"]?.Value, channel, AsyncApiAction.Send);
LoadV2Operation(mapNode["publish"]?.Value, channel, AsyncApiAction.Receive);
return channel;
}
public static string NormalizeChannelKey(string channelKey, ParseNode node = null)
{
string newKey = string.Empty;
foreach (var character in channelKey)
{
if (char.IsLetterOrDigit(character))
{
newKey += character;
}
}
if (node != null)
{
var addresses = node.Context.GetFromTempStorage<Dictionary<string, string>>(TempStorageKeys.ChannelAddresses) ?? new Dictionary<string, string>();
addresses.Add(newKey, channelKey);
node.Context.SetTempStorage(TempStorageKeys.ChannelAddresses, addresses);
}
return newKey;
}
private static void LoadV2Operation(ParseNode node, AsyncApiChannel instance, AsyncApiAction action)
{
if (node == null)
{
return;
}
var operation = LoadOperation(node);
var operationKey = node.CheckMapNode("operation")?["operationId"]?.Value.GetScalarValue() ?? "anonymous-operation-" + Interlocked.Increment(ref node.Context.OperationCounter).ToString();
operation.Action = action;
operation.Channel = new AsyncApiChannelReference("#/channels/" + NormalizeChannelKey(instance.Address));
var globalOperations = node.Context.GetFromTempStorage<Dictionary<string, AsyncApiOperation>>(TempStorageKeys.Operations) ?? new Dictionary<string, AsyncApiOperation>();
if (!globalOperations.TryAdd(operationKey, operation))
{
node.Context.Diagnostic.Errors.Add(new AsyncApiError(node.Context.GetLocation(), $"OperationId: '{operationKey}' is not unique."));
}
node.Context.SetTempStorage(TempStorageKeys.Operations, globalOperations);
}
}
}