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

Run codegen for first party Objective-C to Swift RPC migration shim #435

Merged
merged 13 commits into from
Oct 24, 2024

Conversation

julianlocke
Copy link
Contributor

@julianlocke julianlocke commented Sep 30, 2024

Existing integration tests not expected to pass. This code to be deleted post migration.

Stone changes: dropbox/stone#350

This PR creates a migration shim for first party use. The intention of the shim is to facilitate a feature-gated migration from ObjectiveDropbox to SwiftyDrobpox. Specifically it is to allow broad usage of SwiftyDropbox without modifying ObjectiveDropbox call sites.

There are four pieces that make up the shim:

  1. API object mappers from Objective-C (ObjectiveDropbox) types to Swifty Objective-C compatibility layer types
func mapDBFILESCommitInfoToDBX(object: DBFILESCommitInfo) -> DBXFilesCommitInfo {
    return DBXFilesCommitInfo(path: object.path,
        mode: mapDBFILESWriteModeToDBX(object: object.mode),
        autorename: object.autorename,
        clientModified: object.clientModified,
        mute: object.mute,
        propertyGroups: object.propertyGroups?.compactMap { mapDBFILEPROPERTIESPropertyGroupToDBX(object: $0) },
        strictConflict: object.strictConflict,
        fswRequest: mapDBFILESFswRequestToDBXOptional(object: object.fswRequest),
        fileEncryptionInfo: mapDBENCRYPTIONEncryptedFileToDBXOptional(object: object.fileEncryptionInfo)
    )
}
  1. API object mappers from Swifty Objective-C compatibility layer types to Objective-C (ObjectiveDropbox) types
func mapDBXFilesCommitInfoToDB(object: DBXFilesCommitInfo) -> DBFILESCommitInfo {
    return DBFILESCommitInfo(path: object.path,
        mode: mapDBXFilesWriteModeToDB(object: object.mode),
        autorename: object.autorename,
        clientModified: object.clientModified,
        mute: object.mute,
        propertyGroups: object.propertyGroups?.compactMap { mapDBXFilePropertiesPropertyGroupToDB(object: $0) },
        strictConflict: object.strictConflict,
        fswRequest: mapDBXFilesFswRequestToDBOptional(object: object.fswRequest),
        fileEncryptionInfo: mapDBXEncryptionEncryptedFileToDBOptional(object: object.fileEncryptionInfo)
    )
}
  1. A function that provides a Swifty api request when provided with Objective-C api request context
@objc public static func rpcRequest(for route: DBRoute, args: DBSerializable, userId: String, client: DBXDropboxClient) -> DBXRequest? {
    // Make sure this handles versioned routes properly
    let qualifiedRouteName = route.namespace_ + "/" + route.name
    if qualifiedRouteName == "account/change_name", let args = args as? DBACCOUNTAccountNameArg {
        return client.account.changeName(firstName: args.firstName, lastName: args.lastName)
    }
    if qualifiedRouteName == "account/delete_account", let args = args as? DBACCOUNTDeleteAccountArg {
    etc.
  1. A function that allows you to set an Objective-C response block on a Swifty api request and maps the Swifty API objects received back to Objective-C objects.
@objc public static  func setResponseBlockRPC(block: @escaping DBRpcResponseBlockImpl, on task: DBXRequest, with queue: OperationQueue) -> Bool {
    if let task = task as? DBXAccountChangeNameRpcRequest {
        task.response { routeError, networkError in
            let mappedError = mapDBXAccountChangeNameErrorToDBOptional(object: routeError)
            let mappedCallError = callErrorToDB(error: networkError)
            let wrappedBlock = { block((networkError == nil && routeError == nil) ? DBNilObject() : nil, mappedError, mappedCallError) }
            Self.processCompletion(wrappedBlock, queue: queue)
        }
        return true
    }
    if let task = task as? DBXAccountDeleteAccountRpcRequest {
    etc.

Usage:
Our ObjectiveDropbox transport client subclass will direct all requests for an Objective-C RPC API request to the shim instead, if gated. Similarly a new RPCTask subclass will route response blocks to the shim and cancellations to the underlying swift api request object.

@julianlocke julianlocke changed the title Run codegen for first party Objective-C to Swift migration shim Run codegen for first party Objective-C to Swift RPC migration shim Sep 30, 2024
@julianlocke julianlocke merged commit 10dda4a into objc-shim Oct 24, 2024
2 checks passed
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