Skip to content

Commit b7415b2

Browse files
authored
Merge pull request #2869 from itowlson/wasi-config
Implement wasi-config
2 parents d814705 + 942de93 commit b7415b2

File tree

15 files changed

+177
-1
lines changed

15 files changed

+177
-1
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/expressions/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ edition = { workspace = true }
77
[dependencies]
88
anyhow = { workspace = true }
99
async-trait = { workspace = true }
10+
futures = { workspace = true }
1011
spin-locked-app = { path = "../locked-app" }
1112
thiserror = { workspace = true }
1213

crates/expressions/src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,22 @@ impl ProviderResolver {
5252
self.resolve_template(template).await
5353
}
5454

55+
/// Resolves all variables for the given component.
56+
pub async fn resolve_all(&self, component_id: &str) -> Result<Vec<(String, String)>> {
57+
use futures::FutureExt;
58+
59+
let Some(keys2templates) = self.internal.component_configs.get(component_id) else {
60+
return Ok(vec![]);
61+
};
62+
63+
let resolve_futs = keys2templates.iter().map(|(key, template)| {
64+
self.resolve_template(template)
65+
.map(|r| r.map(|value| (key.to_string(), value)))
66+
});
67+
68+
futures::future::try_join_all(resolve_futs).await
69+
}
70+
5571
/// Resolves the given template.
5672
pub async fn resolve_template(&self, template: &Template) -> Result<String> {
5773
let mut resolved_parts: Vec<Cow<str>> = Vec::with_capacity(template.parts().len());

crates/factor-variables/src/host.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use spin_factors::anyhow;
2-
use spin_world::{async_trait, v1, v2::variables};
2+
use spin_world::{async_trait, v1, v2::variables, wasi::config as wasi_config};
33
use tracing::{instrument, Level};
44

55
use crate::InstanceState;
@@ -37,6 +37,41 @@ impl v1::config::Host for InstanceState {
3737
}
3838
}
3939

40+
#[async_trait]
41+
impl wasi_config::store::Host for InstanceState {
42+
async fn get(&mut self, key: String) -> Result<Option<String>, wasi_config::store::Error> {
43+
match <Self as variables::Host>::get(self, key).await {
44+
Ok(value) => Ok(Some(value)),
45+
Err(variables::Error::Undefined(_)) => Ok(None),
46+
Err(variables::Error::InvalidName(_)) => Ok(None), // this is the guidance from https://github.com/WebAssembly/wasi-runtime-config/pull/19)
47+
Err(variables::Error::Provider(msg)) => Err(wasi_config::store::Error::Upstream(msg)),
48+
Err(variables::Error::Other(msg)) => Err(wasi_config::store::Error::Io(msg)),
49+
}
50+
}
51+
52+
async fn get_all(&mut self) -> Result<Vec<(String, String)>, wasi_config::store::Error> {
53+
let all = self
54+
.expression_resolver
55+
.resolve_all(&self.component_id)
56+
.await;
57+
all.map_err(|e| {
58+
match expressions_to_variables_err(e) {
59+
variables::Error::Undefined(msg) => wasi_config::store::Error::Io(msg), // this shouldn't happen but just in case
60+
variables::Error::InvalidName(msg) => wasi_config::store::Error::Io(msg), // this shouldn't happen but just in case
61+
variables::Error::Provider(msg) => wasi_config::store::Error::Upstream(msg),
62+
variables::Error::Other(msg) => wasi_config::store::Error::Io(msg),
63+
}
64+
})
65+
}
66+
67+
fn convert_error(
68+
&mut self,
69+
err: wasi_config::store::Error,
70+
) -> anyhow::Result<wasi_config::store::Error> {
71+
Ok(err)
72+
}
73+
}
74+
4075
fn expressions_to_variables_err(err: spin_expressions::Error) -> variables::Error {
4176
use spin_expressions::Error;
4277
match err {

crates/factor-variables/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ impl Factor for VariablesFactor {
3131
fn init<T: Send + 'static>(&mut self, mut ctx: InitContext<T, Self>) -> anyhow::Result<()> {
3232
ctx.link_bindings(spin_world::v1::config::add_to_linker)?;
3333
ctx.link_bindings(spin_world::v2::variables::add_to_linker)?;
34+
ctx.link_bindings(spin_world::wasi::config::store::add_to_linker)?;
3435
Ok(())
3536
}
3637

crates/world/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ wasmtime::component::bindgen!({
2828
"fermyon:spin/[email protected]/error" => v2::sqlite::Error,
2929
"fermyon:spin/sqlite/error" => v1::sqlite::Error,
3030
"fermyon:spin/[email protected]/error" => v2::variables::Error,
31+
"wasi:config/[email protected]/error" => wasi::config::store::Error,
3132
},
3233
trappable_imports: true,
3334
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
spin_manifest_version = "1"
2+
authors = [""]
3+
description = ""
4+
name = "variables"
5+
trigger = { type = "http" }
6+
version = "0.1.0"
7+
8+
[variables]
9+
variable = { default = "value" }
10+
11+
[[component]]
12+
id = "variables"
13+
source = "%{source=variables}"
14+
[component.trigger]
15+
route = "/..."
16+
[component.config]
17+
variable = "{{ variable }}"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
spin_manifest_version = "1"
2+
authors = [""]
3+
description = ""
4+
name = "wasi-config"
5+
trigger = { type = "http" }
6+
version = "0.1.0"
7+
8+
[variables]
9+
variable = { default = "value" }
10+
11+
[[component]]
12+
id = "wasi-config"
13+
source = "%{source=wasi-config}"
14+
[component.trigger]
15+
route = "/..."
16+
[component.config]
17+
variable = "{{ variable }}"

tests/test-components/components/Cargo.lock

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "wasi-config"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
crate-type = ["cdylib"]
8+
9+
[dependencies]
10+
helper = { path = "../../helper" }
11+
wit-bindgen = "0.16.0"

0 commit comments

Comments
 (0)