-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Description
Feature Request
Crates
tonic-build
Motivation
Currently, tonic-build generates trait without any way to attach additional attributes or proc-macros.
This makes it difficult to integrate with frameworks or tools that rely on macros, such as automocking struct by trait
Proposal
Add a configuration option in tonic-build to inject custom attributes into generated service traits:
In build options
...
.trait_atttribute("myservice.MyService", "#[mockall::automock]")
...
In generated code (Before)
/// Generated trait containing gRPC methods that should be implemented for use with ProjectsPrivateServer.
#[async_trait]
pub trait MyServce: std::marker::Send + std::marker::Sync + 'static {
...
}
In generated code (After)
/// Generated trait containing gRPC methods that should be implemented for use with ProjectsPrivateServer.
#[async_trait]
#[mockall::automock]
pub trait MyServce: std::marker::Send + std::marker::Sync + 'static {
...
}
Alternatives
I ran into this limitation when trying to use #[mockall::automock]
for a generated service trait to simplify testing.
In my case:
-
I use the trait to describe a client interface for another service.
-
Functions in my code accept a generic parameter implementing this trait, so I can pass either:
- the real gRPC client generated alongside the trait, or
- a mocked implementation for tests.
Without this feature, mocking is cumbersome because I must manually implement the entire trait for mocks instead of simply deriving it via mockall
.
While my primary need is mocking, I believe there are many other potential use cases for injecting proc-macro attributes, such as logging, tracing, or applying custom derives.
If this feature aligns with the vision of the project, I am willing to implement the functionality and open a Pull Request