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

Expose CDVCommandStatus enum to Swift #254

Merged
merged 1 commit into from
Feb 5, 2019
Merged

Conversation

dpogue
Copy link
Member

@dpogue dpogue commented Sep 11, 2016

This allows Swift plugins to write code like

CDVPluginResult(status: .error)

rather than

CDVPluginResult(status: CDVCommandStatus_ERROR)

Its use in Objective C is unchanged, but this is a API breaking change as far as Swift plugins are concerned (so we'd be looking at cordova-ios 6 to land this) I've hacked it to support both the existing name and the "friendly" name in Swift, at the cost of the code making sense 😕

@codecov-io
Copy link

codecov-io commented Sep 11, 2016

Current coverage is 56.95% (diff: 100%)

Merging #254 into master will not change coverage

@@             master       #254   diff @@
==========================================
  Files            12         12          
  Lines          1085       1085          
  Methods         169        169          
  Messages          0          0          
  Branches        174        174          
==========================================
  Hits            618        618          
  Misses          467        467          
  Partials          0          0          

Powered by Codecov. Last update 4975a1f...9977c74

@shazron
Copy link
Member

shazron commented Sep 15, 2016

Haven't played much with Swift enums, is there a way to support both for now, and deprecate the old in Swift?

@lucatorella
Copy link

lucatorella commented Apr 25, 2017

This change isn't breaking old code. The enum is still called CDVCommandStatus in Obj-C. It's just using the newer NSEnum syntax that is the recommended syntax even before Swift came out.

I would go even further and use NS_SWIFT_NAME to have names like .noResult, .ok etc. in Swift.

@shazron
Copy link
Member

shazron commented Apr 26, 2017

@dpogue Can you confirm what @lucatorella said?

In any case, we don't have any Swift tests yet, but that's easy to remedy for next release.

@dpogue
Copy link
Member Author

dpogue commented Apr 26, 2017

Yes, the breaking change is only on the Swift side

@lucatorella
Copy link

No, because now that enum is not accessible in Swift. So if we want to make it accessible, it's better to make it accessible with proper name conventions (.noResult, .ok, ...). If you just add NSEnum then it will be available in Swift with weird names, and then if you decide in a next version to use NS_SWIFT_NAME, then you'll have a breaking change on the Swift side.

@lucatorella
Copy link

lucatorella commented Apr 26, 2017

Sorry, I'm mistaken. The plain C enum is currently already available in Swift. So yes, it'll be a breaking change (just in Swift) as much as your proposal :)

@shazron
Copy link
Member

shazron commented Apr 26, 2017

Sorry, Swift newbie here :) (waiting for it to be stable real soon now!). This will be slated for [email protected], I'll see how to add Swift tests in the current test project...

@shazron
Copy link
Member

shazron commented Feb 26, 2018

@dpogue Reviewing PRs for next cordova-ios release. Am I correct to assume since this is a breaking change, it would be for a major version release (since we follow semver)?

@dpogue
Copy link
Member Author

dpogue commented Feb 26, 2018

Yes, that is correct

@janpio janpio added this to the 5.0.0 milestone Aug 10, 2018
Copy link
Contributor

@knight9999 knight9999 left a comment

Choose a reason for hiding this comment

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

This looks good to me. Are we still planning to release this in next major and when is the best time to merge?

@dpogue
Copy link
Member Author

dpogue commented Sep 6, 2018

This causes all existing Swift plugins to break with compiler errors and it's impossible to be backwards compatible and support both... so whenever we feel like that's a good idea 😬

Maybe now is the best time, since we're already changing how Bridging Headers and SwiftVersion are handled?

@dpogue
Copy link
Member Author

dpogue commented Oct 16, 2018

@shazron @knight9999 Okay... I think I've come up with a (messy) solution to allow this to not be a breaking change.

The existing ObjC enum values are exposed to Swift with friendly names, as per Swift convention:
CDVCommandStatus_NO_RESULT -> CDVCommandStatus.noResult or just .noResult

Then, we make a constant variable in ObjC for each of those enum values named SWIFT_<enum_value_name>, but expose those to Swift with the original enum value name. So these are globally available in Swift, the same way the enum constants were previously.

It's very ugly and now we have a bunch of global constants sitting around that I don't think the compiler will optimize out :(

That said, it seems to work, and I was able to take one of my existing Swift plugins and run it without changing any of the CDVCommandStatus_OK uses into .ok. Both of the following work in Swift after this change:

// Old/current style:
let result = CDVPluginResult(status: CDVCommandStatus_ERROR, messageAs: "AbortError");

// New style
let result = CDVPluginResult(status: .error, messageAs: "AbortError");

@codecov-io
Copy link

codecov-io commented Oct 16, 2018

Codecov Report

Merging #254 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #254   +/-   ##
=======================================
  Coverage   74.29%   74.29%           
=======================================
  Files          12       12           
  Lines        1564     1564           
=======================================
  Hits         1162     1162           
  Misses        402      402

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 23a9387...b7b9931. Read the comment docs.

This allows Swift plugins to write code like `CDVPluginResult(status: .error)`
rather than `CDVPluginResult(status: CDVCommandStatus_ERROR)`.

However, for existing Swift plugins, it attempts to ensure that the old
constants remain exposed with their original names. This is a bit messy
and involves global constants in the Objective C code with aliased names
exposed to Swift :(
Copy link
Member

@erisu erisu left a comment

Choose a reason for hiding this comment

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

LGTM

@erisu erisu merged commit 201e8b3 into apache:master Feb 5, 2019
@dpogue dpogue deleted the swift-enum branch March 6, 2019 05:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants