-
Notifications
You must be signed in to change notification settings - Fork 16
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 – Define new CoreAPI package #782
Conversation
Generated by 🚫 Danger |
// When building for SPM, the compiler doesn't generate the domain constant. | ||
#if SWIFT_PACKAGE | ||
XCTAssertEqual(error.domain, "CoreAPI.WordPressOrgXMLRPCApiError") | ||
#else | ||
XCTAssertEqual(error.domain, WordPressOrgXMLRPCApiErrorDomain) | ||
#endif |
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.
@crazytonyli what do you think of this?
I worry it might backfire in the real world if people where to do a comparison on the domain with a String
literal value.
Maybe it would be safer to explicitly define the domain in a let
and update the code that inits WordPressOrgXMLRPCApiError
to set it?
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.
My preference would be going with the safer option, which is avoiding breaking changes. You can implement CustomNSError
to set a custom domain, which means we shouldn't need to change this assertion here.
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.
Partially addressed in 3f57895.
However, the tests still fail when build for SPM. I believe that has to do with the XML RPC returning WordPressOrgXMLRPCApiError
or WordPressOrgXMLRPCApiFault
.
I'll have to look into the latter as well, I guess.
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 found the source of the issue.
If one removes the @objc
annotation, then the CustomNSError
conversion code is used in its entirety. Otherwise, with the error enum
declared as @objc
, it seems like only the part that computes userInfo
is called.
I verified this with an ad hoc package. I might put it online later. I'm unsure how to move forward in the meantime, because we do need the @objc
visibility for WordPressOrgXMLRPCApiError
. I'll keep thinking about it...
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.
Move my WIP work to #790 and rebased it off this branch.
WPKitLogVerbose("Received OAuth2 response: \(self.cleanedUpResponseForLogging(responseObject as AnyObject? ?? "nil" as AnyObject))") | ||
// WPKitLogVerbose("Received OAuth2 response: \(self.cleanedUpResponseForLogging(responseObject as AnyObject? ?? "nil" as AnyObject))") |
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.
Might need to introduce a new abstraction layer for logging, too.
Or, is logging at this level useful? Can we simply get rid of it altogether?
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 opened an issue about it #787
let WordPressComRestApiErrorDomain = "WordPressKit.WordPressComRestApiError" | ||
|
||
// WordPressComRestApiErrorDomain is accessible only in Swift and, since it's a global, cannot be made @objc. | ||
// As a workaround, here is a builder to init NSError with that domain and a method to check the domain. |
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.
An alternative could be defining the same constant again in Objective-C and making it unavailable to Swift. That way you won't need to change those Objective-C files.
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.
See 6fba3a1
// When building for SPM, the compiler doesn't generate the domain constant. | ||
#if SWIFT_PACKAGE | ||
XCTAssertEqual(error.domain, "CoreAPI.WordPressOrgXMLRPCApiError") | ||
#else | ||
XCTAssertEqual(error.domain, WordPressOrgXMLRPCApiErrorDomain) | ||
#endif |
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.
My preference would be going with the safer option, which is avoiding breaking changes. You can implement CustomNSError
to set a custom domain, which means we shouldn't need to change this assertion here.
79384d2
to
46e3a5f
Compare
22dcdd8
to
7198442
Compare
This DRYs the definition, makes the constant available to both Objective-C and Swift, and removes the need for custom builder and checker. See @crazytonyli's suggestion from #782 (comment)
Description
Moves some of the lower level Swift files that implement API requests into
Sources/CoreAPI
with matching tests.Getting the tests to compile in this setup required some additional work in terms of conditional imports and an SPM-aware
Bundle
loader.Notice that the package exists but is not used anywhere. I just wanted to lock this in, so to speak, to build on top of it later on.
Testing Details
See CI. Although, it only shows that no regression has been introduced by this setup.
To test the SPM setup alone, one needs to:
.xc-
workspace and project folderxcodebuild
, for example with a lane like this one:WordPressKit-iOS/fastlane/Fastfile
Lines 10 to 29 in 5c002d7
OR
Package.swift
(open -a /Applications/Xcode.app .
should do it)CHANGELOG.md
if necessary. — N.A.