Skip to content

Commit dfddee6

Browse files
committed
test(protobuf): add ToProto type inference canary
Adds an assert_no_upstream_toproto! canary test in integration-tests-protobuf covering external model types converted via gaxi::prost::ToProto on main today. If an upstream crate adds a ToProto<OtherProst> implementation for one of these types, the unqualified .to_proto() call in the canary test fails with E0284 before breaking downstream crates. For googleapis#7284
1 parent 4b873f1 commit dfddee6

3 files changed

Lines changed: 126 additions & 3 deletions

File tree

‎Cargo.lock‎

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/protobuf/Cargo.toml‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,15 @@ google-cloud-wkt.workspace = true
3939
tokio.workspace = true
4040

4141
[dev-dependencies]
42-
mockall.workspace = true
43-
serde_json.workspace = true
44-
test-case.workspace = true
42+
gaxi = { workspace = true, features = ["_internal-grpc-client"] }
43+
google-cloud-api.workspace = true
44+
google-cloud-logging-type.workspace = true
45+
google-cloud-longrunning.workspace = true
46+
google-cloud-rpc.workspace = true
47+
google-cloud-type.workspace = true
48+
mockall.workspace = true
49+
serde_json.workspace = true
50+
test-case.workspace = true
4551

4652
[lints]
4753
workspace = true

‎tests/protobuf/src/lib.rs‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,3 +516,114 @@ async fn cleanup_stale_secrets(
516516

517517
Ok(())
518518
}
519+
520+
#[cfg(test)]
521+
mod toproto_canary {
522+
use gaxi::prost::{ConvertError, ToProto};
523+
524+
struct LocalProst;
525+
526+
// Guards external model types converted via `gaxi::prost::ToProto` in
527+
// already-published crates on crates.io (which still have unqualified
528+
// `.to_proto()` calls) as well as any future unqualified `.to_proto()`
529+
// calls on `main`. If an upstream crate (such as `gaxi`) adds a
530+
// `ToProto<OtherProst>` implementation for one of these types, the
531+
// unqualified `.to_proto()` call below will fail to compile with
532+
// `E0284: type annotations needed`, alerting us before breaking downstream
533+
// crates.
534+
macro_rules! assert_no_upstream_toproto {
535+
(
536+
types: [ $( $ty:ty ),* $(,)? ],
537+
exprs: [ $( $ety:ty => $expr:expr ),* $(,)? ] $(,)?
538+
) => {
539+
$(
540+
impl ToProto<LocalProst> for $ty {
541+
type Output = LocalProst;
542+
fn to_proto(self) -> std::result::Result<LocalProst, ConvertError> {
543+
Ok(LocalProst)
544+
}
545+
}
546+
)*
547+
$(
548+
impl ToProto<LocalProst> for $ety {
549+
type Output = LocalProst;
550+
fn to_proto(self) -> std::result::Result<LocalProst, ConvertError> {
551+
Ok(LocalProst)
552+
}
553+
}
554+
)*
555+
556+
#[test]
557+
fn no_upstream_toproto_ambiguity() -> std::result::Result<(), ConvertError> {
558+
$(
559+
let _: LocalProst = <$ty>::default().to_proto()?;
560+
)*
561+
$(
562+
let _: LocalProst = ($expr).to_proto()?;
563+
)*
564+
Ok(())
565+
}
566+
};
567+
}
568+
569+
assert_no_upstream_toproto!(
570+
types: [
571+
// Converted in `gax-internal`, `bigquery`, `firestore`, `spanner`,
572+
// `storage`, `aiplatform-v1`, `ces-v1`, `dialogflow-cx-v3`,
573+
// `dialogflow-v2`, `discoveryengine-v1`, `geminidataanalytics-v1`,
574+
// `retail-v2`, `speech-v2`, `visionai-v1`, `logging-v2`, `routes-v1`,
575+
// `routing-v2`, and `showcase`.
576+
google_cloud_rpc::model::Status,
577+
// Converted in `storage`, `firestore`, `aiplatform-v1`, `dialogflow-cx-v3`,
578+
// `dialogflow-v2`, `discoveryengine-v1`, `geminidataanalytics-v1`,
579+
// `modelarmor-v1`, `retail-v2`, `speech-v2`, `visionai-v1`, `routes-v1`,
580+
// `routing-v2`, and `showcase`.
581+
google_cloud_type::model::Date,
582+
google_cloud_type::model::Expr,
583+
google_cloud_type::model::Interval,
584+
google_cloud_type::model::LatLng,
585+
google_cloud_type::model::LocalizedText,
586+
google_cloud_type::model::Money,
587+
// Converted in `aiplatform-v1`, `apigeeconnect-v1`, `ces-v1`,
588+
// `dialogflow-cx-v3`, `dialogflow-v2`, `discoveryengine-v1`, and `logging-v2`.
589+
google_cloud_api::model::HttpBody,
590+
google_cloud_api::model::MonitoredResource,
591+
// Converted in `logging-v2`.
592+
google_cloud_logging_type::model::HttpRequest,
593+
google_cloud_logging_type::model::LogSeverity,
594+
// Converted in `storage`, `aiplatform-v1`, `biglake-hive-v1`, and `showcase`.
595+
google_cloud_iam_v1::model::AuditConfig,
596+
google_cloud_iam_v1::model::AuditConfigDelta,
597+
google_cloud_iam_v1::model::AuditLogConfig,
598+
google_cloud_iam_v1::model::Binding,
599+
google_cloud_iam_v1::model::BindingDelta,
600+
google_cloud_iam_v1::model::GetIamPolicyRequest,
601+
google_cloud_iam_v1::model::GetPolicyOptions,
602+
google_cloud_iam_v1::model::Policy,
603+
google_cloud_iam_v1::model::PolicyDelta,
604+
google_cloud_iam_v1::model::SetIamPolicyRequest,
605+
google_cloud_iam_v1::model::TestIamPermissionsRequest,
606+
google_cloud_iam_v1::model::TestIamPermissionsResponse,
607+
google_cloud_iam_v1::model::audit_config_delta::Action,
608+
google_cloud_iam_v1::model::audit_log_config::LogType,
609+
google_cloud_iam_v1::model::binding_delta::Action,
610+
// Converted in `storage`, `aiplatform-v1`, `ces-v1`, `datacatalog-lineage-v1`,
611+
// `dialogflow-cx-v3`, `dialogflow-v2`, `discoveryengine-v1`,
612+
// `geminidataanalytics-v1`, `retail-v2`, `speech-v2`, `texttospeech-v1`,
613+
// `visionai-v1`, `logging-v2`, and `showcase`.
614+
google_cloud_longrunning::model::CancelOperationRequest,
615+
google_cloud_longrunning::model::DeleteOperationRequest,
616+
google_cloud_longrunning::model::GetOperationRequest,
617+
google_cloud_longrunning::model::ListOperationsRequest,
618+
google_cloud_longrunning::model::ListOperationsResponse,
619+
google_cloud_longrunning::model::Operation,
620+
google_cloud_longrunning::model::OperationInfo,
621+
google_cloud_longrunning::model::WaitOperationRequest,
622+
],
623+
exprs: [
624+
// Converted in `storage` and generated LRO-enabled gRPC crates.
625+
google_cloud_longrunning::model::operation::Result =>
626+
google_cloud_longrunning::model::operation::Result::Error(Default::default()),
627+
],
628+
);
629+
}

0 commit comments

Comments
 (0)