From 0a6f9acc76b86938c4d74ffc0fcd8c41327c86b9 Mon Sep 17 00:00:00 2001 From: Ryan Levick Date: Fri, 19 Jul 2024 15:50:25 +0200 Subject: [PATCH 1/4] Remove toml assumption from sqlite Signed-off-by: Ryan Levick --- crates/factor-sqlite/src/lib.rs | 26 ++++++----------- crates/factor-sqlite/src/runtime_config.rs | 28 +++++-------------- .../factor-sqlite/src/runtime_config/spin.rs | 5 ++-- crates/factor-sqlite/tests/factor.rs | 22 +++++++++++---- 4 files changed, 34 insertions(+), 47 deletions(-) diff --git a/crates/factor-sqlite/src/lib.rs b/crates/factor-sqlite/src/lib.rs index bdc5d3abb..3139a86a3 100644 --- a/crates/factor-sqlite/src/lib.rs +++ b/crates/factor-sqlite/src/lib.rs @@ -7,19 +7,20 @@ use std::sync::Arc; use host::InstanceState; use async_trait::async_trait; +use serde::de::DeserializeOwned; use spin_factors::{anyhow, Factor}; use spin_locked_app::MetadataKey; use spin_world::v1::sqlite as v1; use spin_world::v2::sqlite as v2; -pub struct SqliteFactor { - runtime_config_resolver: Arc, +pub struct SqliteFactor { + runtime_config_resolver: Arc>, } -impl SqliteFactor { +impl SqliteFactor { /// Create a new `SqliteFactor` pub fn new( - runtime_config_resolver: impl runtime_config::RuntimeConfigResolver + 'static, + runtime_config_resolver: impl runtime_config::RuntimeConfigResolver + 'static, ) -> Self { Self { runtime_config_resolver: Arc::new(runtime_config_resolver), @@ -27,8 +28,8 @@ impl SqliteFactor { } } -impl Factor for SqliteFactor { - type RuntimeConfig = runtime_config::RuntimeConfig; +impl Factor for SqliteFactor { + type RuntimeConfig = runtime_config::RuntimeConfig; type AppState = AppState; type InstanceBuilder = InstanceState; @@ -47,17 +48,8 @@ impl Factor for SqliteFactor { ) -> anyhow::Result { let mut connection_pools = HashMap::new(); if let Some(runtime_config) = ctx.take_runtime_config() { - for ( - database_label, - runtime_config::StoreConfig { - type_: database_kind, - config, - }, - ) in runtime_config.store_configs - { - let pool = self - .runtime_config_resolver - .get_pool(&database_kind, config)?; + for (database_label, config) in runtime_config.store_configs { + let pool = self.runtime_config_resolver.get_pool(config)?; connection_pools.insert(database_label, pool); } } diff --git a/crates/factor-sqlite/src/runtime_config.rs b/crates/factor-sqlite/src/runtime_config.rs index 2089d079a..841a5e2c2 100644 --- a/crates/factor-sqlite/src/runtime_config.rs +++ b/crates/factor-sqlite/src/runtime_config.rs @@ -3,40 +3,26 @@ pub mod spin; use std::{collections::HashMap, sync::Arc}; -use serde::Deserialize; +use serde::{de::DeserializeOwned, Deserialize}; use spin_factors::{anyhow, FactorRuntimeConfig}; use crate::ConnectionPool; #[derive(Deserialize)] #[serde(transparent)] -pub struct RuntimeConfig { - pub store_configs: HashMap, +pub struct RuntimeConfig { + pub store_configs: HashMap, } -impl FactorRuntimeConfig for RuntimeConfig { +impl FactorRuntimeConfig for RuntimeConfig { const KEY: &'static str = "sqlite_database"; } -#[derive(Deserialize)] -pub struct StoreConfig { - #[serde(rename = "type")] - pub type_: String, - #[serde(flatten)] - pub config: toml::Table, -} - /// Resolves some piece of runtime configuration to a connection pool -pub trait RuntimeConfigResolver: Send + Sync { - /// Get a connection pool for a given database kind and the raw configuration. +pub trait RuntimeConfigResolver: Send + Sync { + /// Get a connection pool for a given config. /// - /// `database_kind` is equivalent to the `type` field in the - /// `[sqlite_database.$databasename]` runtime configuration table. - fn get_pool( - &self, - database_kind: &str, - config: toml::Table, - ) -> anyhow::Result>; + fn get_pool(&self, config: C) -> anyhow::Result>; /// If there is no runtime configuration for a given database label, return a default connection pool. /// diff --git a/crates/factor-sqlite/src/runtime_config/spin.rs b/crates/factor-sqlite/src/runtime_config/spin.rs index fa8b618a5..f8d8c33d4 100644 --- a/crates/factor-sqlite/src/runtime_config/spin.rs +++ b/crates/factor-sqlite/src/runtime_config/spin.rs @@ -50,11 +50,10 @@ impl SpinSqliteRuntimeConfig { } } -impl RuntimeConfigResolver for SpinSqliteRuntimeConfig { +impl RuntimeConfigResolver<(&str, toml::Table)> for SpinSqliteRuntimeConfig { fn get_pool( &self, - database_kind: &str, - config: toml::Table, + (database_kind, config): (&str, toml::Table), ) -> anyhow::Result> { let pool = match database_kind { "spin" => { diff --git a/crates/factor-sqlite/tests/factor.rs b/crates/factor-sqlite/tests/factor.rs index eb40212d9..6ce8b06d2 100644 --- a/crates/factor-sqlite/tests/factor.rs +++ b/crates/factor-sqlite/tests/factor.rs @@ -1,6 +1,7 @@ use std::{collections::HashSet, sync::Arc}; use factor_sqlite::SqliteFactor; +use serde::Deserialize; use spin_factors::{ anyhow::{self, bail}, RuntimeFactors, @@ -9,7 +10,7 @@ use spin_factors_test::{toml, TestEnvironment}; #[derive(RuntimeFactors)] struct TestFactors { - sqlite: SqliteFactor, + sqlite: SqliteFactor, } #[tokio::test] @@ -70,7 +71,9 @@ async fn no_error_when_database_is_configured() -> anyhow::Result<()> { [sqlite_database.foo] type = "sqlite" }; - assert!(env.build_instance_state(factors).await.is_ok()); + if let Err(e) = env.build_instance_state(factors).await { + bail!("Expected build_instance_state to succeed but it errored: {e}"); + } Ok(()) } @@ -88,13 +91,12 @@ impl RuntimeConfigResolver { } } -impl factor_sqlite::runtime_config::RuntimeConfigResolver for RuntimeConfigResolver { +impl factor_sqlite::runtime_config::RuntimeConfigResolver for RuntimeConfigResolver { fn get_pool( &self, - database_kind: &str, - config: toml::Table, + config: RuntimeConfig, ) -> anyhow::Result> { - let _ = (database_kind, config); + let _ = config; Ok(Arc::new(InvalidConnectionPool)) } @@ -117,3 +119,11 @@ impl factor_sqlite::ConnectionPool for InvalidConnectionPool { Err(spin_world::v2::sqlite::Error::InvalidConnection) } } + +#[derive(Deserialize)] +pub struct RuntimeConfig { + #[serde(rename = "type")] + pub type_: String, + #[serde(flatten)] + pub config: toml::Table, +} From bbdf0aa5cc8dc22159aa6ba3b0c250d1552184c8 Mon Sep 17 00:00:00 2001 From: Ryan Levick Date: Fri, 19 Jul 2024 16:02:55 +0200 Subject: [PATCH 2/4] Remove toml assumption from key-value Signed-off-by: Ryan Levick --- .../src/delegating_resolver.rs | 35 +++++++++---------- crates/factor-key-value/src/lib.rs | 26 +++++--------- crates/factor-key-value/src/runtime_config.rs | 29 ++++----------- crates/factors/tests/smoke.rs | 11 +++--- 4 files changed, 40 insertions(+), 61 deletions(-) diff --git a/crates/factor-key-value/src/delegating_resolver.rs b/crates/factor-key-value/src/delegating_resolver.rs index 235a2afcb..3644c8e28 100644 --- a/crates/factor-key-value/src/delegating_resolver.rs +++ b/crates/factor-key-value/src/delegating_resolver.rs @@ -1,5 +1,6 @@ use crate::runtime_config::RuntimeConfigResolver; use crate::store::{store_from_toml_fn, MakeKeyValueStore, StoreFromToml}; +use serde::Deserialize; use spin_key_value::StoreManager; use std::{collections::HashMap, sync::Arc}; @@ -9,20 +10,13 @@ pub struct DelegatingRuntimeConfigResolver { defaults: HashMap<&'static str, StoreConfig>, } -type StoreConfig = (&'static str, toml::value::Table); - impl DelegatingRuntimeConfigResolver { pub fn new() -> Self { Self::default() } - pub fn add_default_store( - &mut self, - label: &'static str, - store_kind: &'static str, - config: toml::value::Table, - ) { - self.defaults.insert(label, (store_kind, config)); + pub fn add_default_store(&mut self, label: &'static str, config: StoreConfig) { + self.defaults.insert(label, config); } } @@ -42,21 +36,26 @@ impl DelegatingRuntimeConfigResolver { } } -impl RuntimeConfigResolver for DelegatingRuntimeConfigResolver { - fn get_store( - &self, - store_kind: &str, - config: toml::Table, - ) -> anyhow::Result> { +impl RuntimeConfigResolver for DelegatingRuntimeConfigResolver { + fn get_store(&self, config: StoreConfig) -> anyhow::Result> { + let store_kind = config.type_.as_str(); let store_from_toml = self .store_types .get(store_kind) .ok_or_else(|| anyhow::anyhow!("unknown store kind: {}", store_kind))?; - store_from_toml(config) + store_from_toml(config.config) } fn default_store(&self, label: &str) -> Option> { - let (store_kind, config) = self.defaults.get(label)?; - self.get_store(store_kind, config.to_owned()).ok() + let config = self.defaults.get(label)?; + self.get_store(config.clone()).ok() } } + +#[derive(Deserialize, Clone)] +pub struct StoreConfig { + #[serde(rename = "type")] + pub type_: String, + #[serde(flatten)] + pub config: toml::Table, +} diff --git a/crates/factor-key-value/src/lib.rs b/crates/factor-key-value/src/lib.rs index bc2016467..269eed5e1 100644 --- a/crates/factor-key-value/src/lib.rs +++ b/crates/factor-key-value/src/lib.rs @@ -9,6 +9,7 @@ use std::{ use anyhow::ensure; use runtime_config::RuntimeConfig; +use serde::de::DeserializeOwned; use spin_factors::{ ConfigureAppContext, Factor, FactorInstanceBuilder, InitContext, InstanceBuilders, PrepareContext, RuntimeFactors, @@ -19,13 +20,13 @@ use spin_key_value::{ }; pub use store::MakeKeyValueStore; -pub struct KeyValueFactor { - runtime_config_resolver: Arc, +pub struct KeyValueFactor { + runtime_config_resolver: Arc>, } -impl KeyValueFactor { +impl KeyValueFactor { pub fn new( - runtime_config_resolver: impl runtime_config::RuntimeConfigResolver + 'static, + runtime_config_resolver: impl runtime_config::RuntimeConfigResolver + 'static, ) -> Self { Self { runtime_config_resolver: Arc::new(runtime_config_resolver), @@ -33,8 +34,8 @@ impl KeyValueFactor { } } -impl Factor for KeyValueFactor { - type RuntimeConfig = RuntimeConfig; +impl Factor for KeyValueFactor { + type RuntimeConfig = RuntimeConfig; type AppState = AppState; type InstanceBuilder = InstanceBuilder; @@ -51,17 +52,8 @@ impl Factor for KeyValueFactor { // Build StoreManager from runtime config let mut store_managers: HashMap> = HashMap::new(); if let Some(runtime_config) = ctx.take_runtime_config() { - for ( - store_label, - runtime_config::StoreConfig { - type_: store_kind, - config, - }, - ) in runtime_config.store_configs - { - let store = self - .runtime_config_resolver - .get_store(&store_kind, config)?; + for (store_label, config) in runtime_config.store_configs { + let store = self.runtime_config_resolver.get_store(config)?; store_managers.insert(store_label, store); } } diff --git a/crates/factor-key-value/src/runtime_config.rs b/crates/factor-key-value/src/runtime_config.rs index 721cf5be4..dbafa7431 100644 --- a/crates/factor-key-value/src/runtime_config.rs +++ b/crates/factor-key-value/src/runtime_config.rs @@ -1,38 +1,23 @@ use std::{collections::HashMap, sync::Arc}; -use serde::Deserialize; +use serde::{de::DeserializeOwned, Deserialize}; use spin_factors::{anyhow, FactorRuntimeConfig}; use spin_key_value::StoreManager; #[derive(Deserialize)] #[serde(transparent)] -pub struct RuntimeConfig { - pub store_configs: HashMap, +pub struct RuntimeConfig { + pub store_configs: HashMap, } -impl FactorRuntimeConfig for RuntimeConfig { +impl FactorRuntimeConfig for RuntimeConfig { const KEY: &'static str = "key_value_store"; } -#[derive(Deserialize)] -pub struct StoreConfig { - #[serde(rename = "type")] - pub type_: String, - #[serde(flatten)] - pub config: toml::Table, -} - /// Resolves some piece of runtime configuration to a connection pool -pub trait RuntimeConfigResolver: Send + Sync { - /// Get a store manager for a given store kind and the raw configuration. - /// - /// `store_kind` is equivalent to the `type` field in the - /// `[key_value_store.$storename]` runtime configuration table. - fn get_store( - &self, - store_kind: &str, - config: toml::Table, - ) -> anyhow::Result>; +pub trait RuntimeConfigResolver: Send + Sync { + /// Get a store manager for a given config. + fn get_store(&self, config: C) -> anyhow::Result>; /// Returns a default store manager for a given label. Should only be called /// if there is no runtime configuration for the label. diff --git a/crates/factors/tests/smoke.rs b/crates/factors/tests/smoke.rs index 7ec8cc059..5244e3bd2 100644 --- a/crates/factors/tests/smoke.rs +++ b/crates/factors/tests/smoke.rs @@ -4,7 +4,8 @@ use anyhow::Context; use http_body_util::BodyExt; use spin_app::App; use spin_factor_key_value::{ - delegating_resolver::DelegatingRuntimeConfigResolver, KeyValueFactor, MakeKeyValueStore, + delegating_resolver::{DelegatingRuntimeConfigResolver, StoreConfig}, + KeyValueFactor, MakeKeyValueStore, }; use spin_factor_key_value_redis::RedisKeyValueStore; use spin_factor_key_value_spin::{SpinKeyValueRuntimeConfig, SpinKeyValueStore}; @@ -21,7 +22,7 @@ struct Factors { variables: VariablesFactor, outbound_networking: OutboundNetworkingFactor, outbound_http: OutboundHttpFactor, - key_value: KeyValueFactor, + key_value: KeyValueFactor, } struct Data { @@ -42,8 +43,10 @@ async fn smoke_test_works() -> anyhow::Result<()> { SpinKeyValueRuntimeConfig::default(Some(PathBuf::from("tests/smoke-app/.spin"))); key_value_resolver.add_default_store( "default", - SpinKeyValueStore::RUNTIME_CONFIG_TYPE, - toml::value::Table::try_from(default_config)?, + StoreConfig { + type_: SpinKeyValueStore::RUNTIME_CONFIG_TYPE.to_string(), + config: toml::value::Table::try_from(default_config)?, + }, ); key_value_resolver.add_store_type(SpinKeyValueStore::new(None)?)?; key_value_resolver.add_store_type(RedisKeyValueStore)?; From b48c0475298009568ae4415df146b6bba9e5af07 Mon Sep 17 00:00:00 2001 From: Ryan Levick Date: Fri, 19 Jul 2024 17:33:18 +0200 Subject: [PATCH 3/4] Prefer associate type Signed-off-by: Ryan Levick --- .../src/delegating_resolver.rs | 4 +++- crates/factor-key-value/src/lib.rs | 4 ++-- crates/factor-key-value/src/runtime_config.rs | 7 ++++-- crates/factor-sqlite/src/lib.rs | 4 ++-- crates/factor-sqlite/src/runtime_config.rs | 6 +++-- .../factor-sqlite/src/runtime_config/spin.rs | 24 ++++++++++++------- crates/factor-sqlite/tests/factor.rs | 15 ++++-------- 7 files changed, 36 insertions(+), 28 deletions(-) diff --git a/crates/factor-key-value/src/delegating_resolver.rs b/crates/factor-key-value/src/delegating_resolver.rs index 3644c8e28..73ce2605e 100644 --- a/crates/factor-key-value/src/delegating_resolver.rs +++ b/crates/factor-key-value/src/delegating_resolver.rs @@ -36,7 +36,9 @@ impl DelegatingRuntimeConfigResolver { } } -impl RuntimeConfigResolver for DelegatingRuntimeConfigResolver { +impl RuntimeConfigResolver for DelegatingRuntimeConfigResolver { + type Config = StoreConfig; + fn get_store(&self, config: StoreConfig) -> anyhow::Result> { let store_kind = config.type_.as_str(); let store_from_toml = self diff --git a/crates/factor-key-value/src/lib.rs b/crates/factor-key-value/src/lib.rs index 269eed5e1..e7320445c 100644 --- a/crates/factor-key-value/src/lib.rs +++ b/crates/factor-key-value/src/lib.rs @@ -21,12 +21,12 @@ use spin_key_value::{ pub use store::MakeKeyValueStore; pub struct KeyValueFactor { - runtime_config_resolver: Arc>, + runtime_config_resolver: Arc>, } impl KeyValueFactor { pub fn new( - runtime_config_resolver: impl runtime_config::RuntimeConfigResolver + 'static, + runtime_config_resolver: impl runtime_config::RuntimeConfigResolver + 'static, ) -> Self { Self { runtime_config_resolver: Arc::new(runtime_config_resolver), diff --git a/crates/factor-key-value/src/runtime_config.rs b/crates/factor-key-value/src/runtime_config.rs index dbafa7431..b7d719570 100644 --- a/crates/factor-key-value/src/runtime_config.rs +++ b/crates/factor-key-value/src/runtime_config.rs @@ -15,9 +15,12 @@ impl FactorRuntimeConfig for RuntimeConfig { } /// Resolves some piece of runtime configuration to a connection pool -pub trait RuntimeConfigResolver: Send + Sync { +pub trait RuntimeConfigResolver: Send + Sync { + /// The type of configuration that this resolver can handle. + type Config: DeserializeOwned; + /// Get a store manager for a given config. - fn get_store(&self, config: C) -> anyhow::Result>; + fn get_store(&self, config: Self::Config) -> anyhow::Result>; /// Returns a default store manager for a given label. Should only be called /// if there is no runtime configuration for the label. diff --git a/crates/factor-sqlite/src/lib.rs b/crates/factor-sqlite/src/lib.rs index 3139a86a3..a6eab56bf 100644 --- a/crates/factor-sqlite/src/lib.rs +++ b/crates/factor-sqlite/src/lib.rs @@ -14,13 +14,13 @@ use spin_world::v1::sqlite as v1; use spin_world::v2::sqlite as v2; pub struct SqliteFactor { - runtime_config_resolver: Arc>, + runtime_config_resolver: Arc>, } impl SqliteFactor { /// Create a new `SqliteFactor` pub fn new( - runtime_config_resolver: impl runtime_config::RuntimeConfigResolver + 'static, + runtime_config_resolver: impl runtime_config::RuntimeConfigResolver + 'static, ) -> Self { Self { runtime_config_resolver: Arc::new(runtime_config_resolver), diff --git a/crates/factor-sqlite/src/runtime_config.rs b/crates/factor-sqlite/src/runtime_config.rs index 841a5e2c2..df29a1bf1 100644 --- a/crates/factor-sqlite/src/runtime_config.rs +++ b/crates/factor-sqlite/src/runtime_config.rs @@ -19,10 +19,12 @@ impl FactorRuntimeConfig for RuntimeConfig { } /// Resolves some piece of runtime configuration to a connection pool -pub trait RuntimeConfigResolver: Send + Sync { +pub trait RuntimeConfigResolver: Send + Sync { + type Config; + /// Get a connection pool for a given config. /// - fn get_pool(&self, config: C) -> anyhow::Result>; + fn get_pool(&self, config: Self::Config) -> anyhow::Result>; /// If there is no runtime configuration for a given database label, return a default connection pool. /// diff --git a/crates/factor-sqlite/src/runtime_config/spin.rs b/crates/factor-sqlite/src/runtime_config/spin.rs index f8d8c33d4..003120661 100644 --- a/crates/factor-sqlite/src/runtime_config/spin.rs +++ b/crates/factor-sqlite/src/runtime_config/spin.rs @@ -50,21 +50,29 @@ impl SpinSqliteRuntimeConfig { } } -impl RuntimeConfigResolver<(&str, toml::Table)> for SpinSqliteRuntimeConfig { - fn get_pool( - &self, - (database_kind, config): (&str, toml::Table), - ) -> anyhow::Result> { +#[derive(Deserialize)] +pub struct RuntimeConfig { + #[serde(rename = "type")] + pub type_: String, + #[serde(flatten)] + pub config: toml::Table, +} + +impl RuntimeConfigResolver for SpinSqliteRuntimeConfig { + type Config = RuntimeConfig; + + fn get_pool(&self, config: RuntimeConfig) -> anyhow::Result> { + let database_kind = config.type_.as_str(); let pool = match database_kind { "spin" => { - let config: LocalDatabase = config.try_into()?; + let config: LocalDatabase = config.config.try_into()?; config.pool(&self.local_database_dir)? } "libsql" => { - let config: LibSqlDatabase = config.try_into()?; + let config: LibSqlDatabase = config.config.try_into()?; config.pool()? } - _ => anyhow::bail!("Unknown database kind: {}", database_kind), + _ => anyhow::bail!("Unknown database kind: {database_kind}"), }; Ok(Arc::new(pool)) } diff --git a/crates/factor-sqlite/tests/factor.rs b/crates/factor-sqlite/tests/factor.rs index 6ce8b06d2..bff9218e3 100644 --- a/crates/factor-sqlite/tests/factor.rs +++ b/crates/factor-sqlite/tests/factor.rs @@ -1,7 +1,6 @@ use std::{collections::HashSet, sync::Arc}; -use factor_sqlite::SqliteFactor; -use serde::Deserialize; +use factor_sqlite::{runtime_config::spin::RuntimeConfig, SqliteFactor}; use spin_factors::{ anyhow::{self, bail}, RuntimeFactors, @@ -91,7 +90,9 @@ impl RuntimeConfigResolver { } } -impl factor_sqlite::runtime_config::RuntimeConfigResolver for RuntimeConfigResolver { +impl factor_sqlite::runtime_config::RuntimeConfigResolver for RuntimeConfigResolver { + type Config = RuntimeConfig; + fn get_pool( &self, config: RuntimeConfig, @@ -119,11 +120,3 @@ impl factor_sqlite::ConnectionPool for InvalidConnectionPool { Err(spin_world::v2::sqlite::Error::InvalidConnection) } } - -#[derive(Deserialize)] -pub struct RuntimeConfig { - #[serde(rename = "type")] - pub type_: String, - #[serde(flatten)] - pub config: toml::Table, -} From 10f7b42910feb79ffd9e57844a80bedfc11a9cd5 Mon Sep 17 00:00:00 2001 From: Ryan Levick Date: Mon, 22 Jul 2024 13:44:58 +0200 Subject: [PATCH 4/4] Be generic over config resolver Signed-off-by: Ryan Levick --- crates/factor-key-value/src/lib.rs | 17 +++++++---------- crates/factor-sqlite/src/lib.rs | 18 +++++++++--------- crates/factor-sqlite/src/runtime_config.rs | 2 +- crates/factor-sqlite/tests/factor.rs | 2 +- crates/factors/tests/smoke.rs | 2 +- 5 files changed, 19 insertions(+), 22 deletions(-) diff --git a/crates/factor-key-value/src/lib.rs b/crates/factor-key-value/src/lib.rs index e7320445c..2a7cbeea3 100644 --- a/crates/factor-key-value/src/lib.rs +++ b/crates/factor-key-value/src/lib.rs @@ -8,8 +8,7 @@ use std::{ }; use anyhow::ensure; -use runtime_config::RuntimeConfig; -use serde::de::DeserializeOwned; +use runtime_config::{RuntimeConfig, RuntimeConfigResolver}; use spin_factors::{ ConfigureAppContext, Factor, FactorInstanceBuilder, InitContext, InstanceBuilders, PrepareContext, RuntimeFactors, @@ -20,22 +19,20 @@ use spin_key_value::{ }; pub use store::MakeKeyValueStore; -pub struct KeyValueFactor { - runtime_config_resolver: Arc>, +pub struct KeyValueFactor { + runtime_config_resolver: Arc, } -impl KeyValueFactor { - pub fn new( - runtime_config_resolver: impl runtime_config::RuntimeConfigResolver + 'static, - ) -> Self { +impl KeyValueFactor { + pub fn new(runtime_config_resolver: R) -> Self { Self { runtime_config_resolver: Arc::new(runtime_config_resolver), } } } -impl Factor for KeyValueFactor { - type RuntimeConfig = RuntimeConfig; +impl Factor for KeyValueFactor { + type RuntimeConfig = RuntimeConfig; type AppState = AppState; type InstanceBuilder = InstanceBuilder; diff --git a/crates/factor-sqlite/src/lib.rs b/crates/factor-sqlite/src/lib.rs index a6eab56bf..1c270b9a4 100644 --- a/crates/factor-sqlite/src/lib.rs +++ b/crates/factor-sqlite/src/lib.rs @@ -7,29 +7,29 @@ use std::sync::Arc; use host::InstanceState; use async_trait::async_trait; -use serde::de::DeserializeOwned; +use runtime_config::RuntimeConfigResolver; use spin_factors::{anyhow, Factor}; use spin_locked_app::MetadataKey; use spin_world::v1::sqlite as v1; use spin_world::v2::sqlite as v2; -pub struct SqliteFactor { - runtime_config_resolver: Arc>, +pub struct SqliteFactor { + runtime_config_resolver: Arc, } -impl SqliteFactor { +impl SqliteFactor { /// Create a new `SqliteFactor` - pub fn new( - runtime_config_resolver: impl runtime_config::RuntimeConfigResolver + 'static, - ) -> Self { + /// + /// Takes a `runtime_config_resolver` that can resolve a runtime configuration into a connection pool. + pub fn new(runtime_config_resolver: R) -> Self { Self { runtime_config_resolver: Arc::new(runtime_config_resolver), } } } -impl Factor for SqliteFactor { - type RuntimeConfig = runtime_config::RuntimeConfig; +impl Factor for SqliteFactor { + type RuntimeConfig = runtime_config::RuntimeConfig; type AppState = AppState; type InstanceBuilder = InstanceState; diff --git a/crates/factor-sqlite/src/runtime_config.rs b/crates/factor-sqlite/src/runtime_config.rs index df29a1bf1..10e6f8e72 100644 --- a/crates/factor-sqlite/src/runtime_config.rs +++ b/crates/factor-sqlite/src/runtime_config.rs @@ -20,7 +20,7 @@ impl FactorRuntimeConfig for RuntimeConfig { /// Resolves some piece of runtime configuration to a connection pool pub trait RuntimeConfigResolver: Send + Sync { - type Config; + type Config: DeserializeOwned; /// Get a connection pool for a given config. /// diff --git a/crates/factor-sqlite/tests/factor.rs b/crates/factor-sqlite/tests/factor.rs index bff9218e3..6897e015b 100644 --- a/crates/factor-sqlite/tests/factor.rs +++ b/crates/factor-sqlite/tests/factor.rs @@ -9,7 +9,7 @@ use spin_factors_test::{toml, TestEnvironment}; #[derive(RuntimeFactors)] struct TestFactors { - sqlite: SqliteFactor, + sqlite: SqliteFactor, } #[tokio::test] diff --git a/crates/factors/tests/smoke.rs b/crates/factors/tests/smoke.rs index 5244e3bd2..6ad949c13 100644 --- a/crates/factors/tests/smoke.rs +++ b/crates/factors/tests/smoke.rs @@ -22,7 +22,7 @@ struct Factors { variables: VariablesFactor, outbound_networking: OutboundNetworkingFactor, outbound_http: OutboundHttpFactor, - key_value: KeyValueFactor, + key_value: KeyValueFactor, } struct Data {