add #[serde(default)] to msg types
allows skipping fields when deserializing from json.
This commit is contained in:
parent
a88505efd1
commit
174fb233c1
|
|
@ -1,7 +1,7 @@
|
||||||
|
|
||||||
[package]
|
[package]
|
||||||
name = "r2r"
|
name = "r2r"
|
||||||
version = "0.6.0"
|
version = "0.6.1"
|
||||||
authors = ["Martin Dahl <martin.dahl@gmail.com>"]
|
authors = ["Martin Dahl <martin.dahl@gmail.com>"]
|
||||||
description = "Easy to use, runtime-agnostic, async rust bindings for ROS2."
|
description = "Easy to use, runtime-agnostic, async rust bindings for ROS2."
|
||||||
license = "MIT AND Apache-2.0"
|
license = "MIT AND Apache-2.0"
|
||||||
|
|
@ -21,7 +21,7 @@ thiserror = "1.0.30"
|
||||||
lazy_static = "1.4.0"
|
lazy_static = "1.4.0"
|
||||||
r2r_common = { path = "r2r_common", version = "0.3.0" }
|
r2r_common = { path = "r2r_common", version = "0.3.0" }
|
||||||
r2r_rcl = { path = "r2r_rcl", version = "0.3.0" }
|
r2r_rcl = { path = "r2r_rcl", version = "0.3.0" }
|
||||||
r2r_msg_gen = { path = "r2r_msg_gen", version = "0.3.0" }
|
r2r_msg_gen = { path = "r2r_msg_gen", version = "0.3.1" }
|
||||||
r2r_actions = { path = "r2r_actions", version = "0.3.0" }
|
r2r_actions = { path = "r2r_actions", version = "0.3.0" }
|
||||||
uuid = { version = "0.8.2", features = ["serde", "v4"] }
|
uuid = { version = "0.8.2", features = ["serde", "v4"] }
|
||||||
retain_mut = "0.1.5"
|
retain_mut = "0.1.5"
|
||||||
|
|
@ -35,6 +35,6 @@ rand = "0.8.4"
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
r2r_common = { path = "r2r_common", version = "0.3.0" }
|
r2r_common = { path = "r2r_common", version = "0.3.0" }
|
||||||
r2r_msg_gen = { path = "r2r_msg_gen", version = "0.3.0" }
|
r2r_msg_gen = { path = "r2r_msg_gen", version = "0.3.1" }
|
||||||
|
|
||||||
[workspace]
|
[workspace]
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "r2r_msg_gen"
|
name = "r2r_msg_gen"
|
||||||
version = "0.3.0"
|
version = "0.3.1"
|
||||||
authors = ["Martin Dahl <martin.dahl@gmail.com>"]
|
authors = ["Martin Dahl <martin.dahl@gmail.com>"]
|
||||||
description = "Internal dependency to the r2r crate."
|
description = "Internal dependency to the r2r crate."
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
|
|
||||||
|
|
@ -494,6 +494,7 @@ pub fn generate_rust_msg(module_: &str, prefix_: &str, name_: &str) -> String {
|
||||||
let module_str = format!(
|
let module_str = format!(
|
||||||
"
|
"
|
||||||
#[derive(Clone,Debug,PartialEq,Serialize,Deserialize)]
|
#[derive(Clone,Debug,PartialEq,Serialize,Deserialize)]
|
||||||
|
#[serde(default)]
|
||||||
pub struct {msgname} {{\n
|
pub struct {msgname} {{\n
|
||||||
{fields}
|
{fields}
|
||||||
}}\n
|
}}\n
|
||||||
|
|
|
||||||
|
|
@ -583,6 +583,36 @@ mod tests {
|
||||||
assert_eq!(msg, msg2);
|
assert_eq!(msg, msg2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(r2r__test_msgs__msg__Defaults)]
|
||||||
|
#[test]
|
||||||
|
fn test_untyped_json_default() {
|
||||||
|
// from the msg definition file:
|
||||||
|
// bool bool_value true
|
||||||
|
// byte byte_value 50
|
||||||
|
// char char_value 100
|
||||||
|
// float32 float32_value 1.125
|
||||||
|
// ...
|
||||||
|
|
||||||
|
// let's try to change only a few fields.
|
||||||
|
let json = r#"
|
||||||
|
{
|
||||||
|
"byte_value": 255,
|
||||||
|
"float32_value": 3.14
|
||||||
|
}"#;
|
||||||
|
|
||||||
|
let mut native =
|
||||||
|
WrappedNativeMsgUntyped::new_from("test_msgs/msg/Defaults").unwrap();
|
||||||
|
let v: serde_json::Value = serde_json::from_str(json).unwrap();
|
||||||
|
native.from_json(v).expect("could make default msg");
|
||||||
|
let json2 = native.to_json().unwrap();
|
||||||
|
let msg2: test_msgs::msg::Defaults = serde_json::from_value(json2).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(msg2.bool_value, true); // the default
|
||||||
|
assert_eq!(msg2.byte_value, 255); // from our json
|
||||||
|
assert_eq!(msg2.char_value, 100); // the default
|
||||||
|
assert_eq!(msg2.float32_value, 3.14); // from our json
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(r2r__test_msgs__msg__Arrays)]
|
#[cfg(r2r__test_msgs__msg__Arrays)]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_test_msgs_array() -> () {
|
fn test_test_msgs_array() -> () {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue