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): Add #[deprecated] to deprecated client methods #1879

Merged
merged 3 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ members = [
"tests/service_named_result",
"tests/use_arc_self",
"tests/default_stubs",
"tests/deprecated_methods",
]
resolver = "2"
16 changes: 16 additions & 0 deletions tests/deprecated_methods/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "deprecated_methods"
edition = "2021"
license = "MIT"
publish = false
version = "0.1.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
prost = "0.13"
tonic = { path = "../../tonic" }

[build-dependencies]
prost-build = "0.13"
tonic-build = { path = "../../tonic-build" }
3 changes: 3 additions & 0 deletions tests/deprecated_methods/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
tonic_build::compile_protos("proto/test.proto").unwrap();
}
14 changes: 14 additions & 0 deletions tests/deprecated_methods/proto/test.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
syntax = "proto3";

package test;

service Service1 {
rpc Deprecated(Request) returns (Response) {
option deprecated = true;
}
rpc NotDeprecated(Request) returns (Response);
}

message Request {}

message Response {}
3 changes: 3 additions & 0 deletions tests/deprecated_methods/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod pb {
tonic::include_proto!("test");
}
13 changes: 13 additions & 0 deletions tests/deprecated_methods/tests/deprecated_methods.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use std::{fs, path::PathBuf};

#[test]
fn test() {
let path = PathBuf::from(std::env::var("OUT_DIR").unwrap()).join("test.rs");
let s = fs::read_to_string(path)
.unwrap()
.split_whitespace()
.collect::<Vec<_>>()
.join(" ");
assert!(s.contains("#[deprecated] pub async fn deprecated("));
assert!(!s.contains("#[deprecated] pub async fn not_deprecated("));
}
7 changes: 5 additions & 2 deletions tonic-build/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::collections::HashSet;

use super::{Attributes, Method, Service};
use crate::{
format_method_name, format_method_path, format_service_name, generate_doc_comments,
naive_snake_case,
format_method_name, format_method_path, format_service_name, generate_deprecated,
generate_doc_comments, naive_snake_case,
};
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
Expand Down Expand Up @@ -176,6 +176,9 @@ fn generate_methods<T: Service>(
if !disable_comments.contains(&format_method_name(service, method, emit_package)) {
stream.extend(generate_doc_comments(method.comment()));
}
if method.deprecated() {
stream.extend(generate_deprecated());
}

let method = match (method.client_streaming(), method.server_streaming()) {
(false, false) => generate_unary(
Expand Down
17 changes: 17 additions & 0 deletions tonic-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ pub trait Method {
fn server_streaming(&self) -> bool;
/// Get comments about this item.
fn comment(&self) -> &[Self::Comment];
/// Method is deprecated.
fn deprecated(&self) -> bool {
false
}
/// Type name of request and response.
fn request_response_name(
&self,
Expand Down Expand Up @@ -240,6 +244,19 @@ fn generate_attributes<'a>(
.collect::<Vec<_>>()
}

fn generate_deprecated() -> TokenStream {
let mut deprecated_stream = TokenStream::new();
deprecated_stream.append(Ident::new("deprecated", Span::call_site()));

let group = Group::new(Delimiter::Bracket, deprecated_stream);

let mut stream = TokenStream::new();
stream.append(Punct::new('#', Spacing::Alone));
stream.append(group);

stream
}

// Generate a singular line of a doc comment
fn generate_doc_comment<S: AsRef<str>>(comment: S) -> TokenStream {
let comment = comment.as_ref();
Expand Down
9 changes: 9 additions & 0 deletions tonic-build/src/manual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ pub struct Method {
client_streaming: bool,
/// Identifies if server streams multiple server messages.
server_streaming: bool,
/// Identifies if the method is deprecated.
deprecated: bool,
/// The path to the codec to use for this method
codec_path: String,
}
Expand Down Expand Up @@ -211,6 +213,10 @@ impl crate::Method for Method {
&self.comments
}

fn deprecated(&self) -> bool {
self.deprecated
}

fn request_response_name(
&self,
_proto_path: &str,
Expand Down Expand Up @@ -262,6 +268,8 @@ pub struct MethodBuilder {
client_streaming: bool,
/// Identifies if server streams multiple server messages.
server_streaming: bool,
/// Identifies if the method is deprecated.
deprecated: bool,
/// The path to the codec to use for this method
codec_path: Option<String>,
}
Expand Down Expand Up @@ -338,6 +346,7 @@ impl MethodBuilder {
output_type: self.output_type.unwrap(),
client_streaming: self.client_streaming,
server_streaming: self.server_streaming,
deprecated: self.deprecated,
codec_path: self.codec_path.unwrap(),
}
}
Expand Down
4 changes: 4 additions & 0 deletions tonic-build/src/prost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ impl crate::Method for TonicBuildMethod {
&self.prost_method.comments.leading[..]
}

fn deprecated(&self) -> bool {
self.prost_method.options.deprecated.unwrap_or_default()
}

fn request_response_name(
&self,
proto_path: &str,
Expand Down