Skip to content

Commit 444d95e

Browse files
committed
feat(proto): implement properties conversion for proto
Define function to convert a vec of StoredProp into the corresponding protobuf value. Signed-off-by: Riccardo Gallo <riccardo.gallo@secomind.com>
1 parent 61054fa commit 444d95e

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

src/transport/grpc/convert.rs

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
//! Contains conversion traits to convert the Astarte types in the protobuf format to the
2222
//! Astarte types from the Astarte device SDK.
2323
24+
use std::collections::HashMap;
2425
use std::num::TryFromIntError;
2526

2627
use astarte_message_hub_proto::astarte_data::AstarteData as ProtoData;
@@ -32,6 +33,7 @@ use astarte_message_hub_proto::{
3233
use chrono::TimeZone;
3334
use itertools::Itertools;
3435

36+
use super::{GrpcError, GrpcPayload};
3537
use crate::aggregate::AstarteObject;
3638
use crate::interface::Ownership;
3739
use crate::store::StoredProp;
@@ -42,8 +44,6 @@ use crate::{
4244
};
4345
use crate::{DeviceEvent, Value};
4446

45-
use super::{GrpcError, GrpcPayload};
46-
4747
/// Error returned by the Message Hub types conversions.
4848
#[non_exhaustive]
4949
#[derive(thiserror::Error, Debug)]
@@ -108,6 +108,38 @@ pub(crate) fn map_set_stored_properties(
108108
.try_collect()
109109
}
110110

111+
/// Map a list of stored properties to , unset value will result in an error of the conversion
112+
pub fn map_stored_properties_to_proto(
113+
props: Vec<StoredProp>,
114+
) -> astarte_message_hub_proto::StoredProperties {
115+
let interface_properties =
116+
props
117+
.into_iter()
118+
.fold(HashMap::new(), |mut interface_properties, prop| {
119+
let entry_prop = interface_properties
120+
.entry(prop.interface)
121+
.or_insert_with(|| astarte_message_hub_proto::InterfaceProperties {
122+
ownership: astarte_message_hub_proto::Ownership::from(prop.ownership)
123+
as i32,
124+
version_major: prop.interface_major,
125+
properties: vec![],
126+
});
127+
128+
let property = astarte_message_hub_proto::Property {
129+
path: prop.path,
130+
data: Some(prop.value.into()),
131+
};
132+
133+
entry_prop.properties.push(property);
134+
135+
interface_properties
136+
});
137+
138+
astarte_message_hub_proto::StoredProperties {
139+
interface_properties,
140+
}
141+
}
142+
111143
impl From<astarte_message_hub_proto::Ownership> for Ownership {
112144
fn from(value: astarte_message_hub_proto::Ownership) -> Self {
113145
match value {
@@ -117,6 +149,25 @@ impl From<astarte_message_hub_proto::Ownership> for Ownership {
117149
}
118150
}
119151

152+
impl From<Ownership> for astarte_message_hub_proto::Ownership {
153+
fn from(value: Ownership) -> Self {
154+
match value {
155+
Ownership::Device => astarte_message_hub_proto::Ownership::Device,
156+
Ownership::Server => astarte_message_hub_proto::Ownership::Server,
157+
}
158+
}
159+
}
160+
161+
/// Construct an sdk astarte type from a property that is required to be set
162+
impl TryFrom<astarte_message_hub_proto::Property> for AstarteType {
163+
type Error = MessageHubProtoError;
164+
165+
fn try_from(property: astarte_message_hub_proto::Property) -> Result<Self, Self::Error> {
166+
map_property_to_astarte_type(property)
167+
.and_then(|e| e.ok_or(MessageHubProtoError::ExpectedField("value")))
168+
}
169+
}
170+
120171
impl TryFrom<astarte_message_hub_proto::AstarteData> for AstarteType {
121172
type Error = MessageHubProtoError;
122173

src/transport/grpc/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,7 @@ mod test {
645645
};
646646
use uuid::uuid;
647647

648+
use super::*;
648649
use crate::{
649650
aggregate::AstarteObject,
650651
builder::DEFAULT_VOLATILE_CAPACITY,
@@ -656,8 +657,6 @@ mod test {
656657
DeviceEvent, Value,
657658
};
658659

659-
use super::*;
660-
661660
pub(crate) const ID: Uuid = uuid!("67e55044-10b1-426f-9247-bb680e5fe0c8");
662661

663662
#[derive(Debug)]

0 commit comments

Comments
 (0)