Skip to content

Commit

Permalink
feat: link config support proto paths
Browse files Browse the repository at this point in the history
  • Loading branch information
DLillard0 committed Dec 16, 2024
1 parent 76fc565 commit f4496e6
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 1 deletion.
10 changes: 10 additions & 0 deletions generated/.tailcallrc.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,16 @@
"meta": {
"description": "Additional metadata pertaining to the linked resource."
},
"proto_paths": {
"description": "The proto paths to be used when resolving dependencies. Only valid when [`Link::type_of`] is [`LinkType::Protobuf`]",
"type": [
"array",
"null"
],
"items": {
"type": "string"
}
},
"src": {
"description": "The source of the link. It can be a URL or a path to a file. If a path is provided, it is relative to the file that imports the link.",
"type": "string"
Expand Down
5 changes: 5 additions & 0 deletions src/core/config/directives/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,9 @@ pub struct Link {
/// Additional metadata pertaining to the linked resource.
#[serde(default, skip_serializing_if = "is_default")]
pub meta: Option<serde_json::Value>,
///
/// The proto paths to be used when resolving dependencies.
/// Only valid when [`Link::type_of`] is [`LinkType::Protobuf`]
#[serde(default, skip_serializing_if = "is_default")]
pub proto_paths: Option<Vec<String>>,
}
8 changes: 7 additions & 1 deletion src/core/config/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@ impl ConfigReader {
});
}
LinkType::Protobuf => {
let meta = self.proto_reader.read(path, None).await?;
let proto_paths = link.proto_paths.as_ref().map(|paths| {
paths
.iter()
.map(|p| Self::resolve_path(p, parent_dir))
.collect::<Vec<_>>()
});
let meta = self.proto_reader.read(path, proto_paths.as_deref()).await?;
extensions.add_proto(meta);
}
LinkType::Script => {
Expand Down
1 change: 1 addition & 0 deletions src/core/generator/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ impl Generator {
type_of: LinkType::Protobuf,
headers: None,
meta: None,
proto_paths: None,
});
Ok(config)
}
Expand Down
1 change: 1 addition & 0 deletions src/core/grpc/data_loader_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ mod tests {
type_of: LinkType::Protobuf,
headers: None,
meta: None,
proto_paths: None,
}]);
let method = GrpcMethod {
package: "greetings".to_string(),
Expand Down
48 changes: 48 additions & 0 deletions src/core/grpc/protobuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,18 @@ pub mod tests {
use crate::core::config::{Config, Field, Grpc, Link, LinkType, Resolver, Type};

pub async fn get_proto_file(path: &str) -> Result<FileDescriptorSet> {
get_proto_file_with_config(path, LinkConfig::default()).await
}

#[derive(Default)]
pub struct LinkConfig {
proto_paths: Option<Vec<String>>,
}

pub async fn get_proto_file_with_config(
path: &str,
link_config: LinkConfig,
) -> Result<FileDescriptorSet> {
let runtime = crate::core::runtime::test::init(None);
let reader = ConfigReader::init(runtime);

Expand All @@ -268,6 +280,7 @@ pub mod tests {
type_of: LinkType::Protobuf,
headers: None,
meta: None,
proto_paths: link_config.proto_paths,
}]);

let method = GrpcMethod { package: id, service: "a".to_owned(), name: "b".to_owned() };
Expand Down Expand Up @@ -395,6 +408,41 @@ pub mod tests {
Ok(())
}

#[tokio::test]
async fn news_proto_file_with_proto_paths() -> Result<()> {
let grpc_method = GrpcMethod::try_from("news.NewsService.GetNews").unwrap();

let path: &str = protobuf::NEWS_PROTO_PATHS;
let proto_paths = Some(vec![Path::new(path)
.ancestors()
.nth(2)
.unwrap()
.to_string_lossy()
.to_string()]);
let file = ProtobufSet::from_proto_file(
get_proto_file_with_config(path, LinkConfig { proto_paths }).await?,
)?;
let service = file.find_service(&grpc_method)?;
let operation = service.find_operation(&grpc_method)?;

let input = operation.convert_input(r#"{ "id": 1 }"#)?;

assert_eq!(input, b"\0\0\0\0\x02\x08\x01");

let output = b"\0\0\0\x005\x08\x01\x12\x06Note 1\x1a\tContent 1\"\x0cPost image 1";

let parsed = operation.convert_output::<serde_json::Value>(output)?;

assert_eq!(
serde_json::to_value(parsed)?,
json!({
"id": 1, "title": "Note 1", "body": "Content 1", "postImage": "Post image 1", "status": "PUBLISHED"
})
);

Ok(())
}

#[tokio::test]
async fn oneof_proto_file() -> Result<()> {
let grpc_method = GrpcMethod::try_from("oneof.OneOfService.GetOneOf").unwrap();
Expand Down
1 change: 1 addition & 0 deletions src/core/grpc/request_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ mod tests {
type_of: LinkType::Protobuf,
headers: None,
meta: None,
proto_paths: None,
}]);
let method = GrpcMethod {
package: id.to_string(),
Expand Down

0 comments on commit f4496e6

Please sign in to comment.