-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[pigeon] Adds Swift support for Pigeon #976
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
Conversation
|
It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact Hixie on the #hackers channel in Chat (don't just cc him here, he won't see it! He's on Discord!). If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix? Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. |
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). For more information, open the CLA check for this pull request. |
1aed7d9 to
8946e0d
Compare
stuartmorgan-g
left a comment
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.
Thanks for the contribution!
@gaaclarke should do the main review, but I did a quick high-level pass over the generator code itself.
| indent.write(''); | ||
| for (final EnumeratedClass customClass in getCodecClasses(api, root)) { | ||
| indent.write( | ||
| 'if let value = value as? ${_className(prefix, customClass.name)} '); |
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.
Don't these need to use type(of:) checks to avoid handling subclasses incorrectly?
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 don't think so. At the moment, pigeon generator doesn't support inheritance generation.
In this case, I don't see any risk with it.
Also, using type(of:) needs to check the type and to cast.
if type(of: value), let value = value as! Type { ... }I propose keep this approach and improve the readability, like this:
switch value {
case let value as Type:
...
}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.
At the moment, pigeon generator doesn't support inheritance generation.
And if at some later point data classes are allowed to inherit from each other, who is going to know to come update this code?
In this case, I don't see any risk with it.
The risk is that it implicitly relies on a limitation elsewhere in the code, and if that limitation changes this code will become wrong since it's not actually expressing the correct check currently.
I propose keep this approach and improve the readability, like this:
Correctness is more important than readability (particularly in generated code).
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 completely agree with you.
I'll fix 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.
I've tried this change but it isn't working.
When I use type(of:) it returns __SwiftValue instead Type.self.
If I compare type(of: value) == Type.self then it returns false.
With this, I got the follow error: Unsupported value for standard codec (NSInternalInconsistencyException)
gaaclarke
left a comment
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.
This is looking awesome, thanks. I'm going to download it and play with it a bit more since it's easier if I can see the generated code. How the code is generated looks good and there isn't any glaring omission at first glance. The big thing will be ensuring that the platform_tests coverage is good. Don't worry about e2e tests. Let's focus on the platform_tests as the gold standard.
gaaclarke
left a comment
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.
Can you also add a platform_test that exercises the HostApi? I think there is just one in the async tests. I noticed there wasn't much coverage there and added one in the PR that is about to land: https://github.com/flutter/packages/pull/895/files?show-viewed-files=true&file-filters%5B%5D=#diff-0591c0be35756f8c6961ad4e999d94bf45b3a1aac48500a519259c264af47c3bR48
I'll try to land that today so that you have that as an example to build off of.
packages/pigeon/platform_tests/ios_swift_unit_tests/ios/RunnerTests/EnumTests.swift
Outdated
Show resolved
Hide resolved
@stuartmorgan, I've just fixed the windows tests. There was the need to change some generated class names to adapt the new values. |
...tform_tests/ios_swift_unit_tests/ios/Runner/Assets.xcassets/AppIcon.appiconset/Contents.json
Show resolved
Hide resolved
packages/pigeon/platform_tests/ios_swift_unit_tests/ios/RunnerTests/PrimitiveTests.swift
Outdated
Show resolved
Hide resolved
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.
Request changes for the optional dictionary value type (double optional problem with Expression implicitly coerced from 'Any??' to 'Any?')
@stuartmorgan Gotcha. In this case I think I will do another pass for the swift code. |
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.
LGTM!
@hellohuanlin Unless you find structural issues, since it's still marked experimental and we can make changes as needed, we can err on the side of landing it. I.e., don't worry about making sure that all generated code for every case is 100% perfect. Traditionally with Pigeon we've found a lot of issues in actual use that weren't evident in review, and then fixed them incrementally as we go.
(Not to say you shouldn't look for issues or that we shouldn't fix them before landing, just that given that the overall structure and test scaffolding are in good shape here, the stakes of landing this PR are relatively low based on our experience with incremental fixes.)
hellohuanlin
left a comment
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.
Can you double check if the icons and launch screen images are removed? git histories are forever even if you delete these images later.
...tform_tests/ios_swift_unit_tests/ios/Runner/Assets.xcassets/AppIcon.appiconset/Contents.json
Show resolved
Hide resolved
| "images" : [ | ||
| { | ||
| "idiom" : "universal", | ||
| "filename" : "LaunchImage.png", |
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.
the launch image can also be removed. (you probably also need to remove it in xcode settings)
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.
We have 11 copies of this image in the repository already; there's no meaningful incremental cost to having more copies. We generally don't worry about slimming down the example apps from the template default anyway; having them match the template is the easiest way to maintain them.
| aList: [1, 2], | ||
| aMap: ["hello": 1234], | ||
| nestedList: [[true, false], [true]], | ||
| mapWithAnnotations: ["hello": "world"], |
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.
what's "annotation"?
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.
It's meaningless; that's just what @gaaclarke felt like naming the field.
See https://github.com/flutter/packages/blob/main/packages/pigeon/pigeons/all_datatypes.dart, and more generally https://github.com/flutter/packages/tree/main/packages/pigeon/pigeons. These files are test input. The names of the fields are generally irrelevant, what matters is their types.
...ages/pigeon/platform_tests/ios_swift_unit_tests/ios/RunnerTests/HandlerBinaryMessenger.swift
Outdated
Show resolved
Hide resolved
...ages/pigeon/platform_tests/ios_swift_unit_tests/ios/RunnerTests/HandlerBinaryMessenger.swift
Outdated
Show resolved
Hide resolved
packages/pigeon/platform_tests/ios_swift_unit_tests/ios/RunnerTests/EnumTests.swift
Outdated
Show resolved
Hide resolved
packages/pigeon/platform_tests/ios_swift_unit_tests/ios/RunnerTests/MockBinaryMessenger.swift
Outdated
Show resolved
Hide resolved
hellohuanlin
left a comment
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.
LGTM if it's not an issue checking in these images
|
@ailtonvivaz Once you've had a chance to respond to the final small comments above, let me know it's ready and I can get this landed 🙂 |
|
I'm sorry for this mistake. I thought how the PR was approved , all things were "right". |
|
Thanks again for the great contribution! |
|
hi, i use this for generated swift code, but all class is private. how to make it public ? ;) |
|
@bladeofgod can you open a github issue with your code? PR comment section isn't a great place for discussion. |
Please also be sure to include more details when filing the issue about exactly what you are trying to do and what the problem is, because this is definitely not accurate. |
This PR adds Swift support for Pigeon. This code was based on the Java generator.
Fixes flutter/flutter#66499
Pre-launch Checklist
dart format.)[shared_preferences]pubspec.yamlwith an appropriate new version according to the pub versioning philosophy, or this PR is exempt from version changes.CHANGELOG.mdto add a description of the change, following repository CHANGELOG style.///).