Repository navigation
Expand file tree
/
Copy pathParsingContext.cs
More file actions
134 lines (105 loc) · 4.42 KB
/
Copy pathParsingContext.cs
File metadata and controls
134 lines (105 loc) · 4.42 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
// Copyright (c) The LEGO Group. All rights reserved.
namespace LEGO.AsyncAPI.Readers
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Nodes;
using LEGO.AsyncAPI.Models;
using LEGO.AsyncAPI.Models.Interfaces;
using LEGO.AsyncAPI.Readers.Exceptions;
using LEGO.AsyncAPI.Readers.Interface;
using LEGO.AsyncAPI.Readers.ParseNodes;
using LEGO.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; }
///// <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;
}
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);
this.Diagnostic.SpecificationVersion = AsyncApiVersion.AsyncApi2_0;
break;
default:
throw new AsyncApiUnsupportedSpecVersionException(inputVersion);
}
return doc;
}
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);
}
}
}