-
Notifications
You must be signed in to change notification settings - Fork 145
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
Conversation
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.
475292b
to
a6c4647
Compare
@whatyouhide @ericmj if you agree on approach here, I'll add a few tests, documentation, etc :) |
lib/protobuf/dsl.ex
Outdated
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__) |
There was a problem hiding this comment.
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 :)
b80bf30
to
3ea9f46
Compare
I tested this in our codebases and it resolves dialyzer issues. We are using transformers for, for example, well known types. |
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. |
There was a problem hiding this 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.
18fb755
to
b9c00da
Compare
typespec/1
macro to transform module behaviour
@whatyouhide I believe I addressed all your comments, plus I made a few simplifications. I'll go ahead and merge this :) |
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 thebefore_compile
callback to set the typespec, which is set in the form of:Then, transform modules can implement proper typespecs.
No changes if
transform_module
is not overriden.Closes #382