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

feat(build): allow optional service method return overrides #1

Merged
merged 1 commit into from
Apr 17, 2023

Conversation

NickLarsenNZ
Copy link

@NickLarsenNZ NickLarsenNZ commented Apr 16, 2023

Motivation

As per my comments:

It would be nice to have configurable return values.

Solution

Warning:

  • There are no tests (yet), and I have not run this, only run cargo build.
  • We might need to consider adding proto package names to the mapping in case of duplicate service names in different packages.
  • I have mostly copied existing patterns, so some signatures contain references even though copies/clones are occurring.
  • Docs and variable/method names could use some massaging.

Here's an example of the feature in use (I have not yet tested it):

fn main() -> Result<(), Box<dyn std::error::Error>> {
   tonic_build::configure()
        .service_return_overrides(tonic_build::prost::ServiceReturnOverrideMap::from([
            (String::from("Echo.unary_echo"), ServiceReturnOverride::None), // implied if no mapping is made for the service. trait method implementation therefore required.
            (String::from("Echo.server_streaming_echo"), ServiceReturnOverride::OkDefault), // return the default value for the response message.
            (String::from("Echo.client_streaming_echo"), ServiceReturnOverride::ErrUnimplemented), // return the unimplemented error status.
            // any other service not mapped here will require an explicit trait method implementation.
        ]))
        .compile(
            &["proto/echo/echo.proto"],
            &["proto/echo"],
        )?;
   Ok(())
}

I did consider another variant to allow custom tonic::Status errors to be returned, but this was a little tricky without importing tonic into tonic-build - only for access to that single enum. It can be added later if the need surfaces.

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.

2 participants