Repository navigation
Expand file tree
/
Copy pathParsingContext.cs
More file actions
153 lines (121 loc) · 5.09 KB
/
Copy pathParsingContext.cs
File metadata and controls
153 lines (121 loc) · 5.09 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
namespace ByteBard.AsyncAPI.Readers
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using ByteBard.AsyncAPI.Models;
using ByteBard.AsyncAPI.Models.Interfaces;
using ByteBard.AsyncAPI.Readers.Exceptions;
using ByteBard.AsyncAPI.Readers.Interface;
using ByteBard.AsyncAPI.Readers.ParseNodes;
using ByteBard.AsyncAPI.Readers.V2;
public class ParsingContext
{
private readonly Stack<string> currentLocation = new();
internal Dictionary<string, Func<AsyncApiAny, IAsyncApiExtension>> ExtensionParsers
{
get;
set;
}
= new();
internal Dictionary<string, IBindingParser<IServerBinding>> ServerBindingParsers { get; set; } = new();
internal Dictionary<string, IBindingParser<IChannelBinding>> ChannelBindingParsers { get; set; } = new();
internal Dictionary<string, IBindingParser<IOperationBinding>> OperationBindingParsers { get; set; } = new();
internal Dictionary<string, IBindingParser<IMessageBinding>> MessageBindingParsers { get; set; } = new();
internal RootNode RootNode { get; set; }
internal List<AsyncApiTag> Tags { get; private set; } = new();
public AsyncApiDiagnostic Diagnostic { get; }
/// <summary>
/// Gets the settings used fore reading json.
/// </summary>
public AsyncApiReaderSettings Settings { get; }
public AsyncApiWorkspace Workspace { get; }
///// <summary>
///// Initializes a new instance of the <see cref="ParsingContext"/> class.
///// </summary>
/// <param name="diagnostic">The diagnostics.</param>
[Obsolete($"Please use the overloaded version that takes in an instance of {nameof(AsyncApiReaderSettings)} isntead.")]
public ParsingContext(AsyncApiDiagnostic diagnostic)
: this(diagnostic, new AsyncApiReaderSettings())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ParsingContext"/> class.
/// </summary>
/// <param name="diagnostic">The diagnostics.</param>
/// <param name="settings">The settings used to read json.</param>
public ParsingContext(AsyncApiDiagnostic diagnostic, AsyncApiReaderSettings settings)
{
this.Diagnostic = diagnostic;
this.Settings = settings;
this.Workspace = new AsyncApiWorkspace();
}
internal AsyncApiDocument Parse(JsonNode jsonNode)
{
this.RootNode = new RootNode(this, jsonNode);
var inputVersion = GetVersion(this.RootNode);
AsyncApiDocument doc;
switch (inputVersion)
{
case string version when version.StartsWith("2"):
this.VersionService = new AsyncApiV2VersionService(this.Diagnostic);
doc = this.VersionService.LoadDocument(this.RootNode);
// Register components
this.Workspace.RegisterComponents(doc); // pre-register components.
this.Workspace.RegisterComponent(string.Empty, this.ParseToStream(jsonNode)); // register root document.
this.Diagnostic.SpecificationVersion = AsyncApiVersion.AsyncApi2_0;
break;
default:
throw new AsyncApiUnsupportedSpecVersionException(inputVersion);
}
return doc;
}
private Stream ParseToStream(JsonNode node)
{
var stream = new MemoryStream();
using (var writer = new Utf8JsonWriter(stream))
{
node.WriteTo(writer);
}
stream.Position = 0;
return stream;
}
internal T ParseFragment<T>(JsonNode jsonNode, AsyncApiVersion version)
where T : IAsyncApiElement
{
var node = ParseNode.Create(this, jsonNode);
T element = default(T);
switch (version)
{
case AsyncApiVersion.AsyncApi2_0:
this.VersionService = new AsyncApiV2VersionService(this.Diagnostic);
element = this.VersionService.LoadElement<T>(node);
break;
}
return element;
}
private static string GetVersion(RootNode rootNode)
{
var versionNode = rootNode.Find(new JsonPointer("/asyncapi"));
return versionNode?.GetScalarValue().Replace("\"", string.Empty);
}
internal IAsyncApiVersionService VersionService { get; set; }
public void EndObject()
{
this.currentLocation.Pop();
}
public string GetLocation()
{
return "#/" + string.Join(
"/",
this.currentLocation.Reverse().Select(s => s.Replace("~", "~0").Replace("/", "~1")).ToArray());
}
public void StartObject(string objectName)
{
this.currentLocation.Push(objectName);
}
}
}