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

Passing arguments to macros #158

Open
simolus3 opened this issue Dec 24, 2024 · 0 comments
Open

Passing arguments to macros #158

simolus3 opened this issue Dec 24, 2024 · 0 comments

Comments

@simolus3
Copy link
Contributor

I would like to port macro arguments to the new macro system, starting with the easier constants (so no Code or TypeAnnotation initially).

It would help to have an idea on how to pass this information to macro implementation. I was thinking about having a representation for constants similar to the DartObject class from the analyzer (so a fully resolved constant instead of a syntax-related representation of the macro annotation).
The question is how we'd pass this to the macro implementation. Considering an argument like this:

enum FieldRename { none, kebab, snake, pascal, screamingSnake }

class JsonCodable {
  final FieldRename rename;
  const JsonCodable({this.rename = FieldRename.none});
}

Perhaps we could pass the information about the instantiated macro along as part of the Model? Or we could add a getter to Builder, like this

abstract interface class Builder<T extends Object> {
  T get target;

  /// The resolved macro instance, with constant arguments
  DartObject get macro;

Which the implementation could then query like this:

  @override
  void buildDeclarationsForClass(ClassDeclarationsBuilder builder) {
    final rename = FieldRename.values.byName(builder.macro.fields['rename'].asObject.fields['name']);

I'm happy to wire up the implementation myself, but some guidance on how the API should look like would be helpful :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant