Skip to content

Commit 2fed8a1

Browse files
committed
Adds new dynamism metadata for Providers
New logic only allows no default variable before runtime if Provider it is of a Dynamic type Signed-off-by: Aminu Oluwaseun Joshua <[email protected]>
1 parent 740543e commit 2fed8a1

File tree

27 files changed

+398
-862
lines changed

27 files changed

+398
-862
lines changed

Cargo.lock

Lines changed: 40 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ lazy_static = "1.5"
145145
path-absolutize = "3"
146146
quote = "1"
147147
rand = "0.9"
148-
redis = "0.32.5"
148+
redis = "0.29"
149149
regex = "1"
150150
reqwest = { version = "0.12", features = ["stream", "blocking"] }
151151
rusqlite = "0.34"

crates/common/src/assert.rs

Lines changed: 0 additions & 33 deletions
This file was deleted.

crates/common/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
// - Code should have at least 2 dependents
1010

1111
pub mod arg_parser;
12-
pub mod assert;
1312
pub mod data_dir;
1413
pub mod paths;
1514
pub mod sha256;

crates/expressions/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ edition = { workspace = true }
88
anyhow = { workspace = true }
99
async-trait = { workspace = true }
1010
futures = { workspace = true }
11+
serde = { workspace = true }
1112
spin-locked-app = { path = "../locked-app" }
1213
thiserror = { workspace = true }
1314

crates/expressions/src/lib.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ pub use provider::Provider;
1111
use template::Part;
1212
pub use template::Template;
1313

14+
use crate::provider::ProviderVariableKind;
15+
1416
/// A [`ProviderResolver`] that can be shared.
1517
pub type SharedPreparedResolver =
1618
std::sync::Arc<std::sync::OnceLock<std::sync::Arc<PreparedResolver>>>;
@@ -90,6 +92,30 @@ impl ProviderResolver {
9092
Ok(PreparedResolver { variables })
9193
}
9294

95+
/// Prepares the resolver by attempting to resolve all variables, printing warnings for any
96+
/// that cannot be resolved.
97+
pub async fn pre_runtime_prepare(&self) -> Result<()> {
98+
for name in self.internal.variables.keys() {
99+
self.check_variable_existence(name).await?;
100+
}
101+
Ok(())
102+
}
103+
104+
async fn check_variable_existence(&self, key: &str) -> Result<()> {
105+
for provider in &self.providers {
106+
if provider.kind() == &ProviderVariableKind::Dynamic {
107+
continue;
108+
}
109+
110+
match provider.get(&Key(key)).await {
111+
Ok(Some(_)) => return Ok(()),
112+
Err(_) | Ok(None) => return self.internal.resolve_variable(key).map(|_| ()),
113+
}
114+
}
115+
116+
Ok(())
117+
}
118+
93119
async fn resolve_variable(&self, key: &str) -> Result<String> {
94120
for provider in &self.providers {
95121
if let Some(value) = provider.get(&Key(key)).await.map_err(Error::Provider)? {
@@ -309,6 +335,8 @@ pub enum Error {
309335
mod tests {
310336
use async_trait::async_trait;
311337

338+
use crate::provider::ProviderVariableKind;
339+
312340
use super::*;
313341

314342
#[derive(Debug)]
@@ -323,6 +351,10 @@ mod tests {
323351
_ => Ok(None),
324352
}
325353
}
354+
355+
fn kind(&self) -> &ProviderVariableKind {
356+
&ProviderVariableKind::Static
357+
}
326358
}
327359

328360
async fn test_resolve(template: &str) -> Result<String> {

crates/expressions/src/provider.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::fmt::Debug;
22

33
use async_trait::async_trait;
4+
use serde::Deserialize;
45

56
use crate::Key;
67

@@ -9,4 +10,15 @@ use crate::Key;
910
pub trait Provider: Debug + Send + Sync {
1011
/// Returns the value at the given config path, if it exists.
1112
async fn get(&self, key: &Key) -> anyhow::Result<Option<String>>;
13+
fn kind(&self) -> &ProviderVariableKind;
14+
}
15+
16+
/// The dynamism of a Provider.
17+
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
18+
pub enum ProviderVariableKind {
19+
/// Variable must be declared on start
20+
#[default]
21+
Static,
22+
/// Variable can be made available at runtime
23+
Dynamic,
1224
}

crates/factor-outbound-http/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ wasmtime-wasi = { workspace = true }
2727
wasmtime-wasi-http = { workspace = true }
2828

2929
[dev-dependencies]
30-
spin-common = { path = "../common" }
3130
spin-factor-variables = { path = "../factor-variables" }
3231
spin-factors-test = { path = "../factors-test" }
3332

0 commit comments

Comments
 (0)