Skip to content

Commit

Permalink
Merge pull request #342 from rgallor/fix/purge-property
Browse files Browse the repository at this point in the history
fix(purge): purge only server owned properties
  • Loading branch information
harlem88 authored May 22, 2024
2 parents d7f9c55 + 6d902db commit f6f7cc3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- Purge property deletes only the server property [#342](https://github.com/astarte-platform/astarte-device-sdk-rust/pull/342)

## [0.5.3] - 2024-03-20
### Added
- Add semver-check for release
Expand Down
15 changes: 12 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ impl AstarteDeviceSdk {
let bdata = publish.payload.to_vec();

if interface == "control" && path == "/consumer/properties" {
self.purge_properties(bdata).await?;
self.purge_server_properties(bdata).await?;
continue;
}

Expand Down Expand Up @@ -512,13 +512,22 @@ impl AstarteDeviceSdk {
format!("{}/{}", self.realm, self.device_id)
}

async fn purge_properties(&self, bdata: Vec<u8>) -> Result<(), AstarteError> {
/// This function deletes all the stored server owned properties after receiving a publish on
/// `/control/consumer/properties`
async fn purge_server_properties(&self, bdata: Vec<u8>) -> Result<(), AstarteError> {
if let Some(db) = &self.database {
let interfaces = self.interfaces.read().await;

let stored_props = db.load_all_props().await?;

let server_owned_properties = stored_props.into_iter().filter(|prop| {
interfaces.get_ownership(&prop.interface)
== Some(crate::interface::Ownership::Server)
});

let paths = utils::extract_set_properties(&bdata);

for stored_prop in stored_props {
for stored_prop in server_owned_properties {
if paths.contains(&(stored_prop.interface.clone() + &stored_prop.path)) {
continue;
}
Expand Down

0 comments on commit f6f7cc3

Please sign in to comment.