|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftOpenAPIGenerator open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import Foundation |
| 16 | + |
| 17 | +/// Provides a route to encode or decode base64-encoded data |
| 18 | +/// |
| 19 | +/// This type holds raw, unencoded, data as a slice of bytes. It can be used to encode that |
| 20 | +/// data to a provided `Encoder` as base64-encoded data or to decode from base64 encoding when |
| 21 | +/// initialized from a decoder. |
| 22 | +/// |
| 23 | +/// There is a convenience initializer to create an instance backed by provided data in the form |
| 24 | +/// of a slice of bytes: |
| 25 | +/// ```swift |
| 26 | +/// let bytes: ArraySlice<UInt8> = ... |
| 27 | +/// let base64EncodedData = Base64EncodedData(data: bytes) |
| 28 | +/// ``` |
| 29 | +/// |
| 30 | +/// To decode base64-encoded data it is possible to call the initializer directly, providing a decoder: |
| 31 | +/// ```swift |
| 32 | +/// let base64EncodedData = Base64EncodedData(from: decoder) |
| 33 | +///``` |
| 34 | +/// |
| 35 | +/// However more commonly the decoding initializer would be called by a decoder, for example: |
| 36 | +/// ```swift |
| 37 | +/// let encodedData: Data = ... |
| 38 | +/// let decoded = try JSONDecoder().decode(Base64EncodedData.self, from: encodedData) |
| 39 | +///``` |
| 40 | +/// |
| 41 | +/// Once an instance is holding data, it may be base64 encoded to a provided encoder: |
| 42 | +/// ```swift |
| 43 | +/// let bytes: ArraySlice<UInt8> = ... |
| 44 | +/// let base64EncodedData = Base64EncodedData(data: bytes) |
| 45 | +/// base64EncodedData.encode(to: encoder) |
| 46 | +/// ``` |
| 47 | +/// |
| 48 | +/// However more commonly it would be called by an encoder, for example: |
| 49 | +/// ```swift |
| 50 | +/// let bytes: ArraySlice<UInt8> = ... |
| 51 | +/// let encodedData = JSONEncoder().encode(encodedBytes) |
| 52 | +/// ``` |
| 53 | +public struct Base64EncodedData: Sendable, Hashable { |
| 54 | + /// A container of the raw bytes. |
| 55 | + public var data: ArraySlice<UInt8> |
| 56 | + |
| 57 | + /// Initializes an instance of ``Base64EncodedData`` wrapping the provided slice of bytes. |
| 58 | + /// - Parameter data: The underlying bytes to wrap. |
| 59 | + public init(data: ArraySlice<UInt8>) { |
| 60 | + self.data = data |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +extension Base64EncodedData: Codable { |
| 65 | + public init(from decoder: any Decoder) throws { |
| 66 | + let container = try decoder.singleValueContainer() |
| 67 | + let base64EncodedString = try container.decode(String.self) |
| 68 | + |
| 69 | + // permissive decoding |
| 70 | + let options = Data.Base64DecodingOptions.ignoreUnknownCharacters |
| 71 | + |
| 72 | + guard let data = Data(base64Encoded: base64EncodedString, options: options) else { |
| 73 | + throw RuntimeError.invalidBase64String(base64EncodedString) |
| 74 | + } |
| 75 | + self.init(data: ArraySlice(data)) |
| 76 | + } |
| 77 | + |
| 78 | + public func encode(to encoder: any Encoder) throws { |
| 79 | + var container = encoder.singleValueContainer() |
| 80 | + |
| 81 | + // https://datatracker.ietf.org/doc/html/rfc4648#section-3.1 |
| 82 | + // "Implementations MUST NOT add line feeds to base-encoded data unless |
| 83 | + // the specification referring to this document explicitly directs base |
| 84 | + // encoders to add line feeds after a specific number of characters." |
| 85 | + let options = Data.Base64EncodingOptions() |
| 86 | + |
| 87 | + let base64String = Data(data).base64EncodedString(options: options) |
| 88 | + try container.encode(base64String) |
| 89 | + } |
| 90 | +} |
0 commit comments