Skip to content

Commit

Permalink
fix(build): snake_case service names (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
alce authored and LucioFranco committed Dec 14, 2019
1 parent 82facb6 commit 3a5c66d
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 14 deletions.
2 changes: 1 addition & 1 deletion examples/src/routeguide/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub mod route_guide {
tonic::include_proto!("routeguide");
}

use route_guide::routeguide_client::RouteGuideClient;
use route_guide::route_guide_client::RouteGuideClient;

async fn print_features(client: &mut RouteGuideClient<Channel>) -> Result<(), Box<dyn Error>> {
let rectangle = Rectangle {
Expand Down
6 changes: 3 additions & 3 deletions examples/src/routeguide/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ pub mod routeguide {
tonic::include_proto!("routeguide");
}

use routeguide::{routeguide_server, Feature, Point, Rectangle, RouteNote, RouteSummary};
use routeguide::{route_guide_server, Feature, Point, Rectangle, RouteNote, RouteSummary};

#[derive(Debug)]
pub struct RouteGuide {
features: Arc<Vec<Feature>>,
}

#[tonic::async_trait]
impl routeguide_server::RouteGuide for RouteGuide {
impl route_guide_server::RouteGuide for RouteGuide {
async fn get_feature(&self, request: Request<Point>) -> Result<Response<Feature>, Status> {
println!("GetFeature = {:?}", request);

Expand Down Expand Up @@ -154,7 +154,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
features: Arc::new(data::load()),
};

let svc = routeguide_server::RouteGuideServer::new(route_guide);
let svc = route_guide_server::RouteGuideServer::new(route_guide);

Server::builder().add_service(svc).serve(addr).await?;

Expand Down
2 changes: 1 addition & 1 deletion interop/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
pb::testservice_client::*, pb::unimplementedservice_client::*, pb::*, test_assert,
pb::test_service_client::*, pb::unimplemented_service_client::*, pb::*, test_assert,
TestAssertion,
};
use futures_util::{future, stream, StreamExt};
Expand Down
8 changes: 4 additions & 4 deletions interop/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use std::pin::Pin;
use std::time::Duration;
use tonic::{Code, Request, Response, Status};

pub use pb::testservice_server::TestServiceServer;
pub use pb::unimplementedservice_server::UnimplementedServiceServer;
pub use pb::test_service_server::TestServiceServer;
pub use pb::unimplemented_service_server::UnimplementedServiceServer;

#[derive(Default, Clone)]
pub struct TestService;
Expand All @@ -18,7 +18,7 @@ type Stream<T> = Pin<
>;

#[tonic::async_trait]
impl pb::testservice_server::TestService for TestService {
impl pb::test_service_server::TestService for TestService {
async fn empty_call(&self, _request: Request<Empty>) -> Result<Empty> {
Ok(Response::new(Empty {}))
}
Expand Down Expand Up @@ -154,7 +154,7 @@ impl pb::testservice_server::TestService for TestService {
pub struct UnimplementedService;

#[tonic::async_trait]
impl pb::unimplementedservice_server::UnimplementedService for UnimplementedService {
impl pb::unimplemented_service_server::UnimplementedService for UnimplementedService {
async fn unimplemented_call(&self, _req: Request<Empty>) -> Result<Empty> {
Err(Status::unimplemented(""))
}
Expand Down
2 changes: 1 addition & 1 deletion tests/included_service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ pub mod pb {
// Ensure that an RPC service, defined before including a file that defines
// another service in a different protocol buffer package, is not incorrectly
// cleared from the context of its package.
type _Test = dyn pb::topservice_server::TopService;
type _Test = dyn pb::top_service_server::TopService;
4 changes: 2 additions & 2 deletions tonic-build/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::generate_doc_comments;
use crate::{generate_doc_comments, naive_snake_case};
use proc_macro2::TokenStream;
use prost_build::{Method, Service};
use quote::{format_ident, quote};

pub(crate) fn generate(service: &Service, proto: &str) -> TokenStream {
let service_ident = quote::format_ident!("{}Client", service.name);
let client_mod = quote::format_ident!("{}_client", service.name.to_ascii_lowercase());
let client_mod = quote::format_ident!("{}_client", naive_snake_case(&service.name));
let methods = generate_methods(service, proto);

let connect = generate_connect(&service_ident);
Expand Down
28 changes: 28 additions & 0 deletions tonic-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,31 @@ fn replace_wellknown(proto_path: &str, method: &Method) -> (TokenStream, TokenSt

(request, response)
}

fn naive_snake_case(name: &str) -> String {
let mut s = String::new();
let mut it = name.chars().peekable();

while let Some(x) = it.next() {
s.push(x.to_ascii_lowercase());
if let Some(y) = it.peek() {
if y.is_uppercase() {
s.push('_');
}
}
}

s
}

#[test]
fn test_snake_case() {
for case in &[
("Service", "service"),
("ThatHasALongName", "that_has_a_long_name"),
("greeter", "greeter"),
("ABCServiceX", "a_b_c_service_x"),
] {
assert_eq!(naive_snake_case(case.0), case.1)
}
}
4 changes: 2 additions & 2 deletions tonic-build/src/server.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{generate_doc_comment, generate_doc_comments};
use crate::{generate_doc_comment, generate_doc_comments, naive_snake_case};
use proc_macro2::{Span, TokenStream};
use prost_build::{Method, Service};
use quote::quote;
Expand All @@ -9,7 +9,7 @@ pub(crate) fn generate(service: &Service, proto_path: &str) -> TokenStream {

let server_service = quote::format_ident!("{}Server", service.name);
let server_trait = quote::format_ident!("{}", service.name);
let server_mod = quote::format_ident!("{}_server", service.name.to_ascii_lowercase());
let server_mod = quote::format_ident!("{}_server", naive_snake_case(&service.name));
let generated_trait = generate_trait(service, proto_path, server_trait.clone());
let service_doc = generate_doc_comments(&service.comments.leading);

Expand Down

0 comments on commit 3a5c66d

Please sign in to comment.