forked from apple/swift-openapi-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONSequenceDecoding.swift
More file actions
236 lines (196 loc) · 8.55 KB
/
JSONSequenceDecoding.swift
File metadata and controls
236 lines (196 loc) · 8.55 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftOpenAPIGenerator open source project
//
// Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
#if canImport(Darwin)
public import class Foundation.JSONDecoder
#else
@preconcurrency public import class Foundation.JSONDecoder
#endif
public import protocol Foundation.LocalizedError
public import struct Foundation.Data
/// A sequence that parses arbitrary byte chunks into lines using the JSON Sequence format.
public struct JSONSequenceDeserializationSequence<Upstream: AsyncSequence & Sendable>: Sendable
where Upstream.Element == ArraySlice<UInt8> {
/// The upstream sequence.
private let upstream: Upstream
/// Creates a new sequence.
/// - Parameter upstream: The upstream sequence of arbitrary byte chunks.
public init(upstream: Upstream) { self.upstream = upstream }
}
extension JSONSequenceDeserializationSequence: AsyncSequence {
/// The type of element produced by this asynchronous sequence.
public typealias Element = ArraySlice<UInt8>
/// An error thrown by the deserializer.
struct DeserializerError<UpstreamIterator: AsyncIteratorProtocol>: Swift.Error, CustomStringConvertible,
LocalizedError
where UpstreamIterator.Element == Element {
/// The underlying error emitted by the state machine.
let error: Iterator<UpstreamIterator>.StateMachine.ActionError
var description: String {
switch error {
case .missingInitialRS: return "Missing an initial <RS> character, the bytes might not be a JSON Sequence."
}
}
var errorDescription: String? { description }
}
/// The iterator of `JSONSequenceDeserializationSequence`.
public struct Iterator<UpstreamIterator: AsyncIteratorProtocol>: AsyncIteratorProtocol
where UpstreamIterator.Element == Element {
/// The upstream iterator of arbitrary byte chunks.
var upstream: UpstreamIterator
/// The state machine of the iterator.
var stateMachine: StateMachine = .init()
/// Asynchronously advances to the next element and returns it, or ends the
/// sequence if there is no next element.
public mutating func next() async throws -> ArraySlice<UInt8>? {
while true {
switch stateMachine.next() {
case .returnNil: return nil
case .emitLine(let line): return line
case .needsMore:
let value = try await upstream.next()
switch stateMachine.receivedValue(value) {
case .returnNil: return nil
case .emitLine(let line): return line
case .noop: continue
}
case .emitError(let error): throw DeserializerError(error: error)
case .noop: continue
}
}
}
}
/// Creates the asynchronous iterator that produces elements of this
/// asynchronous sequence.
public func makeAsyncIterator() -> Iterator<Upstream.AsyncIterator> {
Iterator(upstream: upstream.makeAsyncIterator())
}
}
extension AsyncSequence where Element == ArraySlice<UInt8> {
/// Returns another sequence that decodes each JSON Sequence event as the provided type using the provided decoder.
/// - Parameters:
/// - eventType: The type to decode the JSON event into.
/// - decoder: The JSON decoder to use.
/// - Returns: A sequence that provides the decoded JSON events.
public func asDecodedJSONSequence<Event: Decodable>(
of eventType: Event.Type = Event.self,
decoder: JSONDecoder = .init()
) -> AsyncThrowingMapSequence<JSONSequenceDeserializationSequence<Self>, Event> {
JSONSequenceDeserializationSequence(upstream: self)
.map { line in try decoder.decode(Event.self, from: Data(line)) }
}
}
extension JSONSequenceDeserializationSequence.Iterator {
/// A state machine representing the JSON Lines deserializer.
struct StateMachine {
/// The possible states of the state machine.
enum State: Hashable {
/// Has not yet fully parsed the initial boundary.
case initial(buffer: [UInt8])
/// Is parsing a line, waiting for the end newline.
case parsingLine(buffer: [UInt8])
/// Finished, the terminal state.
case finished
/// Helper state to avoid copy-on-write copies.
case mutating
}
/// The current state of the state machine.
private(set) var state: State
/// Creates a new state machine.
init() { self.state = .initial(buffer: []) }
/// An error returned by the state machine.
enum ActionError {
/// The initial boundary `<RS>` was not found.
case missingInitialRS
}
/// An action returned by the `next` method.
enum NextAction {
/// Return nil to the caller, no more bytes.
case returnNil
/// Emit a full line.
case emitLine(ArraySlice<UInt8>)
/// Emit an error.
case emitError(ActionError)
/// The line is not complete yet, needs more bytes.
case needsMore
/// Rerun the parsing loop.
case noop
}
/// Read the next line parsed from upstream bytes.
/// - Returns: An action to perform.
mutating func next() -> NextAction {
switch state {
case .initial(var buffer):
guard !buffer.isEmpty else { return .needsMore }
guard buffer.first! == ASCII.rs else { return .emitError(.missingInitialRS) }
state = .mutating
buffer.removeFirst()
state = .parsingLine(buffer: buffer)
return .noop
case .parsingLine(var buffer):
state = .mutating
guard let indexOfRecordSeparator = buffer.firstIndex(of: ASCII.rs) else {
state = .parsingLine(buffer: buffer)
return .needsMore
}
let line = buffer[..<indexOfRecordSeparator]
buffer.removeSubrange(...indexOfRecordSeparator)
state = .parsingLine(buffer: buffer)
if line.isEmpty { return .noop } else { return .emitLine(line) }
case .finished: return .returnNil
case .mutating: preconditionFailure("Invalid state")
}
}
/// An action returned by the `receivedValue` method.
enum ReceivedValueAction {
/// Return nil to the caller, no more lines.
case returnNil
/// Emit a full line.
case emitLine(ArraySlice<UInt8>)
/// No action, rerun the parsing loop.
case noop
}
/// Ingest the provided bytes.
/// - Parameter value: A new byte chunk. If `nil`, then the source of bytes is finished.
/// - Returns: An action to perform.
mutating func receivedValue(_ value: ArraySlice<UInt8>?) -> ReceivedValueAction {
switch state {
case .initial(var buffer):
if let value {
state = .mutating
buffer.append(contentsOf: value)
state = .initial(buffer: buffer)
return .noop
} else {
let line = ArraySlice(buffer)
buffer = []
state = .finished
if line.isEmpty { return .returnNil } else { return .emitLine(line) }
}
case .parsingLine(var buffer):
if let value {
state = .mutating
buffer.append(contentsOf: value)
state = .parsingLine(buffer: buffer)
return .noop
} else {
let line = ArraySlice(buffer)
buffer = []
state = .finished
if line.isEmpty { return .returnNil } else { return .emitLine(line) }
}
case .finished, .mutating: preconditionFailure("Invalid state")
}
}
}
}