Skip to content
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

SPM Prep – Use WordPressComRESTAPIInterfacing in DomainServiceRemote and AccountServiceRemoteREST #766

Merged
merged 4 commits into from
Apr 4, 2024

Conversation

mokagio
Copy link
Contributor

@mokagio mokagio commented Mar 25, 2024

Description

A small PR to get feedback on my approach to adopt the WordPressComRESTAPIInterfacing abstraction layer from #760 in Swift.

This required a few minor tweaks, mainly to make sure the code across Objective-C and Swift deals with Any instead of NSObject. I think this is okay. Any is as loose a type as it comes in Swift, but the loss of type safety is only a matter of making the two languages working together. All the code I've dealt with so far uses stricter types at runtime, of course.

Testing Details

Ensure CI is green.

Next Steps

Assuming the implementation direction I've taken is appropriate, after this lands I'll move on with porting it to the rest of the Swift files.


  • Please check here if your pull request includes additional test coverage. — N.A.
  • I have considered if this change warrants release notes and have added them to the appropriate section in the CHANGELOG.md if necessary. — N.A.

@@ -23,7 +23,7 @@ extension AccountServiceRemoteREST {
oAuthClientID: String,
oAuthClientSecret: String,
success: @escaping (() -> Void),
failure: @escaping ((NSError) -> Void)) {
failure: @escaping ((Error) -> Void)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think this counts as a breaking change, does it?

The API became looser not stricter. Any code passing (NSError) -> Void to failure should still compile now that the expected type is (Error) -> Void.

Anyways, if it's a breaking change, I'll just add it to the existing list 😅

Copy link
Contributor

Choose a reason for hiding this comment

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

IMO, it's still a breaking change. The consumers may use NSError.domain in their code, which won't compile with this updated code.

One way to avoid this breaking change is casing as NSError in the failure callback below: failure(error as NSError).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If that's okay with you, I'd keep Error and count this as a breaking change, then.

Error is more Swifty, which I think is the best approach going forward.

I'll merge under that assumption and the approval you already gave. Happy to followup with a fix PR if needed.

Copy link
Contributor

Choose a reason for hiding this comment

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

👍 I don't have a strong opinion on this.

Comment on lines 7 to 10
/// - Note: `parameters` has `id` instead of the more common `NSObject *` as its value type so it will convert to `AnyObject` in Swift.
/// In Swift, it's simpler to work with `AnyObject` than with `NSObject`. For example `"abc" as AnyObject` over `"abc" as NSObject`.
- (NSProgress * _Nullable)get:(NSString * _Nonnull)URLString
parameters:(NSDictionary<NSString *, NSObject *> * _Nullable)parameters
parameters:(NSDictionary<NSString *, id> * _Nullable)parameters
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Does the explanation make sense?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep. It makes sense to me. Some Foundation API uses the same "fake generic" signature, i.e. UserDefaults

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Awesome! TIL

@mokagio mokagio force-pushed the mokagio/spm/abstraction-layer-swift branch from 4776cd8 to 52a7ea4 Compare April 2, 2024 02:58
}

public func post(
_ URLString: String,
parameters: [String: NSObject]?,
parameters: [String: Any]?,
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this is a breaking change, because [String: NSObject] is a "sub-type" of [String: Any], thus can be passed in here?

@mokagio mokagio merged commit 009a5a4 into trunk Apr 4, 2024
9 checks passed
@mokagio mokagio deleted the mokagio/spm/abstraction-layer-swift branch April 4, 2024 05:08
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.

2 participants