|
8 | 8 | // See https://swift.org/CONTRIBUTORS.txt for Swift project authors
|
9 | 9 | //
|
10 | 10 |
|
| 11 | +#if canImport(Foundation) |
| 12 | +private import Foundation |
| 13 | +#endif |
| 14 | + |
11 | 15 | extension ABI {
|
12 | 16 | /// A type implementing the JSON encoding of ``Attachment`` for the ABI entry
|
13 | 17 | /// point and event stream output.
|
14 | 18 | ///
|
15 | 19 | /// This type is not part of the public interface of the testing library. It
|
16 | 20 | /// assists in converting values to JSON; clients that consume this JSON are
|
17 | 21 | /// expected to write their own decoders.
|
18 |
| - /// |
19 |
| - /// - Warning: Attachments are not yet part of the JSON schema. |
20 | 22 | struct EncodedAttachment<V>: Sendable where V: ABI.Version {
|
21 | 23 | /// The path where the attachment was written.
|
22 | 24 | var path: String?
|
23 | 25 |
|
| 26 | + /// The preferred name of the attachment. |
| 27 | + /// |
| 28 | + /// - Warning: Attachments' preferred names are not yet part of the JSON |
| 29 | + /// schema. |
| 30 | + var _preferredName: String? |
| 31 | + |
| 32 | + /// The raw content of the attachment, if available. |
| 33 | + /// |
| 34 | + /// The value of this property is set if the attachment was not first saved |
| 35 | + /// to a file. It may also be `nil` if an error occurred while trying to get |
| 36 | + /// the original attachment's serialized representation. |
| 37 | + /// |
| 38 | + /// - Warning: Inline attachment content is not yet part of the JSON schema. |
| 39 | + var _bytes: Bytes? |
| 40 | + |
| 41 | + /// The source location where this attachment was created. |
| 42 | + /// |
| 43 | + /// - Warning: Attachment source locations are not yet part of the JSON |
| 44 | + /// schema. |
| 45 | + var _sourceLocation: SourceLocation? |
| 46 | + |
24 | 47 | init(encoding attachment: borrowing Attachment<AnyAttachable>, in eventContext: borrowing Event.Context) {
|
25 | 48 | path = attachment.fileSystemPath
|
| 49 | + |
| 50 | + if V.versionNumber >= ABI.v6_3.versionNumber { |
| 51 | + _preferredName = attachment.preferredName |
| 52 | + |
| 53 | + if path == nil { |
| 54 | + _bytes = try? attachment.withUnsafeBytes { bytes in |
| 55 | + return Bytes(rawValue: [UInt8](bytes)) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + _sourceLocation = attachment.sourceLocation |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /// A structure representing the bytes of an attachment. |
| 64 | + struct Bytes: Sendable, RawRepresentable { |
| 65 | + var rawValue: [UInt8] |
26 | 66 | }
|
27 | 67 | }
|
28 | 68 | }
|
29 | 69 |
|
30 | 70 | // MARK: - Codable
|
31 | 71 |
|
32 | 72 | extension ABI.EncodedAttachment: Codable {}
|
| 73 | + |
| 74 | +extension ABI.EncodedAttachment.Bytes: Codable { |
| 75 | + func encode(to encoder: any Encoder) throws { |
| 76 | +#if canImport(Foundation) |
| 77 | + // If possible, encode this structure as Base64 data. |
| 78 | + try rawValue.withUnsafeBytes { rawValue in |
| 79 | + let data = Data(bytesNoCopy: .init(mutating: rawValue.baseAddress!), count: rawValue.count, deallocator: .none) |
| 80 | + var container = encoder.singleValueContainer() |
| 81 | + try container.encode(data) |
| 82 | + } |
| 83 | +#else |
| 84 | + // Otherwise, it's an array of integers. |
| 85 | + var container = encoder.singleValueContainer() |
| 86 | + try container.encode(rawValue) |
| 87 | +#endif |
| 88 | + } |
| 89 | + |
| 90 | + init(from decoder: any Decoder) throws { |
| 91 | + let container = try decoder.singleValueContainer() |
| 92 | + |
| 93 | +#if canImport(Foundation) |
| 94 | + // If possible, decode a whole Foundation Data object. |
| 95 | + if let data = try? container.decode(Data.self) { |
| 96 | + self.init(rawValue: [UInt8](data)) |
| 97 | + return |
| 98 | + } |
| 99 | +#endif |
| 100 | + |
| 101 | + // Fall back to trying to decode an array of integers. |
| 102 | + let bytes = try container.decode([UInt8].self) |
| 103 | + self.init(rawValue: bytes) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +// MARK: - Attachable |
| 108 | + |
| 109 | +extension ABI.EncodedAttachment: Attachable { |
| 110 | + var estimatedAttachmentByteCount: Int? { |
| 111 | + _bytes?.rawValue.count |
| 112 | + } |
| 113 | + |
| 114 | + /// An error type that is thrown when ``ABI/EncodedAttachment`` cannot satisfy |
| 115 | + /// a request for the underlying attachment's bytes. |
| 116 | + fileprivate struct BytesUnavailableError: Error {} |
| 117 | + |
| 118 | + borrowing func withUnsafeBytes<R>(for attachment: borrowing Attachment<Self>, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R { |
| 119 | + if let bytes = _bytes?.rawValue { |
| 120 | + return try bytes.withUnsafeBytes(body) |
| 121 | + } |
| 122 | + |
| 123 | +#if !SWT_NO_FILE_IO |
| 124 | + guard let path else { |
| 125 | + throw BytesUnavailableError() |
| 126 | + } |
| 127 | +#if canImport(Foundation) |
| 128 | + // Leverage Foundation's file-mapping logic since we're using Data anyway. |
| 129 | + let url = URL(fileURLWithPath: path, isDirectory: false) |
| 130 | + let bytes = try Data(contentsOf: url, options: [.mappedIfSafe]) |
| 131 | +#else |
| 132 | + let fileHandle = try FileHandle(forReadingAtPath: path) |
| 133 | + let bytes = try fileHandle.readToEnd() |
| 134 | +#endif |
| 135 | + return try bytes.withUnsafeBytes(body) |
| 136 | +#else |
| 137 | + // Cannot read the attachment from disk on this platform. |
| 138 | + throw BytesUnavailableError() |
| 139 | +#endif |
| 140 | + } |
| 141 | + |
| 142 | + borrowing func preferredName(for attachment: borrowing Attachment<Self>, basedOn suggestedName: String) -> String { |
| 143 | + _preferredName ?? suggestedName |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +extension ABI.EncodedAttachment.BytesUnavailableError: CustomStringConvertible { |
| 148 | + var description: String { |
| 149 | + "The attachment's content could not be deserialized." |
| 150 | + } |
| 151 | +} |
0 commit comments