Skip to content

Commit 4d92818

Browse files
committed
refactor(object)!: remove AstarteAgregate completelly
Introduce the AstarteObject type, which replaces the HashMap for objects with an array and remove the conversion trait with a TryFrom. Signed-off-by: Joshua Chapman <joshua.chapman@secomind.com>
1 parent dbebfc8 commit 4d92818

File tree

16 files changed

+236
-332
lines changed

16 files changed

+236
-332
lines changed

astarte-device-sdk-derive/src/lib.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -155,29 +155,27 @@ impl ObjectDerive {
155155

156156
let name = &self.name;
157157
let (impl_generics, ty_generics, where_clause) = self.generics.split_for_impl();
158+
let capacity = self.fields.len();
158159
let fields = self.fields.iter().map(|i| {
159160
let name = i.to_string();
160161
let name = rename_rule.apply_to_field(&name);
161162
quote_spanned! {i.span() =>
162163
// TODO *Temporarily* ignore this new lint will be fixed in a new pr
163164
#[allow(unknown_lints)]
164165
#[allow(clippy::unnecessary_fallible_conversions)]
165-
let value: astarte_device_sdk::types::AstarteType = std::convert::TryInto::try_into(self.#i)?;
166-
result.insert(#name.to_string(), value);
166+
let v: astarte_device_sdk::types::AstarteType = ::std::convert::TryInto::try_into(value.#i)?;
167+
object.insert(#name.to_string(), v);
167168
}
168169
});
169170

170171
quote! {
171-
impl #impl_generics astarte_device_sdk::IntoAstarteObject for #name #ty_generics #where_clause {
172-
fn into_astarte_object(
173-
self,
174-
) -> ::std::result::Result<
175-
std::collections::HashMap<String, astarte_device_sdk::types::AstarteType>,
176-
astarte_device_sdk::error::Error,
177-
> {
178-
let mut result = std::collections::HashMap::new();
172+
impl #impl_generics ::std::convert::TryFrom<#name #ty_generics> for astarte_device_sdk::aggregate::AstarteObject #where_clause {
173+
type Error = astarte_device_sdk::error::Error;
174+
175+
fn try_from(value: #name #ty_generics) -> ::std::result::Result<Self, Self::Error> {
176+
let mut object = Self::with_capacity(#capacity);
179177
#(#fields)*
180-
Ok(result)
178+
Ok(object)
181179
}
182180
}
183181
}

astarte-device-sdk-mock/src/lib.rs

Lines changed: 26 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,32 @@
1919
use std::{future::Future, path::Path};
2020

2121
use astarte_device_sdk::{
22+
aggregate::AstarteObject,
2223
client::{ClientDisconnect, RecvError},
2324
properties::PropAccess,
2425
store::StoredProp,
25-
AstarteType, DeviceEvent, Error, Interface, IntoAstarteObject,
26+
AstarteType, DeviceEvent, Error, Interface,
2627
};
2728
use mockall::mock;
2829

2930
// Export public facing dependencies
3031
pub use mockall;
3132

3233
pub trait Client {
33-
fn send_object_with_timestamp<D>(
34+
fn send_object_with_timestamp(
3435
&self,
3536
interface_name: &str,
3637
interface_path: &str,
37-
data: D,
38+
data: AstarteObject,
3839
timestamp: chrono::DateTime<chrono::Utc>,
39-
) -> impl Future<Output = Result<(), Error>> + Send
40-
where
41-
D: IntoAstarteObject + Send + 'static;
40+
) -> impl Future<Output = Result<(), Error>> + Send;
4241

43-
fn send_object<D>(
42+
fn send_object(
4443
&self,
4544
interface_name: &str,
4645
interface_path: &str,
47-
data: D,
48-
) -> impl Future<Output = Result<(), Error>> + Send
49-
where
50-
D: IntoAstarteObject + Send + 'static;
46+
data: AstarteObject,
47+
) -> impl Future<Output = Result<(), Error>> + Send;
5148

5249
fn send_with_timestamp<D>(
5350
&self,
@@ -137,24 +134,20 @@ mock! {
137134
pub DeviceClient<S: 'static> { }
138135

139136
impl<S: Send + Sync> Client for DeviceClient<S> {
140-
async fn send_object_with_timestamp<D>(
137+
async fn send_object_with_timestamp(
141138
&self,
142139
interface_name: &str,
143140
interface_path: &str,
144-
data: D,
141+
data: AstarteObject,
145142
timestamp: chrono::DateTime<chrono::Utc>,
146-
) -> Result<(), Error>
147-
where
148-
D: IntoAstarteObject + Send + 'static;
143+
) -> Result<(), Error>;
149144

150-
async fn send_object<D>(
145+
async fn send_object(
151146
&self,
152147
interface_name: &str,
153148
interface_path: &str,
154-
data: D,
155-
) -> Result<(), Error>
156-
where
157-
D: IntoAstarteObject + Send + 'static;
149+
data: AstarteObject,
150+
) -> Result<(), Error>;
158151

159152
async fn send_with_timestamp<D>(
160153
&self,
@@ -245,16 +238,13 @@ mod tests {
245238
struct CheckMocks {}
246239

247240
impl Client for CheckMocks {
248-
async fn send_object_with_timestamp<D>(
241+
async fn send_object_with_timestamp(
249242
&self,
250243
interface_name: &str,
251244
interface_path: &str,
252-
data: D,
245+
data: AstarteObject,
253246
timestamp: chrono::DateTime<chrono::Utc>,
254-
) -> Result<(), Error>
255-
where
256-
D: IntoAstarteObject + Send + 'static,
257-
{
247+
) -> Result<(), Error> {
258248
astarte_device_sdk::Client::send_object_with_timestamp(
259249
self,
260250
interface_name,
@@ -265,15 +255,12 @@ mod tests {
265255
.await
266256
}
267257

268-
async fn send_object<D>(
258+
async fn send_object(
269259
&self,
270260
interface_name: &str,
271261
interface_path: &str,
272-
data: D,
273-
) -> Result<(), Error>
274-
where
275-
D: IntoAstarteObject + Send + 'static,
276-
{
262+
data: AstarteObject,
263+
) -> Result<(), Error> {
277264
astarte_device_sdk::Client::send_object(self, interface_name, interface_path, data)
278265
.await
279266
}
@@ -320,28 +307,22 @@ mod tests {
320307
}
321308

322309
impl astarte_device_sdk::Client for CheckMocks {
323-
async fn send_object_with_timestamp<D>(
310+
async fn send_object_with_timestamp(
324311
&self,
325312
_interface_name: &str,
326313
_interface_path: &str,
327-
_data: D,
314+
_data: AstarteObject,
328315
_timestamp: chrono::DateTime<chrono::Utc>,
329-
) -> Result<(), Error>
330-
where
331-
D: IntoAstarteObject + Send,
332-
{
316+
) -> Result<(), Error> {
333317
Ok(())
334318
}
335319

336-
async fn send_object<D>(
320+
async fn send_object(
337321
&self,
338322
_interface_name: &str,
339323
_interface_path: &str,
340-
_data: D,
341-
) -> Result<(), Error>
342-
where
343-
D: IntoAstarteObject + Send,
344-
{
324+
_data: AstarteObject,
325+
) -> Result<(), Error> {
345326
Ok(())
346327
}
347328

docs/get-started.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -554,13 +554,13 @@ interface.
554554
# use tokio::task::JoinSet;
555555
# use tracing::info;
556556
# #[cfg(not(feature = "derive"))]
557-
# use astarte_device_sdk_derive::AstarteAggregate;
557+
# use astarte_device_sdk_derive::IntoAstarteObject;
558558
# /// Load connection configuration and connect a device to Astarte.
559559
# async fn init() -> eyre::Result<(DeviceClient<SqliteStore>,DeviceConnection<SqliteStore, Mqtt<SqliteStore>>)> {
560560
# todo!()
561561
# }
562562
563-
#[derive(Debug, AstarteAggregate)]
563+
#[derive(Debug, IntoAstarteObject)]
564564
struct DataObject {
565565
double_endpoint: f64,
566566
string_endpoint: String,
@@ -585,7 +585,7 @@ async fn send_aggregate(client: DeviceClient<SqliteStore>) -> eyre::Result<()> {
585585
.send_object(
586586
"org.astarte-platform.rust.get-started.Aggregated",
587587
"/group_data",
588-
data,
588+
data.try_into()?,
589589
)
590590
.await?;
591591

e2e-test/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ async fn main() -> eyre::Result<()> {
237237
sensor_n.remove(0);
238238
rx_data.0 = sensor_n;
239239

240-
for (key, value) in var {
240+
for (key, value) in var.into_key_values() {
241241
rx_data.1.insert(key, value);
242242
}
243243
} else if event.interface == test_cfg.interface_property_so {
@@ -412,7 +412,7 @@ async fn test_aggregate_device_to_server(
412412
.send_object(
413413
&test_cfg.interface_aggregate_do,
414414
&format!("/{sensor_number}"),
415-
tx_data.clone(),
415+
tx_data.clone().try_into().unwrap(),
416416
)
417417
.await?;
418418

examples/object_datastream/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use astarte_device_sdk::{
2525
transport::mqtt::MqttConfig,
2626
};
2727
#[cfg(not(feature = "derive"))]
28-
use astarte_device_sdk_derive::AstarteAggregate;
28+
use astarte_device_sdk_derive::IntoAstarteObject;
2929

3030
#[derive(Serialize, Deserialize)]
3131
struct Config {
@@ -81,7 +81,7 @@ async fn main() -> Result<(), Error> {
8181
.send_object(
8282
"org.astarte-platform.rust.examples.object-datastream.DeviceDatastream",
8383
"/23",
84-
data,
84+
data.try_into().unwrap(),
8585
)
8686
.await
8787
.unwrap();

0 commit comments

Comments
 (0)