Skip to content

Commit ec69fc1

Browse files
committed
ContainerRegistry: Factor out common JSONEncoder configuration
1 parent df73b53 commit ec69fc1

File tree

3 files changed

+13
-22
lines changed

3 files changed

+13
-22
lines changed

Sources/ContainerRegistry/ImageSource.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
import struct Foundation.Data
16+
import class Foundation.JSONEncoder
17+
18+
/// Create a JSONEncoder configured according to the requirements of the image specification.
19+
func containerJSONEncoder() -> JSONEncoder {
20+
let encoder = JSONEncoder()
21+
encoder.outputFormatting = [.sortedKeys, .prettyPrinted, .withoutEscapingSlashes]
22+
encoder.dateEncodingStrategy = .iso8601
23+
return encoder
24+
}
1625

1726
/// A source, such as a registry, from which container images can be fetched.
1827
public protocol ImageSource {

Sources/ContainerRegistry/RegistryClient.swift

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,37 +66,21 @@ public struct RegistryClient {
6666
/// - registry: HTTP URL of the registry's API endpoint.
6767
/// - client: HTTPClient object to use to connect to the registry.
6868
/// - auth: An authentication handler which can provide authentication credentials.
69-
/// - encoder: JSONEncoder to use when encoding messages to the registry.
70-
/// - decoder: JSONDecoder to use when decoding messages from the registry.
7169
/// - Throws: If the registry name is invalid.
7270
/// - Throws: If a connection to the registry cannot be established.
7371
public init(
7472
registry: URL,
7573
client: HTTPClient,
76-
auth: AuthHandler? = nil,
77-
encodingWith encoder: JSONEncoder? = nil,
78-
decodingWith decoder: JSONDecoder? = nil
74+
auth: AuthHandler? = nil
7975
) async throws {
8076
registryURL = registry
8177
self.client = client
8278
self.auth = auth
8379

8480
// The registry server does not normalize JSON and calculates digests over the raw message text.
8581
// We must use consistent encoder settings when encoding and calculating digests.
86-
//
87-
// We must also configure the date encoding strategy otherwise the dates are printed as
88-
// fractional numbers of seconds, whereas the container image requires ISO8601.
89-
if let encoder {
90-
self.encoder = encoder
91-
} else {
92-
self.encoder = JSONEncoder()
93-
self.encoder.outputFormatting = [.sortedKeys, .prettyPrinted, .withoutEscapingSlashes]
94-
self.encoder.dateEncodingStrategy = .iso8601
95-
}
96-
97-
// No special configuration is required for the decoder, but we should use a single instance
98-
// rather than creating new instances where we need them.
99-
self.decoder = decoder ?? JSONDecoder()
82+
self.encoder = containerJSONEncoder()
83+
self.decoder = JSONDecoder()
10084

10185
// Verify that we can talk to the registry
10286
self.authChallenge = try await RegistryClient.checkAPI(client: self.client, registryURL: self.registryURL)

Sources/ContainerRegistry/ScratchImage.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ public struct ScratchImage {
2525
public init(architecture: String, os: String) {
2626
self.architecture = architecture
2727
self.os = os
28-
self.encoder = JSONEncoder()
29-
self.encoder.outputFormatting = [.sortedKeys, .prettyPrinted, .withoutEscapingSlashes]
30-
self.encoder.dateEncodingStrategy = .iso8601
28+
self.encoder = containerJSONEncoder()
3129
}
3230
}
3331

0 commit comments

Comments
 (0)