Skip to content

Add Support for Swift 5.7 #153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jul 5, 2023
Merged

Conversation

Andrea-Scuderi
Copy link
Contributor

Add support for Swift 5.7

  • Add new action signatures with async/await and throws
  • Backward compatibility with old action signatures

swift57Action

  • Add support async to Whisk class

swiftbuild.py

  • simplify the code added by the build process to:
try await _WhiskRuntime.wiskRunLoop { jsonData in
   await _WhiskRuntime.runAsyncMain(mainFunction: <action>, json: jsonData)
}

swiftbuild.py.launcher.swift

  • Add _WhiskRuntime struct
  • Refactor Error messages
  • Add new signatures
// Examples of Actions supported by Swift 5.7

// Action with Any Input and Any Output
func main(args: Any) -> Any {
    let newArgs = args as! [String:Any]
    if let name = newArgs["name"] as? String {
        return [ "greeting" : "Hello \(name)!" ]
    } else {
        return [ "greeting" : "Hello stranger!" ]
    }
}
 
// Async Action with Any Input and Any Output
func mainAsync(args: Any) async -> Any {
    do {
        //async code sleep for 1 sec
        try await Task.sleep(nanoseconds: 1_000_000_000)
        
        let newArgs = args as! [String:Any]
        if let name = newArgs["name"] as? String {
            return [ "greeting" : "Hello \(name)!" ]
        } else {
            return [ "greeting" : "Hello stranger!" ]
        }
    } catch {
        return ["error" : error.localizedDescription]
    }
}

// Async throwing Action with Any Input and Any Output
func mainAsyncThrows(args: Any) async throws -> Any {
    //async code sleep for 1 sec
    try await Task.sleep(nanoseconds: 1_000_000_000)
    
    let newArgs = args as! [String:Any]
    if let name = newArgs["name"] as? String {
        return [ "greeting" : "Hello \(name)!" ]
    } else {
        return [ "greeting" : "Hello stranger!" ]
    }
}

struct Input: Codable {
    let name: String?
}

struct Output: Codable {
    let count: Int
}

// Action with Codable Input and completion with Codable Output and Error
func mainCompletionCodable(input: Input, completion: @escaping (Output?, Error?) -> Void) -> Void {
    if let name = input.name {
        let output = Output(count: name.count)
        completion(output, nil)
    } else {
        let output = Output(count: 0)
        completion(output, nil)
    }
}

// Action with Codable Input and completion with Codable Output and Error
func mainCompletionCodableNoInput(completion: @escaping (Output?, Error?) -> Void) -> Void {
    let output = Output(count: 0)
    completion(output, nil)
}

// Async throwing Action with Codable Output
func mainCodableAsyncThrowsNoInput() async throws -> Output? {
    try await Task.sleep(nanoseconds: 1_000_000_000)
    return Output(count: 0)
}

// Async throwing Action with a Codable Input and a Codable Output
func mainCodableAsyncThrows(input: Input) async throws -> Output? {
    try await Task.sleep(nanoseconds: 1_000_000_000)
    if let name = input.name {
        return Output(count: name.count)
    } else {
        return Output(count: 0)
    }
}

Merry Christmas and Happy New Year! 🎄

@rabbah
Copy link
Member

rabbah commented Jan 5, 2023

Thanks @Andrea-Scuderi for this contribution.

Do you have an Apache CLA on file I was not able to locate one.

We may continue to hold merging this PR until we've cut over to the new GHA flow and away from Travis.

@Andrea-Scuderi
Copy link
Contributor Author

Do you have an Apache CLA on file I was not able to locate one.

Yes, I've sent a CLA the 11/11/2019.

@dgrove-oss
Copy link
Member

Hi @Andrea-Scuderi -- we've finally managed to get GitHub Action based CI working for this repository. Apologies for the very long delay. Would you have the time and interest to rebase this PR to the current master so we can get it merged?

Copy link
Member

@dgrove-oss dgrove-oss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you have the time to accept the two small changes to the CHANGELOG files and update the .github/workflows/ci.yml that would be great.

@Andrea-Scuderi
Copy link
Contributor Author

@dgrove-oss I've updated the branch with your suggestions. Could you run the CI?
Thanks

@dgrove-oss
Copy link
Member

Thanks for the quick response! CI run approved.

Copy link
Member

@dgrove-oss dgrove-oss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM -- Thanks for your patience!

@dgrove-oss dgrove-oss merged commit c4d08d0 into apache:master Jul 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants