Skip to content

Commit

Permalink
feat: commands and a hierarchical model
Browse files Browse the repository at this point in the history
  • Loading branch information
ctron committed Aug 17, 2022
1 parent 1aaaa3e commit 08226f2
Show file tree
Hide file tree
Showing 22 changed files with 1,969 additions and 923 deletions.
19 changes: 10 additions & 9 deletions backend/src/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub async fn things_update<S: Storage, N: Notifier, Si: Sink, Cmd: CommandSink>(
let payload = payload.into_inner();

service
.update(&Id { application, thing }, payload, &OPTS)
.update(&Id { application, thing }, &payload, &OPTS)
.await?;

Ok(HttpResponse::NoContent().json(json!({})))
Expand All @@ -67,7 +67,7 @@ pub async fn things_patch<S: Storage, N: Notifier, Si: Sink, Cmd: CommandSink>(
let payload = payload.into_inner();

service
.update(&path.into_inner(), JsonPatchUpdater(payload), &OPTS)
.update(&path.into_inner(), &JsonPatchUpdater(payload), &OPTS)
.await?;

Ok(HttpResponse::NoContent().json(json!({})))
Expand All @@ -81,7 +81,7 @@ pub async fn things_merge<S: Storage, N: Notifier, Si: Sink, Cmd: CommandSink>(
let payload = payload.into_inner();

service
.update(&path.into_inner(), JsonMergeUpdater(payload), &OPTS)
.update(&path.into_inner(), &JsonMergeUpdater(payload), &OPTS)
.await?;

Ok(HttpResponse::NoContent().json(json!({})))
Expand All @@ -97,7 +97,7 @@ pub async fn things_update_reported_state<S: Storage, N: Notifier, Si: Sink, Cmd
service
.update(
&path.into_inner(),
ReportedStateUpdater(payload, UpdateMode::Merge),
&ReportedStateUpdater(payload, UpdateMode::Merge),
&OPTS,
)
.await?;
Expand All @@ -116,7 +116,7 @@ pub async fn things_update_synthetic_state<S: Storage, N: Notifier, Si: Sink, Cm
service
.update(
&Id::new(application, thing),
SyntheticStateUpdater(state, payload),
&SyntheticStateUpdater(state, payload),
&OPTS,
)
.await?;
Expand All @@ -135,7 +135,7 @@ pub async fn things_update_desired_state<S: Storage, N: Notifier, Si: Sink, Cmd:
service
.update(
&Id::new(application, thing),
DesiredStateUpdater(state, payload),
&DesiredStateUpdater(state, payload),
&OPTS,
)
.await?;
Expand Down Expand Up @@ -176,7 +176,7 @@ pub async fn things_update_desired_state_value<
service
.update(
&Id::new(application, thing),
DesiredStateValueUpdater(values),
&DesiredStateValueUpdater(values),
&OPTS,
)
.await?;
Expand All @@ -191,7 +191,7 @@ pub async fn things_update_reconciliation<S: Storage, N: Notifier, Si: Sink, Cmd
) -> Result<HttpResponse, actix_web::Error> {
let payload = payload.into_inner();

service.update(&path.into_inner(), payload, &OPTS).await?;
service.update(&path.into_inner(), &payload, &OPTS).await?;

Ok(HttpResponse::NoContent().json(json!({})))
}
Expand All @@ -200,7 +200,8 @@ pub async fn things_delete<S: Storage, N: Notifier, Si: Sink, Cmd: CommandSink>(
service: web::Data<Service<S, N, Si, Cmd>>,
path: web::Path<Id>,
) -> Result<HttpResponse, actix_web::Error> {
service.delete(&path.into_inner()).await?;
// FIXME: allow adding preconditions
service.delete(&path.into_inner(), None).await?;

Ok(HttpResponse::NoContent().json(json!({})))
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct Command {

#[async_trait]
pub trait CommandSink: Sized + Send + Sync + 'static {
type Error: std::error::Error;
type Error: std::error::Error + Send + Sync;
type Config: Clone + Debug + DeserializeOwned;

fn from_config(spawner: &mut dyn Spawner, config: Self::Config) -> anyhow::Result<Self>;
Expand Down
36 changes: 36 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,39 @@ pub mod version;
pub mod waker;

pub use self::serde::is_default;
use crate::model::Thing;

#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Preconditions<'o> {
/// Required resource version.
pub resource_version: Option<&'o str>,
/// Required resource UID.
pub uid: Option<&'o str>,
}

impl<'o> From<&'o Thing> for Preconditions<'o> {
fn from(thing: &'o Thing) -> Self {
Self {
resource_version: thing.metadata.resource_version.as_deref(),
uid: thing.metadata.uid.as_deref(),
}
}
}

impl Preconditions<'_> {
pub fn matches(&self, thing: &Thing) -> bool {
if let Some(resource_version) = self.resource_version {
if Some(resource_version) != thing.metadata.resource_version.as_deref() {
return false;
}
}

if let Some(uid) = self.uid {
if Some(uid) != thing.metadata.uid.as_deref() {
return false;
}
}

return true;
}
}

0 comments on commit 08226f2

Please sign in to comment.