Skip to content

Commit

Permalink
app: Move backward-compat logic into get_trigger_metadata
Browse files Browse the repository at this point in the history
Signed-off-by: Lann Martin <lann.martin@fermyon.com>
  • Loading branch information
lann authored and carlokok committed Jan 23, 2024
1 parent 71c35c6 commit b8490df
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 35 deletions.
34 changes: 24 additions & 10 deletions crates/app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#![deny(missing_docs)]

mod host_component;
use serde_json::Value;
pub use spin_locked_app::locked;
pub use spin_locked_app::values;
pub use spin_locked_app::{Error, MetadataKey, Result};
Expand Down Expand Up @@ -212,16 +213,29 @@ impl<'a, L> App<'a, L> {
&'this self,
trigger_type: &'a str,
) -> Result<Option<T>> {
self.locked.metadata.get("triggers").map_or(Ok(None), |t| {
t.get(trigger_type)
.map(T::deserialize)
.transpose()
.map_err(|err| {
Error::MetadataError(format!(
"invalid metadata value for {trigger_type:?}: {err:?}"
))
})
})
let Some(value) = self.get_trigger_metadata_value(trigger_type) else {
return Ok(None);
};
let metadata = T::deserialize(value).map_err(|err| {
Error::MetadataError(format!(
"invalid metadata value for {trigger_type:?}: {err:?}"
))
})?;
Ok(Some(metadata))
}

fn get_trigger_metadata_value(&self, trigger_type: &str) -> Option<Value> {
if let Some(trigger_configs) = self.locked.metadata.get("triggers") {
// New-style: `{"triggers": {"<type>": {...}}}`
trigger_configs.get(trigger_type).cloned()
} else if self.locked.metadata["trigger"]["type"] == trigger_type {
// Old-style: `{"trigger": {"type": "<type>", ...}}`
let mut meta = self.locked.metadata["trigger"].clone();
meta.as_object_mut().unwrap().remove("type");
Some(meta)
} else {
None
}
}

/// Returns an iterator of [`AppTrigger`]s defined for this app with
Expand Down
17 changes: 3 additions & 14 deletions crates/redis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@ use anyhow::{anyhow, Context, Result};
use futures::{future::join_all, StreamExt};
use redis::{Client, ConnectionLike};
use serde::{de::IgnoredAny, Deserialize, Serialize};
use spin_app::MetadataKey;
use spin_core::async_trait;
use spin_trigger::{cli::NoArgs, TriggerAppEngine, TriggerExecutor};

use crate::spin::SpinRedisExecutor;

const TRIGGER_METADATA_KEY: MetadataKey<TriggerMetadata> = MetadataKey::new("trigger");

pub(crate) type RuntimeData = ();
pub(crate) type Store = spin_core::Store<RuntimeData>;

Expand Down Expand Up @@ -56,17 +53,9 @@ impl TriggerExecutor for RedisTrigger {

async fn new(engine: TriggerAppEngine<Self>) -> Result<Self> {
let address = engine
.app()
.get_metadata(TRIGGER_METADATA_KEY)?
.map_or_else(
|| {
engine
.trigger_metadata::<TriggerMetadata>()
.unwrap_or_default()
.map_or(String::new(), |f| f.address)
},
|t| t.address,
);
.trigger_metadata::<TriggerMetadata>()?
.unwrap_or_default()
.address;

let mut channel_components: HashMap<String, Vec<String>> = HashMap::new();

Expand Down
14 changes: 3 additions & 11 deletions crates/trigger-http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,9 @@ impl TriggerExecutor for HttpTrigger {

async fn new(engine: TriggerAppEngine<Self>) -> Result<Self> {
let mut base = engine
.app()
.get_metadata(spin_http::trigger::METADATA_KEY)?
.map_or_else(
|| {
engine
.trigger_metadata::<spin_http::trigger::Metadata>()
.unwrap_or_default()
.map_or(String::new(), |f| f.base)
},
|t| t.base,
);
.trigger_metadata::<spin_http::trigger::Metadata>()?
.unwrap_or_default()
.base;

if !base.starts_with('/') {
base = format!("/{base}");
Expand Down

0 comments on commit b8490df

Please sign in to comment.