Skip to content

Conversation

@ailtonvivaz
Copy link
Contributor

@ailtonvivaz ailtonvivaz commented Feb 27, 2022

This PR adds Swift support for Pigeon. This code was based on the Java generator.

Fixes flutter/flutter#66499

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the relevant style guides and ran the auto-formatter. (Unlike the flutter/flutter repo, the flutter/packages repo does use dart format.)
  • I signed the CLA.
  • The title of the PR starts with the name of the package surrounded by square brackets, e.g. [shared_preferences]
  • I listed at least one issue that this PR fixes in the description above.
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy, or this PR is exempt from version changes.
  • I updated CHANGELOG.md to add a description of the change, following repository CHANGELOG style.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is test-exempt.
  • All existing and new tests are passing.

@flutter-dashboard
Copy link

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.

@google-cla
Copy link

google-cla bot commented Feb 27, 2022

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.

@ailtonvivaz ailtonvivaz changed the title [pigeon] Add Swift support for Pigeon [pigeon] Adds Swift support for Pigeon Feb 27, 2022
Copy link
Collaborator

@stuartmorgan-g stuartmorgan-g left a 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)} ');
Copy link
Collaborator

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?

Copy link
Contributor Author

@ailtonvivaz ailtonvivaz Mar 2, 2022

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:
...
}

Copy link
Collaborator

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).

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 completely agree with you.
I'll fix it.

Copy link
Contributor Author

@ailtonvivaz ailtonvivaz Mar 4, 2022

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)

@stuartmorgan-g stuartmorgan-g requested a review from gaaclarke March 1, 2022 21:05
Copy link
Member

@gaaclarke gaaclarke left a 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.

Copy link
Member

@gaaclarke gaaclarke left a 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.

@ailtonvivaz
Copy link
Contributor Author

LGTM with a few nits. Thanks for all the work here!

This is failing Windows tests in CI, which probably means the Windows tests were broken by changes in pigeons/. The CI is reporting it as a hang (we're still looking into why), but locally it'll actually be a compile failure. Do you have a Windows machine to investigate, or do you need one of us to take a look?

@stuartmorgan, I've just fixed the windows tests. There was the need to change some generated class names to adapt the new values.

Copy link
Contributor

@hellohuanlin hellohuanlin left a 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?')

@hellohuanlin
Copy link
Contributor

hellohuanlin commented Jun 10, 2022

automated enforcement of style isn't a blocker for using Swift.

@stuartmorgan Gotcha. In this case I think I will do another pass for the swift code.

Copy link
Collaborator

@stuartmorgan-g stuartmorgan-g left a 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.)

Copy link
Contributor

@hellohuanlin hellohuanlin left a 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.

"images" : [
{
"idiom" : "universal",
"filename" : "LaunchImage.png",
Copy link
Contributor

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)

Copy link
Collaborator

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"],
Copy link
Contributor

Choose a reason for hiding this comment

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

what's "annotation"?

Copy link
Collaborator

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.

Copy link
Contributor

@hellohuanlin hellohuanlin left a 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

@stuartmorgan-g
Copy link
Collaborator

@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 🙂

@ailtonvivaz
Copy link
Contributor Author

I'm sorry for this mistake. I thought how the PR was approved , all things were "right".
Now, it's fixed.

@stuartmorgan-g stuartmorgan-g added autosubmit Merge PR when tree becomes green via auto submit App and removed needs tests labels Jun 22, 2022
@stuartmorgan-g
Copy link
Collaborator

Thanks again for the great contribution!

@auto-submit auto-submit bot merged commit 67ce87a into flutter:main Jun 22, 2022
@bladeofgod
Copy link

hi, i use this for generated swift code, but all class is private. how to make it public ? ;)

@hellohuanlin
Copy link
Contributor

@bladeofgod can you open a github issue with your code? PR comment section isn't a great place for discussion.

@stuartmorgan-g
Copy link
Collaborator

but all class is private

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

autosubmit Merge PR when tree becomes green via auto submit App p: pigeon

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[pigeon] add support for direct generation to Swift.

5 participants