Skip to content

Commit 29032ed

Browse files
committed
[Macros] Cleanup JSON
* Rename PluginMessageJSON to JSON * Make JSON encoder/decoder SPI * Make encoding/decoding internals private
1 parent e20a65c commit 29032ed

File tree

6 files changed

+87
-32
lines changed

6 files changed

+87
-32
lines changed

Sources/SwiftCompilerPlugin/CompilerPlugin.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ internal struct PluginHostConnection: MessageConnection {
189189

190190
func sendMessage<TX: Encodable>(_ message: TX) throws {
191191
// Encode the message as JSON.
192-
let payload = try PluginMessageJSON.encode(message)
192+
let payload = try JSON.encode(message)
193193

194194
// Write the header (a 64-bit length field in little endian byte order).
195195
let count = payload.count
@@ -217,7 +217,7 @@ internal struct PluginHostConnection: MessageConnection {
217217
// Read the JSON payload.
218218
return try _reading(inputStream, count: Int(count)) { buffer in
219219
// Decode and return the message.
220-
try PluginMessageJSON.decode(RX.self, from: buffer.bindMemory(to: UInt8.self))
220+
try JSON.decode(RX.self, from: buffer.bindMemory(to: UInt8.self))
221221
}
222222
}
223223

Sources/SwiftCompilerPluginMessageHandling/JSON/CodingUtilities.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
112

213
//===----------------------------------------------------------------------===//
314
// Coding Path Node
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
@_spi(PluginMessage)
14+
public enum JSON {
15+
/// Encode Swift value to an UInt8 array.
16+
public static func encode<T: Encodable>(_ value: T) throws -> [UInt8] {
17+
try encodeToJSON(value: value)
18+
}
19+
20+
/// Decode a JSON data to a Swift value.
21+
public static func decode<T: Decodable>(_ type: T.Type, from json: UnsafeBufferPointer<UInt8>) throws -> T {
22+
try decodeFromJSON(json: json)
23+
}
24+
}

Sources/SwiftCompilerPluginMessageHandling/JSON/JSONDecoding.swift

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
112

213
import Darwin
314

4-
struct JSONMap {
15+
func decodeFromJSON<T: Decodable>(json: UnsafeBufferPointer<UInt8>) throws -> T {
16+
try withExtendedLifetime(try JSONScanner.scan(buffer: json)) { map in
17+
let decoder = JSONDecoding(value: map.value, codingPathNode: .root)
18+
return try T.init(from: decoder)
19+
}
20+
}
21+
22+
private struct JSONMap {
523
enum Descriptor: Int {
624
case nullKeyword // [desc]
725
case trueKeyword // [desc]
@@ -19,7 +37,7 @@ struct JSONMap {
1937
}
2038
}
2139

22-
struct JSONMapValue {
40+
private struct JSONMapValue {
2341
typealias Map = Array<Int>.SubSequence
2442
typealias Index = Map.Index
2543
let map: Map
@@ -82,7 +100,7 @@ extension JSONMapValue {
82100
}
83101

84102
// MARK: Scalar values
85-
enum _JSONStringDecoder {
103+
private enum _JSONStringDecoder {
86104
/// Trim '"' from string literal buffer.
87105
static func trimQuotes(source: UnsafeBufferPointer<UInt8>) -> UnsafeBufferPointer<UInt8> {
88106
assert(source.count >= 2)
@@ -142,7 +160,7 @@ enum _JSONStringDecoder {
142160
}
143161
}
144162

145-
enum _JSONNumberDecoder {
163+
private enum _JSONNumberDecoder {
146164
static func parseInteger<Integer: FixedWidthInteger>(source: UnsafeBufferPointer<UInt8>) -> Integer? {
147165
var source = source[...]
148166
let isNegative = source.first == UInt8(ascii: "-")
@@ -272,7 +290,7 @@ extension JSONMapValue {
272290
}
273291
}
274292

275-
struct JSONMapBuilder {
293+
private struct JSONMapBuilder {
276294
var mapData: [Int]
277295
init() {
278296
mapData = []
@@ -309,7 +327,7 @@ struct JSONMapBuilder {
309327
}
310328
}
311329

312-
struct JSONScanner {
330+
private struct JSONScanner {
313331
typealias Cursor = UnsafePointer<UInt8>
314332

315333
let endPtr: Cursor
@@ -496,7 +514,7 @@ struct JSONScanner {
496514
}
497515
}
498516

499-
struct JSONDecoding {
517+
private struct JSONDecoding {
500518
var value: JSONMapValue
501519
var codingPathNode: _CodingPathNode
502520
}
@@ -538,12 +556,12 @@ extension JSONDecoding: Decoder {
538556
}
539557
var userInfo: [CodingUserInfoKey : Any] { [:] }
540558

541-
struct KeyedContainer<Key: CodingKey> {
559+
fileprivate struct KeyedContainer<Key: CodingKey> {
542560
var codingPathNode: _CodingPathNode
543561
var mapping: [String: JSONMapValue]
544562
}
545563

546-
struct UnkeyedContainer {
564+
fileprivate struct UnkeyedContainer {
547565
var codingPathNode: _CodingPathNode
548566
var array: JSONMapValue.JSONArray
549567
var currentIndex: JSONMapValue.JSONArray.Index

Sources/SwiftCompilerPluginMessageHandling/JSON/JSONEncoding.swift

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
func encodeToJSON(value: some Encodable) throws -> [UInt8] {
14+
let encoder = JSONEncoding()
15+
try value.encode(to: encoder)
16+
return JSONWriter.serialize(encoder.reference ?? .null)
17+
}
118

219
/// Intermediate representation for serializing JSON structure.
3-
class JSONReference {
20+
private class JSONReference {
421
enum Backing {
522
case null
623
case trueKeyword
@@ -70,7 +87,8 @@ class JSONReference {
7087
}
7188
}
7289

73-
struct JSONWriter {
90+
/// Serialize JSONReference to [UInt8] data.
91+
private struct JSONWriter {
7492
var data: [UInt8]
7593
init() {
7694
data = []
@@ -209,7 +227,7 @@ struct JSONWriter {
209227
}
210228
}
211229

212-
class JSONEncoding {
230+
private class JSONEncoding {
213231
var reference: JSONReference?
214232
var codingPathNode: _CodingPathNode
215233

@@ -256,13 +274,13 @@ extension JSONEncoding: Encoder {
256274
}
257275
var userInfo: [CodingUserInfoKey : Any] { [:] }
258276

259-
struct KeyedContainer<Key: CodingKey> {
277+
fileprivate struct KeyedContainer<Key: CodingKey> {
260278
var encoder: JSONEncoding
261279
var reference: JSONReference
262280
var codingPathNode: _CodingPathNode
263281
}
264282

265-
struct UnkeyedContainer {
283+
fileprivate struct UnkeyedContainer {
266284
var encoder: JSONEncoding
267285
var reference: JSONReference
268286
var codingPathNode: _CodingPathNode

Sources/SwiftCompilerPluginMessageHandling/JSON/PluginJSON.swift

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)