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

Fixes: #1156 Prevent error when "Result" is used as name of service in proto #1203

Merged
merged 7 commits into from
Dec 21, 2022

Conversation

eskawl
Copy link
Contributor

@eskawl eskawl commented Dec 17, 2022

Motivation

Fixes a name conflict when the service itself is named as Result. The conflict occurs with the std::result::Result included in the prelude. Issue described in #1156.

Solution

To prevent the conflict, the generated code will now refer to the Result in a qualified manner viz, std::result::Result.

Test Sample

result.proto

syntax = "proto3";

import "google/protobuf/any.proto";
import "google/protobuf/empty.proto";

option go_package = "github.com/stevenhansel/binus-logbook/scraper/internal/grpc/proto/result";

package result;

service Result {
  rpc Listen(google.protobuf.Empty) returns (Reply) {}
}

message Reply {
  int32 step = 1;
  string name = 2;
}

server.rs

use tonic::{transport::Server, Request, Response, Status};

pub mod pb {
    tonic::include_proto!("result");
}

use pb::{Reply, result_server};


#[derive(Debug, Default)]
pub struct ResultAPI {}

#[tonic::async_trait]
impl result_server::Result for ResultAPI {
    async fn listen(&self, _request: Request<()>) -> Result<Response<Reply>, Status> {
        return Ok(Response::new(Reply { step: 1, name: "23".to_string() }))
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let addr = "[::1]:50051".parse()?;
    let api = ResultAPI::default();

    Server::builder()
        .add_service(result_server::ResultServer::new(api))
        .serve(addr)
        .await?;

    Ok(())
}

client.rs

pub mod pb {
    tonic::include_proto!("result");
}

use pb::{result_client};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut client = result_client::ResultClient::connect("http://[::1]:50051").await?;

    let request = tonic::Request::new(());

    let response = client.listen(request).await?;

    println!("RESPONSE={:?}", response);

    Ok(())
}

This is done to prevent git diff testcase in bechmark.rs  from failing.
This is done to prevent git diff testcase in bechmark.rs  from failing.
@eskawl eskawl changed the title Prevent error when "Result" is used as name of service in proto #1156 Fixes: #1156 Prevent error when "Result" is used as name of service in proto Dec 17, 2022
Copy link
Member

@LucioFranco LucioFranco left a comment

Choose a reason for hiding this comment

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

LGTM I think we just need to remove that license file

tests/service_named_result/LICENSE Show resolved Hide resolved
@eskawl eskawl requested a review from LucioFranco December 20, 2022 16:35
@LucioFranco LucioFranco merged commit a562a3c into hyperium:master Dec 21, 2022
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