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

[pigeon] Relocates generator classes and updates imports #2985

Merged
merged 5 commits into from
Dec 20, 2022

Conversation

tarrinneal
Copy link
Contributor

Relocates generator classes and updates imports. Initial set up for flutter/flutter#117416

@tarrinneal tarrinneal added override: no versioning needed Override the check requiring version bumps for most changes override: no changelog needed Override the check requiring CHANGELOG updates for most changes labels Dec 20, 2022
@tarrinneal tarrinneal removed override: no versioning needed Override the check requiring version bumps for most changes override: no changelog needed Override the check requiring CHANGELOG updates for most changes labels Dec 20, 2022
Copy link
Contributor

@stuartmorgan stuartmorgan left a comment

Choose a reason for hiding this comment

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

Initial restricting LGTM. (Eventually we'll want to revisit things like C++ and ObjC being two generators, and probably the sink creation location.)

import 'generator_tools.dart';
import 'pigeon_lib.dart' show Error;
import 'pigeon_lib.dart' show Error, PigeonOptions, lineReader, openSink;
Copy link
Contributor

Choose a reason for hiding this comment

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

We should ideally move these two utility methods to a utility file, and find somewhere for the other things to live, so we don't have circular includes. Than can be done later though.

@tarrinneal tarrinneal added the autosubmit Merge PR when tree becomes green via auto submit App label Dec 20, 2022
@auto-submit auto-submit bot merged commit 3b5ae00 into flutter:main Dec 20, 2022
import 'generator_tools.dart';
import 'pigeon_lib.dart' show Error;
import 'pigeon_lib.dart' show Error, PigeonOptions, lineReader, openSink;
Copy link
Member

Choose a reason for hiding this comment

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

The reason that the Generator was in the place it was previously is that it was keeping PigeonOptions out of the generator files. The idea being that the generators are cleaner/safer/easier-to-maintain if they only have access to the options designated for them instead of giving them every single option. The idea of the Generator was more of an adapter of the public functions in the generator files which were the actual generators.

If you wanted to retain that organization it's going to be more difficult if you move the Generator implementations to the generator files. You'll have to make Generator generic:

abstract class Generator<T> {
  void generate(StringSink sink, T options, Root root);
  List<Error> validate(T options, Root root);
}

Then when you store adapters between PigeonOptions and the input that the Generator needs where you store all your generators.

/// Adapts a Generator to PigeonOptions
class GeneratorAdapter {
  GeneratorAdapter({this.generate, this.shouldGenerate, this.validate});
  void Function(StringSink sink, PigeonOptions options, Root root) generate;
  IOSink? Function(PigeonOptions options) shouldGenerate;
  List<Error> Function(PigeonOptions options, Root root) validate;
}

var generator = Generator();
generators.add(GeneratorAdapter(
  GeneratorAdapter(
    generate:(sink, options, root) => generator.generate(sink, options.fooOptions, root),
    shouldGenerate:(options) => _openSink(options.fooOut),
    validate:(options, root) => generator.validate(options.fooOptions, root)));

The benefits being:

  1. it makes the Generator API more simple
  2. it separates the idea of generating code and adapting it to the PigeonOptions format
  3. it keeps PigeonOptions out of the generator files

How do you like that?

Copy link
Member

Choose a reason for hiding this comment

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

Sonofabitch, late comment. For architecture questions can you guys touch base first. I didn't really have an opportunity to react to this.

Copy link
Contributor

Choose a reason for hiding this comment

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

We can revert+retract if this is an issue. I didn't expect putting the generator classes in the generator files to be controversial.

The idea of the Generator was more of an adapter of the public functions in the generator files which were the actual generators.

This is why naming is important; I didn't expect that in a package where the most important conceptual thing are generators that the Generator class wasn't intended to represent a generator.

Copy link
Member

Choose a reason for hiding this comment

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

Yea, it should have been "_Generator" but was made public to help with testing. We don't need to retract. I think separating the adapter and the generator makes everything tidier and easier to maintain but we can do that in a different PR. I think having a Generator abstract class that people follow along in the generator files makes it a bit easier conceptually instead of having loose functions, so that is an improvement.

@@ -340,7 +342,8 @@ Iterable<String> _lineReader(String path) sync* {
}
}

IOSink? _openSink(String? output) {
/// Creates IOSink.
Copy link
Member

Choose a reason for hiding this comment

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

I think there is more to be said here if we want to make this public =)

tarrinneal added a commit to tarrinneal/packages that referenced this pull request Dec 21, 2022
auto-submit bot pushed a commit that referenced this pull request Dec 21, 2022
* start

* Class file transfer and skeleton comments

* relocate generator classes and update imports

* merge main and changelog update

* update openSink to openWriteSink

* Revert "[pigeon] Relocates generator classes and updates imports (#2985)"

This reverts commit 3b5ae00.
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
3 participants