Skip to content

Commit 39acd5a

Browse files
committed
[JSON] Formatting
1 parent 3d676d0 commit 39acd5a

File tree

3 files changed

+175
-98
lines changed

3 files changed

+175
-98
lines changed

Sources/SwiftCompilerPluginMessageHandling/JSON/CodingUtilities.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ internal enum _CodingPathNode {
2020
indirect case node(CodingKey, _CodingPathNode)
2121
indirect case indexNode(Int, _CodingPathNode)
2222

23-
var path : [any CodingKey] {
23+
var path: [any CodingKey] {
2424
switch self {
2525
case .root:
2626
return []
@@ -63,7 +63,7 @@ internal enum _CodingPathNode {
6363
// Shared Key Type
6464
//===----------------------------------------------------------------------===//
6565

66-
internal enum _CodingKey : CodingKey {
66+
internal enum _CodingKey: CodingKey {
6767
case string(String)
6868
case int(Int)
6969
case index(Int)
@@ -99,4 +99,3 @@ internal enum _CodingKey : CodingKey {
9999
}
100100
}
101101
}
102-

Sources/SwiftCompilerPluginMessageHandling/JSON/JSONDecoding.swift

Lines changed: 129 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ func decodeFromJSON<T: Decodable>(json: UnsafeBufferPointer<UInt8>) throws -> T
2121

2222
private struct JSONMap {
2323
enum Descriptor: Int {
24-
case nullKeyword // [desc]
25-
case trueKeyword // [desc]
24+
case nullKeyword // [desc]
25+
case trueKeyword // [desc]
2626
case falseKeyword // [desc]
27-
case number // [desc, pointer, length]
27+
case number // [desc, pointer, length]
2828
case simpleString // [desc, pointer, length]
29-
case string // [desc, pointer, length]
30-
case object // [desc, count, (key, value)...]
31-
case array // [desc, count, element...]
29+
case string // [desc, pointer, length]
30+
case object // [desc, count, (key, value)...]
31+
case array // [desc, count, element...]
3232
}
3333
let data: [Int]
3434

@@ -78,7 +78,7 @@ private struct JSONMapValue {
7878

7979
@inline(__always)
8080
func value(at i: Index) -> JSONMapValue {
81-
return .init(map: map[i ..< index(afterValue: i)])
81+
return .init(map: map[i..<index(afterValue: i)])
8282
}
8383
}
8484

@@ -137,11 +137,11 @@ private enum _JSONStringDecoder {
137137
case UInt8(ascii: "'"): string.append("'")
138138
case UInt8(ascii: "\\"): string.append("\\")
139139
case UInt8(ascii: "/"): string.append("/")
140-
case UInt8(ascii: "b"): string.append("\u{08}") // \b
141-
case UInt8(ascii: "f"): string.append("\u{0C}") // \f
142-
case UInt8(ascii: "n"): string.append("\u{0A}") // \n
143-
case UInt8(ascii: "r"): string.append("\u{0D}") // \r
144-
case UInt8(ascii: "t"): string.append("\u{09}") // \t
140+
case UInt8(ascii: "b"): string.append("\u{08}") // \b
141+
case UInt8(ascii: "f"): string.append("\u{0C}") // \f
142+
case UInt8(ascii: "n"): string.append("\u{0A}") // \n
143+
case UInt8(ascii: "r"): string.append("\u{0D}") // \r
144+
case UInt8(ascii: "t"): string.append("\u{09}") // \t
145145
case UInt8(ascii: "u"):
146146
fatalError("unimplemented")
147147
default:
@@ -178,9 +178,10 @@ private enum _JSONNumberDecoder {
178178
guard !overflowed else {
179179
return nil
180180
}
181-
(value, overflowed) = isNegative
182-
? value.subtractingReportingOverflow(digitValue)
183-
: value.addingReportingOverflow(digitValue)
181+
(value, overflowed) =
182+
isNegative
183+
? value.subtractingReportingOverflow(digitValue)
184+
: value.addingReportingOverflow(digitValue)
184185
guard !overflowed else {
185186
return nil
186187
}
@@ -312,14 +313,14 @@ private struct JSONMapBuilder {
312313
mutating func startCollection(_ descriptor: JSONMap.Descriptor) -> Int {
313314
let handle = mapData.count
314315
mapData.append(descriptor.rawValue)
315-
mapData.append(0) // Count, this will be updated in closeCollection()
316+
mapData.append(0) // Count, this will be updated in closeCollection()
316317
return handle
317318
}
318319

319320
mutating func closeCollection(handle: Int) {
320321
// 'handle': descriptor index.
321322
// 'handle+1': counter index.
322-
mapData[handle+1] = mapData.count - handle - 2
323+
mapData[handle + 1] = mapData.count - handle - 2
323324
}
324325

325326
func finalize() -> JSONMap {
@@ -430,21 +431,21 @@ private struct JSONScanner {
430431
_ = try advance()
431432
}
432433
try expect("\"")
433-
map.record(hasEscape ? .string : .simpleString, range: start ..< ptr)
434+
map.record(hasEscape ? .string : .simpleString, range: start..<ptr)
434435
}
435436

436437
mutating func scanNumber(start: Cursor) throws {
437438
ptr = start
438439
_ = advance(if: "-")
439-
while advance(if: "0" ... "9") {}
440+
while advance(if: "0"..."9") {}
440441
if advance(if: ".") {
441-
while advance(if: "0" ... "9") {}
442+
while advance(if: "0"..."9") {}
442443
}
443444
if advance(if: "e") || advance(if: "E") {
444445
_ = advance(if: "-") || advance(if: "+")
445-
while advance(if: "0" ... "9") {}
446+
while advance(if: "0"..."9") {}
446447
}
447-
map.record(.number, range: start ..< ptr)
448+
map.record(.number, range: start..<ptr)
448449
}
449450

450451
mutating func scanObject() throws {
@@ -496,7 +497,7 @@ private struct JSONScanner {
496497
try scanFalse()
497498
case UInt8(ascii: "\""):
498499
try scanString(start: start)
499-
case UInt8(ascii: "-"), UInt8(ascii: "0") ... UInt8(ascii: "9"):
500+
case UInt8(ascii: "-"), UInt8(ascii: "0")...UInt8(ascii: "9"):
500501
try scanNumber(start: start)
501502
case UInt8(ascii: "{"):
502503
try scanObject()
@@ -526,33 +527,65 @@ private struct JSONDecoding {
526527

527528
extension JSONDecoding {
528529
@inline(__always)
529-
static func _unwrapOrThrow<T>(_ v: T?, codingPathNode: _CodingPathNode, _ additionalKey: (some CodingKey)?) throws -> T {
530+
static func _unwrapOrThrow<T>(
531+
_ v: T?,
532+
codingPathNode: _CodingPathNode,
533+
_ additionalKey: (some CodingKey)?
534+
) throws -> T {
530535
guard let v = v else {
531-
throw DecodingError.typeMismatch(T.self, .init(
532-
codingPath: codingPathNode.path(byAppending: additionalKey),
533-
debugDescription: "type mismatch"
534-
))
536+
throw DecodingError.typeMismatch(
537+
T.self,
538+
.init(
539+
codingPath: codingPathNode.path(byAppending: additionalKey),
540+
debugDescription: "type mismatch"
541+
)
542+
)
535543
}
536544
return v
537545
}
538546
@inline(__always)
539-
static func _decode(_ value: JSONMapValue, as _: Bool.Type, codingPathNode: _CodingPathNode, _ additionalKey: (some CodingKey)?) throws -> Bool {
547+
static func _decode(
548+
_ value: JSONMapValue,
549+
as _: Bool.Type,
550+
codingPathNode: _CodingPathNode,
551+
_ additionalKey: (some CodingKey)?
552+
) throws -> Bool {
540553
try _unwrapOrThrow(value.asBool, codingPathNode: codingPathNode, additionalKey)
541554
}
542555
@inline(__always)
543-
static func _decode(_ value: JSONMapValue, as _: String.Type, codingPathNode: _CodingPathNode, _ additionalKey: (some CodingKey)?) throws -> String {
556+
static func _decode(
557+
_ value: JSONMapValue,
558+
as _: String.Type,
559+
codingPathNode: _CodingPathNode,
560+
_ additionalKey: (some CodingKey)?
561+
) throws -> String {
544562
try _unwrapOrThrow(value.asString, codingPathNode: codingPathNode, additionalKey)
545563
}
546564
@inline(__always)
547-
static func _decode<Integer: FixedWidthInteger>(_ value: JSONMapValue, as type: Integer.Type, codingPathNode: _CodingPathNode, _ additionalKey: (some CodingKey)?) throws -> Integer {
565+
static func _decode<Integer: FixedWidthInteger>(
566+
_ value: JSONMapValue,
567+
as type: Integer.Type,
568+
codingPathNode: _CodingPathNode,
569+
_ additionalKey: (some CodingKey)?
570+
) throws -> Integer {
548571
try _unwrapOrThrow(value.asInteger(type), codingPathNode: codingPathNode, additionalKey)
549572
}
550573
@inline(__always)
551-
static func _decode<Floating: BinaryFloatingPoint>(_ value: JSONMapValue, as type: Floating.Type, codingPathNode: _CodingPathNode, _ additionalKey: (some CodingKey)?) throws -> Floating {
574+
static func _decode<Floating: BinaryFloatingPoint>(
575+
_ value: JSONMapValue,
576+
as type: Floating.Type,
577+
codingPathNode: _CodingPathNode,
578+
_ additionalKey: (some CodingKey)?
579+
) throws -> Floating {
552580
try _unwrapOrThrow(value.asFloatingPoint(type), codingPathNode: codingPathNode, additionalKey)
553581
}
554582

555-
static func _decodeGeneric<T: Decodable>(_ value: JSONMapValue, as type: T.Type, codingPathNode: _CodingPathNode, _ additionalKey: (some CodingKey)?) throws -> T {
583+
static func _decodeGeneric<T: Decodable>(
584+
_ value: JSONMapValue,
585+
as type: T.Type,
586+
codingPathNode: _CodingPathNode,
587+
_ additionalKey: (some CodingKey)?
588+
) throws -> T {
556589
let decoder = Self(value: value, codingPathNode: codingPathNode.appending(additionalKey))
557590
return try T.init(from: decoder)
558591
}
@@ -562,7 +595,7 @@ extension JSONDecoding: Decoder {
562595
var codingPath: [any CodingKey] {
563596
codingPathNode.path
564597
}
565-
var userInfo: [CodingUserInfoKey : Any] { [:] }
598+
var userInfo: [CodingUserInfoKey: Any] { [:] }
566599

567600
fileprivate struct KeyedContainer<Key: CodingKey> {
568601
var codingPathNode: _CodingPathNode
@@ -577,10 +610,12 @@ extension JSONDecoding: Decoder {
577610
}
578611

579612
func container<Key: CodingKey>(keyedBy type: Key.Type) throws -> KeyedDecodingContainer<Key> {
580-
return try KeyedDecodingContainer(KeyedContainer<Key>(
581-
value: value,
582-
codingPathNode: codingPathNode
583-
))
613+
return try KeyedDecodingContainer(
614+
KeyedContainer<Key>(
615+
value: value,
616+
codingPathNode: codingPathNode
617+
)
618+
)
584619
}
585620

586621
func unkeyedContainer() throws -> any UnkeyedDecodingContainer {
@@ -656,7 +691,7 @@ extension JSONDecoding: SingleValueDecodingContainer {
656691
try JSONDecoding._decode(value, as: type, codingPathNode: codingPathNode, _CodingKey?.none)
657692
}
658693

659-
func decode<T>(_ type: T.Type) throws -> T where T : Decodable {
694+
func decode<T>(_ type: T.Type) throws -> T where T: Decodable {
660695
try JSONDecoding._decodeGeneric(value, as: type, codingPathNode: codingPathNode, _CodingKey?.none)
661696
}
662697
}
@@ -677,9 +712,13 @@ extension JSONDecoding.KeyedContainer: KeyedDecodingContainerProtocol {
677712
@inline(__always)
678713
func _getOrThrow(forKey key: Key) throws -> JSONMapValue {
679714
guard let value = mapping[key.stringValue] else {
680-
throw DecodingError.keyNotFound(key, .init(
681-
codingPath: codingPathNode.path,
682-
debugDescription: "No value associated with key \(key) (\"\(key.stringValue)\")."))
715+
throw DecodingError.keyNotFound(
716+
key,
717+
.init(
718+
codingPath: codingPathNode.path,
719+
debugDescription: "No value associated with key \(key) (\"\(key.stringValue)\")."
720+
)
721+
)
683722
}
684723
return value
685724
}
@@ -748,11 +787,16 @@ extension JSONDecoding.KeyedContainer: KeyedDecodingContainerProtocol {
748787
try JSONDecoding._decodeGeneric(_getOrThrow(forKey: key), as: type, codingPathNode: codingPathNode, key)
749788
}
750789

751-
func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type, forKey key: Key) throws -> KeyedDecodingContainer<NestedKey> where NestedKey : CodingKey {
752-
return try KeyedDecodingContainer(JSONDecoding.KeyedContainer<NestedKey>(
753-
value: try _getOrThrow(forKey: key),
754-
codingPathNode: codingPathNode.appending(key)
755-
))
790+
func nestedContainer<NestedKey: CodingKey>(
791+
keyedBy keyType: NestedKey.Type,
792+
forKey key: Key
793+
) throws -> KeyedDecodingContainer<NestedKey> {
794+
return try KeyedDecodingContainer(
795+
JSONDecoding.KeyedContainer<NestedKey>(
796+
value: try _getOrThrow(forKey: key),
797+
codingPathNode: codingPathNode.appending(key)
798+
)
799+
)
756800
}
757801

758802
func nestedUnkeyedContainer(forKey key: Key) throws -> any UnkeyedDecodingContainer {
@@ -772,20 +816,26 @@ extension JSONDecoding.KeyedContainer: KeyedDecodingContainerProtocol {
772816

773817
init(value: JSONMapValue, codingPathNode: _CodingPathNode) throws {
774818
guard value.isObject else {
775-
throw DecodingError.typeMismatch([String: Any].self, .init(
776-
codingPath: codingPathNode.path,
777-
debugDescription: "not an array"
778-
))
819+
throw DecodingError.typeMismatch(
820+
[String: Any].self,
821+
.init(
822+
codingPath: codingPathNode.path,
823+
debugDescription: "not an array"
824+
)
825+
)
779826
}
780827
self.codingPathNode = codingPathNode
781828
self.mapping = [:]
782829
var iter = value.makeObjectIterator()!
783830
while let elem = iter.next() {
784831
guard let keyStr = elem.key.asString else {
785-
throw DecodingError.typeMismatch(String.self, .init(
786-
codingPath: codingPathNode.path,
787-
debugDescription: "expected a string as a key"
788-
))
832+
throw DecodingError.typeMismatch(
833+
String.self,
834+
.init(
835+
codingPath: codingPathNode.path,
836+
debugDescription: "expected a string as a key"
837+
)
838+
)
789839
}
790840
self.mapping[keyStr] = elem.value
791841
}
@@ -815,10 +865,13 @@ extension JSONDecoding.UnkeyedContainer: UnkeyedDecodingContainer {
815865
mutating func _getOrThrow() throws -> (index: any CodingKey, value: JSONMapValue) {
816866
let idx = currentIndex
817867
guard !isAtEnd else {
818-
throw DecodingError.valueNotFound(Any.self, .init(
819-
codingPath: codingPathNode.path(byAppendingIndex: idx),
820-
debugDescription: "Unkeyed container is at end"
821-
))
868+
throw DecodingError.valueNotFound(
869+
Any.self,
870+
.init(
871+
codingPath: codingPathNode.path(byAppendingIndex: idx),
872+
debugDescription: "Unkeyed container is at end"
873+
)
874+
)
822875
}
823876
let value = array[_currMapIdx]
824877
advanceToNextValue()
@@ -904,17 +957,21 @@ extension JSONDecoding.UnkeyedContainer: UnkeyedDecodingContainer {
904957
try _decodeInteger(type)
905958
}
906959

907-
mutating func decode<T>(_ type: T.Type) throws -> T where T : Decodable {
960+
mutating func decode<T>(_ type: T.Type) throws -> T where T: Decodable {
908961
let (idx, value) = try _getOrThrow()
909962
return try JSONDecoding._decodeGeneric(value, as: type, codingPathNode: codingPathNode, idx)
910963
}
911964

912-
mutating func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type) throws -> KeyedDecodingContainer<NestedKey> where NestedKey : CodingKey {
965+
mutating func nestedContainer<NestedKey: CodingKey>(
966+
keyedBy keyType: NestedKey.Type
967+
) throws -> KeyedDecodingContainer<NestedKey> {
913968
let (idx, value) = try _getOrThrow()
914-
return try KeyedDecodingContainer(JSONDecoding.KeyedContainer<NestedKey>(
915-
value: value,
916-
codingPathNode: codingPathNode.appending(idx)
917-
))
969+
return try KeyedDecodingContainer(
970+
JSONDecoding.KeyedContainer<NestedKey>(
971+
value: value,
972+
codingPathNode: codingPathNode.appending(idx)
973+
)
974+
)
918975
}
919976

920977
mutating func nestedUnkeyedContainer() throws -> any UnkeyedDecodingContainer {
@@ -931,10 +988,13 @@ extension JSONDecoding.UnkeyedContainer: UnkeyedDecodingContainer {
931988

932989
init(value: JSONMapValue, codingPathNode: _CodingPathNode) throws {
933990
guard value.isArray else {
934-
throw DecodingError.typeMismatch([Any].self, .init(
935-
codingPath: codingPathNode.path,
936-
debugDescription: "not an array"
937-
))
991+
throw DecodingError.typeMismatch(
992+
[Any].self,
993+
.init(
994+
codingPath: codingPathNode.path,
995+
debugDescription: "not an array"
996+
)
997+
)
938998
}
939999
self.codingPathNode = codingPathNode
9401000
self.currentIndex = 0

0 commit comments

Comments
 (0)