Skip to content

Commit

Permalink
feat: @link directive support proto paths
Browse files Browse the repository at this point in the history
  • Loading branch information
DLillard0 committed Dec 14, 2024
1 parent 76fc565 commit 2ab74f0
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 4 deletions.
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
13 changes: 12 additions & 1 deletion src/cli/generator/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,18 @@ impl Generator {
if let Some(relative_path_to_proto) = to_relative_path(output_dir, &path) {
metadata.path = relative_path_to_proto;
}
input_samples.push(Input::Proto { metadata, url, connect_rpc });
let relative_proto_paths = proto_paths.map(|paths| {
paths
.iter()
.filter_map(|p| to_relative_path(output_dir, p))
.collect::<Vec<_>>()
});
input_samples.push(Input::Proto {
metadata,
url,
connect_rpc,
proto_paths: relative_proto_paths,
});
}
Source::Config { src } => {
let path = src.0;
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/config/transformer/ambiguous_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ mod tests {
metadata: ProtoMetadata { descriptor_set: set, path: news_proto.to_string() },
url,
connect_rpc: None,
proto_paths: None,
}])
.generate(false)?;

Expand Down
11 changes: 9 additions & 2 deletions src/core/generator/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub enum Input {
url: String,
metadata: ProtoMetadata,
connect_rpc: Option<bool>,
proto_paths: Option<Vec<String>>,
},
Config {
schema: String,
Expand Down Expand Up @@ -85,11 +86,13 @@ impl Generator {
}

/// Generates the configuration from the provided protobuf.
#[allow(clippy::too_many_arguments)]
fn generate_from_proto(
&self,
metadata: &ProtoMetadata,
operation_name: &str,
url: &str,
proto_paths: &Option<Vec<String>>,
) -> anyhow::Result<Config> {
let descriptor_set = resolve_file_descriptor_set(metadata.descriptor_set.clone())?;
let mut config = from_proto(&[descriptor_set], operation_name, url)?;
Expand All @@ -99,6 +102,7 @@ impl Generator {
type_of: LinkType::Protobuf,
headers: None,
meta: None,
proto_paths: proto_paths.to_owned(),
});
Ok(config)
}
Expand Down Expand Up @@ -135,8 +139,9 @@ impl Generator {
config = config
.merge_right(self.generate_from_json(&type_name_generator, &[req_sample])?);
}
Input::Proto { metadata, url, connect_rpc } => {
let proto_config = self.generate_from_proto(metadata, &self.query, url)?;
Input::Proto { metadata, url, connect_rpc, proto_paths } => {
let proto_config =
self.generate_from_proto(metadata, &self.query, url, proto_paths)?;
let proto_config = if connect_rpc == &Some(true) {
ConnectRPC.transform(proto_config).to_result()?
} else {
Expand Down Expand Up @@ -272,6 +277,7 @@ pub mod test {
},
url: "http://localhost:50051".to_string(),
connect_rpc: None,
proto_paths: None,
}])
.generate(false)?;

Expand Down Expand Up @@ -326,6 +332,7 @@ pub mod test {
},
url: "http://localhost:50051".to_string(),
connect_rpc: None,
proto_paths: None,
};

// Config input
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
1 change: 1 addition & 0 deletions src/core/grpc/protobuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ pub mod tests {
type_of: LinkType::Protobuf,
headers: None,
meta: None,
proto_paths: None,
}]);

let method = GrpcMethod { package: id, service: "a".to_owned(), name: "b".to_owned() };
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 2ab74f0

Please sign in to comment.