Skip to content

Commit e9397c9

Browse files
Switch Swift SDK generation to Swift 6
1 parent 5f2a9fa commit e9397c9

33 files changed

Lines changed: 1320 additions & 702 deletions

codegen/Templates/swift/APIHelper.mustache

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
import Foundation{{#useVapor}}
88
import Vapor{{/useVapor}}
99

10-
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} struct APIHelper {
11-
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func rejectNil(_ source: [String: Any?]) -> [String: Any]? {
12-
let destination = source.reduce(into: [String: Any]()) { result, item in
10+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} struct APIHelper: Sendable {
11+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func rejectNil(_ source: [String: (any Sendable)?]) -> [String: any Sendable]? {
12+
let destination = source.reduce(into: [String: any Sendable]()) { result, item in
1313
if let value = item.value {
1414
result[item.key] = value
1515
}
@@ -21,7 +21,7 @@ import Vapor{{/useVapor}}
2121
return destination
2222
}
2323

24-
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func rejectNilHeaders(_ source: [String: Any?]) -> [String: String] {
24+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func rejectNilHeaders(_ source: [String: (any Sendable)?]) -> [String: String] {
2525
return source.reduce(into: [String: String]()) { result, item in
2626
if let collection = item.value as? [Any?] {
2727
result[item.key] = collection
@@ -33,12 +33,12 @@ import Vapor{{/useVapor}}
3333
}
3434
}
3535

36-
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func convertBoolToString(_ source: [String: Any]?) -> [String: Any]? {
36+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func convertBoolToString(_ source: [String: any Sendable]?) -> [String: any Sendable]? {
3737
guard let source = source else {
3838
return nil
3939
}
4040

41-
return source.reduce(into: [String: Any]()) { result, item in
41+
return source.reduce(into: [String: any Sendable]()) { result, item in
4242
switch item.value {
4343
case let x as Bool:
4444
result[item.key] = x.description
@@ -71,7 +71,7 @@ import Vapor{{/useVapor}}
7171
/// maps all values from source to query parameters
7272
///
7373
/// explode attribute is respected: collection values might be either joined or split up into separate key value pairs
74-
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func mapValuesToQueryItems(_ source: [String: (wrappedValue: Any?, isExplode: Bool)]) -> [URLQueryItem]? {
74+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func mapValuesToQueryItems(_ source: [String: (wrappedValue: (any Sendable)?, isExplode: Bool)]) -> [URLQueryItem]? {
7575
let destination = source.filter { $0.value.wrappedValue != nil }.reduce(into: [URLQueryItem]()) { result, item in
7676
if let collection = item.value.wrappedValue as? [Any?] {
7777
@@ -100,7 +100,7 @@ import Vapor{{/useVapor}}
100100
/// maps all values from source to query parameters
101101
///
102102
/// collection values are always exploded
103-
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func mapValuesToQueryItems(_ source: [String: Any?]) -> [URLQueryItem]? {
103+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func mapValuesToQueryItems(_ source: [String: (any Sendable)?]) -> [URLQueryItem]? {
104104
let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in
105105
if let collection = item.value as? [Any?] {
106106
collection

codegen/Templates/swift/APIs.mustache

Lines changed: 253 additions & 48 deletions
Large diffs are not rendered by default.

codegen/Templates/swift/AsposeBarcodeCloudClient.mustache

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Foundation
33
import FoundationNetworking
44
#endif
55

6-
public enum AsposeBarcodeCloudClientError: Error, CustomStringConvertible {
6+
public enum AsposeBarcodeCloudClientError: Error, CustomStringConvertible, @unchecked Sendable {
77
case missingCredentials
88
case invalidTokenURL(String)
99
case invalidTokenResponse
@@ -29,7 +29,19 @@ public enum AsposeBarcodeCloudClientError: Error, CustomStringConvertible {
2929
}
3030
}
3131

32-
public final class AsposeBarcodeCloudConfiguration {
32+
public enum AsposeBarcodeCloudAPI {
33+
public static var basePath: String {
34+
get { AsposeBarcodeCloudAPIConfiguration.shared.basePath }
35+
set { AsposeBarcodeCloudAPIConfiguration.shared.basePath = newValue }
36+
}
37+
38+
public static var customHeaders: [String: String] {
39+
get { AsposeBarcodeCloudAPIConfiguration.shared.customHeaders }
40+
set { AsposeBarcodeCloudAPIConfiguration.shared.customHeaders = newValue }
41+
}
42+
}
43+
44+
public final class AsposeBarcodeCloudConfiguration: @unchecked Sendable {
3345
public static let defaultHost = "https://api.aspose.cloud/v4.0"
3446
public static let defaultTokenURL = "https://id.aspose.cloud/connect/token"
3547
public static let defaultSdkName = "swift sdk"
@@ -90,12 +102,12 @@ public final class AsposeBarcodeCloudConfiguration {
90102
}
91103
}
92104

93-
public typealias AsposeBarcodeCloudTokenFetcher = (
105+
public typealias AsposeBarcodeCloudTokenFetcher = @Sendable (
94106
AsposeBarcodeCloudConfiguration,
95-
@escaping (Result<String, AsposeBarcodeCloudClientError>) -> Void
107+
@escaping @Sendable (Result<String, AsposeBarcodeCloudClientError>) -> Void
96108
) -> Void
97109

98-
public final class AsposeBarcodeCloudClient {
110+
public final class AsposeBarcodeCloudClient: @unchecked Sendable {
99111
public let configuration: AsposeBarcodeCloudConfiguration
100112
private let tokenFetcher: AsposeBarcodeCloudTokenFetcher
101113
@@ -143,7 +155,7 @@ public final class AsposeBarcodeCloudClient {
143155
}
144156
}
145157

146-
public func authorize(completion: @escaping (Result<String, AsposeBarcodeCloudClientError>) -> Void) {
158+
public func authorize(completion: @escaping @Sendable (Result<String, AsposeBarcodeCloudClientError>) -> Void) {
147159
if let accessToken = configuration.accessToken, !accessToken.isEmpty {
148160
apply()
149161
completion(.success(accessToken))
@@ -165,16 +177,16 @@ public final class AsposeBarcodeCloudClient {
165177
@discardableResult
166178
public func authorize() throws -> String {
167179
let semaphore = DispatchSemaphore(value: 0)
168-
var tokenResult: Result<String, AsposeBarcodeCloudClientError>?
180+
let tokenResult = OpenAPIMutex<Result<String, AsposeBarcodeCloudClientError>?>(nil)
169181
170182
authorize { result in
171-
tokenResult = result
183+
tokenResult.withValue { $0 = result }
172184
semaphore.signal()
173185
}
174186

175187
semaphore.wait()
176188

177-
switch tokenResult {
189+
switch tokenResult.value {
178190
case let .success(accessToken):
179191
return accessToken
180192
case let .failure(error):
@@ -199,7 +211,7 @@ public final class AsposeBarcodeCloudClient {
199211

200212
private static func defaultTokenFetcher(
201213
configuration: AsposeBarcodeCloudConfiguration,
202-
completion: @escaping (Result<String, AsposeBarcodeCloudClientError>) -> Void
214+
completion: @escaping @Sendable (Result<String, AsposeBarcodeCloudClientError>) -> Void
203215
) {
204216
let request: URLRequest
205217
do {
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
github "Flight-School/AnyCodable" ~> 0.6{{#useAlamofire}}
2-
github "Alamofire/Alamofire" ~> 5.7{{/useAlamofire}}{{#usePromiseKit}}
3-
github "mxcl/PromiseKit" ~> 6.15{{/usePromiseKit}}{{#useRxSwift}}
4-
github "ReactiveX/RxSwift" ~> 6.2{{/useRxSwift}}
1+
{{#useAlamofire}}
2+
github "Alamofire/Alamofire" ~> 5.10{{/useAlamofire}}{{#usePromiseKit}}
3+
github "mxcl/PromiseKit" ~> 8.1{{/usePromiseKit}}{{#useRxSwift}}
4+
github "ReactiveX/RxSwift" ~> 6.8{{/useRxSwift}}

codegen/Templates/swift/CodableHelper.mustache

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,64 @@
77

88
import Foundation
99

10-
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} class CodableHelper {
11-
private static var customDateFormatter: DateFormatter?
12-
private static var defaultDateFormatter: DateFormatter = OpenISO8601DateFormatter()
13-
14-
private static var customJSONDecoder: JSONDecoder?
15-
private static var defaultJSONDecoder: JSONDecoder = {
16-
let decoder = JSONDecoder()
17-
decoder.dateDecodingStrategy = .formatted(CodableHelper.dateFormatter)
18-
return decoder
19-
}()
20-
21-
private static var customJSONEncoder: JSONEncoder?
22-
private static var defaultJSONEncoder: JSONEncoder = {
23-
let encoder = JSONEncoder()
24-
encoder.dateEncodingStrategy = .formatted(CodableHelper.dateFormatter)
25-
encoder.outputFormatting = .prettyPrinted
26-
return encoder
27-
}()
28-
29-
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static var dateFormatter: DateFormatter {
30-
get { return customDateFormatter ?? defaultDateFormatter }
31-
set { customDateFormatter = newValue }
10+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} class CodableHelper: @unchecked Sendable {
11+
12+
// MARK: - Private state
13+
14+
private struct State {
15+
var customDateFormatter: DateFormatter?
16+
var defaultDateFormatter: DateFormatter = OpenISO8601DateFormatter()
17+
18+
var customJSONDecoder: JSONDecoder?
19+
var defaultJSONDecoder: JSONDecoder = JSONDecoder()
20+
21+
var customJSONEncoder: JSONEncoder?
22+
var defaultJSONEncoder: JSONEncoder = JSONEncoder()
23+
24+
init() {
25+
defaultJSONEncoder.outputFormatting = .prettyPrinted
26+
rebuildDefaultCoders()
27+
}
28+
29+
mutating func rebuildDefaultCoders() {
30+
defaultJSONDecoder.dateDecodingStrategy = .formatted(customDateFormatter ?? defaultDateFormatter)
31+
defaultJSONEncoder.dateEncodingStrategy = .formatted(customDateFormatter ?? defaultDateFormatter)
32+
}
33+
}
34+
35+
private let _state = OpenAPIMutex(State())
36+
37+
// MARK: - Init
38+
39+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} init() {}
40+
41+
// MARK: - Public interface
42+
43+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} var dateFormatter: DateFormatter {
44+
get { _state.withValue { $0.customDateFormatter ?? $0.defaultDateFormatter } }
45+
set {
46+
_state.withValue { state in
47+
state.customDateFormatter = newValue
48+
state.rebuildDefaultCoders()
49+
}
50+
}
3251
}
33-
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static var jsonDecoder: JSONDecoder {
34-
get { return customJSONDecoder ?? defaultJSONDecoder }
35-
set { customJSONDecoder = newValue }
52+
53+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} var jsonDecoder: JSONDecoder {
54+
get { _state.withValue { $0.customJSONDecoder ?? $0.defaultJSONDecoder } }
55+
set { _state.withValue { $0.customJSONDecoder = newValue } }
3656
}
37-
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static var jsonEncoder: JSONEncoder {
38-
get { return customJSONEncoder ?? defaultJSONEncoder }
39-
set { customJSONEncoder = newValue }
57+
58+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} var jsonEncoder: JSONEncoder {
59+
get { _state.withValue { $0.customJSONEncoder ?? $0.defaultJSONEncoder } }
60+
set { _state.withValue { $0.customJSONEncoder = newValue } }
4061
}
4162

42-
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} class func decode<T>(_ type: T.Type, from data: Data) -> Swift.Result<T, Error> where T: Decodable {
63+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} func decode<T>(_ type: T.Type, from data: Data) -> Swift.Result<T, Error> where T: Decodable {
4364
return Swift.Result { try jsonDecoder.decode(type, from: data) }
4465
}
4566

46-
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} class func encode<T>(_ value: T) -> Swift.Result<Data, Error> where T: Encodable {
67+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} func encode<T>(_ value: T) -> Swift.Result<Data, Error> where T: Encodable {
4768
return Swift.Result { try jsonEncoder.encode(value) }
4869
}
4970
}

codegen/Templates/swift/Configuration.mustache

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)