-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathAsyncApiTextReader.cs
More file actions
128 lines (115 loc) · 5.07 KB
/
Copy pathAsyncApiTextReader.cs
File metadata and controls
128 lines (115 loc) · 5.07 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
// Copyright (c) The LEGO Group. All rights reserved.
namespace LEGO.AsyncAPI.Readers
{
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading;
using System.Threading.Tasks;
using LEGO.AsyncAPI.Models;
using LEGO.AsyncAPI.Models.Interfaces;
using LEGO.AsyncAPI.Readers.Interface;
using YamlDotNet.RepresentationModel;
/// <summary>
/// Service class for converting contents of TextReader into AsyncApiDocument instances.
/// </summary>
public class AsyncApiTextReader : IAsyncApiReader<TextReader, AsyncApiDiagnostic>
{
private readonly AsyncApiReaderSettings settings;
/// <summary>
/// Create stream reader with custom settings if desired.
/// </summary>
/// <param name="settings"></param>
public AsyncApiTextReader(AsyncApiReaderSettings settings = null)
{
this.settings = settings ?? new AsyncApiReaderSettings();
}
/// <summary>
/// Reads the stream input and parses it into an AsyncApi document.
/// </summary>
/// <param name="input">TextReader containing AsyncApi description to parse.</param>
/// <param name="diagnostic">Returns diagnostic object containing errors detected during parsing.</param>
/// <returns>Instance of newly created AsyncApiDocument.</returns>
public AsyncApiDocument Read(TextReader input, out AsyncApiDiagnostic diagnostic)
{
JsonNode jsonNode;
// Parse the YAML/JSON text in the TextReader into the YamlDocument
try
{
jsonNode = LoadYamlDocument(input, this.settings);
}
catch (JsonException ex)
{
diagnostic = new AsyncApiDiagnostic();
diagnostic.Errors.Add(new AsyncApiError($"#line={ex.LineNumber}", ex.Message));
return new AsyncApiDocument();
}
return new AsyncApiJsonDocumentReader(this.settings).Read(jsonNode, out diagnostic);
}
/// <summary>
/// Reads the content of the TextReader.
/// </summary>
/// <param name="input">TextReader containing AsyncApi description to parse.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// A ReadResult instance that contains the resulting AsyncApiDocument and a diagnostics instance.
/// </returns>
public async Task<ReadResult> ReadAsync(TextReader input, CancellationToken cancellationToken)
{
JsonNode jsonNode;
// Parse the YAML/JSON text in the TextReader into the YamlDocument
try
{
jsonNode = LoadYamlDocument(input, this.settings);
}
catch (JsonException ex)
{
var diagnostic = new AsyncApiDiagnostic();
diagnostic.Errors.Add(new AsyncApiError($"#line={ex.LineNumber}", ex.Message));
return new ReadResult
{
AsyncApiDocument = null,
AsyncApiDiagnostic = diagnostic,
};
}
return await new AsyncApiJsonDocumentReader(this.settings).ReadAsync(jsonNode, cancellationToken);
}
/// <summary>
/// Reads the stream input and parses the fragment of an AsyncApi description into an AsyncApi Element.
/// </summary>
/// <param name="input">TextReader containing AsyncApi description to parse.</param>
/// <param name="version">Version of the AsyncApi specification that the fragment conforms to.</param>
/// <param name="diagnostic">Returns diagnostic object containing errors detected during parsing.</param>
/// <returns>Instance of newly created AsyncApiDocument.</returns>
public T ReadFragment<T>(TextReader input, AsyncApiVersion version, out AsyncApiDiagnostic diagnostic)
where T : IAsyncApiElement
{
JsonNode jsonNode;
// Parse the YAML/JSON
try
{
jsonNode = LoadYamlDocument(input, this.settings);
}
catch (JsonException ex)
{
diagnostic = new AsyncApiDiagnostic();
diagnostic.Errors.Add(new AsyncApiError($"#line={ex.LineNumber}", ex.Message));
return default;
}
return new AsyncApiJsonDocumentReader(this.settings).ReadFragment<T>(jsonNode, version,
out diagnostic);
}
/// <summary>
/// Helper method to turn streams into YamlDocument.
/// </summary>
/// <param name="input">Stream containing YAML formatted text.</param>
/// <returns>Instance of a YamlDocument.</returns>
static JsonNode LoadYamlDocument(TextReader input, AsyncApiReaderSettings settings)
{
var yamlStream = new YamlStream();
yamlStream.Load(input);
return yamlStream.Documents.First().ToJsonNode(settings.CultureInfo);
}
}
}