-
Notifications
You must be signed in to change notification settings - Fork 200
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
fix(api): add collection type casting in swift 5.7 #3602
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
AmplifyPlugins/API/Sources/AWSAPIPlugin/Support/Utils/Array+Error+TypeCast.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
|
||
import Foundation | ||
|
||
@_spi(AmplifyAPI) | ||
extension Array where Element == Error { | ||
func cast<T>(to type: T.Type) -> [T]? { | ||
self.reduce([]) { partialResult, ele in | ||
if let partialResult, let ele = ele as? T { | ||
return partialResult + [ele] | ||
} | ||
return nil | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
AmplifyPlugins/API/Tests/AWSAPIPluginTests/Support/Utils/Array+Error+TypeCastTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
|
||
import XCTest | ||
@testable @_spi(AmplifyAPI) import AWSAPIPlugin | ||
|
||
class ArrayWithErrorElementExtensionTests: XCTestCase { | ||
|
||
/** | ||
Given: errors with generic protocol type | ||
When: cast to the correct underlying concrete type | ||
Then: successfully casted to underlying concrete type | ||
*/ | ||
func testCast_toCorrectErrorType_returnCastedErrorType() { | ||
let errors: [Error] = [ | ||
Error1(), Error1(), Error1() | ||
] | ||
|
||
let error1s = errors.cast(to: Error1.self) | ||
XCTAssertNotNil(error1s) | ||
XCTAssertTrue(!error1s!.isEmpty) | ||
XCTAssertEqual(errors.count, error1s!.count) | ||
} | ||
|
||
/** | ||
Given: errors with generic protocol type | ||
When: cast to the wong underlying concrete type | ||
Then: return nil | ||
*/ | ||
func testCast_toWrongErrorType_returnNil() { | ||
let errors: [Error] = [ | ||
Error1(), Error1(), Error1() | ||
] | ||
|
||
let error2s = errors.cast(to: Error2.self) | ||
XCTAssertNil(error2s) | ||
} | ||
|
||
/** | ||
Given: errors with generic protocol type | ||
When: some of the elements failed to cast to the underlying concrete type | ||
Then: return nil | ||
*/ | ||
|
||
func testCast_partiallyToWrongErrorType_returnNil() { | ||
let errors: [Error] = [ | ||
Error2(), Error2(), Error1() | ||
] | ||
|
||
let error2s = errors.cast(to: Error2.self) | ||
XCTAssertNil(error2s) | ||
} | ||
|
||
struct Error1: Error { } | ||
|
||
struct Error2: Error { } | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like you can probably reduce this once more
Or even further, just use
cast
everywhere. This would avoid having to duplicate business logic while maintaining the Collection type casting in one place. Once we support 5.8 and up, then we can replace usages ofcast
withas
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the idea of having the compiler directive in the
cast
function insteadThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Including the directive in the
cast
func will make unit testing harder. Once we upgrade the Swift tool version to 5.8, removing the code within the annotation will become straightforward.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would it make unit testing harder? We don't need to unit-test that we support both 5.7 and 5.8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Chatted with @5d, the CI set up will cause
cast
to be unit tested against Swift 5.9 so it won't reach the code path. We'd have to refactor the unit test to test against something like:We to upgrade to min 5.8 soon so as Di mentioned it will be easier to remove the code later in the current PR