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

fix(grpc): import proto files relative to parent proto #1705

Merged
merged 3 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions src/grpc/tests/proto/cycle.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ syntax = "proto3";

package cycle;

import "src/grpc/tests/proto/nested0.proto";
import "src/grpc/tests/proto/duplicate.proto";
import "nested0.proto";
import "duplicate.proto";
4 changes: 2 additions & 2 deletions src/grpc/tests/proto/duplicate.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ syntax = "proto3";

package duplicate;

import "src/grpc/tests/proto/greetings.proto";
import "src/grpc/tests/proto/news.proto";
import "greetings.proto";
import "news.proto";
6 changes: 3 additions & 3 deletions src/grpc/tests/proto/nested0.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ syntax = "proto3";

package nested0;

import "src/grpc/tests/proto/greetings.proto";
import "src/grpc/tests/proto/nested1.proto";
import "src/grpc/tests/proto/news_no_pkg.proto";
import "greetings.proto";
import "nested1.proto";
import "news_no_pkg.proto";
4 changes: 2 additions & 2 deletions src/grpc/tests/proto/nested1.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ syntax = "proto3";

package nested1;

import "src/grpc/tests/proto/news.proto";
import "src/grpc/tests/proto/cycle.proto";
import "news.proto";
import "cycle.proto";
41 changes: 33 additions & 8 deletions src/proto_reader.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::{HashMap, VecDeque};
use std::path::{Path, PathBuf};

use anyhow::Context;
use futures_util::future::join_all;
Expand Down Expand Up @@ -31,12 +32,14 @@
}

pub async fn read<T: AsRef<str>>(&self, path: T) -> anyhow::Result<ProtoMetadata> {
let file_read = self.read_proto(path.as_ref()).await?;
let file_read = self.read_proto(path.as_ref(), None).await?;
if file_read.package.is_none() {
anyhow::bail!("Package name is required");
}

let descriptors = self.resolve_descriptors(file_read).await?;
let descriptors = self
.resolve_descriptors(file_read, PathBuf::from(path.as_ref()).parent())
.await?;

Check warning on line 42 in src/proto_reader.rs

View check run for this annotation

Codecov / codecov/patch

src/proto_reader.rs#L42

Added line #L42 was not covered by tests
let metadata = ProtoMetadata {
descriptor_set: FileDescriptorSet { file: descriptors },
path: path.as_ref().to_string(),
Expand All @@ -48,6 +51,7 @@
async fn resolve_descriptors(
&self,
parent_proto: FileDescriptorProto,
parent_path: Option<&Path>,
) -> anyhow::Result<Vec<FileDescriptorProto>> {
let mut descriptors: HashMap<String, FileDescriptorProto> = HashMap::new();
let mut queue = VecDeque::new();
Expand All @@ -57,7 +61,7 @@
let futures: Vec<_> = file
.dependency
.iter()
.map(|import| self.read_proto(import))
.map(|import| self.read_proto(import, parent_path))
.collect();

let results = join_all(futures).await;
Expand All @@ -80,15 +84,35 @@

/// Tries to load well-known google proto files and if not found uses normal
/// file and http IO to resolve them
async fn read_proto(&self, path: &str) -> anyhow::Result<FileDescriptorProto> {
let content = if let Ok(file) = GoogleFileResolver::new().open_file(path) {
async fn read_proto<T: AsRef<str>>(
&self,
path: T,
parent_dir: Option<&Path>,
) -> anyhow::Result<FileDescriptorProto> {
let content = if let Ok(file) = GoogleFileResolver::new().open_file(path.as_ref()) {
file.source()
.context("Unable to extract content of google well-known proto file")?
.to_string()
} else {
let path = Self::resolve_path(path.as_ref(), parent_dir);
self.resource_reader.read_file(path).await?.content
};
Ok(protox_parse::parse(path, &content)?)
Ok(protox_parse::parse(path.as_ref(), &content)?)
}
/// Checks if path is absolute else it joins file path with relative dir
/// path
fn resolve_path(src: &str, root_dir: Option<&Path>) -> String {
if src.starts_with("http") {
return src.to_string();
}

if Path::new(&src).is_absolute() {
src.to_string()
} else if let Some(path) = root_dir {
path.join(src).to_string_lossy().to_string()
} else {
src.to_string()

Check warning on line 114 in src/proto_reader.rs

View check run for this annotation

Codecov / codecov/patch

src/proto_reader.rs#L114

Added line #L114 was not covered by tests
}
}
}

Expand All @@ -106,7 +130,7 @@
// Skipping IO tests as they are covered in reader.rs
let reader = ProtoReader::init(crate::runtime::test::init(None));
reader
.read_proto("google/protobuf/empty.proto")
.read_proto("google/protobuf/empty.proto", None)
.await
.unwrap();
}
Expand Down Expand Up @@ -134,8 +158,9 @@
let file_rt = runtime.file.clone();

let reader = ProtoReader::init(runtime);
let pathbuf = PathBuf::from(&test_file);
let helper_map = reader
.resolve_descriptors(reader.read_proto(&test_file).await?)
.resolve_descriptors(reader.read_proto(&test_file, None).await?, pathbuf.parent())
.await?;
let files = test_dir.read_dir()?;
for file in files {
Expand Down
Loading