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

Allow file descriptor be generated without --include_source_info #786

Merged
merged 2 commits into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions prost-build/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ pub struct Service {
/// The package name as it appears in the .proto file.
pub package: String,
/// The service comments.
pub comments: Comments,
pub comments: Option<Comments>,
Copy link
Member

Choose a reason for hiding this comment

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

instead of making this breaking change can we instead make it so comments has a Comments::default()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looking at prost-build/src/lib.rs:154, the ast module isn't public, so I assumed this was an implementation detail of prost-build.

mod ast

Did I misunderstand that somehow?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh, nevermind. Missed line 147:

pub use crate::ast::{Comments, Method, Service};

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a derived Default to Comment. I went that direction instead of directly defining one since all of the fields of Comment are public, so unlikely to change, and the derived default is what I'd expect here.

/// The service methods.
pub methods: Vec<Method>,
/// The service options.
Expand All @@ -149,7 +149,7 @@ pub struct Method {
/// The name of the method as it appears in the .proto file.
pub proto_name: String,
/// The method comments.
pub comments: Comments,
pub comments: Option<Comments>,
/// The input Rust type.
pub input_type: String,
/// The output Rust type.
Expand Down
38 changes: 19 additions & 19 deletions prost-build/src/code_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ enum Syntax {
pub struct CodeGenerator<'a> {
config: &'a mut Config,
package: String,
source_info: SourceCodeInfo,
source_info: Option<SourceCodeInfo>,
syntax: Syntax,
message_graph: &'a MessageGraph,
extern_paths: &'a ExternPaths,
Expand All @@ -51,16 +51,14 @@ impl<'a> CodeGenerator<'a> {
file: FileDescriptorProto,
buf: &mut String,
) {
let mut source_info = file
.source_code_info
.expect("no source code info in request");
source_info.location.retain(|location| {
let len = location.path.len();
len > 0 && len % 2 == 0
let source_info = file.source_code_info.map(|mut s| {
s.location.retain(|loc| {
let len = loc.path.len();
len > 0 && len % 2 == 0
});
s.location.sort_by(|a, b| a.path.cmp(&b.path));
s
});
source_info
.location
.sort_by_key(|location| location.path.clone());

let syntax = match file.syntax.as_ref().map(String::as_str) {
None | Some("proto2") => Syntax::Proto2,
Expand Down Expand Up @@ -569,14 +567,13 @@ impl<'a> CodeGenerator<'a> {
self.buf.push_str("}\n");
}

fn location(&self) -> &Location {
let idx = self
.source_info
fn location(&self) -> Option<&Location> {
let source_info = self.source_info.as_ref()?;
let idx = source_info
.location
.binary_search_by_key(&&self.path[..], |location| &location.path[..])
.unwrap();

&self.source_info.location[idx]
Some(&source_info.location[idx])
}

fn append_doc(&mut self, fq_name: &str, field_name: Option<&str>) {
Expand All @@ -589,7 +586,9 @@ impl<'a> CodeGenerator<'a> {
self.config.disable_comments.get(fq_name).next().is_none()
};
if append_doc {
Comments::from_location(self.location()).append_with_indent(self.depth, self.buf)
if let Some(comments) = self.location().map(Comments::from_location) {
comments.append_with_indent(self.depth, self.buf);
}
}
}

Expand Down Expand Up @@ -715,7 +714,7 @@ impl<'a> CodeGenerator<'a> {

for variant in variant_mappings.iter() {
self.push_indent();
self.buf.push_str("\"");
self.buf.push('\"');
self.buf.push_str(variant.proto_name);
self.buf.push_str("\" => Some(Self::");
self.buf.push_str(&variant.generated_variant_name);
Expand All @@ -742,7 +741,7 @@ impl<'a> CodeGenerator<'a> {
let name = service.name().to_owned();
debug!(" service: {:?}", name);

let comments = Comments::from_location(self.location());
let comments = self.location().map(Comments::from_location);

self.path.push(2);
let methods = service
Expand All @@ -751,8 +750,9 @@ impl<'a> CodeGenerator<'a> {
.enumerate()
.map(|(idx, mut method)| {
debug!(" method: {:?}", method.name());

self.path.push(idx as i32);
let comments = Comments::from_location(self.location());
let comments = self.location().map(Comments::from_location);
self.path.pop();

let name = method.name.take().unwrap();
Expand Down
11 changes: 7 additions & 4 deletions prost-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,8 +664,7 @@ impl Config {

/// In combination with with `file_descriptor_set_path`, this can be used to provide a file
/// descriptor set as an input file, rather than having prost-build generate the file by calling
/// protoc. Prost-build does require that the descriptor set was generated with
/// --include_source_info.
/// protoc.
///
/// In `build.rs`:
///
Expand Down Expand Up @@ -1333,12 +1332,16 @@ mod tests {
impl ServiceGenerator for ServiceTraitGenerator {
fn generate(&mut self, service: Service, buf: &mut String) {
// Generate a trait for the service.
service.comments.append_with_indent(0, buf);
if let Some(comments) = service.comments {
comments.append_with_indent(0, buf);
}
buf.push_str(&format!("trait {} {{\n", &service.name));

// Generate the service methods.
for method in service.methods {
method.comments.append_with_indent(1, buf);
if let Some(comments) = method.comments {
comments.append_with_indent(1, buf);
}
buf.push_str(&format!(
" fn {}(_: {}) -> {};\n",
method.name, method.input_type, method.output_type
Expand Down