diff --git a/crates/app/src/lib.rs b/crates/app/src/lib.rs index 2ac39ce04..1fbc7cdf7 100644 --- a/crates/app/src/lib.rs +++ b/crates/app/src/lib.rs @@ -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}; @@ -212,16 +213,29 @@ impl<'a, L> App<'a, L> { &'this self, trigger_type: &'a str, ) -> Result> { - 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 { + if let Some(trigger_configs) = self.locked.metadata.get("triggers") { + // New-style: `{"triggers": {"": {...}}}` + trigger_configs.get(trigger_type).cloned() + } else if self.locked.metadata["trigger"]["type"] == trigger_type { + // Old-style: `{"trigger": {"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 diff --git a/crates/redis/src/lib.rs b/crates/redis/src/lib.rs index 44850b24b..10d032b90 100644 --- a/crates/redis/src/lib.rs +++ b/crates/redis/src/lib.rs @@ -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 = MetadataKey::new("trigger"); - pub(crate) type RuntimeData = (); pub(crate) type Store = spin_core::Store; @@ -56,17 +53,9 @@ impl TriggerExecutor for RedisTrigger { async fn new(engine: TriggerAppEngine) -> Result { let address = engine - .app() - .get_metadata(TRIGGER_METADATA_KEY)? - .map_or_else( - || { - engine - .trigger_metadata::() - .unwrap_or_default() - .map_or(String::new(), |f| f.address) - }, - |t| t.address, - ); + .trigger_metadata::()? + .unwrap_or_default() + .address; let mut channel_components: HashMap> = HashMap::new(); diff --git a/crates/trigger-http/src/lib.rs b/crates/trigger-http/src/lib.rs index 55d05ecd3..a414d6a76 100644 --- a/crates/trigger-http/src/lib.rs +++ b/crates/trigger-http/src/lib.rs @@ -95,17 +95,9 @@ impl TriggerExecutor for HttpTrigger { async fn new(engine: TriggerAppEngine) -> Result { let mut base = engine - .app() - .get_metadata(spin_http::trigger::METADATA_KEY)? - .map_or_else( - || { - engine - .trigger_metadata::() - .unwrap_or_default() - .map_or(String::new(), |f| f.base) - }, - |t| t.base, - ); + .trigger_metadata::()? + .unwrap_or_default() + .base; if !base.starts_with('/') { base = format!("/{base}");