-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main' into 5d/api-behavior
- Loading branch information
Showing
15 changed files
with
367 additions
and
29 deletions.
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
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
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 | ||
} | ||
} | ||
} |
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
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 { } | ||
} |
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
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
Oops, something went wrong.