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

[swift2objc] Filtering Support #1730

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

nikeokoronkwo
Copy link

This Pull Request is to add filtering support to the swiftgen tool.

  • Add filtering support to swiftgen Config
  • Implementing filtering support into transform logic
  • Create and test unit tests for such filtering logic

More information can be found here: #1394

Copy link
Contributor

@liamappelbe liamappelbe left a comment

Choose a reason for hiding this comment

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

Looks like you've also got a merge conflict to resolve.

pkgs/swift2objc/lib/src/config.dart Outdated Show resolved Hide resolved
pkgs/swift2objc/lib/src/config.dart Outdated Show resolved Hide resolved
pkgs/swift2objc/test/unit/filter_test.dart Outdated Show resolved Hide resolved
Copy link
Contributor

@liamappelbe liamappelbe left a comment

Choose a reason for hiding this comment

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

Tests are looking good. You can resolve the formatting and analysis issues in CI using dart format . and dart analyze.

final TransformationMap transformationMap;
final _filter = filter ?? (declaration) => true;

final _declarations = declarations.where((d) => _filter(d));
Copy link
Contributor

Choose a reason for hiding this comment

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

This can just be declarations.where(_filter);

self.wrappedInstance = wrappedInstance
}

@objc init(brand: String, gearCount: Int, dimensions: DimensionsWrapper) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I thought this might happen. This code won't compile, because you're not generating DimensionsWrapper. This is what makes filtering tricky, because you need to think about transitive dependencies.

There are a few ways to handle it:

  1. Omit any methods/properties/initializers that refer to declarations that aren't included. I think this would be confusing to users, and not expected behavior.
  2. Replace any omitted declarations with NSObject, and just cast them to the target object type instead of doing .wrappedInstance. This is easy, but loses type safety. Also I don't think it'll work for structs.
  3. Fully generate all transitive deps. This can and up generating quite a lot of bindings the user doesn't care about, because of the transitive bit. You have to generate the deps of the deps too.
  4. Generate the direct deps of the included declarations as stubs (a stub would be like a class that only has the wrappedInstance and one wrapping init method, no other properties or methods). This is the best solution, because it avoids the need to recursively generate deps of deps (the deps are stubs, so they don't have any deps). It's harder to implement though, because you have to upgrade the code generator to have a notion of stub generation.

Ffigen used to do (3), but I recently upgraded it to do (4) by default (with config options to do (3) if you want). Jnigen used to do (2), but plans to switch to (4).

(4) is a bit hard to implement though, so feel free to do (3) for now.

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.

2 participants