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

Add typespec/1 macro to transform module behaviour #384

Merged
merged 8 commits into from
Dec 25, 2024

Conversation

v0idpwn
Copy link
Collaborator

@v0idpwn v0idpwn commented Dec 4, 2024

Adds a typespec/1 macrocallback to transform module behaviour.

This macro can modify the typespec for modules using it. This way, we can provide correct typespecs for modules using transformers. A "breaking" change is that transform modules can't depend on Protobuf definitions anymore. This is quite easily solvable, though.

Closes #382


Previous description, from previous implementation:

We detect that transform_module/0 was defined through a on_definition hook. We then store the module in an attribute. This attribute is later used by the before_compile callback to set the typespec, which is set in the form of:

@type t() :: transform_module.t(__MODULE__)

Then, transform modules can implement proper typespecs.

No changes if transform_module is not overriden.

Closes #382

We detect that `transform_module/0` was defined through a on_definition hook.
We then store the module in an attribute. This attribute is later used
by the `before_compile` callback to set the typespec, which is set in
the form of:

```elixir
@t transform_module.t(__MODULE__)
```

Then, transform modules can implement proper typespecs.

No changes if `transform_module` is not overriden.
@v0idpwn v0idpwn force-pushed the typespec-for-protobuf-transformer branch from 475292b to a6c4647 Compare December 4, 2024 07:46
@v0idpwn
Copy link
Collaborator Author

v0idpwn commented Dec 4, 2024

@whatyouhide @ericmj if you agree on approach here, I'll add a few tests, documentation, etc :)

defp def_t_typespec(%MessageProps{enum?: true} = props, _extension_props) do
defp def_t_typespec(_props, _extension_props, transform_module_ast) when not is_nil(transform_module_ast) do
quote do
@type t() :: unquote(transform_module_ast).t(__MODULE__)
Copy link
Collaborator Author

@v0idpwn v0idpwn Dec 4, 2024

Choose a reason for hiding this comment

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

The biggest potential issue is that we are assuming that the function ast will always be a module. But I can't see it being anything else if you are using the library properly :)

@v0idpwn v0idpwn force-pushed the typespec-for-protobuf-transformer branch from b80bf30 to 3ea9f46 Compare December 4, 2024 08:07
@v0idpwn v0idpwn requested review from whatyouhide and ericmj and removed request for whatyouhide December 6, 2024 11:21
@v0idpwn
Copy link
Collaborator Author

v0idpwn commented Dec 6, 2024

I tested this in our codebases and it resolves dialyzer issues. We are using transformers for, for example, well known types.

@v0idpwn
Copy link
Collaborator Author

v0idpwn commented Dec 11, 2024

JFYI, I don't think the typespecs as they stand in this PR are good enough. I'm working on a new implementation that, although more complex, provides better typespecs.

Copy link
Collaborator

@whatyouhide whatyouhide left a comment

Choose a reason for hiding this comment

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

Very clever, this is awesome 💟

Module is redundant. One can use __CALLER__ to retrieve the calling module env.
Wasn't able to make it work in the way I intended without significant refactoring,
think the existing error is good enough for now.
@v0idpwn v0idpwn force-pushed the typespec-for-protobuf-transformer branch from 18fb755 to b9c00da Compare December 22, 2024 16:19
@v0idpwn v0idpwn changed the title When module has transform_module, use it on typespec Add typespec/1 macro to transform module behaviour Dec 25, 2024
@v0idpwn
Copy link
Collaborator Author

v0idpwn commented Dec 25, 2024

@whatyouhide I believe I addressed all your comments, plus I made a few simplifications. I'll go ahead and merge this :)

@v0idpwn v0idpwn merged commit 8cf602d into main Dec 25, 2024
3 checks passed
@v0idpwn v0idpwn deleted the typespec-for-protobuf-transformer branch December 25, 2024 22:24
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

Successfully merging this pull request may close these issues.

Types using protobuf transformer have wrong typespecs
2 participants