Skip to content

Commit 4cac5ff

Browse files
committed
refactor(proto): change get properties proto structures
Signed-off-by: Riccardo Gallo <riccardo.gallo@secomind.com>
1 parent 75986e6 commit 4cac5ff

File tree

4 files changed

+116
-122
lines changed

4 files changed

+116
-122
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ rustc-args = ["--cfg=docsrs"]
134134
[workspace.dependencies]
135135
astarte-device-sdk = { path = "./", version = "=0.9.6" }
136136
astarte-device-sdk-derive = { version = "=0.9.6", path = "./astarte-device-sdk-derive" }
137-
astarte-message-hub-proto = { git = "https://github.com/astarte-platform/astarte-message-hub-proto", rev = "8e8198de9589c606e63ada8ec104bace81ae9cfa" }
138-
astarte-message-hub-proto-mock = { git = "https://github.com/astarte-platform/astarte-message-hub-proto", rev = "8e8198de9589c606e63ada8ec104bace81ae9cfa" }
137+
astarte-message-hub-proto = { git = "https://github.com/astarte-platform/astarte-message-hub-proto", rev = "87e9b3937cd563576de4bcb801a91c700bf196bb" }
138+
astarte-message-hub-proto-mock = { git = "https://github.com/astarte-platform/astarte-message-hub-proto", rev = "87e9b3937cd563576de4bcb801a91c700bf196bb" }
139139
async-trait = "0.1.67"
140140
base64 = "0.22.0"
141141
bson = "2.7.0"

src/transport/grpc/convert.rs

Lines changed: 77 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -80,30 +80,28 @@ pub(crate) fn map_property_to_astarte_type(
8080

8181
/// Map a list of properties, unset properties will be ignored
8282
pub(crate) fn map_set_stored_properties(
83-
mut message_hub_properties: astarte_message_hub_proto::StoredProperties,
83+
message_hub_properties: astarte_message_hub_proto::StoredProperties,
8484
) -> Result<Vec<StoredProp>, MessageHubProtoError> {
8585
message_hub_properties
86-
.interface_properties
87-
.iter_mut()
88-
.flat_map(|(name, prop_data)| {
89-
prop_data.properties.iter().filter_map(|p| {
90-
let path = p.path.clone();
91-
let value: AstarteType =
92-
match map_property_to_astarte_type(p.clone()).transpose()? {
93-
Ok(s) => s,
94-
Err(e) => return Some(Err(e)),
95-
};
96-
97-
let res = StoredProp {
98-
interface: name.clone(),
99-
path,
100-
value,
101-
interface_major: prop_data.version_major,
102-
ownership: prop_data.ownership().into(),
103-
};
104-
105-
Some(Ok(res))
106-
})
86+
.properties
87+
.into_iter()
88+
.filter_map(|prop| {
89+
let ownership = prop.ownership().into();
90+
91+
let value = match map_property_to_astarte_type(prop.clone()).transpose()? {
92+
Ok(s) => s,
93+
Err(e) => return Some(Err(e)),
94+
};
95+
96+
let res = StoredProp {
97+
interface: prop.interface_name,
98+
path: prop.path,
99+
value,
100+
interface_major: prop.version_major,
101+
ownership,
102+
};
103+
104+
Some(Ok(res))
107105
})
108106
.try_collect()
109107
}
@@ -406,8 +404,7 @@ pub(crate) mod test {
406404
use std::collections::HashMap;
407405

408406
use astarte_message_hub_proto::{
409-
AstarteData, AstarteDatastreamObject, AstarteMessage, AstartePropertyIndividual,
410-
InterfaceProperties,
407+
AstarteData, AstarteDatastreamObject, AstarteMessage, AstartePropertyIndividual, Property,
411408
};
412409
use chrono::Utc;
413410
use pretty_assertions::assert_eq;
@@ -426,16 +423,6 @@ pub(crate) mod test {
426423
}
427424
}
428425

429-
pub(crate) fn new_property(
430-
path: String,
431-
data: Option<AstarteType>,
432-
) -> astarte_message_hub_proto::Property {
433-
astarte_message_hub_proto::Property {
434-
path,
435-
data: data.map(|s| s.into()),
436-
}
437-
}
438-
439426
#[test]
440427
fn proto_conversions_success() {
441428
let cases = [
@@ -1014,31 +1001,32 @@ pub(crate) mod test {
10141001
assert_eq!(ProtoData::Double(expected_data), double_data);
10151002
}
10161003

1017-
fn make_messagehub_property(
1018-
path: &str,
1019-
value: Option<AstarteType>,
1020-
) -> astarte_message_hub_proto::Property {
1021-
astarte_message_hub_proto::Property {
1022-
path: path.to_owned(),
1023-
data: value.map(|v| v.into()),
1024-
}
1025-
}
1026-
10271004
#[test]
10281005
fn map_property_to_astarte_type_ok() {
1029-
let value: AstarteType = AstarteType::String("test".to_owned());
1030-
1031-
let prop = make_messagehub_property("/path11", Some(value.clone()));
1006+
let prop = Property {
1007+
interface_name: "com.test.interface".to_owned(),
1008+
path: "/path11".to_owned(),
1009+
version_major: 0,
1010+
ownership: astarte_message_hub_proto::Ownership::Device.into(),
1011+
data: Some(AstarteData {
1012+
astarte_data: Some(ProtoData::String("test".to_owned())),
1013+
}),
1014+
};
10321015

10331016
let astarte_type = map_property_to_astarte_type(prop).unwrap().unwrap();
10341017

1035-
assert_eq!(value, astarte_type);
1018+
assert_eq!(AstarteType::String("test".to_string()), astarte_type);
10361019
}
10371020

10381021
#[test]
10391022
fn map_property_to_astarte_type_none() {
1040-
let prop = make_messagehub_property("/path11", None);
1041-
1023+
let prop = Property {
1024+
interface_name: "com.test.interface".to_owned(),
1025+
path: "/path11".to_owned(),
1026+
version_major: 0,
1027+
ownership: astarte_message_hub_proto::Ownership::Device.into(),
1028+
data: None,
1029+
};
10421030
let astarte_type_err = map_property_to_astarte_type(prop);
10431031

10441032
assert!(matches!(astarte_type_err, Ok(None)));
@@ -1049,46 +1037,46 @@ pub(crate) mod test {
10491037
const INTERFACE_1: &str = "com.test.interface1";
10501038
const INTERFACE_2: &str = "com.test.interface2";
10511039

1052-
let interface_properties_map = vec![
1053-
(
1054-
INTERFACE_1.to_owned(),
1055-
InterfaceProperties {
1056-
ownership: astarte_message_hub_proto::Ownership::Device.into(),
1057-
version_major: 0,
1058-
properties: vec![
1059-
make_messagehub_property(
1060-
"/path11",
1061-
Some(AstarteType::String("test".to_owned())),
1062-
),
1063-
make_messagehub_property("/path12", Some(AstarteType::Integer(0))),
1064-
],
1065-
},
1066-
),
1067-
(
1068-
INTERFACE_2.to_owned(),
1069-
InterfaceProperties {
1070-
ownership: astarte_message_hub_proto::Ownership::Server.into(),
1071-
version_major: 0,
1072-
properties: vec![
1073-
make_messagehub_property(
1074-
"/path21",
1075-
Some(AstarteType::BinaryBlob(vec![0, 54, 0, 23])),
1076-
),
1077-
make_messagehub_property(
1078-
"/path22",
1079-
Some(AstarteType::Double(std::f64::consts::PI)),
1080-
),
1081-
],
1082-
},
1083-
),
1084-
]
1085-
.into_iter()
1086-
.collect();
1040+
let prop11 = Property {
1041+
interface_name: INTERFACE_1.to_owned(),
1042+
path: "/path11".to_owned(),
1043+
version_major: 0,
1044+
ownership: astarte_message_hub_proto::Ownership::Device.into(),
1045+
data: Some(AstarteData {
1046+
astarte_data: Some(ProtoData::String("test".to_owned())),
1047+
}),
1048+
};
1049+
let prop12 = Property {
1050+
interface_name: INTERFACE_1.to_owned(),
1051+
path: "/path12".to_owned(),
1052+
version_major: 0,
1053+
ownership: astarte_message_hub_proto::Ownership::Device.into(),
1054+
data: Some(AstarteData {
1055+
astarte_data: Some(ProtoData::Integer(0)),
1056+
}),
1057+
};
1058+
let prop21 = Property {
1059+
interface_name: INTERFACE_2.to_owned(),
1060+
path: "/path21".to_owned(),
1061+
version_major: 0,
1062+
ownership: astarte_message_hub_proto::Ownership::Server.into(),
1063+
data: Some(AstarteData {
1064+
astarte_data: Some(ProtoData::BinaryBlob(vec![0, 54, 0, 23])),
1065+
}),
1066+
};
1067+
let prop22 = Property {
1068+
interface_name: INTERFACE_2.to_owned(),
1069+
path: "/path22".to_owned(),
1070+
version_major: 0,
1071+
ownership: astarte_message_hub_proto::Ownership::Server.into(),
1072+
data: Some(AstarteData {
1073+
astarte_data: Some(ProtoData::Double(std::f64::consts::PI)),
1074+
}),
1075+
};
10871076

1088-
let message_hub_stored_properties: astarte_message_hub_proto::StoredProperties =
1089-
astarte_message_hub_proto::StoredProperties {
1090-
interface_properties: interface_properties_map,
1091-
};
1077+
let message_hub_stored_properties = astarte_message_hub_proto::StoredProperties {
1078+
properties: vec![prop11, prop12, prop21, prop22],
1079+
};
10921080

10931081
let inner_vec = map_set_stored_properties(message_hub_stored_properties).unwrap();
10941082

0 commit comments

Comments
 (0)