Skip to content

Commit

Permalink
Merge pull request #4 from nerzh/async_await
Browse files Browse the repository at this point in the history
Test support swift Async/Await + UP 1.41.1
  • Loading branch information
nerzh authored Mar 19, 2023
2 parents ec9f542 + 5eb8490 commit 7bb67f5
Show file tree
Hide file tree
Showing 16 changed files with 3,377 additions and 45 deletions.
62 changes: 62 additions & 0 deletions ApiParser/Sources/ApiParser/CodeGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,65 @@ class CodeGenerator {

return result
}

// public func get_endpoints() async throws -> TSDKResultOfGetEndpoints {
// try await withCheckedThrowingContinuation { continuation in
// do {
// let method: String = "get_endpoints"
// try binding.requestLibraryAsync(methodName(module, method), "") { (requestId, params, responseType, finished) in
// var response: TSDKBindingResponse<TSDKResultOfGetEndpoints, TSDKClientError> = .init()
// response.update(requestId, params, responseType, finished)
// if let error = response.error {
// continuation.resume(throwing: error)
// } else if let result = response.result {
// continuation.resume(returning: result)
// } else {
// continuation.resume(throwing: TSDKClientError("Nothing for return"))
// }
// }
// } catch {
// continuation.resume(throwing: error)
// }
// }
// }

private func generateAsyncAwaitFunction(_ swiftFunction: SDKSwiftFunction) -> String {
var result: String = .init()
if let summary = swiftFunction.summary { result.append("\(tab)/// \(checkComment(summary))\n") }
if let description = swiftFunction.description { result.append("\(tab)/// \(checkComment(description))\n") }
result.append("\(tab)\(swiftFunction.accessType) func \(swiftFunction.name)(")
for parameter in swiftFunction.params {
result.append("_ \(parameter.name): \(parameter.type)")
}
let resultType: String = swiftFunction.willReturn.type == "Void" ? "\(libPrefix)NoneResult" : swiftFunction.willReturn.type
result.append(") async throws -> \(resultType) {\n")
let methodName: String = swiftFunction.name == "initialize" ? "init" : swiftFunction.name
result.append("\(tab)\(tab)try await withCheckedThrowingContinuation { continuation in\n")
result.append("\(tab)\(tab)\(tab)do {\n")
result.append("\(tab)\(tab)\(tab)\(tab)let method: String = \"\(methodName)\"\n")
if swiftFunction.params.isEmpty {
result.append("\(tab)\(tab)\(tab)\(tab)try binding.requestLibraryAsyncAwait(methodName(module, method), \"\") { (requestId, params, responseType, finished) in\n")
} else {
result.append("\(tab)\(tab)\(tab)\(tab)try binding.requestLibraryAsyncAwait(methodName(module, method), payload) { (requestId, params, responseType, finished) in\n")
}
result.append("\(tab)\(tab)\(tab)\(tab)\(tab)var response: TSDKBindingResponse<\(resultType), \(libPrefix)ClientError> = .init()\n")
result.append("\(tab)\(tab)\(tab)\(tab)\(tab)response.update(requestId, params, responseType, finished)\n")
result.append("\(tab)\(tab)\(tab)\(tab)\(tab)if let error = response.error {\n")
result.append("\(tab)\(tab)\(tab)\(tab)\(tab)\(tab)continuation.resume(throwing: error)\n")
result.append("\(tab)\(tab)\(tab)\(tab)\(tab)} else if let result = response.result {\n")
result.append("\(tab)\(tab)\(tab)\(tab)\(tab)\(tab)continuation.resume(returning: result)\n")
result.append("\(tab)\(tab)\(tab)\(tab)\(tab)} else {\n")
result.append("\(tab)\(tab)\(tab)\(tab)\(tab)\(tab)continuation.resume(throwing: TSDKClientError(\"Nothing for return\"))\n")
result.append("\(tab)\(tab)\(tab)\(tab)\(tab)}\n")
result.append("\(tab)\(tab)\(tab)\(tab)}\n")
result.append("\(tab)\(tab)\(tab)} catch {\n")
result.append("\(tab)\(tab)\(tab)\(tab)continuation.resume(throwing: error)\n")
result.append("\(tab)\(tab)\(tab)}\n")
result.append("\(tab)\(tab)}\n")
result.append("\(tab)}\n\n")

return result
}

private func generateModule(_ swiftModule: SDKSwiftApi.Module) -> String {
var result: String = "public final class \(libPrefix)\(swiftModule.name.capitalized)Module {\n\n"
Expand All @@ -165,6 +224,8 @@ class CodeGenerator {

for function in swiftModule.functions {
result.append(generateFunction(function))
if function.name.contains("subscribe") { continue }
result.append(generateAsyncAwaitFunction(function))
}
result.append("}\n")

Expand Down Expand Up @@ -192,6 +253,7 @@ class CodeGenerator {

for function in swiftModule.functions {
result.append(generateFunction(function))
result.append(generateAsyncAwaitFunction(function))
}

/// PRINT DEBUG DEINIT CLIENT
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</p>

[![SPM](https://img.shields.io/badge/swift-package%20manager-green)](https://swift.org/package-manager/)
[![SPM](https://img.shields.io/badge/SDK%20VERSION-1.41.0-orange)](https://github.com/tonlabs/ever-sdk)
[![SPM](https://img.shields.io/badge/SDK%20VERSION-1.41.1-orange)](https://github.com/tonlabs/ever-sdk)

Swift is a strongly typed language that has long been used not only for iOS development. Apple is actively promoting it to new platforms and today it can be used for almost any task. Thanks to this, this implementation provides the work of Everscale SDK on many platforms at once, including the native one for mobile phones. Let me remind you that swift can also be built for android.

Expand Down
Loading

0 comments on commit 7bb67f5

Please sign in to comment.