use saddle::__private::standard_wire::{Encode, Writer};
saddle::grpc_bindings! {
pub mod protocol {
proto_root "../../tests/fixtures/standard-grpc";
dependency mirror { service "alpha.Mirror"; methods { Echo => "Echo"; Numbers => "Numbers"; } }
dependency alternate { service "beta.Alternate"; methods { Echo => "Echo"; } }
}
}
fn encode(message: &impl Encode) -> Vec<u8> {
let mut bytes = vec![0; message.encoded_len().unwrap()];
let mut writer = Writer::new(&mut bytes);
message.encode(&mut writer).unwrap();
writer.finish().unwrap();
bytes
}
#[test]
fn full_identity_and_supported_fields_are_standard_wire() {
let mut nested = protocol::types::alpha::Value_types::Nested_Fields::default();
nested.id = 7;
nested.description = "nested";
let mut first = protocol::types::alpha::Value_types::LabelsEntry_Fields::default();
first.key = "key";
first.value = 1;
let mut second = protocol::types::alpha::Value_types::LabelsEntry_Fields::default();
second.key = "key";
second.value = 2;
let labels = [first, second];
let mut request = protocol::types::alpha::Value_Fields::default();
request.text = "hello";
request.data = b"\0\xff";
request.limit = Some(0);
request.numbers = &[-1, 0, 1];
request.labels = &labels;
request.child = Some(&nested);
request.children = std::slice::from_ref(&nested);
request.choice = Some(protocol::types::alpha::Value_choice_Fields::chosen(&nested));
request.kind = 77;
let bytes = encode(&request);
let view = protocol::types::alpha::Value::parse(&bytes).unwrap();
assert_eq!(view.text(), "hello");
assert_eq!(view.data(), b"\0\xff");
assert_eq!(view.limit(), Some(0));
assert_eq!(view.numbers().collect::<Vec<_>>(), [-1, 0, 1]);
assert_eq!(
view.labels()
.map(|entry| (entry.key(), entry.value()))
.collect::<Vec<_>>(),
[("key", 2)]
);
assert_eq!(view.child().unwrap().id(), 7);
assert_eq!(view.children().next().unwrap().description(), "nested");
match view.choice().unwrap() {
protocol::types::alpha::Value_choice::chosen(child) => assert_eq!(child.id(), 7),
_ => panic!("wrong oneof"),
};
assert_eq!(view.kind(), 77);
use saddle::grpc::Method;
assert_eq!(
protocol::methods::mirror::Echo::SPEC.path,
"/alpha.Mirror/Echo"
);
assert_eq!(
protocol::methods::alternate::Echo::SPEC.path,
"/beta.Alternate/Echo"
);
assert_eq!(
protocol::methods::mirror::Echo::SPEC.request.name,
".alpha.Value"
);
assert_eq!(
protocol::methods::alternate::Echo::SPEC.request.name,
".beta.Value"
);
assert_ne!(
(protocol::methods::mirror::Echo::SPEC.marker)(),
(protocol::methods::alternate::Echo::SPEC.marker)()
);
assert_eq!(protocol::METHODS.len(), 3);
}
#[test]
fn all_numeric_fields_preserve_value_and_unknown_fields() {
let mut request = protocol::types::alpha::Scalars_Fields::default();
request.double_value = -1.5;
request.float_value = 2.25;
request.int32_value = -3;
request.int64_value = -4;
request.uint32_value = u32::MAX;
request.uint64_value = u64::MAX;
request.sint32_value = i32::MIN;
request.sint64_value = i64::MIN;
request.fixed32_value = 5;
request.fixed64_value = 6;
request.sfixed32_value = -7;
request.sfixed64_value = -8;
request.bool_value = true;
let mut bytes = encode(&request);
bytes.extend_from_slice(b"\x98\x06\x01");
let view = protocol::types::alpha::Scalars::parse(&bytes).unwrap();
assert_eq!(view.double_value(), -1.5);
assert_eq!(view.float_value(), 2.25);
assert_eq!(view.int32_value(), -3);
assert_eq!(view.int64_value(), -4);
assert_eq!(view.uint32_value(), u32::MAX);
assert_eq!(view.uint64_value(), u64::MAX);
assert_eq!(view.sint32_value(), i32::MIN);
assert_eq!(view.sint64_value(), i64::MIN);
assert_eq!(view.fixed32_value(), 5);
assert_eq!(view.fixed64_value(), 6);
assert_eq!(view.sfixed32_value(), -7);
assert_eq!(view.sfixed64_value(), -8);
assert!(view.bool_value());
}
saddle::grpc_bindings! {
mod name_edges {
proto_root "../../tests/fixtures/standard-names";
dependency names_service { service "names.Service"; methods { Echo => "Echo"; } }
}
}
#[test]
fn descriptor_fields_keep_parser_and_lifetime_names() {
use saddle::__private::standard_wire::Decode;
let mut fields = name_edges::types::names::Value_Fields::default();
assert_eq!(i32::from(name_edges::types::names::Kind::number), 0);
assert_eq!(i32::from(name_edges::types::names::Kind::from_number), 1);
assert_eq!(i32::from(name_edges::types::names::Kind::from(77)), 77);
fields.parse = "original field";
fields._lifetime = 17;
fields.__pb_73656c66 = 31; fields.__pb_5f5f70625f3733363536633636 = 32; fields.__pb_6372617465 = 33; fields.__pb_7375706572 = 34; let mut choice = name_edges::types::names::Choice_Fields::default();
choice.choice = Some(name_edges::types::names::Choice_choice_Fields::__Lifetime(
"choice",
));
fields.selected = Some(&choice);
let bytes = encode(&fields);
let view = <name_edges::types::names::Value as Decode>::parse(&bytes).unwrap();
assert_eq!(view.parse(), "original field");
assert_eq!(view._lifetime(), 17);
assert_eq!(view.__pb_73656c66(), 31);
assert_eq!(view.__pb_5f5f70625f3733363536633636(), 32);
assert_eq!(view.__pb_6372617465(), 33);
assert_eq!(view.__pb_7375706572(), 34);
match view.selected().unwrap().choice().unwrap() {
name_edges::types::names::Choice_choice::__Lifetime(value) => assert_eq!(value, "choice"),
_ => panic!("wrong original variant"),
}
let method_view =
<name_edges::methods::names_service::Echo as saddle::grpc::Method>::view(&bytes);
assert_eq!(method_view.parse(), "original field");
}
saddle::grpc_bindings! {
mod collision_edges {
proto_root "../../tests/fixtures/standard-collisions";
dependency __types { service "__types.Service"; methods { Echo => "Echo"; } }
dependency types { service "__types.Service"; methods { Echo => "Echo"; } }
dependency methods { service "__types.Service"; methods { Echo => "Echo"; } }
}
}
#[test]
fn original_packages_types_and_dependency_aliases_coexist() {
use saddle::grpc::Method;
let mut fields = <collision_edges::methods::__types::Echo as Method>::Fields::default();
let mut child = collision_edges::types::__types::Value_types_::Inside_Fields::default();
child.value = 23;
fields.child = Some(&child);
fields.choice = Some(collision_edges::types::__types::Value_choice_Fields_::text(
"choice",
));
let bytes = encode(&fields);
let view = collision_edges::types::__types::Value::parse(&bytes).unwrap();
assert_eq!(view.child().unwrap().value(), 23);
match view.choice().unwrap() {
collision_edges::types::__types::Value_choice_::text(value) => assert_eq!(value, "choice"),
_ => panic!("wrong choice"),
}
let _ = collision_edges::types::__types::Value_Fields::parse(&[]).unwrap();
let _ = collision_edges::types::__types::Value_types::parse(&[]).unwrap();
let _ = collision_edges::types::__types::Value_choice::parse(&[]).unwrap();
let _ = collision_edges::types::__types::Value_choice_Fields::parse(&[]).unwrap();
let _ = collision_edges::types::__types::__types::parse(&[]).unwrap();
assert_eq!(
collision_edges::methods::types::Echo::SPEC.path,
"/__types.Service/Echo"
);
assert_eq!(
collision_edges::methods::methods::Echo::SPEC.path,
"/__types.Service/Echo"
);
}