Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

app: Move backward-compat logic into get_trigger_metadata #3

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -94,17 +94,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