Skip to content

Commit a8c8212

Browse files
committed
docs
1 parent dffa64c commit a8c8212

File tree

1 file changed

+59
-2
lines changed

1 file changed

+59
-2
lines changed

Sources/OpenAPIRuntime/Base/Base64EncodedData.swift

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,60 @@
1414

1515
import Foundation
1616

17-
public struct Base64EncodedData: Sendable, Codable, Hashable {
18-
var data: ArraySlice<UInt8>
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 e.g.
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 e.g.
49+
/// ```swift
50+
/// let bytes: ArraySlice<UInt8> = ...
51+
/// let encodedData = JSONEncoder().encode(encodedBytes)
52+
/// ```
53+
public struct Base64EncodedData: Sendable, Hashable {
54+
/// data to be encoded
55+
public var data: ArraySlice<UInt8>
1956

57+
/// Initializes an instance of ``Base64EncodedData`` wrapping the provided slice of bytes.
58+
/// - Parameter data: The underlying bytes to wrap.
2059
public init(data: ArraySlice<UInt8>) {
2160
self.data = data
2261
}
62+
}
2363

64+
extension Base64EncodedData: Codable {
65+
/// Creates a new instance by decoding from the given decoder.
66+
///
67+
/// This initializer throws an error if reading from the decoder fails, or
68+
/// if the data read is corrupted or otherwise invalid.
69+
///
70+
/// - Parameter decoder: The decoder to read data from.
2471
public init(from decoder: any Decoder) throws {
2572
let container = try decoder.singleValueContainer()
2673
let base64EncodedString = try container.decode(String.self)
@@ -34,6 +81,16 @@ public struct Base64EncodedData: Sendable, Codable, Hashable {
3481
self.init(data: ArraySlice(data))
3582
}
3683

84+
85+
/// Encodes this value into the given encoder.
86+
///
87+
/// If the value fails to encode anything, `encoder` will encode an empty
88+
/// keyed container in its place.
89+
///
90+
/// This function throws an error if any values are invalid for the given
91+
/// encoder's format.
92+
///
93+
/// - Parameter encoder: The encoder to write data to.
3794
public func encode(to encoder: any Encoder) throws {
3895
var container = encoder.singleValueContainer()
3996

0 commit comments

Comments
 (0)