-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathAsyncApiStreamReader.cs
More file actions
91 lines (82 loc) · 3.74 KB
/
Copy pathAsyncApiStreamReader.cs
File metadata and controls
91 lines (82 loc) · 3.74 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
// Copyright (c) The LEGO Group. All rights reserved.
namespace LEGO.AsyncAPI.Readers
{
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using LEGO.AsyncAPI.Models;
using LEGO.AsyncAPI.Models.Interfaces;
using LEGO.AsyncAPI.Readers.Interface;
/// <summary>
/// Service class for converting streams into AsyncApiDocument instances.
/// </summary>
public class AsyncApiStreamReader : IAsyncApiReader<Stream, AsyncApiDiagnostic>
{
private readonly AsyncApiReaderSettings settings;
/// <summary>
/// Create stream reader with custom settings if desired.
/// </summary>
/// <param name="settings"></param>
public AsyncApiStreamReader(AsyncApiReaderSettings settings = null)
{
this.settings = settings ?? new AsyncApiReaderSettings();
}
/// <summary>
/// Reads the stream input and parses it into an AsyncApi document.
/// </summary>
/// <param name="input">Stream 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(Stream input, out AsyncApiDiagnostic diagnostic)
{
var reader = new StreamReader(input);
var result = new AsyncApiTextReader(this.settings).Read(reader, out diagnostic);
if (!this.settings.LeaveStreamOpen)
{
reader.Dispose();
}
return result;
}
/// <summary>
/// Reads the stream input and parses it into an AsyncApi document.
/// </summary>
/// <param name="input">Stream containing AsyncApi description to parse.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// Instance result containing newly created AsyncApiDocument and diagnostics object from the process.
/// </returns>
public async Task<ReadResult> ReadAsync(Stream input, CancellationToken cancellationToken)
{
MemoryStream bufferedStream;
if (input is MemoryStream)
{
bufferedStream = (MemoryStream)input;
}
else
{
// Buffer stream so that AsyncApiTextReaderReader can process it synchronously
// YamlDocument doesn't support async reading.
bufferedStream = new MemoryStream();
await input.CopyToAsync(bufferedStream);
bufferedStream.Position = 0;
}
var reader = new StreamReader(bufferedStream);
return await new AsyncApiTextReader(this.settings).ReadAsync(reader, cancellationToken);
}
/// <summary>
/// Reads the stream input and parses the fragment of an AsyncApi description into an AsyncApi Element.
/// </summary>
/// <param name="input">Stream 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>(Stream input, AsyncApiVersion version, out AsyncApiDiagnostic diagnostic)
where T : IAsyncApiReferenceable
{
using (var reader = new StreamReader(input))
{
return new AsyncApiTextReader(this.settings).ReadFragment<T>(reader, version, out diagnostic);
}
}
}
}