Skip to content

Commit 8db4ffc

Browse files
committed
Add conditional compilation for Date.ISO8601FormatStyle
1 parent 884f904 commit 8db4ffc

File tree

1 file changed

+56
-21
lines changed

1 file changed

+56
-21
lines changed

Sources/SwiftkubeClient/Client/KubernetesClient.swift

+56-21
Original file line numberDiff line numberDiff line change
@@ -43,31 +43,66 @@ public class KubernetesClient {
4343
internal let httpClient: HTTPClient
4444
internal let logger: Logger
4545

46-
internal let jsonDecoder: JSONDecoder = {
47-
let timeFormatter = Date.ISO8601FormatStyle.iso8601
48-
let microTimeFormatter = Date.ISO8601FormatStyle.iso8601.time(includingFractionalSeconds: true)
49-
50-
let jsonDecoder = JSONDecoder()
51-
jsonDecoder.dateDecodingStrategy = .custom { decoder -> Date in
52-
let string = try decoder.singleValueContainer().decode(String.self)
53-
54-
if let date = try? timeFormatter.parse(string) {
55-
return date
46+
#if compiler(>=6.0)
47+
internal let jsonDecoder: JSONDecoder = {
48+
let timeFormatter = Date.ISO8601FormatStyle.iso8601
49+
let microTimeFormatter = Date.ISO8601FormatStyle.iso8601.time(includingFractionalSeconds: true)
50+
let jsonDecoder = JSONDecoder()
51+
jsonDecoder.dateDecodingStrategy = .custom { decoder -> Date in
52+
let string = try decoder.singleValueContainer().decode(String.self)
53+
54+
if let date = try? timeFormatter.parse(string) {
55+
return date
56+
}
57+
58+
if let date = try? microTimeFormatter.parse(string) {
59+
return date
60+
}
61+
62+
let context = DecodingError.Context(
63+
codingPath: decoder.codingPath,
64+
debugDescription: "Expected date string to be either ISO8601 or ISO8601 with milliseconds."
65+
)
66+
throw DecodingError.dataCorrupted(context)
5667
}
5768

58-
if let date = try? microTimeFormatter.parse(string) {
59-
return date
69+
return jsonDecoder
70+
}()
71+
#else
72+
internal let jsonDecoder: JSONDecoder = {
73+
let timeFormatter: ISO8601DateFormatter = {
74+
let formatter = ISO8601DateFormatter()
75+
formatter.formatOptions = .withInternetDateTime
76+
return formatter
77+
}()
78+
79+
let microTimeFormatter = {
80+
let formatter = ISO8601DateFormatter()
81+
formatter.formatOptions = .withFractionalSeconds
82+
return formatter
83+
}()
84+
let jsonDecoder = JSONDecoder()
85+
jsonDecoder.dateDecodingStrategy = .custom { decoder -> Date in
86+
let string = try decoder.singleValueContainer().decode(String.self)
87+
88+
if let date = timeFormatter.date(from: string) {
89+
return date
90+
}
91+
92+
if let date = microTimeFormatter.date(from: string) {
93+
return date
94+
}
95+
96+
let context = DecodingError.Context(
97+
codingPath: decoder.codingPath,
98+
debugDescription: "Expected date string to be either ISO8601 or ISO8601 with milliseconds."
99+
)
100+
throw DecodingError.dataCorrupted(context)
60101
}
61102

62-
let context = DecodingError.Context(
63-
codingPath: decoder.codingPath,
64-
debugDescription: "Expected date string to be either ISO8601 or ISO8601 with milliseconds."
65-
)
66-
throw DecodingError.dataCorrupted(context)
67-
}
68-
69-
return jsonDecoder
70-
}()
103+
return jsonDecoder
104+
}()
105+
#endif
71106

72107
/// Create a new instance of the Kubernetes client.
73108
///

0 commit comments

Comments
 (0)